#!/usr/bin/perl -w
BEGIN {
$| = 1;
$^W = 1;
}
my
$good_dir
= catdir(
't'
,
'data'
,
'04_good'
);
ok( -d
$good_dir
,
'Good test directory exists'
);
ok( -w
$good_dir
,
'Good test directory is writable'
);
my
$bad_dir
= catdir(
't'
,
'data'
,
'04_bad'
);
ok( ! -d
$bad_dir
,
'Bad test directory is not writable'
);
isa_ok(
$good_location
,
'HTML::Location'
);
isa_ok(
$bad_location
,
'HTML::Location'
);
my
$image1
= catfile(
't'
,
'data'
,
'03_image.gif'
);
my
$image2
= catfile(
't'
,
'data'
,
'04_help.gif'
);
ok( (-f
$image1
and -r
$image1
),
'Found usable test image 1'
);
ok( (-f
$image2
and -r
$image2
),
'Found usable test image 2'
);
my
$good_provider
= Image::Delivery::Provider::File->new(
$image1
);
isa_ok(
$good_provider
,
'Image::Delivery::Provider'
);
my
$new_provider
= Image::Delivery::Provider::File->new(
$image2
);
isa_ok(
$new_provider
,
'Image::Delivery::Provider'
);
my
$good_path
=
$good_provider
->TransformPath;
isa_ok(
$good_path
,
'Digest::TransformPath'
);
my
$new_path
=
$new_provider
->TransformPath;
isa_ok(
$new_path
,
'Digest::TransformPath'
);
is( Image::Delivery->new(),
undef
,
'->new with no params returns undef'
);
is( Image::Delivery->new(
Location
=>
undef
),
undef
,
'->new with bad params returns undef'
);
is( Image::Delivery->new(
Location
=>
'foo'
),
undef
,
'->new with bad params returns undef'
);
is( Image::Delivery->new(
Location
=>
'HTML::Location'
),
undef
,
'->new with bad params returns undef'
);
is( Image::Delivery->new(
Location
=>
$bad_location
),
undef
,
'->new with bad location returns undef'
);
my
$Delivery
= Image::Delivery->new(
Location
=>
$good_location
);
isa_ok(
$Delivery
,
'Image::Delivery'
);
isa_ok(
$Delivery
->Location,
'HTML::Location'
);
is(
$Delivery
->Location->path,
$good_dir
,
'->Location returns original location'
);
is(
$Delivery
->filename(
$good_path
),
'3/377f84afe0'
,
'->filename(TransformPath) returns correct filename'
);
is(
$Delivery
->filename(
$good_provider
),
'3/377f84afe0'
,
'->filename(Provider) returns correct filename'
);
is(
$Delivery
->filename(
$new_path
),
'e/ee2067f1d7'
,
'->filename(TransformPath) returns correct filename'
);
is(
$Delivery
->filename(
$new_provider
),
'e/ee2067f1d7'
,
'->filename(Provider) returns correct filename'
);
{
my
$Location
=
$Delivery
->
exists
(
$good_path
);
isa_ok(
$Location
,
'HTML::Location'
);
is(
$Location
->path,
't/data/04_good/3/377f84afe0.gif'
,
'->exists(TransformPath) finds the correct existing file'
);
$Location
=
$Delivery
->
exists
(
$good_provider
);
isa_ok(
$Location
,
'HTML::Location'
);
is(
$Location
->path,
't/data/04_good/3/377f84afe0.gif'
,
'->exists(TransformPath) finds the correct existing file'
);
}
{
my
$Location
=
$Delivery
->
exists
(
$new_path
);
ok( (
defined
$Location
and !
ref
$Location
),
'->exists(TransformPath) returns defined and non-reference'
);
is(
$Location
,
''
,
'->exists(TransformPath) correctly finds no file'
);
$Location
=
$Delivery
->
exists
(
$new_provider
);
ok( (
defined
$Location
and !
ref
$Location
),
'->exists(TransformPath) returns defined and non-reference'
);
is(
$Location
,
''
,
'->exists(TransformPath) correctly finds no file'
);
}
{
my
$data1
=
$Delivery
->get(
$good_path
);
my
$data2
=
$Delivery
->get(
$good_provider
);
ok(
$data1
,
'->get(TransformPath) returns the data'
);
ok(
ref
$data1
eq
'SCALAR'
,
'->get returns a scalar reference'
);
is_deeply(
$data1
,
$data2
,
'->get returns same same for TransformPath or Provider args'
);
is_deeply(
$data1
,
$good_provider
->image,
'->get returns data matching the provider\'s original'
);
}
{
is(
$Delivery
->set(
$new_path
),
undef
,
'->set(TransformPath) return undef'
);
my
$Location
=
$Delivery
->set(
$new_provider
);
isa_ok(
$Location
,
'HTML::Location'
);
is(
$Location
->path,
't/data/04_good/e/ee2067f1d7.gif'
,
'->set returns the expected path'
);
is( File::Slurp::read_file(
$image2
), File::Slurp::read_file(
$Location
->path),
'->set wrote data matching the source file'
);
is_deeply(
$Location
,
$Delivery
->
exists
(
$new_provider
),
'->exists now returns true for the provider we ->set'
);
is_deeply(
$Delivery
->get(
$new_provider
),
$new_provider
->image,
'->get fetches image data that matches original data'
);
}
END {
foreach
my
$path
(
catfile(
$good_dir
,
'e'
,
'ee2067f1d7.gif'
),
catdir(
$good_dir
,
'e'
),
) {
File::Remove::remove \1,
$path
;
}
}
{
my
$Location
=
$Delivery
->
exists
(
$new_provider
);
isa_ok(
$Location
,
'HTML::Location'
);
ok( -f
$Location
->path,
'Double checking location exists'
);
my
$rv
=
$Delivery
->clear(
$new_provider
);
is(
$rv
, 1,
'->clear returns true'
);
is_deeply(
$Delivery
->
exists
(
$new_provider
),
''
,
'->exists returns false after ->clear'
);
ok( ! -e
$Location
->path,
'File does appear to be actually gone'
);
}
1;