1 |
dpavlin |
1 |
# $Id: 01-parse.t 1921 2006-02-28 02:50:52Z btrott $ |
2 |
|
|
|
3 |
|
|
use strict; |
4 |
|
|
use Test::More tests => 72; |
5 |
|
|
use XML::Feed; |
6 |
|
|
use URI; |
7 |
|
|
|
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'; |
17 |
|
|
$feed = XML::Feed->parse($file); |
18 |
|
|
isa_ok($feed, 'XML::Feed::Atom'); |
19 |
|
|
is($feed->title, 'First Weblog'); |
20 |
|
|
open my $fh, $file or die "Can't open $file: $!"; |
21 |
|
|
$feed = XML::Feed->parse($fh); |
22 |
|
|
isa_ok($feed, 'XML::Feed::Atom'); |
23 |
|
|
is($feed->title, 'First Weblog'); |
24 |
|
|
seek $fh, 0, 0; |
25 |
|
|
my $xml = do { local $/; <$fh> }; |
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'); |
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; |
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.'); |
44 |
|
|
my $dt = $feed->modified; |
45 |
|
|
isa_ok($dt, 'DateTime'); |
46 |
|
|
$dt->set_time_zone('UTC'); |
47 |
|
|
is($dt->iso8601, '2004-05-30T07:39:57'); |
48 |
|
|
is($feed->author, 'Melody'); |
49 |
|
|
|
50 |
|
|
my @entries = $feed->entries; |
51 |
|
|
is(scalar @entries, 2); |
52 |
|
|
my $entry = $entries[0]; |
53 |
|
|
is($entry->title, 'Entry Two'); |
54 |
|
|
is($entry->link, 'http://localhost/weblog/2004/05/entry_two.html'); |
55 |
|
|
$dt = $entry->issued; |
56 |
|
|
isa_ok($dt, 'DateTime'); |
57 |
|
|
$dt->set_time_zone('UTC'); |
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'); |
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. |