File: 062xtmserialize.t

package info (click to toggle)
libtm-perl 1.56-7
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,692 kB
  • ctags: 1,084
  • sloc: perl: 35,266; makefile: 48
file content (269 lines) | stat: -rw-r--r-- 7,036 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
use strict;
use warnings;

use Test::More qw(no_plan);

use Data::Dumper;
$Data::Dumper::Indent = 1;
use TM::Materialized::AsTMa;

#== TESTS ===========================================================================
my $tm = new TM::Materialized::AsTMa (baseuri=>"tm://", inline=> '
nackertes_topic 

atop
bn: just a topic

btop (ctop dtop)
bn: something
bn@ascope: some other thing

ctop
bn: over the top!
in: something
in: somemore
oc: http://somewhere
in@ascope: scoped
in@ascope (sometype): also typed
oc (sometype): http://typedoc
oc @ascope (sometype): http://typedandscopedoc

(sucks-more-than)
sucker: ctop
winner: atop
winner: others

(sucks-more-than) @ascope
sucker: nobody
winner: nobody

thistop reifies http://rumsti
bn: reification
sin: http://nowhere.ever.never
sin: http://nowhere.never.ever

(sucks-more-than) is-reified-by atop
winner: nobody
sucker: nobody

thistop_name
bn: this will reify the thistop name
')->sync_in;

{
    my ($name) = $tm->match_forall (char => 1, topic => $tm->tids ('thistop'));
    $tm->internalize ('thistop_name' => $name->[TM->LID]);  # create a reification
}

Class::Trait->apply ($tm, "TM::Serializable::XTM");
can_ok $tm, 'serialize';

{
    my $content = $tm->serialize (version => '2.0');
#    warn $content;

    use XML::LibXML;
    my $xp  = XML::LibXML->new();
    my $doc = $xp->parse_string ($content);
#    use XML::LibXML::Dtd;
    my $dtd = XML::LibXML::Dtd->new("SOME // Public / ID / 1.0",'schemas/xtm20.dtd');
    ok ( $doc->validate($dtd), 'validates XTM 2.0');
}

{
    my $content = $tm->serialize (version => '2.0');
#    warn $content;exit;
    $content =~ s{xmlns="http://www.topicmaps.org/xtm/"}{};  # get rid of the namespace

    use XML::LibXML;
    my $xp  = XML::LibXML->new();
    my $doc = $xp->parse_string ($content);

    ok (eq_set ([
		 map { "tm://$_" }
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic/@id')
		 ],
		[
		 map { $_->[TM->LID] } $tm->toplets (\ '+all -infrastructure')
		 ]), 'topic ids');
    is ('2.0', $doc->findvalue ('/topicMap/@version'), 'version');

    ok (
	eq_set (
                [
                 map { $_->nodeValue }
		 $doc->findnodes('/topicMap/topic[@id="btop"]/instanceOf/topicRef/@href')
		],
	        [
	          '#ctop','#dtop'
		]), 'instances btop');

    ok ($doc->findnodes('/topicMap/association[itemIdentity/@href="068ce15eb7cf7cc4536d504c73a4c05c"]/type/topicRef[@href="#sucks-more-than"]'),
        'found assoc');
    
    ok (
	eq_set ([
		 map { $_->nodeValue }
		 $doc->findnodes('/topicMap/association[itemIdentity/@href="4abe49897cefeb950e4affaab0418e4f"]/role[type/topicRef/@href = "#winner"]/topicRef/@href')],
		[
		 '#atop', '#others'
		 ]
		), 'found assoc II');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="btop"]/name/value/text()')
		 ],
		['something', 'some other thing' ]), 'name value');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="btop"]/name[scope/topicRef/@href = "#ascope"]/value/text()')
		 ],
		['some other thing' ]), 'basename value, scoped');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="ctop"]/occurrence[type/topicRef/@href = "#sometype"]/resourceRef/@href')
		 ],
		[
		 'http://typedoc',
		 'http://typedandscopedoc'
		 ]), 'occ value, scoped and unscoped');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="ctop"]/occurrence[type/topicRef/@href = "#sometype"][scope/topicRef/@href = "#ascope"]/resourceRef/@href')
		 ],
		[
		 'http://typedandscopedoc'
		 ]), 'occ value, scoped');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="thistop"]/subjectIdentifier/@href')
		 ],
		[
		 'http://nowhere.ever.never',
		 'http://nowhere.never.ever'
		 ]), 'indicators');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[@id="thistop"]/subjectLocator/@href')
		 ],
		[
		 'http://rumsti'
		 ]), 'address');

    ok (! $doc->findnodes('/topicMap/topic[@id="ctop"]/subjectLocator'),    'no address');
    ok (! $doc->findnodes('/topicMap/topic[@id="ctop"]/subjectIdentifier'), 'no indicators');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes('/topicMap/topic[count(*) = 0]/@id')
		 ],
		[
		 'dtop',
		 'nackertes_topic',
		 'nobody',
		 'others',
		 'sometype',
		 'sucker',
		 'sucks-more-than',
		 'winner'
		 ]), 'empty topics');

    my ($aid) = map { $_->nodeValue } $doc->findnodes('/topicMap/association[@reifier = "atop"]/itemIdentity/@href');
