#!/usr/bin/perl -w # Some basic GraphML tests with the format=yED use Test::More; use strict; use utf8; BEGIN { plan tests => 14; chdir 't' if -d 't'; use lib '../lib'; use_ok ("Graph::Easy") or die($@); use_ok ("Graph::Easy::Parser") or die($@); }; can_ok ('Graph::Easy', qw/ as_graphml as_graphml_file /); ############################################################################# my $graph = Graph::Easy->new(); my $graphml_file = $graph->as_graphml_file( format => 'yED' ); $graphml_file =~ s/\n.*\n//; _compare ($graph, $graphml_file, 'as_graphml and as_graphml_file are equal'); my $graphml = $graph->as_graphml( format => 'yED' ); like ($graphml, qr/<\?xml version="1.0" encoding="UTF-8"\?>/, 'as_graphml looks like xml'); ############################################################################# # some nodes and edges $graph->add_edge('Ursel','Viersen'); $graphml = $graph->as_graphml(); like ($graphml, qr/ { color: blue; } [B] $graph = Graph::Easy->new(); my ($A,$B,$edge) = $graph->add_edge('A','B'); $graph->set_attribute('node.foo','color','red'); $edge->set_attribute('color','blue'); $A->set_attribute('class','foo'); my $result = < black black red blue EOT ; _compare($graph, $result, 'GraphML with attributes'); ############################################################################# # some attributes with no default valu with no default value: # Also test escaping for valid XML: $edge->set_attribute('label', 'train-station & <Überlingen "Süd">'); $result = < black black red blue train-station & <Überlingen "Süd"> EOT2 ; _compare($graph, $result, 'GraphML with attributes'); ############################################################################# # node names with things that need escaping: $graph->rename_node('A', '<&\'">'); $result = < black black red blue train-station & <Überlingen "Süd"> EOT3 ; _compare($graph, $result, 'GraphML with attributes'); ############################################################################# # double attributes $graph = Graph::Easy->new(); ($A,$B,$edge) = $graph->add_edge('A','B'); my ($C,$D,$edge2) = $graph->add_edge('A','C'); $edge->set_attribute('label','car'); $edge2->set_attribute('label','train'); $result = < car train EOT4 ; _compare($graph, $result, 'GraphML with attributes'); ############################################################################# # as_graphml() with groups (bug until v0.63): $graph = Graph::Easy->new(); my $bonn = Graph::Easy::Node->new('Bonn'); my $cities = $graph->add_group('Cities"'); $cities->add_nodes($bonn); $result = < EOT5 ; _compare($graph, $result, 'GraphML with group'); # all tests done ############################################################################# ############################################################################# sub _compare { my ($graph, $result, $name) = @_; my $graphml = $graph->as_graphml( { format => 'yED' } ); $graphml =~ s/\n.*\n//; $result = < EOR . $result unless $result =~ /<\?xml/; if (!is ($result, $graphml, $name)) { eval { require Test::Differences; }; if (defined $Test::Differences::VERSION) { Test::Differences::eq_or_diff ($result, $graphml); } } }