Update tests
[catagits/XML-Feed.git] / t / 01-parse.t
CommitLineData
ecac864a 1# $Id: 01-parse.t 1921 2006-02-28 02:50:52Z btrott $
0d5e38d1 2
3use strict;
bf34c07e 4use Test::More tests => 75;
0d5e38d1 5use XML::Feed;
6use URI;
7
0d5e38d1 8my %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.
15my $feed;
16my $file = 't/samples/atom.xml';
973e1f9e 17$feed = XML::Feed->parse($file);
948f9350 18isa_ok($feed, 'XML::Feed::Format::Atom');
973e1f9e 19is($feed->title, 'First Weblog');
0d5e38d1 20open my $fh, $file or die "Can't open $file: $!";
973e1f9e 21$feed = XML::Feed->parse($fh);
948f9350 22isa_ok($feed, 'XML::Feed::Format::Atom');
973e1f9e 23is($feed->title, 'First Weblog');
0d5e38d1 24seek $fh, 0, 0;
25my $xml = do { local $/; <$fh> };
973e1f9e 26$feed = XML::Feed->parse(\$xml);
948f9350 27isa_ok($feed, 'XML::Feed::Format::Atom');
973e1f9e 28is($feed->title, 'First Weblog');
29$feed = XML::Feed->parse(URI->new("file:$file"));
948f9350 30isa_ok($feed, 'XML::Feed::Format::Atom');
973e1f9e 31is($feed->title, 'First Weblog');
0d5e38d1 32
33## Then try calling all of the unified API methods.
34for my $file (sort keys %Feeds) {
35 my $feed = XML::Feed->parse($file) or die XML::Feed->errstr;
973e1f9e 36 my($subclass) = $Feeds{$file} =~ /^(\w+)/;
948f9350 37 isa_ok($feed, 'XML::Feed::Format::' . $subclass);
973e1f9e 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!...');
bf34c07e 61 is(($entry->category)[0], 'Travel');
973e1f9e 62 is($entry->category, 'Travel');
63 is($entry->author, 'Melody');
0d5e38d1 64 ok($entry->id);
65}
a749d9b9 66
67$feed = XML::Feed->parse('t/samples/rss20-no-summary.xml')
68 or die XML::Feed->errstr;
69my $entry = ($feed->entries)[0];
70ok(!$entry->summary->body);
973e1f9e 71like($entry->content->body, qr/<p>This is a test.<\/p>/);
ecac864a 72
73$feed = XML::Feed->parse('t/samples/rss10-invalid-date.xml')
74 or die XML::Feed->errstr;
75$entry = ($feed->entries)[0];
76ok(!$entry->issued); ## Should return undef, but not die.
77ok(!$entry->modified); ## Same.