1#!/usr/bin/perl -w 2 3# Test Graph::Easy::Group and Graph::Easy::Group::Cell 4 5use Test::More; 6use strict; 7 8BEGIN 9 { 10 plan tests => 72; 11 chdir 't' if -d 't'; 12 use lib '../lib'; 13 use_ok ("Graph::Easy::Group") or die($@); 14 use_ok ("Graph::Easy::Group::Cell") or die($@); 15 use_ok ("Graph::Easy") or die($@); 16 }; 17 18can_ok ("Graph::Easy::Group", qw/ 19 new 20 error 21 name 22 add_node 23 add_nodes 24 add_member 25 has_as_successor 26 has_as_predecessor 27 successors 28 predecessors 29 30 nodes 31 edges 32 33 _add_cell _del_cell _cells _clear_cells 34 35 del_node 36 del_edge 37 del_member 38 39 /); 40 41can_ok ("Graph::Easy::Group::Cell", qw/ 42 _set_type 43 class 44 /); 45 46############################################################################# 47 48my $group = Graph::Easy::Group->new(); 49 50is (ref($group), 'Graph::Easy::Group'); 51 52is ($group->error(), '', 'no error yet'); 53 54my $graph = Graph::Easy->new(); 55 56use_ok ('Graph::Easy::As_txt'); 57 58# "insert" into a graph to get default attributes 59$group->{graph} = $graph; 60 61is ($group->as_txt(), "( Group \\#0 )\n\n", 'as_txt (empty group)'); 62is (scalar $group->nodes(), 0, 'no nodes in group'); 63is (scalar $group->edges(), 0, 'no edges in group'); 64is ($group->name(), 'Group #0', 'name()'); 65 66my $first = Graph::Easy::Node->new( name => 'first' ); 67my $second = Graph::Easy::Node->new( name => 'second' ); 68 69$group->add_node($first); 70is (scalar $group->nodes(), 1, 'one node in group'); 71is ($first->attribute('group'), $group->name(), 'node has group attribute set'); 72 73$group->add_nodes($first, $second); 74is (scalar $group->nodes(), 2, 'two nodes in group'); 75is ($second->attribute('group'), $group->name(), 'node has group attribute set'); 76is ($second->{group}, $group, 'add_nodes() worked'); 77 78is ($group->as_txt(), <<HERE 79( Group \\#0 80 [ first ] 81 [ second ] 82) 83 84HERE 85, 'as_txt (group with two nodes)'); 86 87############################################################################# 88# attribute nodeclass 89 90$group = Graph::Easy::Group->new(); 91$group->set_attributes ( { 'nodeclass' => 'city', } ); 92 93is ($first->class(),'node', 'class is "node"'); 94 95$group->add_node($first); 96 97is ($first->class(),'node.city', 'class is now "node.city"'); 98 99############################################################################# 100# Group::Cells 101 102my $c = '_cells'; 103 104my $cell = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => 0, ); 105is (scalar keys %{$group->{$c}}, 1, 'one cell'); 106 107my $cells = { '0,0' => $cell }; 108 109$cell->_set_type( $cells ); 110 111is ($cell->class(), 'group ga', 'group ga'); 112 113is ($cell->group( $group->{name} ), $group, "group()"); 114 115my $cell2 = Graph::Easy::Group::Cell->new( group => $group, x => 1, y => 0 ); 116is (scalar keys %{$group->{$c}}, 2, 'one more cell'); 117$cells->{'1,0'} = $cell2; 118 119my $cell3 = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => -1 ); 120is (scalar keys %{$group->{$c}}, 3, 'one more cell'); 121$cells->{'0,-1'} = $cell3; 122 123my $cell4 = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => 1 ); 124is (scalar keys %{$group->{$c}}, 4, 'one more cell'); 125$cells->{'0,1'} = $cell4; 126 127is ($cell2->group( $group->{name} ), $group, "group()"); 128 129$cell->_set_type( $cells ); 130is ($cell->class(), 'group gl', 'group gl'); 131 132############################################################################# 133# attributes on cells 134 135# The default attributes are returned by attribute(): 136 137is ($group->attribute('border-style'), 'dashed', 'group border'); 138is ($group->attribute('borderstyle'), 'dashed', 'group border'); 139is ($cell->attribute('border'), '', 'default border on this cell'); 140is ($cell->attribute('border-style'), 'dashed', 'default border on this cell'); 141 142is ($group->default_attribute('border-style'), 'dashed', 'group is dashed'); 143is ($cell->default_attribute('border'), 'dashed 1px #000000', 'dashed border on this cell'); 144is ($cell->default_attribute('border-style'), 'dashed', 'dashed border on this cell'); 145 146is ($group->default_attribute('fill'), '#a0d0ff', 'fill on group'); 147is ($group->attribute('fill'), '#a0d0ff', 'fill on group'); 148is ($cell->default_attribute('fill'), '#a0d0ff', 'fill on group cell'); 149is ($cell->attribute('fill'), '#a0d0ff', 'fill on group cell'); 150 151############################################################################# 152# del_cell(); 153 154#print join (" ", keys %{$group->{cells}}),"\n"; 155 156is (scalar keys %{$group->{$c}}, 4, 'one less'); 157$group->_del_cell($cell); 158 159is (scalar keys %{$group->{$c}}, 3, 'one less'); 160is ($cell->group(), undef, "no group() on deleted cell"); 161 162############################################################################# 163# del_node() & del_edge(), when node/edge are in a group (bug until 0.39) 164 165$graph = Graph::Easy->new(); 166 167$group = $graph->add_group('group'); 168 169my ($A,$B,$E) = $graph->add_edge('A','B','E'); 170 171for my $m ($A,$B,$E) 172 { 173 $group->add_member($m); 174 } 175 176is ($group->nodes(), 2, '2 nodes in group'); 177is ($group->edges(), 0, '0 edges going from/to group'); 178is ($group->edges_within(), 1, '1 edge in group'); 179 180is ($A->attribute('group'), $group->name(), 'group attribute got added'); 181$graph->del_node($A); 182 183is ($A->attribute('group'), '', 'group attribute got deleted'); 184is ($group->nodes(), 1, '1 node in group'); 185is ($group->edges(), 0, '0 edges in group'); 186 187($A,$B,$E) = $graph->add_edge('A','B','E'); 188 189$group->add_member($A); 190$group->add_member($E); 191 192is ($group->nodes(), 2, '2 nodes in group'); 193is ($group->edges(), 0, '0 edges going from/to group'); 194is ($group->edges_within(), 1, '1 edge in group'); 195 196$graph->del_edge($E); 197 198is ($group->nodes(), 2, '2 nodes in group'); 199is ($group->edges(), 0, '0 edges in group'); 200is ($group->edges_within(), 0, '0 edges in group'); 201 202############################################################################# 203# successors and predecessors 204 205$graph = Graph::Easy->new(); 206 207$group = $graph->add_group('group'); 208 209my ($g1,$bonn) = $graph->add_edge($group, 'Bonn'); 210my ($berlin,$g2) = $graph->add_edge('Berlin', $group); 211 212is ($group->has_as_successor($bonn), 1, 'group -> bonn'); 213is ($group->has_as_successor($berlin), 0, '! group -> berlin'); 214is ($group->has_as_predecessor($berlin), 1, 'berlin -> group'); 215is ($group->has_as_predecessor($bonn), 0, '! bonn -> group'); 216 217is ($bonn->has_as_successor($group), 0, '! group -> bonn'); 218is ($berlin->has_as_predecessor($group), 0, 'group -> berlin'); 219is ($bonn->has_as_predecessor($group), 1, 'bonn -> group'); 220 221my @suc = $group->successors(); 222 223is (scalar @suc, 1, 'one successor'); 224is ($suc[0], $bonn, 'group => bonn'); 225 226############################################################################# 227# add_node('Bonn'), add_member('Bonn','Berlin') etc. 228 229$graph = Graph::Easy->new(); 230 231$group = $graph->add_group('group'); 232$bonn = $group->add_node('Bonn'); 233 234is (ref($bonn), 'Graph::Easy::Node', "add_node('Bonn') works for groups"); 235 236($bonn,$berlin) = $group->add_nodes('Bonn','Berlin'); 237 238is (ref($bonn), 'Graph::Easy::Node', "add_nodes('Bonn') works for groups"); 239is ($bonn->name(), 'Bonn', "add_nodes('Bonn') works for groups"); 240is (ref($berlin), 'Graph::Easy::Node', "add_nodes('Berlin') works for groups"); 241is ($berlin->name(), 'Berlin', "add_nodes('Berlin') works for groups"); 242 243# add_edge() 244my $edge = $group->add_edge('Bonn','Kassel'); 245 246my $kassel = $graph->node('Kassel'); 247 248is (ref($kassel), 'Graph::Easy::Node', "add_edge('Bonn','Kassel') works for groups"); 249 250# add_edge_once() 251 252$edge = $group->add_edge_once('Bonn','Kassel'); 253 254my @edges = $graph->edges('Bonn','Kassel'); 255is (scalar @edges, 1, 'one edge from Bonn => Kassel'); 256 257# add_edge() twice 258 259$edge = $group->add_edge('Bonn','Kassel'); 260 261@edges = $graph->edges('Bonn','Kassel'); 262is (scalar @edges, 2, 'two edges from Bonn => Kassel'); 263 264