File: svg.t

package info (click to toggle)
libgraph-easy-as-svg-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 1,924; makefile: 7
file content (352 lines) | stat: -rw-r--r-- 11,221 bytes parent folder | download | duplicates (2)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env perl

use Test::More;
use strict;

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

use Graph::Easy::Edge::Cell qw/EDGE_END_E EDGE_END_N EDGE_END_S EDGE_END_W EDGE_HOR/;

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

is (ref($graph), 'Graph::Easy');

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

is ($graph->nodes(), 0, '0 nodes');
is ($graph->edges(), 0, '0 edges');

is (join (',', $graph->edges()), '', '0 edges');

# this will load As_svg:
my $svg = $graph->as_svg();

# after loading As_svg, this should work:
can_ok ('Graph::Easy::Node', qw/as_svg/);
can_ok ('Graph::Easy', qw/as_svg_file/);
can_ok ('Graph::Easy::As_svg', qw/_text_length/);

like ($svg, qr/enerated at .* by/, 'contains generator notice');
like ($svg, qr/<svg/, 'looks like SVG');
like ($svg, qr/<\/svg/, 'looks like SVG');
like ($svg, qr/1\.1/, 'looks like SVG v1.1');
like ($svg, qr/\.node/, 'contains .node class');

#############################################################################
# with some nodes

my $bonn = Graph::Easy::Node->new( name => 'Bonn' );
my $berlin = Graph::Easy::Node->new( 'Berlin' );

$graph->add_edge ($bonn, $berlin);

$svg = $graph->as_svg();

like ($svg, qr/Bonn/, 'contains Bonn');
like ($svg, qr/Berlin/, 'contains Berlin');
like ($svg, qr/<text/, 'contains <text');

like ($svg, qr/<rect/, 'contains <rect');
like ($svg, qr/<line/, 'contains <line (for edge)');

unlike ($svg, qr/<text.*?><\/text>/, "doesn't contain empty text tags");

#print $graph->as_svg(),"\n";

#############################################################################
# as_svg_file

$svg = $graph->as_svg_file();

