Debian Codegraph @

t/01-parse.t

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