Commit | Line | Data |
ecac864a |
1 | # $Id: 01-parse.t 1921 2006-02-28 02:50:52Z btrott $ |
0d5e38d1 |
2 | |
3 | use strict; |
ecac864a |
4 | use Test::More tests => 72; |
0d5e38d1 |
5 | use XML::Feed; |
6 | use URI; |
7 | |
0d5e38d1 |
8 | my %Feeds = ( |
9 | 't/samples/atom.xml' => 'Atom', |
10 | 't/samples/rss10.xml' => 'RSS 1.0', |
11 | 't/samples/rss20.xml' => 'RSS 2.0', |
12 | ); |
13 | |
14 | ## First, test all of the various ways of calling parse. |
15 | my $feed; |
16 | my $file = 't/samples/atom.xml'; |
973e1f9e |
17 | $feed = XML::Feed->parse($file); |
18 | isa_ok($feed, 'XML::Feed::Atom'); |
19 | is($feed->title, 'First Weblog'); |
0d5e38d1 |
20 | open my $fh, $file or die "Can't open $file: $!"; |
973e1f9e |
21 | $feed = XML::Feed->parse($fh); |
22 | isa_ok($feed, 'XML::Feed::Atom'); |
23 | is($feed->title, 'First Weblog'); |
0d5e38d1 |
24 | seek $fh, 0, 0; |
25 | my $xml = do { local $/; <$fh> }; |
973e1f9e |
26 | $feed = XML::Feed->parse(\$xml); |
27 | isa_ok($feed, 'XML::Feed::Atom'); |
28 | is($feed->title, 'First Weblog'); |
29 | $feed = XML::Feed->parse(URI->new("file:$file")); |
30 | isa_ok($feed, 'XML::Feed::Atom'); |
31 | is($feed->title, 'First Weblog'); |
0d5e38d1 |
32 | |
33 | ## Then try calling all of the unified API methods. |
34 | for my $file (sort keys %Feeds) { |
35 | my $feed = XML::Feed->parse($file) or die XML::Feed->errstr; |
973e1f9e |
36 | my($subclass) = $Feeds{$file} =~ /^(\w+)/; |
37 | isa_ok($feed, 'XML::Feed::' . $subclass); |
38 | is($feed->format, $Feeds{$file}); |
39 | is($feed->language, 'en-us'); |
40 | is($feed->title, 'First Weblog'); |
41 | is($feed->link, 'http://localhost/weblog/'); |
42 | is($feed->tagline, 'This is a test weblog.'); |
43 | is($feed->description, 'This is a test weblog.'); |
0d5e38d1 |
44 | my $dt = $feed->modified; |
973e1f9e |
45 | isa_ok($dt, 'DateTime'); |
0d5e38d1 |
46 | $dt->set_time_zone('UTC'); |
973e1f9e |
47 | is($dt->iso8601, '2004-05-30T07:39:57'); |
48 | is($feed->author, 'Melody'); |
0d5e38d1 |
49 | |
50 | my @entries = $feed->entries; |
973e1f9e |
51 | is(scalar @entries, 2); |
0d5e38d1 |
52 | my $entry = $entries[0]; |
973e1f9e |
53 | is($entry->title, 'Entry Two'); |
54 | is($entry->link, 'http://localhost/weblog/2004/05/entry_two.html'); |
0d5e38d1 |
55 | $dt = $entry->issued; |
973e1f9e |
56 | isa_ok($dt, 'DateTime'); |
0d5e38d1 |
57 | $dt->set_time_zone('UTC'); |
973e1f9e |
58 | is($dt->iso8601, '2004-05-30T07:39:25'); |
59 | like($entry->content->body, qr/<p>Hello!<\/p>/); |
60 | is($entry->summary->body, 'Hello!...'); |
61 | is($entry->category, 'Travel'); |
62 | is($entry->author, 'Melody'); |
0d5e38d1 |
63 | ok($entry->id); |
64 | } |
a749d9b9 |
65 | |
66 | $feed = XML::Feed->parse('t/samples/rss20-no-summary.xml') |
67 | or die XML::Feed->errstr; |
68 | my $entry = ($feed->entries)[0]; |
69 | ok(!$entry->summary->body); |
973e1f9e |
70 | like($entry->content->body, qr/<p>This is a test.<\/p>/); |
ecac864a |
71 | |
72 | $feed = XML::Feed->parse('t/samples/rss10-invalid-date.xml') |
73 | or die XML::Feed->errstr; |
74 | $entry = ($feed->entries)[0]; |
75 | ok(!$entry->issued); ## Should return undef, but not die. |
76 | ok(!$entry->modified); ## Same. |