/
graphml_yed.t
234 lines (184 loc) · 5.96 KB
/
graphml_yed.t
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
#!/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/<node.*id="Ursel"/, 'as_graphml contains nodes');
like ($graphml, qr/<node.*id="Viersen"/, 'as_graphml contains nodes');
like ($graphml, qr/<edge.*source="Ursel"/, 'as_graphml contains edge');
like ($graphml, qr/<edge.*target="Viersen"/, 'as_graphml contains edge');
#############################################################################
# some attributes:
# node.foo { color: red; } [A] {class:foo;}-> { 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 = <<EOT
<key id="d0" for="node" attr.name="color" attr.type="string">
<default>black</default>
</key>
<key id="d1" for="edge" attr.name="color" attr.type="string">
<default>black</default>
</key>
<graph id="G" edgedefault="directed">
<node id="A">
<data key="d0">red</data>
</node>
<node id="B">
</node>
<edge source="A" target="B">
<data key="d1">blue</data>
</edge>
</graph>
</graphml>
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 = <<EOT2
<key id="d0" for="node" attr.name="color" attr.type="string">
<default>black</default>
</key>
<key id="d1" for="edge" attr.name="color" attr.type="string">
<default>black</default>
</key>
<key id="d2" for="edge" attr.name="label" attr.type="string"/>
<graph id="G" edgedefault="directed">
<node id="A">
<data key="d0">red</data>
</node>
<node id="B">
</node>
<edge source="A" target="B">
<data key="d1">blue</data>
<data key="d2">train-station & <Überlingen "Süd"></data>
</edge>
</graph>
</graphml>
EOT2
;
_compare($graph, $result, 'GraphML with attributes');
#############################################################################
# node names with things that need escaping:
$graph->rename_node('A', '<&\'">');
$result = <<EOT3
<key id="d0" for="node" attr.name="color" attr.type="string">
<default>black</default>
</key>
<key id="d1" for="edge" attr.name="color" attr.type="string">
<default>black</default>
</key>
<key id="d2" for="edge" attr.name="label" attr.type="string"/>
<graph id="G" edgedefault="directed">
<node id="<&'">">
<data key="d0">red</data>
</node>
<node id="B">
</node>
<edge source="<&'">" target="B">
<data key="d1">blue</data>
<data key="d2">train-station & <Überlingen "Süd"></data>
</edge>
</graph>
</graphml>
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 = <<EOT4
<key id="d0" for="edge" attr.name="label" attr.type="string"/>
<graph id="G" edgedefault="directed">
<node id="A">
</node>
<node id="B">
</node>
<node id="C">
</node>
<edge source="A" target="B">
<data key="d0">car</data>
</edge>
<edge source="A" target="C">
<data key="d0">train</data>
</edge>
</graph>
</graphml>
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
<graph id="G" edgedefault="directed">
<graph id="Cities"" edgedefault="directed">
<node id="Bonn">
</node>
</graph>
</graph>
</graphml>
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
<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:y="http://www.yworks.com/xml/graphml"
xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
http://www.yworks.com/xml/schema/graphml/1.0/ygraphml.xsd">
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);
}
}
}