File: group.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 (263 lines) | stat: -rw-r--r-- 7,610 bytes parent folder | download | duplicates (5)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/perl -w

# Test Graph::Easy::Group and Graph::Easy::Group::Cell

use Test::More;
use strict;

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

can_ok ("Graph::Easy::Group", qw/
  new
  error
  name
  add_node
  add_nodes
  add_member
  has_as_successor
  has_as_predecessor
  successors
  predecessors

  nodes
  edges

  _add_cell _del_cell _cells _clear_cells

  del_node
  del_edge
  del_member

  /);

can_ok ("Graph::Easy::Group::Cell", qw/
  _set_type
  class
  /);

#############################################################################

my $group = Graph::Easy::Group->new();

is (ref($group), 'Graph::Easy::Group');

is ($group->error(), '', 'no error yet');

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

use_ok ('Graph::Easy::As_txt');

# "insert" into a graph to get default attributes
$group->{graph} = $graph;

is ($group->as_txt(), "( Group \\#0 )\n\n", 'as_txt (empty group)');
is (scalar $group->nodes(), 0, 'no nodes in group');
is (scalar $group->edges(), 0, 'no edges in group');
is ($group->name(), 'Group #0', 'name()');

my $first = Graph::Easy::Node->new( name => 'first' );
my $second = Graph::Easy::Node->new( name => 'second' );

$group->add_node($first);
is (scalar $group->nodes(), 1, 'one node in group');
is ($first->attribute('group'), $group->name(), 'node has group attribute set');

$group->add_nodes($first, $second);
is (scalar $group->nodes(), 2, 'two nodes in group');
is ($second->attribute('group'), $group->name(), 'node has group attribute set');
is ($second->{group}, $group, 'add_nodes() worked');

is ($group->as_txt(), <<HERE
( Group \\#0
  [ first ]
  [ second ]
)

HERE
, 'as_txt (group with two nodes)');

#############################################################################
# attribute nodeclass

$group = Graph::Easy::Group->new();
$group->set_attributes ( { 'nodeclass' => 'city', } );

is ($first->class(),'node', 'class is "node"');

$group->add_node($first);

is ($first->class(),'node.city', 'class is now "node.city"');

#############################################################################
# Group::Cells

my $c = '_cells';

my $cell = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => 0, );
is (scalar keys %{$group->{$c}}, 1, 'one cell');

my $cells = { '0,0' => $cell };

$cell->_set_type( $cells );

is ($cell->class(), 'group ga', 'group ga');

is ($cell->group( $group->{name} ), $group, "group()");

my $cell2 = Graph::Easy::Group::Cell->new( group => $group, x => 1, y => 0 );
is (scalar keys %{$group->{$c}}, 2, 'one more cell');
$cells->{'1,0'} = $cell2;

my $cell3 = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => -1 );
is (scalar keys %{$group->{$c}}, 3, 'one more cell');
$cells->{'0,-1'} = $cell3;

my $cell4 = Graph::Easy::Group::Cell->new( group => $group, x => 0, y => 1 );
is (scalar keys %{$group->{$c}}, 4, 'one more cell');
$cells->{'0,1'} = $cell4;

is ($cell2->group( $group->{name} ), $group, "group()");

$cell->_set_type( $cells );
is ($cell->class(), 'group gl', 'group gl');

#############################################################################
# attributes on cells

# The default attributes are returned by attribute():

is ($group->attribute('border-style'), 'dashed', 'group border');
is ($group->attribute('borderstyle'), 'dashed', 'group border');
is ($cell->attribute('border'), '', 'default border on this cell');
is ($cell->attribute('border-style'), 'dashed', 'default border on this cell');

is ($group->default_attribute('border-style'), 'dashed', 'group is dashed');
is ($cell->default_attribute('border'), 'dashed 1px #000000', 'dashed border on this cell');
is ($cell->default_attribute('border-style'), 'dashed', 'dashed border on this cell');

is ($group->default_attribute('fill'), '#a0d0ff', 'fill on group');
is ($group->attribute('fill'), '#a0d0ff', 'fill on group');
is ($cell->default_attribute('fill'), '#a0d0ff', 'fill on group cell');
is ($cell->attribute('fill'), '#a0d0ff', 'fill on group cell');

