File: split.t

package info (click to toggle)
libgraph-easy-perl 0.71-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,284 kB
  • sloc: perl: 24,909; makefile: 2
file content (114 lines) | stat: -rw-r--r-- 3,121 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl -w

# parser.t does general parser tests, this one deals only with "[A|B|C]" style
# nodes and tests that this feature does work correctly.

use Test::More;
use strict;

BEGIN
   {
   plan tests => 20;
   chdir 't' if -d 't';
   use lib '../lib';
   use_ok ("Graph::Easy::Parser") or die($@);
   };

can_ok ("Graph::Easy::Parser", qw/
  new
  from_text
  from_file
  reset
  error
  _parse_attributes
  /);

#############################################################################
# parser object

my $parser = Graph::Easy::Parser->new();

is (ref($parser), 'Graph::Easy::Parser');
is ($parser->error(), '', 'no error yet');


#############################################################################
# split a node and check all relevant fields

my $graph = $parser->from_text("[A|B|C]");

is (scalar $graph->nodes(), 3, '3 nodes');

my $A = $graph->node('ABC.0');
is (ref($A), 'Graph::Easy::Node', 'node is node');
is ($A->origin(), undef, 'A is the origin itself');

my $B = $graph->node('ABC.1');
is (ref($B), 'Graph::Easy::Node', 'node is node');
is ($B->origin(), $A, 'A is the origin of B');
is (join(",", $B->offset()), "1,0", 'B is at +1,0');

my $C = $graph->node('ABC.2');
is (ref($C), 'Graph::Easy::Node', 'node is node');
is ($C->origin(), $B, 'B is the origin of C');
is (join(",", $C->offset()), "1,0", 'C is at +1,0 from B');

#############################################################################
# general split tests

my $line = 0;

foreach (<DATA>)
  {
  chomp;
  next if $_ =~ /^\s*\z/;			# skip empty lines
  next if $_ =~ /^#/;				# skip comments

  die ("Illegal line $line in testdata") unless $_ =~ /^(.*)\|([^\|]*)$/;
  my ($in,$result) = ($1,$2);

  my $txt = $in;
  $txt =~ s/\\n/\n/g;				# insert real newlines

  Graph::Easy::Node->_reset_id();		# to get "#0" for each test
  my $graph = $parser->from_text($txt);		# reuse parser object

  if (!defined $graph)
    {
    fail($parser->error());
    next;
    }
 
  my $got = scalar $graph->nodes();

  my @edges = $graph->edges();

  my $es = 0;
  foreach my $e (sort { $a->label() cmp $b->label() } @edges)
    {
    $es ++ if $e->label() ne '';
    }

  $got .= '+' . $es if $es > 0;

  for my $n ( sort { $a->{name} cmp $b->{name} } ($graph->nodes(), $graph->edges()) )
    {
    # normalize color output
    my $b = Graph::Easy->color_as_hex($n->attribute('fill'));
    my $dx = $n->{dx}||0;
    my $dy = $n->{dy}||0;
    $got .= ";" . $n->name() . "," . $n->label() . "=$dx.$dy." . $b;
    } 
  
  is ($got, $result, $in);
  }

__DATA__
# split tests with attributes
[A|B|C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=1.0.#ffffff;ABC.2,C=1.0.#ffffff
[A|B|C] { fill: red; }|3;ABC.0,A=0.0.#ff0000;ABC.1,B=1.0.#ff0000;ABC.2,C=1.0.#ff0000
[A|B|C] { label: foo; fill: red; }|3;ABC.0,foo=0.0.#ff0000;ABC.1,foo=1.0.#ff0000;ABC.2,foo=1.0.#ff0000
[A| |C]|3;AC.0,A=0.0.#ffffff;AC.1, =1.0.#ffffff;AC.2,C=1.0.#ffffff
[A||B|C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=0.1.#ffffff;ABC.2,C=1.0.#ffffff
[A||B||C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=0.1.#ffffff;ABC.2,C=0.1.#ffffff
[A|| |C]|3;AC.0,A=0.0.#ffffff;AC.1, =0.1.#ffffff;AC.2,C=1.0.#ffffff