Debian Codegraph @

t/03complex.t

raw
1 =pod
2
3 =encoding utf-8
4
5 =head1 PURPOSE
6
7 Serialize a more complex graph and check the result is isomorphic
8 to the input.
9
10 =head1 AUTHOR
11
12 Toby Inkster E<lt>[email protected]<gt>.
13
14 =head1 COPYRIGHT AND LICENCE
15
16 This software is copyright (c) 2013 by Toby Inkster.
17
18 This is free software; you can redistribute it and/or modify it under
19 the same terms as the Perl 5 programming language system itself.
20
21 =cut
22
23 use strict;
24 use warnings;
25 use Test::More;
26
27 use JSON qw( to_json -convert_blessed_universally );
28 use RDF::Trine;
29 use match::smart qw(match);
30
31 require RDF::Trine::Graph;
32 require RDF::Trine::Model;
33 require RDF::Trine::Parser::Turtle;
34 require RDF::Trine::Serializer::Turtle;
35 require RDF::TrineX::Serializer::MockTurtleSoup;
36
37 require RDF::Prefixes;
38 plan match("RDF::Prefixes"->VERSION, [qw(0.003 0.004)])
39 ? (tests => 3)
40 : (skip_all => "tests designed for RDF::Prefixes 0.003/0.004");
41
42 sub check
43 {
44 my ($input, $opts, $expected) = @_;
45
46 my $do_str_test = !!delete($opts->{str_test});
47 my $prio = delete($opts->{priorities}) and $opts->{priorities} = 1;
48
49 subtest sprintf("testing with opts %s", to_json($opts, {canonical=>1,convert_blessed=>1})), sub
50 {
51 plan tests => ($do_str_test ? 2 : 1);
52
53 my $mts = "RDF::TrineX::Serializer::MockTurtleSoup"->new(%$opts, priorities => $prio);
54 my $got = $mts->serialize_model_to_string($input);
55
56 is($got, $expected, "serialized string matches") if $do_str_test;
57
58 my $model = "RDF::Trine::Model"->new;
59 "RDF::Trine::Parser::Turtle"->new->parse_into_model(
60 "http://localhost/",
61 $got,
62 $model,
63 );
64
65 my $g1 = "RDF::Trine::Graph"->new($input);
66 my $g2 = "RDF::Trine::Graph"->new($model);
67 ok($g1->equals($g2), "graphs are isomorphic") or diag($got);
68 };
69 }
70
71 my $model = "RDF::Trine::Model"->new;
72 "RDF::Trine::Parser::Turtle"->new->parse_file_into_model(
73 "http://localhost/",
74 \*DATA,
75 $model,
76 );
77
78 check($model, { str_test => 0 }, "");
79 check($model, { str_test => 0, repeats => 1 }, "");
80 check($model, { str_test => 0, priorities => sub { return 100 if $_[1]->is_blank; return } }, "");
81
82 __DATA__
83 @base <http://example.com/>.
84
85 <foo>
86 <bar>
87 ( _:baz _:quux ) .
88
89 _:quux <xyzzy> _:baz .
90
91 <bar> a _:monkey .
92 <baz> a _:monkey, _:puzzle .
93
94 _:puzzle a <foo>.
95
96 _:cyclic <cycle> _:cyclic ; <branch> _:branch .