like ($svg, qr/Bonn/, 'contains Bonn');
like ($svg, qr/standalone="yes"/, 'standalone');
like ($svg, qr/xmlns="/, 'xmlns');
like ($svg, qr/<\?xml/, 'contains <xml');

#print $graph->as_svg(),"\n";

#############################################################################
#############################################################################
# edge drawing (line_straigh)

sub LINE_HOR () { 0; }
sub LINE_VER () { 1; }

my $edge = Graph::Easy::Edge->new();
my $cell = Graph::Easy::Edge::Cell->new( edge => $edge, type => EDGE_HOR);
$cell->{w} = 100;
$cell->{h} = 50;

$svg = join ('', $cell->_svg_line_straight(0, 0, LINE_HOR(), 0.1, 0.1 ));
is ($svg, '<line x1="10" y1="25" x2="90" y2="25" />', 'line hor');

$svg = join ('', $cell->_svg_line_straight(0, 0, LINE_VER(), 0.1, 0.1 ));
is ($svg, '<line x1="50" y1="5" x2="50" y2="45" />', 'line ver');

$svg = join ('', $cell->_svg_line_straight(0, 0, LINE_VER(), 0.1, 0.1 ));
is ($svg, '<line x1="50" y1="5" x2="50" y2="45" />', 'line ver');

#############################################################################
# arrorw drawing

$svg = $cell->_svg_arrow({}, 0, 0, EDGE_END_E, , '' );
is ($svg, '<use xlink:href="#ah" x="90" y="25"/>'."\n", 'arrowhead east');

$svg = $cell->_svg_arrow({}, 0, 0, EDGE_END_N, , '' );
is ($svg, '<use xlink:href="#ah" transform="translate(50 5)rotate(-90)"/>'."\n", 'arrowhead north');

$svg = $cell->_svg_arrow({}, 0, 0, EDGE_END_S, , '' );
is ($svg, '<use xlink:href="#ah" transform="translate(50 45)rotate(90)"/>'."\n", 'arrowhead south');

#############################################################################
# with some nodes with attributes

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

$edge = $graph->add_edge ($bonn, $berlin);

$bonn->set_attribute( 'shape' => 'circle' );

is ($bonn->predecessors(), 0, 'no pre');
is ($berlin->successors(), 0, 'no pre');
is ($bonn->successors(), 1, 'one pre');
is ($berlin->predecessors(), 1, 'one pre');

is (keys %{$graph->{cells}}, 0, 'no cells');
is ($bonn->{graph}, $graph, 'graph is ok');
is ($berlin->{graph}, $graph, 'graph is ok');
is ($edge->{graph}, $graph, 'graph on edge is ok');

$svg = $graph->as_svg();

like ($svg, qr/Bonn/, 'contains Bonn');
like ($svg, qr/Berlin/, 'contains Bonn');
like ($svg, qr/circle/, 'contains circle shape');

#print $graph->as_svg(),"\n";

$bonn->set_attribute( 'shape' => 'rounded' );

$svg = $graph->as_svg();

like ($svg, qr/Bonn/, 'contains Bonn');
like ($svg, qr/Berlin/, 'contains Bonn');
like ($svg, qr/rect.*rx/, 'contains rect shape with rx/ry');
like ($svg, qr/rx="15" ry="15"/, 'contains rect shape with rx/ry');
like ($svg, qr/line/, 'contains edge');
like ($svg, qr/text/, 'contains text');
like ($svg, qr/#ah/, 'contains arrowhead');

#print $graph->as_svg(),"\n";

$edge->set_attribute('style', 'double-dash');

$graph->layout();

$svg = $graph->as_svg();
like ($svg, qr/stroke-dasharray/, 'double dash contains dash array');

#############################################################################
# unused definitions are not in the output

unlike ($svg, qr/(diamond|circle|triangle)/, 'unused defs are not there');

#############################################################################
# color on edge labels

$edge->set_attribute('color', 'orange');

$svg = $graph->as_svg();
like ($svg, qr/stroke="#ffa500"/, 'orange stroke on edge');
unlike ($svg, qr/color="#ffa500"/, 'no orange color on edge');
unlike ($svg, qr/fill="#ffa500"/, 'no orange fill on edge');

$edge->set_attribute('label', 'Schmabel');

is ($edge->label(), 'Schmabel', 'edge label');

$svg = $graph->as_svg();
like ($svg, qr/stroke="#ffa500"/, 'orange stroke on edge');
like ($svg, qr/fill="#ffa500"/, 'orange color on edge label');
unlike ($svg, qr/color="#ffa500"/, 'no orange color on edge');

#############################################################################
# text-style support

$edge->set_attribute('text-style', 'bold underline');

$svg = $graph->as_svg();
like ($svg, qr/font-weight="bold" text-decoration="underline"/, 'text-style');

$edge->set_attribute('text-style', 'bold underline overline');

$svg = $graph->as_svg();
like ($svg, qr/font-weight="bold" text-decoration="underline overline"/, 'text-style');

#############################################################################
# font-size support

$edge->set_attribute('font-size', '2em');

$svg = $graph->as_svg();
my $expect = $graph->EM() * 2;
like ($svg, qr/style=".*font-size:${expect}px"/, '2em');

#############################################################################
# <title>

$svg = $graph->as_svg();
like ($svg, qr/<title>Untitled graph<\/title>/, 'no title by default');

$graph->set_attribute('graph','label', 'My Graph');
$svg = $graph->as_svg();
like ($svg, qr/<title>My Graph<\/title>/, 'set title');

$graph->set_attribute('graph','title', 'My Graph Title');
$svg = $graph->as_svg();
like ($svg, qr/<title>My Graph Title<\/title>/, 'title overrides label');


#############################################################################
# support for rotate

$bonn->set_attribute( 'rotate' => 'right' );

is ($bonn->attribute('rotate'), 'right', 'rotate right is +90 degrees');
is ($bonn->angle(), '180', 'rotate right is 90 (default) +90 == 180 degrees');

$svg = $graph->as_svg();
like ($svg, qr/transform="rotate\(180,/, 'rotate right => 180');

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

$bonn->set_attribute( 'label' => 'My\nMultiline' );

$svg = $graph->as_svg();
unlike ($svg, qr/<tspan[^>]+><\/tspan>/, 'no empty tspan');

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

$bonn->set_attribute( 'label' => 'dontseeme' );
$bonn->set_attribute( 'shape' => 'point');
$bonn->set_attribute( 'point-style' => 'invisible');

$svg = $graph->as_svg();
like ($svg, qr/<!-- dontseeme/, 'invisible');
unlike ($svg, qr/invisible/, 'no "invisible" in svg');

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

$bonn->set_attribute( 'label' => 'quote & < > "' );
$bonn->set_attribute( 'shape' => 'rect');
$bonn->del_attribute( 'point-style');

$svg = $graph->as_svg();
like ($svg, qr/<!-- quote &amp; &lt; &gt; ",/, 'quoted');
like ($svg, qr/>quote &amp; &lt; &gt; &quot;<\/text>/, 'quoted');

#############################################################################
# check that node.cities is converted to "node_cities"

$bonn->set_attribute( 'class' => 'cities' );

$svg = $graph->as_svg();
like ($svg, qr/class="node_cities"/, 'node.cities => node_cities');
unlike ($svg, qr/.node,\s*.node_cities/, 'no class style cities yet' );

$graph->set_attribute( 'node.cities', 'color', 'red' );
$svg = $graph->as_svg();

like ($svg, qr/class="node_cities"/, 'node.cities => node_cities');
like ($svg, qr/.node,\s*.node_cities/, 'node.cities => node_cities');

#############################################################################
# edges with no fill but arrowstyle: fill

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

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

$edge->set_attribute('arrowstyle','filled');
$edge->set_attribute('color','green');

$svg = $graph->as_svg();

like ($svg, qr/fill="#008000"/, 'edge fill is not inherit');

#############################################################################
# check that we really filter out labelpos etc.

$graph = Graph::Easy->new();
$edge = $graph->add_edge ('A','B');
$edge->set_attribute('arrow-shape','triangle');
$edge->set_attribute('arrow-style','open');
$graph->set_attribute('label-pos','bottom');
$graph->set_attribute('text-style','bold');
$graph->node('A')->set_attribute('auto-title','label');
$graph->node('B')->set_attribute('auto-label','10');

$svg = $graph->as_svg();

for my $not (qw/labelpos arrowshape arrowstyle autotitle autolabel textstyle/)
  {
  unlike ($svg, qr/$not/, "$not not output");
  }

#############################################################################
# see that we output the font for the graph itself

$graph = Graph::Easy->new();
$edge = $graph->add_edge ('A','B');
$graph->set_attribute('font','Foo');
$graph->set_attribute('label','Labels');

$svg = $graph->as_svg();

like ($svg, qr/font-family: Foo/, "font-family was output");

#############################################################################
# see that we output the font for the nodes

$graph = Graph::Easy->new();
$edge = $graph->add_edge ('A','B');
$graph->set_attribute('font','Foo');
$graph->node('A')->set_attribute('font','Fooobar');

$svg = $graph->as_svg();

like ($svg, qr/font-family:Fooobar/, "font-family for node was output");

#############################################################################
# output background for rounded nodes in groups

$graph = Graph::Easy->new();
my ($A,$B);
($A,$B,$edge) = $graph->add_edge ('A','B');
my $group = $graph->add_group ('');
$group->add_node($A);
$graph->node('A')->set_attribute('shape','rounded');

$svg = $graph->as_svg();

# rect x="19" y="19" width="5" height="3" fill="#a0d0ff"
like ($svg, qr/rounded(.|\n)+rect.+fill=".a0d0ff"/, "background for rounded node");

#############################################################################
# quote "&" in links as well as add links on edges

$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge ('A','B','test');
$edge->set_attribute('link','http://bloodgate.com/?foo=a&bar=b');

$svg = $graph->as_svg();

like ($svg, qr/xlink:href="http:\/\/bloodgate.com.*\&amp;/, "link has &amp;");