#############################################################################
# del_cell();

#print join (" ", keys %{$group->{cells}}),"\n";

is (scalar keys %{$group->{$c}}, 4, 'one less');
$group->_del_cell($cell);

is (scalar keys %{$group->{$c}}, 3, 'one less');
is ($cell->group(), undef, "no group() on deleted cell");

#############################################################################
# del_node() & del_edge(), when node/edge are in a group (bug until 0.39)

$graph = Graph::Easy->new();

$group = $graph->add_group('group');

my ($A,$B,$E) = $graph->add_edge('A','B','E');

for my $m ($A,$B,$E)
  {
  $group->add_member($m);
  }

is ($group->nodes(), 2, '2 nodes in group');
is ($group->edges(), 0, '0 edges going from/to group');
is ($group->edges_within(), 1, '1 edge in group');

is ($A->attribute('group'), $group->name(), 'group attribute got added');
$graph->del_node($A);

is ($A->attribute('group'), '', 'group attribute got deleted');
is ($group->nodes(), 1, '1 node in group');
is ($group->edges(), 0, '0 edges in group');

($A,$B,$E) = $graph->add_edge('A','B','E');

$group->add_member($A);
$group->add_member($E);

is ($group->nodes(), 2, '2 nodes in group');
is ($group->edges(), 0, '0 edges going from/to group');
is ($group->edges_within(), 1, '1 edge in group');

$graph->del_edge($E);

is ($group->nodes(), 2, '2 nodes in group');
is ($group->edges(), 0, '0 edges in group');
is ($group->edges_within(), 0, '0 edges in group');

#############################################################################
# successors and predecessors

$graph = Graph::Easy->new();

$group = $graph->add_group('group');

my ($g1,$bonn) = $graph->add_edge($group, 'Bonn');
my ($berlin,$g2) = $graph->add_edge('Berlin', $group);

is ($group->has_as_successor($bonn), 1, 'group -> bonn');
is ($group->has_as_successor($berlin), 0, '! group -> berlin');
is ($group->has_as_predecessor($berlin), 1, 'berlin -> group');
is ($group->has_as_predecessor($bonn), 0, '! bonn -> group');

is ($bonn->has_as_successor($group), 0, '! group -> bonn');
is ($berlin->has_as_predecessor($group), 0, 'group -> berlin');
is ($bonn->has_as_predecessor($group), 1, 'bonn -> group');

my @suc = $group->successors();

is (scalar @suc, 1, 'one successor');
is ($suc[0], $bonn, 'group => bonn');

#############################################################################
# add_node('Bonn'), add_member('Bonn','Berlin') etc.

$graph = Graph::Easy->new();

$group = $graph->add_group('group');
$bonn = $group->add_node('Bonn');

is (ref($bonn), 'Graph::Easy::Node', "add_node('Bonn') works for groups");

($bonn,$berlin) = $group->add_nodes('Bonn','Berlin');

is (ref($bonn), 'Graph::Easy::Node', "add_nodes('Bonn') works for groups");
is ($bonn->name(), 'Bonn', "add_nodes('Bonn') works for groups");
is (ref($berlin), 'Graph::Easy::Node', "add_nodes('Berlin') works for groups");
is ($berlin->name(), 'Berlin', "add_nodes('Berlin') works for groups");

# add_edge()
my $edge = $group->add_edge('Bonn','Kassel');

my $kassel = $graph->node('Kassel');

is (ref($kassel), 'Graph::Easy::Node', "add_edge('Bonn','Kassel') works for groups");

# add_edge_once()

$edge = $group->add_edge_once('Bonn','Kassel');

my @edges = $graph->edges('Bonn','Kassel');
is (scalar @edges, 1, 'one edge from Bonn => Kassel');

# add_edge() twice

$edge = $group->add_edge('Bonn','Kassel');

@edges = $graph->edges('Bonn','Kassel');
is (scalar @edges, 2, 'two edges from Bonn => Kassel');