PageRenderTime 53ms CodeModel.GetById 1ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/XML-FeedPP-Plugin-DumpJSON/t/42_slim_pp.t

http://xml-treepp.googlecode.com/
Unknown | 183 lines | 170 code | 13 blank | 0 comment | 0 complexity | a6e8926d63fc267d49182f11194b6e8c MD5 | raw file
  1# ----------------------------------------------------------------
  2    use strict;
  3    use Test::More;
  4# ----------------------------------------------------------------
  5{
  6    plan skip_all => 'JSON.pm is not supported at the moment.' unless exists $ENV{USE_JSON_PP};
  7    local $@;
  8    eval { require JSON; };
  9    plan skip_all => 'JSON.pm is not loaded.' if $@;
 10    plan tests => 62;
 11    use_ok('XML::FeedPP');
 12    ok( defined $JSON::VERSION, "JSON $JSON::VERSION" );
 13}
 14# ----------------------------------------------------------------
 15sub __decode_json {
 16    my $data = shift;
 17
 18    my $ver = ( $JSON::VERSION =~ /^([\d\.]+)/ )[0];
 19    if ( $ver < 1.99 ) {
 20        my $json = JSON->new();
 21        return $json->jsonToObj($data);
 22    }
 23    my $json = JSON->new();
 24#   $json->allow_singlequote(1);
 25#   $json->relaxed();
 26    $json->decode($data);
 27}
 28# ----------------------------------------------------------------
 29    my $ftitle = "Title of the site";
 30    my $fdesc  = "Description of the site";
 31    my $fright = "Owner of the site";
 32    my $flink  = "http://www.kawa.net/";
 33    my $flang  = "ja";
 34    my $link1  = "http://www.perl.org/";
 35    my $link2  = "http://use.perl.org/";
 36    my $title1 = "The Perl Directory - perl.org";
 37    my $title2 = "use Perl: All the Perl that's Practical to Extract and Report";
 38# ----------------------------------------------------------------
 39    my $date110w = "2004-11-09T11:33:20Z";              # 1100000000
 40    my $date110h = "Tue, 09 Nov 2004 11:33:20 GMT";
 41    my $date111w = "2005-03-05T14:20:00+09:00";         # 1110000000
 42    my $date111h = "Sat, 05 Mar 2005 14:20:00 +0900";
 43    my $date112w = "2005-06-29T08:06:30-09:00";         # 1120000000
 44    my $date112h = "Wed, 29 Jun 2005 08:06:30 -0900";
 45# ----------------------------------------------------------------
 46    my $orig1key = 'kawanet:foo';
 47    my $orig1val = 'AAA';
 48    my $orig2key = 'kawanet:foo/kawanet:bar';
 49    my $orig2val = 'BBB';
 50    my $orig3key = 'kawanet:hoge@pomu';
 51    my $orig3val = 'CCC';
 52    my $mostslim = [ 'link' ];
 53# ----------------------------------------------------------------
 54    &test_slim();
 55    &test_more_slim();
 56# ----------------------------------------------------------------
 57sub test_slim {
 58    my $opt = {};
 59    $opt->{slim} = 1;
 60    $opt->{slim_element_add} = [ $orig1key, $orig2key, $orig3key ];
 61    $opt->{use_json_syck} = 0;
 62    $opt->{use_json_pp}   = 1;
 63
 64    my $rssfeed = &init_feed( XML::FeedPP::RSS->new() );
 65    my $rssjson = $rssfeed->call( DumpJSON => %$opt );
 66    my $rssslim = __decode_json($rssjson);
 67    is( $rssslim->{rss}->{channel}->{link},    $flink,    'rss channel link' );
 68    is( $rssslim->{rss}->{channel}->{title},   $ftitle,   'rss channel title' );
 69    is( $rssslim->{rss}->{channel}->{pubDate}, $date110h, 'rss channel pubDate' );
 70    is( $rssslim->{rss}->{channel}->{item}->[0]->{link}, $link1, 'rss item0 link' );
 71    is( $rssslim->{rss}->{channel}->{item}->[1]->{link}, $link2, 'rss item1 link' );
 72    is( $rssslim->{rss}->{channel}->{item}->[0]->{title}, $title1, 'rss item0 title' );
 73    is( $rssslim->{rss}->{channel}->{item}->[1]->{title}, $title2, 'rss item1 title' );
 74    is( $rssslim->{rss}->{channel}->{item}->[0]->{pubDate}, $date111h, 'rss item0 pubDate' );
 75    is( $rssslim->{rss}->{channel}->{item}->[1]->{pubDate}, $date112h, 'rss item1 pubDate' );
 76
 77    my $rdffeed = &init_feed( XML::FeedPP::RDF->new() );
 78    my $rdfjson = $rdffeed->call( DumpJSON => %$opt );
 79    my $rdfslim = __decode_json($rdfjson);
 80    is( $rdfslim->{'rdf:RDF'}->{channel}->{link},      $flink,    'rdf channel link' );
 81    is( $rdfslim->{'rdf:RDF'}->{channel}->{title},     $ftitle,   'rdf channel title' );
 82    is( $rdfslim->{'rdf:RDF'}->{channel}->{'dc:date'}, $date110w, 'rdf channel dc:date' );
 83    is( $rdfslim->{'rdf:RDF'}->{item}->[0]->{link}, $link1, 'rdf item0 link' );
 84    is( $rdfslim->{'rdf:RDF'}->{item}->[1]->{link}, $link2, 'rdf item1 link' );
 85    is( $rdfslim->{'rdf:RDF'}->{item}->[0]->{title}, $title1, 'rdf item0 title' );
 86    is( $rdfslim->{'rdf:RDF'}->{item}->[1]->{title}, $title2, 'rdf item1 title' );
 87    is( $rdfslim->{'rdf:RDF'}->{item}->[0]->{'dc:date'}, $date111w, 'rdf item0 dc:date' );
 88    is( $rdfslim->{'rdf:RDF'}->{item}->[1]->{'dc:date'}, $date112w, 'rdf item1 dc:date' );
 89
 90    my $atomfeed = &init_feed( XML::FeedPP::Atom->new() );
 91    my $atomjson = $atomfeed->call( DumpJSON => %$opt );
 92    my $atomslim = __decode_json($atomjson);
 93    is( $atomslim->{feed}->{link},     $flink,    'atom channel link' );
 94    is( $atomslim->{feed}->{title},    $ftitle,   'atom channel title' );
 95    is( $atomslim->{feed}->{modified}, $date110w, 'atom channel modified' );
 96    is( $atomslim->{feed}->{entry}->[0]->{link}, $link1, 'atom item0 link' );
 97    is( $atomslim->{feed}->{entry}->[1]->{link}, $link2, 'atom item1 link' );
 98    is( $atomslim->{feed}->{entry}->[0]->{title}, $title1, 'atom item0 title' );
 99    is( $atomslim->{feed}->{entry}->[1]->{title}, $title2, 'atom item1 title' );
100    is( $atomslim->{feed}->{entry}->[0]->{issued}, $date111w, 'atom item0 issued' );
101    is( $atomslim->{feed}->{entry}->[1]->{issued}, $date112w, 'atom item1 issued' );
102
103    is( $rssslim->{rss}->{channel}->{$orig1key},              $orig1val, 'rss channel orig' );
104    is( $rssslim->{rss}->{channel}->{item}->[0]->{$orig2key}, $orig2val, 'rss item0 orig' );
105    is( $rssslim->{rss}->{channel}->{item}->[1]->{$orig3key}, $orig3val, 'rss item1 orig' );
106    is( $rdfslim->{'rdf:RDF'}->{channel}->{$orig1key},   $orig1val, 'rdf channel orig' );
107    is( $rdfslim->{'rdf:RDF'}->{item}->[0]->{$orig2key}, $orig2val, 'rdf item0 orig' );
108    is( $rdfslim->{'rdf:RDF'}->{item}->[1]->{$orig3key}, $orig3val, 'rdf item1 orig' );
109    is( $atomslim->{feed}->{$orig1key},                  $orig1val, 'atom channel orig' );
110    is( $atomslim->{feed}->{entry}->[0]->{$orig2key},    $orig2val, 'atom item0 orig' );
111    is( $atomslim->{feed}->{entry}->[1]->{$orig3key},    $orig3val, 'atom item1 orig' );
112
113    ok( ! exists $rssslim->{rss}->{'-xmlns'},         'rss channel xmlns' );
114    ok( ! exists $rssslim->{rss}->{'-version'},       'rss channel version' );
115    ok( ! exists $rdfslim->{'rdf:RDF'}->{'-xmlns'},   'rdf channel xmlns' );
116    ok( ! exists $rdfslim->{'rdf:RDF'}->{'-version'}, 'rdf channel version' );
117    ok( ! exists $atomslim->{feed}->{'-xmlns'},       'atom channel xmlns' );
118    ok( ! exists $atomslim->{feed}->{'-version'},     'atom channel version' );
119}
120# ----------------------------------------------------------------
121sub test_more_slim {
122    my $opt = {};
123    $opt->{slim_element} = $mostslim;
124    $opt->{use_json_syck} = 0;
125    $opt->{use_json_pp}   = 1;
126
127    my $rssfeed = &init_feed( XML::FeedPP::RSS->new() );
128    my $rssjson = $rssfeed->call( DumpJSON => %$opt );
129    my $rssslim = __decode_json($rssjson);
130    is( $rssslim->{rss}->{channel}->{link},              $flink, 'rss channel link' );
131    is( $rssslim->{rss}->{channel}->{item}->[0]->{link}, $link1, 'rss item0 link' );
132    is( $rssslim->{rss}->{channel}->{item}->[1]->{link}, $link2, 'rss item1 link' );
133    ok( ! exists $rssslim->{rss}->{channel}->{title},              'rss channel title' );
134    ok( ! exists $rssslim->{rss}->{channel}->{item}->[0]->{title}, 'rss item0 title' );
135    ok( ! exists $rssslim->{rss}->{channel}->{item}->[1]->{title}, 'rss item1 title' );
136
137    my $rdffeed = &init_feed( XML::FeedPP::RDF->new() );
138    my $rdfjson = $rdffeed->call( DumpJSON => %$opt );
139    my $rdfslim = __decode_json($rdfjson);
140    is( $rdfslim->{'rdf:RDF'}->{channel}->{link},   $flink, 'rdf channel link' );
141    is( $rdfslim->{'rdf:RDF'}->{item}->[0]->{link}, $link1, 'rdf item0 link' );
142    is( $rdfslim->{'rdf:RDF'}->{item}->[1]->{link}, $link2, 'rdf item1 link' );
143    ok( ! exists $rdfslim->{'rdf:RDF'}->{channel}->{title},   'rdf channel title' );
144    ok( ! exists $rdfslim->{'rdf:RDF'}->{item}->[0]->{title}, 'rdf item0 title' );
145    ok( ! exists $rdfslim->{'rdf:RDF'}->{item}->[1]->{title}, 'rdf item1 title' );
146
147    my $atomfeed = &init_feed( XML::FeedPP::Atom->new() );
148    my $atomjson = $atomfeed->call( DumpJSON => %$opt );
149    my $atomslim = __decode_json($atomjson);
150    is( $atomslim->{feed}->{link},               $flink, 'atom channel link' );
151    is( $atomslim->{feed}->{entry}->[0]->{link}, $link1, 'atom item0 link' );
152    is( $atomslim->{feed}->{entry}->[1]->{link}, $link2, 'atom item1 link' );
153    ok( ! exists $atomslim->{feed}->{title},               'atom channel title' );
154    ok( ! exists $atomslim->{feed}->{entry}->[0]->{title}, 'atom item0 title' );
155    ok( ! exists $atomslim->{feed}->{entry}->[1]->{title}, 'atom item1 title' );
156}
157# ----------------------------------------------------------------
158sub init_feed {
159    my $feed = shift;
160    $feed->title( $ftitle );
161    $feed->description( $fdesc );
162    $feed->pubDate( $date110w );
163    $feed->copyright( $fright );
164    $feed->link( $flink );
165    $feed->language( $flang );
166
167    my $item1 = $feed->add_item( $link1 );
168    $item1->title( $title1 );
169    $item1->pubDate( $date111w );
170
171    my $item2 = $feed->add_item( $link2 );
172    $item2->title( $title2 );
173    $item2->pubDate( $date112h );
174
175    $feed->set( $orig1key, $orig1val );
176    $item1->set( $orig2key, $orig2val );
177    $item2->set( $orig3key, $orig3val );
178
179    $feed;
180}
181# ----------------------------------------------------------------
182;1;
183# ----------------------------------------------------------------