1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 41;
7
8use_ok('WWW::TV::Episode');
9use LWP::UserAgent;
10
11{   # Create a blank episode with a name but don't check anything else
12    # because that would hit up the interwebs.
13    diag("Testing with generic episode prepopulated with name only")
14        unless $ENV{HARNESS_ACTIVE};
15    isa_ok(
16        my $episode = WWW::TV::Episode->new(
17            id   => 1,
18            name => 'foo',
19        ),
20        'WWW::TV::Episode'
21    );
22    is($episode->id, 1, 'id');
23    is($episode->name, 'foo', 'name');
24
25    # Verify site() accessor/mutator
26    is($episode->site, 'www', 'site defaults to: www');
27    $episode->site('us');
28    is($episode->site, 'us', 'changed site to: us');
29    $episode->site('bogus');
30    is($episode->site, 'us', 'ignored change to invalid site');
31    $episode->site('');
32    is($episode->site, 'www', 'reset site back to default');
33
34    # Verify agent() accessor/mutator
35    is($episode->agent, LWP::UserAgent::_agent, 'agent');
36    $episode->agent('Egg Scrambler 1.0');
37    is($episode->agent, 'Egg Scrambler 1.0', 'set custom user agent');
38    $episode->agent('');
39    is($episode->agent, LWP::UserAgent::_agent, 'reset agent to default');
40}
41
42{   # Pilot episode for Prison Break
43    diag("Testing by ID using pilot episode for Prison Break")
44        unless $ENV{HARNESS_ACTIVE};
45    isa_ok(
46        my $episode = WWW::TV::Episode->new(id => 423504),
47        'WWW::TV::Episode',
48    );
49    ok($episode->summary =~ /Fox River/, 'summary includes: Fox River');
50    ok($episode->summary !~ /Watch Video/, 'summary does not include: Watch Video');
51    is($episode->name, 'Pilot', 'title is: Pilot');
52    is($episode->season_number, 1, 'season number is 1');
53    is($episode->episode_number, 1, 'episode_number is 1');
54    ok($episode->stars =~ /\bWentworth Miller\b/, 'stars include: Wentworth Miller');
55    ok($episode->guest_stars =~ /\bJeff Parker\b/, 'guest_stars include: Jeff Parker');
56    ok($episode->recurring_roles =~ /\bStacy Keach\b/, 'recurring_roles include: Stacy Keach');
57    is($episode->directors, 'Brett Ratner', 'directors is: Brett Ratner');
58    is($episode->writers, 'Paul T. Scheuring', 'writers is: Paul T. Scheuring');
59    is($episode->first_aired, '2005-08-29', 'first_aired is: 2005-08-29');
60
61    # Test individual format specifiers
62    is(
63        $episode->format_details('%I'),
64        '31635',
65        'format_details: %I - series ID'
66    );
67    is(
68        $episode->format_details('%N'),
69        'Prison Break',
70        'format_details: %N - series name'
71    );
72    is(
73        $episode->format_details('%s'),
74        '1',
75        'format_details: %s - season number'
76    );
77    is(
78        $episode->format_details('%S'),
79        '01',
80        'format_details: %S - season number (0-padded to two digits, if required)'
81    );
82    is(
83        $episode->format_details('%i'),
84        '423504',
85        'format_details: %i - episode ID'
86    );
87    is(
88        $episode->format_details('%e'),
89        '1',
90        'format_details: %e - episode number'
91    );
92    is(
93        $episode->format_details('%E'),
94        '01',
95        'format_details: %E - episode number (0-padded to two digits, if required)'
96    );
97    is(
98        $episode->format_details('%n'),
99        'Pilot',
100        'format_details: %n - episode name'
101    );
102    is(
103        $episode->format_details('%d'),
104        '2005-08-29',
105        'format_details: %d - date episode first aired'
106    );
107
108    is(
109        $episode->format_details,
110        'Prison Break.s01e01 - Pilot',
111        'format_details with default settings'
112    );
113
114    is(
115        $episode->format_details('Series %I: %N, Episode %i: %n (%d)'),
116        'Series 31635: Prison Break, Episode 423504: Pilot (2005-08-29)',
117        'format_details including multiple formats'
118    );
119
120    diag('Checking $episode->series') unless $ENV{HARNESS_ACTIVE};
121    my $series = $episode->series;
122    isa_ok($series, 'WWW::TV::Series');
123
124    diag('Checking $episode->season') unless $ENV{HARNESS_ACTIVE};
125
126    my @season_eps = $episode->season;
127    is(scalar(@season_eps), 23, '23 episodes for season 1');
128}
129
130{   # Testing an episode that aired in October. Ticket #42284
131    diag("Testing aired date for Desperate Housewives")
132      unless $ENV{HARNESS_ACTIVE};
133    isa_ok(
134        my $episode = WWW::TV::Episode->new(id => 1230998),
135        'WWW::TV::Episode',
136    );
137    is($episode->first_aired, '2008-10-12', 'first_aired is: 2008-10-12');
138}
139
140{   # Get the last episode of the Mr. Bean animated series which never aired
141    # and check to make sure the first aired date comes back ok.
142    diag('Testing an episode that never aired') unless $ENV{HARNESS_ACTIVE};
143    isa_ok(
144        my $episode = WWW::TV::Episode->new(id => 331173),
145        'WWW::TV::Episode',
146    );
147    is(
148        $episode->name,
149        'Egg & Bean / Hopping Mad',
150        'got the right Mr Bean ep'
151    );
152    is($episode->first_aired, 'n/a', 'first aired is n/a');
153}
154
155exit 0;
156