#    warn $aid;

    ok ($aid =~ /^.{32}$/, 'internal reification');

    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes(qq|/topicMap/association[itemIdentity/\@href="$aid"]/role
						          [type/topicRef/\@href = "#winner"]
						          /topicRef/\@href|)
		 ],
		[
		 '#nobody'
		 ]), 'reified assoc, role + players');
    ok (
	eq_set ([
		 map { $_->nodeValue } $doc->findnodes(qq|/topicMap/association[itemIdentity/\@href="$aid"]/role
						          [type/topicRef/\@href = "#sucker"]
						          /topicRef/\@href|)
		 ],
		[
		 '#nobody'
		 ]), 'reified assoc, role + players II');

#print Dumper [
#	      map { $_->nodeValue } $doc->findnodes('/topicMap/topic[count(*) = 0]/@id')
#	     ];
}

{
    my $content = $tm->serialize (omit_trivia => 1, version => '2.0');
    $content =~ s{xmlns="http://www.topicmaps.org/xtm/"}{};  # get rid of the namespace

    use XML::LibXML;
    my $xp  = XML::LibXML->new();
    my $doc = $xp->parse_string($content);
    
    ok (eq_set ([
		 map { "tm://$_" }
		 (
		  'dtop',
		  'nackertes_topic',
		  'nobody',
		  'others',
		  'sometype',
		  'sucker',
		  'sucks-more-than',
		  'winner',
		  map { $_->nodeValue } $doc->findnodes('/topicMap/topic/@id')
		  )
		 ],
		[
		 map { $_->[TM->LID] } $tm->toplets (\ '+all -infrastructure')
		 ]), 'topic ids (trivia)');
}

{
    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    can_ok $tm2, 'deserialize';

    $tm2->deserialize ($tm->serialize (version => '2.0'));

    is_deeply( $tm->{mid2iid},    $tm2->{mid2iid},    'toplet structure identical' );
#warn Dumper $tm2;
#    foreach my $a (keys %{ $tm2->{assertions} }) {
#	warn "checking $a";
#	ok ($tm->{assertions}->{$a}, $a . " exists in tm");
#    }
#    foreach my $a (keys %{ $tm->{assertions} }) {
#	warn "checking $a";
#	ok ($tm2->{assertions}->{$a}, $a . " exists in tm2");
#	warn Dumper $tm->{assertions}->{$a} unless $tm2->{assertions}->{$a};
#    }

    is_deeply( $tm->{assertions}, $tm2->{assertions}, 'asserts structure identical' );
}

{
    my $content = $tm->serialize (version => '2.0');

    my $tm2 = new TM (baseuri=>"tm://");
    Class::Trait->apply ($tm2, "TM::Serializable::XTM");
    $tm2->deserialize ($content);
    my $content2 = $tm2->serialize;
    is ($content, $content2, 'round tripping');

#    use Text::Diff;
#    my $diff = diff \$content, \$content2, { STYLE => "Context" }; #,   \%options;
#    warn Dumper $diff;
}

__END__