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 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
|
#!/usr/bin/perl -w
use Test::More;
use strict;
BEGIN
{
plan tests => 74;
chdir 't' if -d 't';
use lib '../lib';
use_ok ("Graph::Easy") or die($@);
};
#############################################################################
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');
my $html = $graph->as_html();
like ($html, qr/<table/, 'looks like HTML to me');
#############################################################################
# with some nodes
my $bonn = Graph::Easy::Node->new( name => 'Bonn' );
my $berlin = Graph::Easy::Node->new( 'Berlin' );
my $edge = $graph->add_edge ($bonn, $berlin);
$html = $graph->as_html();
like ($html, qr/Bonn/, 'contains Bonn');
like ($html, qr/Berlin/, 'contains Berlin');
#############################################################################
# with some nodes with attributes
$bonn->set_attribute( 'autotitle' => 'name' );
$html = $graph->as_html();
like ($html, qr/title='Bonn'/, 'contains title="Bonn"');
unlike ($html, qr/title=['"]Berlin['"]/, "doesn't contain title Berlin");
#############################################################################
# edges do not have a name, will fallback to the label
$edge->set_attribute( 'autotitle' => 'name' );
$html = $graph->as_html();
like ($html, qr/title='Bonn'/, 'contains title="Bonn"');
unlike ($html, qr/title=['"]Berlin['"]/, "doesn't contain title Berlin");
unlike ($html, qr/title=['"]['"]/, "no empty title");
$edge->set_attribute( 'label' => 'my edge' );
$html = $graph->as_html();
like ($html, qr/title="my edge"/, 'contains title="my edge"');
#############################################################################
# check that "shape:" does not appear in CSS or HTML
$bonn->set_attribute( 'shape' => 'circle' );
$graph->set_attribute ( 'node', 'shape', 'ellipse' );
my $css = $graph->css();
$html = $graph->as_html();
unlike ($css, qr/shape/, 'shape does not appear in CSS');
unlike ($html, qr/shape/, 'shape does not appear in HTML');
#############################################################################
# "shape: invisible" should result in an empty td tag w/ "border: none"
$bonn->set_attribute( 'shape' => 'invisible' );
$css = $graph->css();
$html = $graph->as_html();
unlike ($html, qr/display:\s*none/, 'shape invisible is not display: none');
like ($html, qr/td.*border:\s*none/, 'shape invisible results in border: none');
#############################################################################
# label colors
$graph->set_attribute( 'edge', 'label-color' => 'blue' );
$edge->set_attribute( 'label-color' => 'red' );
$css = $graph->css();
$html = $graph->as_html();
unlike ($html, qr/border-bottom:.*;\s*color: #0000ff/, 'no edge is green');
like ($html, qr/border-bottom:.*;\s*color: #ff0000/, 'some edge is red');
#############################################################################
# edge color vs. label colors
$edge->set_attribute( 'color' => 'green' );
$html = $graph->as_html();
unlike ($html, qr/border-bottom:.*#0000ff/, 'no edge got blue');
unlike ($html, qr/border-bottom:.*;\s*color: #0000ff/, 'no edge got blue');
like ($html, qr/border-bottom:.*#008000.*;\s*color: #ff0000/,
'color green, label-color red');
#############################################################################
# caption from label
$graph->set_attribute( 'graph', 'label' => 'My Graph Label' );
$html = $graph->as_html();
like ($html, qr/<td colspan=12 style="text-align: center">My Graph Label<\/td>/,
'graph caption from label');
#############################################################################
# caption with label-pos
$graph->set_attribute( 'graph', 'label' => 'My Graph Label' );
$graph->set_attribute( 'graph', 'label-pos' => 'bottom' );
$html = $graph->as_html();
like ($html, qr/<td colspan=12 style="text-align: center">My Graph Label<\/td>/,
'graph caption from label');
#############################################################################
# html_file includes <title> and charset:
$html = $graph->as_html_file();
my $charset =
quotemeta('<meta http-equiv="Content-Type" content="text/html; charset=utf-8">');
like ($html, qr/$charset/, 'html_file includes charset definition');
like ($html, qr/<title>My Graph Label<\/title>/, 'html_file includes <title>');
#############################################################################
# egdes with links, titles and colors
$graph = Graph::Easy->new();
$edge = $graph->add_edge('Friedrichshafen', 'Immenstaad');
$edge->set_attribute('title', 'Vrooom!');
$edge->set_attribute('color', 'orange');
$edge->set_attribute('text-style', 'none');
$edge->set_attribute('font-size', '1.5em');
$edge->set_attribute('link', 'http://bloodgate.com');
$edge->set_attribute('label', 'Schiff');
# This tests edge->as_html(), which will not be called for normal operations,
# in these cases we would convert the single edge cells to HTML.
my $edge_html = <<EDGE
<td colspan=4 rowspan=4 class='edge' title='Vrooom!'><a href='http://bloodgate.com' style="color: #ffa500; text-decoration: none; font-size: 1.5em">Schiff</a></td>
EDGE
;
is ($edge->as_html(), $edge_html, 'edge->as_html()');
# entire graph as html
$html = $graph->as_html();
$edge_html = <<EDGE_CELL
<td colspan=2 rowspan=2 class="edge lh" style="border-bottom: solid 2px #ffa500;" title="Vrooom!"><a href='http://bloodgate.com' style='color: #ffa500; text-decoration: none; font-size: 1.5em;'>Schiff</a></td>
EDGE_CELL
;
my $like = quotemeta($edge_html);
like ($html, qr/$like/, 'graph->as_html() contains proper edge html');
#############################################################################
# edge style double, double-dash, bold etc
$graph = Graph::Easy->new();
$edge = $graph->add_edge('Friedrichshafen', 'Immenstaad');
$edge->set_attribute('style', 'double');
$edge_html = <<EDGE_2
<td colspan=4 rowspan=4 class='edge'></td>
EDGE_2
;
is ($edge->as_html(), $edge_html, 'edge->as_html()');
$edge_html = <<EDGE_CELL
<td colspan=2 rowspan=2 class="edge lh" style="border-bottom: double #000000;"> </td>
EDGE_CELL
;
$like = quotemeta($edge_html);
$html = $graph->as_html();
like ($html, qr/$like/, 'edge->as_html()');
$edge->set_attribute('style', 'double-dash');
$edge_html = <<EDGE_CELL
<td colspan=2 rowspan=2 class="edge lh" style="border-bottom: double #000000;"> </td>
EDGE_CELL
;
$like = quotemeta($edge_html);
$html = $graph->as_html();
like ($html, qr/$like/, 'edge->as_html()');
#############################################################################
# edge color and label-color
$edge->set_attribute('label-color', 'blue');
$edge_html = <<EDGE_CELL
<td colspan=2 rowspan=2 class="edge lh" style="border-bottom: double #000000;color: #0000ff;"> </td>
EDGE_CELL
;
$like = quotemeta($edge_html);
$html = $graph->as_html();
like ($html, qr/$like/, 'edge->as_html()');
#############################################################################
# a node with a link and a fill color at the same time
my $f = $graph->node('Friedrichshafen');
$f->set_attribute('link', 'http://bloodgate.com');
$f->set_attribute('fill', 'red');
$html = $f->as_html();
is ($html, <<EOF
<td colspan=4 rowspan=4 class='node' style="background: #ff0000"><a href='http://bloodgate.com'>Friedrichshafen</a></td>
EOF
, 'fill is on the TD, not the A HREF');
#############################################################################
# a node with a link and a border at the same time
$f->set_attribute('border', 'orange');
$html = $f->as_html();
is ($html, <<EOF
<td colspan=4 rowspan=4 class='node' style="background: #ff0000;border: solid 1px #ffa500"><a href='http://bloodgate.com'>Friedrichshafen</a></td>
EOF
, 'border is on the TD, not the A HREF');
#############################################################################
# as_html_file() includes the proper classes
$html = $graph->as_html_file();
for my $c (qw/eb lh lv va el sh shl/)
{
like ($html, qr/table.graph \.$c/, "includes '$c'");
}
#############################################################################
# group labels are left-aligned
$graph = Graph::Easy->new();
my $group = $graph->add_group('Cities');
my ($A,$B) = $graph->add_edge('Krefeld', 'Düren');
$group->add_nodes($A,$B);
$css = $graph->css();
like ($css, qr/group[^\}]*text-align: left;/, 'contains text-align: left');
#############################################################################
# setting a graph color does not override nodes/edges/groups
$graph->set_attribute('color', 'red');
$css = $graph->css();
for my $e (qw/node_anon edge group_anon/)
{
unlike ($css, qr/table.graph\s+\.$e\s+\{[^\}]*[^-]color: #ff0000;/m, "contains not $e color red");
}
#############################################################################
# setting a graph font/fill does not override nodes/edges/groups
$graph->set_attribute('font', 'times');
$graph->set_attribute('fill', 'blue');
$graph->set_attribute('font-size', '8em');
$graph->set_attribute('align', 'left');
$css = $graph->css();
unlike ($css, qr/table.graph\s+\{[^\}]*font-family: /m, "doesn't contain font-family");
unlike ($css, qr/table.graph\s+\{[^\}]*fill: /m, "doesn't contain fill");
unlike ($css, qr/table.graph\s+\{[^\}]*color: /m, "doesn't contain color");
unlike ($css, qr/table.graph\s+\{[^\}]*background[^\}]*background/m, "doesn't contain two times background");
unlike ($css, qr/table.graph\s+\{[^\}]*text-align/m, "doesn't contain font-size");
unlike ($css, qr/table.graph\s+\{[^\}]*font-size/m, "doesn't contain text-align");
#############################################################################
# multiline labels with \c, \r, and \l in them
$graph = Graph::Easy->new();
($A,$B) = $graph->add_edge('Köln', 'Rüdesheim');
$A->set_attribute('label', 'Köln\r(am Rhein)\l(NRW)\c(Deutschland)');
$html = $graph->as_html_file();
like ($html,
qr/class='node'>Köln<br><span class="r">\(am Rhein\)<\/span><br><span class="l">\(NRW\)<\/span><br>\(Deutschland\)</,
'Köln with multiline text');
$A->set_attribute('align', 'right');
$html = $graph->as_html_file();
like ($html,
qr/class='node' style="text-align: center"><span class="r">Köln<\/span><br><span class="r">\(am Rhein\)<\/span><br><span class="l">\(NRW\)<\/span><br>\(Deutschland\)</,
'Köln with multiline text');
#############################################################################
# multiline labels with "textwrap: N;"
$graph = Graph::Easy->new();
($A,$B) = $graph->add_edge('Köln', 'Rüdesheim');
$A->set_attribute('label', 'Köln\r(am Rhein)\l(NRW)\c(Deutschland)');
$A->set_attribute('textwrap', 10);
#print join (" ", $A->_label_as_html() );
$html = $graph->as_html_file();
like ($html,
qr/class='node'>Köln \(am<br>Rhein\)<br>\(NRW\)<br>\(Deutschland\)</,
'Köln with multiline text');
#############################################################################
# invisible edges
$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge('Hamm', 'Hagen');
$edge->set_attribute('style','invisible');
$edge->set_attribute('label','foobarbaz');
$edge->set_attribute('color','red');
$html = $graph->as_html_file();
unlike ($html, qr/invisible/, 'no border on invisible edges');
unlike ($html, qr/#ff0000/, 'no color on invisible edges');
unlike ($html, qr/foobarbaz/, 'no label on invisible edges');
#############################################################################
# inheritance of attributes via classes
$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge('green', 'blue.foo');
$graph->set_attribute('color','red');
$graph->set_attribute('node','color','blue');
$graph->set_attribute('node.foo','color','inherit');
$graph->set_attribute('node.bar','color','green');
$graph->set_attribute('edge','color','inherit');
$graph->set_attribute('edge.foo','color','inherit');
$A->set_attribute('class','bar');
$B->set_attribute('class','foo');
$edge->set_attribute('class','foobar'); # no color set
my ($C,$D,$E) = $graph->add_edge('blue','red');
$E->set_attribute('class','foo'); # inherits red
$D->set_attribute('color','inherit'); # inherits red from graph
is ($A->attribute('color'),'green', 'node.bar is green');
is ($B->attribute('color'),'blue', 'node.foo inherits blue from node');
is ($C->attribute('color'),'blue', 'node is just blue');
is ($D->attribute('color'),'red', 'inherits red from graph');
is ($edge->attribute('color'),'black', 'no color set, so defaults to black');
is ($E->attribute('color'),'red', 'inherit red from graph');
#############################################################################
# comments
$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge('green', 'blue.foo');
$graph->set_attribute('comment', 'My comment --> graph');
$A->set_attribute('comment', 'My comment --> A');
$edge->set_attribute('comment', 'My comment --> edge');
$html = $graph->as_html_file();
like ($html, qr/<!-- My comment --> graph -->/, 'graph comment');
like ($html, qr/<!-- My comment --> A -->/, 'node comment');
like ($html, qr/<!-- My comment --> edge -->/, 'edge comment');
#############################################################################
# colorscheme and class attributes
$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge('A', 'B');
$graph->set_attribute('colorscheme', 'pastel19');
$graph->set_attribute('node.yellow', 'fill', '1');
$graph->set_attribute('node.yellow', 'color', 'silver');
$A->set_attribute('class', 'yellow');
$html = $graph->as_html_file();
like ($html,
qr/node_yellow(.|\n)*background: #fbb4ae;/, 'background is not 1');
like ($html,
qr/node_yellow(.|\n)*color: silver;/, 'color is silver');
#############################################################################
# support for \N, \E, \H, \T, \G in titles and labels
$graph = Graph::Easy->new();
($A,$B,$edge) = $graph->add_edge('A', 'B');
$graph->set_attribute('label', 'My Graph');
$graph->set_attribute('node', 'title', 'My \N in \G');
$graph->set_attribute('edge', 'title', 'My \E in \G (\T => \H)');
$html = $graph->as_html_file();
like ($html, qr/title='My A in My Graph'/, 'title with \N and \G');
like ($html, qr/title='My B in My Graph'/, 'title with \N and \G');
like ($html, qr/title="My A->B in My Graph \(A => B\)"/, 'title with \E, \H, \T');
# support for \L in titles
$graph->set_attribute('node', 'label', 'labeled "My \N"');
$graph->set_attribute('node', 'title', 'My \L');
$html = $graph->as_html_file();
like ($html, qr/title='My labeled "My A"'/, 'title with \L');
|