1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
use strict;
use warnings;
use Test::More tests => 29;
BEGIN {
use_ok("XML::RSS");
use_ok("POSIX");
}
use constant DATE_TEMPLATE_LONG => "%Y-%m-%dT%H:%M:%S%z";
use constant DATE_TEMPLATE_SHORT => "%Y/%m/%d";
use constant DATE_TEMPLATE_PUB => "%c GMT";
my $current_date = &POSIX::strftime( DATE_TEMPLATE_LONG, gmtime );
my $short_date = &POSIX::strftime( DATE_TEMPLATE_SHORT, gmtime );
my $pub_date = &POSIX::strftime( DATE_TEMPLATE_PUB, gmtime );
ok( $current_date, "Current date: $current_date" );
use constant RSS_VERSION => "2.0";
use constant RSS_SAVEAS => "t/generated/" . RSS_VERSION . "-generated.xml";
use constant RSS_MOD_PREFIX => "my";
use constant RSS_MOD_URI => 'http://purl.org/my/rss/module/';
use constant RSS_BLOGCHANNEL_PREFIX => "blogChannel";
use constant RSS_BLOGCHANNEL_URI => "http://backend.userland.com/blogChannelModule";
use constant RSS_CREATOR => "joeuser\@example.com";
use constant RSS_ITEM_TITLE => "This is an item";
use constant RSS_ITEM_LINK => "http://example.com/" . &POSIX::strftime( DATE_TEMPLATE_SHORT, gmtime ); # "$short_date";
use constant RSS_ITEM_DESC => "Yadda yadda yadda - R&D;";
use constant RSS_XML_BASE => "http://example.com";
my $rss = XML::RSS->new( version => RSS_VERSION, 'xml:base' => RSS_XML_BASE );
isa_ok( $rss, "XML::RSS" );
is( $rss->{'version'}, RSS_VERSION, 'Version is ' . RSS_VERSION );
is( $rss->{'xml:base'}, RSS_XML_BASE, 'Base is ' . RSS_XML_BASE );
# This includes all fields, only title, link, and description
# are required.
ok( $rss->channel(
'title' => "Test 2.0 Feed",
'link' => "http://example.com/",
'description' => "",
'language' => 'en-us',
copyright => 'Copyright 2002',
pubDate => $current_date,
lastBuildDate => $current_date,
docs => 'http://backend.userland.com/rss',
managingEditor => 'editor\@example.com',
webMaster => 'webmaster\@example.com',
category => 'MyCategory',
ttl => '60',
'generator' => 'XML::RSS Test',
), "Set RSS channel" );
ok($rss->image(
title => 'Test Image',
url => 'http://example.com/example.gif',
'link' => 'http://example.com/',
description => 'Test Image',
height => '25',
weight => '144',
), "Set RSS image" );
ok($rss->textinput(
title => 'Search',
description => 'Search for an example',
name => 'q',
'link' => 'http://example.com/search.pl',
), "Set RSS text input" );
ok($rss->add_item(
title => RSS_ITEM_TITLE,
'link' => RSS_ITEM_LINK,
description => RSS_ITEM_DESC,
author => RSS_CREATOR,
category => 'MyCategory',
comments => "http://example.com/$short_date/comments.html",
permaLink => "http://example.com/$short_date",
pubDate => $pub_date,
source => 'my brain',
sourceUrl => 'http://example.com',
enclosure => { type=>"application/x-bittorrent", url => 'http://127.0.0.1/torrents/The_Passion_of_Dave_Winer.torrent' },
), "Set one RSS item" );
ok( $rss->add_module( prefix => RSS_MOD_PREFIX, uri => RSS_MOD_URI ),
"Added module: " . RSS_MOD_PREFIX );
my $uri = RSS_MOD_URI;
#use Data::Dumper;
#warn Data::Dumper->Dump([\$rss], [qw(rss)] );
is( $rss->{modules}->{$uri}, RSS_MOD_PREFIX, "Namespace URI is " . RSS_MOD_URI);
my $as_string = $rss->as_string();
my $len = length($as_string);
ok( $len, "RSS feed has '$len' characters" );
ok( $rss->save(RSS_SAVEAS), "Wrote to disk: " . RSS_SAVEAS );
my $file_contents;
{
local $/;
open I, "<", RSS_SAVEAS();
$file_contents = <I>;
close(I);
}
is($file_contents,$as_string,RSS_SAVEAS." contains the as_string() result");
eval { $rss->parsefile( RSS_SAVEAS ) };
is( $@, '', "Parsed " . RSS_SAVEAS );
is( $rss->{channel}->{lastBuildDate}, $current_date,
"Last built: " . $current_date );
is( $rss->{channel}->{category}, 'MyCategory', 'channel->{category}');
is( $rss->{'xml:base'}, RSS_XML_BASE, 'Base has been reparsed');
cmp_ok( keys %{ $rss->{namespaces} }, ">=", 1,
"RSS feed has at least one namespace");
my $prefix = RSS_BLOGCHANNEL_PREFIX;
ok( exists $rss->{namespaces}->{$prefix}, "$prefix namespace is registered" );
is($rss->{namespaces}->{$prefix}, RSS_BLOGCHANNEL_URI, RSS_BLOGCHANNEL_URI );
isa_ok( $rss->{'items'} ,"ARRAY", "RSS object has an array of objects" );
is( scalar( @{$rss->{'items'}} ), 1, "RSS object has one item" );
is( $rss->{items}->[0]->{title}, RSS_ITEM_TITLE, RSS_ITEM_TITLE );
is( $rss->{items}->[0]->{link}, RSS_ITEM_LINK, RSS_ITEM_LINK );
is( $rss->{items}->[0]->{description}, RSS_ITEM_DESC, RSS_ITEM_DESC );
is( $rss->{items}->[0]->{author}, RSS_CREATOR, RSS_CREATOR );
eval
{
$rss->save(".");
};
ok ($@ =~ m{\ACannot open file \. for write},
"Exception upon saving to an invalid location"
);
#END{ unlink RSS_SAVEAS }
__END__
=head1 NAME
2.0-generate.t - tests for generating RSS 2.0 data with XML::RSS.pm
=head1 SYNOPSIS
use Test::Harness qw (runtests);
runtests (./XML-RSS/t/*.t);
=head1 DESCRIPTION
Tests for generating RSS 2.0 data with XML::RSS.pm
=head1 VERSION
$Revision: 1.8 $
=head1 DATE
$Date: 2004/04/21 02:44:40 $
=head1 AUTHOR
Aaron Straup Cope
=head1 SEE ALSO
http://backend.userland.com/rss2
=cu
|