1#!/usr/bin/perl -w
2
3# parser.t does general parser tests, this one deals only with "[A|B|C]" style
4# nodes and tests that this feature does work correctly.
5
6use Test::More;
7use strict;
8
9BEGIN
10   {
11   plan tests => 20;
12   chdir 't' if -d 't';
13   use lib '../lib';
14   use_ok ("Graph::Easy::Parser") or die($@);
15   };
16
17can_ok ("Graph::Easy::Parser", qw/
18  new
19  from_text
20  from_file
21  reset
22  error
23  _parse_attributes
24  /);
25
26#############################################################################
27# parser object
28
29my $parser = Graph::Easy::Parser->new();
30
31is (ref($parser), 'Graph::Easy::Parser');
32is ($parser->error(), '', 'no error yet');
33
34
35#############################################################################
36# split a node and check all relevant fields
37
38my $graph = $parser->from_text("[A|B|C]");
39
40is (scalar $graph->nodes(), 3, '3 nodes');
41
42my $A = $graph->node('ABC.0');
43is (ref($A), 'Graph::Easy::Node', 'node is node');
44is ($A->origin(), undef, 'A is the origin itself');
45
46my $B = $graph->node('ABC.1');
47is (ref($B), 'Graph::Easy::Node', 'node is node');
48is ($B->origin(), $A, 'A is the origin of B');
49is (join(",", $B->offset()), "1,0", 'B is at +1,0');
50
51my $C = $graph->node('ABC.2');
52is (ref($C), 'Graph::Easy::Node', 'node is node');
53is ($C->origin(), $B, 'B is the origin of C');
54is (join(",", $C->offset()), "1,0", 'C is at +1,0 from B');
55
56#############################################################################
57# general split tests
58
59my $line = 0;
60
61foreach (<DATA>)
62  {
63  chomp;
64  next if $_ =~ /^\s*\z/;			# skip empty lines
65  next if $_ =~ /^#/;				# skip comments
66
67  die ("Illegal line $line in testdata") unless $_ =~ /^(.*)\|([^\|]*)$/;
68  my ($in,$result) = ($1,$2);
69
70  my $txt = $in;
71  $txt =~ s/\\n/\n/g;				# insert real newlines
72
73  Graph::Easy::Node->_reset_id();		# to get "#0" for each test
74  my $graph = $parser->from_text($txt);		# reuse parser object
75
76  if (!defined $graph)
77    {
78    fail($parser->error());
79    next;
80    }
81
82  my $got = scalar $graph->nodes();
83
84  my @edges = $graph->edges();
85
86  my $es = 0;
87  foreach my $e (sort { $a->label() cmp $b->label() } @edges)
88    {
89    $es ++ if $e->label() ne '';
90    }
91
92  $got .= '+' . $es if $es > 0;
93
94  for my $n ( sort { $a->{name} cmp $b->{name} } ($graph->nodes(), $graph->edges()) )
95    {
96    # normalize color output
97    my $b = Graph::Easy->color_as_hex($n->attribute('fill'));
98    my $dx = $n->{dx}||0;
99    my $dy = $n->{dy}||0;
100    $got .= ";" . $n->name() . "," . $n->label() . "=$dx.$dy." . $b;
101    }
102
103  is ($got, $result, $in);
104  }
105
106__DATA__
107# split tests with attributes
108[A|B|C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=1.0.#ffffff;ABC.2,C=1.0.#ffffff
109[A|B|C] { fill: red; }|3;ABC.0,A=0.0.#ff0000;ABC.1,B=1.0.#ff0000;ABC.2,C=1.0.#ff0000
110[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
111[A| |C]|3;AC.0,A=0.0.#ffffff;AC.1, =1.0.#ffffff;AC.2,C=1.0.#ffffff
112[A||B|C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=0.1.#ffffff;ABC.2,C=1.0.#ffffff
113[A||B||C]|3;ABC.0,A=0.0.#ffffff;ABC.1,B=0.1.#ffffff;ABC.2,C=0.1.#ffffff
114[A|| |C]|3;AC.0,A=0.0.#ffffff;AC.1, =0.1.#ffffff;AC.2,C=1.0.#ffffff
115