App-MaMGal

 view release on metacpan or  search on metacpan

t/010_unit_imageinfo.t  view on Meta::CPAN

	my %parsers = (
		jpg                         => Test::MockObject->new->mock('parse', sub { 1231231231 }),
		jpg_no_0x9003               => Test::MockObject->new->mock('parse', sub { 1231231232 }),
		jpg_no_0x9003_0x9004        => Test::MockObject->new->mock('parse', sub { 1231231233 }),
		jpg_no_0x9003_0x9004_0x0132 => Test::MockObject->new->mock('parse', sub { undef }),
	);
	# inject parsers
	$self->{$_}->{parser} = $parsers{$_} for keys %parsers;

	is($self->{jpg}->creation_time, '1231231231', 'returned datetime is the mocked time');
	is($self->{jpg_no_0x9003}->creation_time, '1231231232', 'returned datetime is the mocked time');
	is($self->{jpg_no_0x9003_0x9004}->creation_time, '1231231233', 'returned datetime is the mocked time');
	is($self->{jpg_no_0x9003_0x9004_0x0132}->creation_time, undef, 'returned datetime original is undefined');
}

sub _test_creation_time {
	my $self = shift;
	my $file = shift;
	my $mp = $self->{$file}->{parser} = Test::MockObject->new;
	my $ml = $self->{$file}->{logger};
	my $parse_map = shift;
	my $expected_result = shift;
	my $expected_tag = shift;
	my $expected_warning = shift;
	my $expected_filename = shift;
	$mp->mock('parse', sub { exists $parse_map->{$_[1]} ? return &{$parse_map->{$_[1]}} : die "arg ".$_[1]." not found in map" });
	$ml->clear;
	my $level = $Test::Builder::Level;
	local $Test::Builder::Level = $level + 1;
	my $actual_result = $self->{$file}->creation_time;
	if ($expected_warning) {
		logged_only_ok($ml, $expected_warning, $expected_filename);
	} else {
		ok(! $ml->called('log_message'), 'log message was not called');
		ok(1, 'dummy test to keep test count constant');
		ok(1, 'dummy test to keep test count constant');
	}
	is($actual_result, $expected_result, "creation time returns parse value for $expected_tag");
}

sub when_all_tags_present_and_datetime_original_crashes_then_creation_time_returns_datetime_digitized: Test(5) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { 1234567891 },
		'2008:11:27 20:43:52' => sub { 1234567892 },
		'2008:11:27 20:43:53' => sub { die "parsing failed" },
	);
	$self->_test_creation_time('jpg', \%parse_map, 1234567891, 'datetime_digitized', qr{EXIF tag 0x9003: parsing failed}, 'td/varying_datetimes.jpg');
}

sub when_all_tags_present_and_parse_then_creation_time_returns_datetime_original: Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { 1234567891 },
		'2008:11:27 20:43:52' => sub { 1234567892 },
		'2008:11:27 20:43:53' => sub { 1234567893 },
	);
	$self->_test_creation_time('jpg', \%parse_map, 1234567893, 'datetime_original');
}

sub when_all_tags_present_and_just_datetime_original_does_not_parse_then_creation_time_returns_datetime_digitized : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { 1234567891 },
		'2008:11:27 20:43:52' => sub { 1234567892 },
		'2008:11:27 20:43:53' => sub { undef },
	);
	$self->_test_creation_time('jpg', \%parse_map, 1234567891, 'datetime_digitized');
}

sub when_all_tags_present_and_just_datetime_parses_then_creation_time_returns_datetime : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { undef },
		'2008:11:27 20:43:52' => sub { 1234567892 },
		'2008:11:27 20:43:53' => sub { undef },
	);
	$self->_test_creation_time('jpg', \%parse_map, 1234567892, 'datetime');
}

sub when_all_tags_present_and_none_parses_then_creation_time_returns_undef : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { undef },
		'2008:11:27 20:43:52' => sub { undef },
		'2008:11:27 20:43:53' => sub { undef },
	);
	$self->_test_creation_time('jpg', \%parse_map, undef, 'undef');
}

sub when_datetime_original_tag_not_present_and_rest_parse_then_creation_time_returns_datetime_digitized : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { 1234567891 },
		'2008:11:27 20:43:52' => sub { 1234567892 },
	);
	$self->_test_creation_time('jpg_no_0x9003', \%parse_map, 1234567891, 'datetime_digitized');
}

sub when_datetime_original_tag_not_present_and_just_datetime_parses_then_creation_time_returns_datetime_digitized : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { undef },
		'2008:11:27 20:43:52' => sub { 1234567892 },
	);
	$self->_test_creation_time('jpg_no_0x9003', \%parse_map, 1234567892, 'datetime');
}

sub when_datetime_original_tag_not_present_and_none_parses_then_creation_time_returns_undef : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:51' => sub { undef },
		'2008:11:27 20:43:52' => sub { undef },
	);
	$self->_test_creation_time('jpg_no_0x9003', \%parse_map, undef, 'undef');
}


sub when_just_datetime_tag_present_and_parses_then_creation_time_returns_datetime : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:52' => sub { 1234567892 },
	);
	$self->_test_creation_time('jpg_no_0x9003_0x9004', \%parse_map, 1234567892, 'datetime');
}

sub when_just_datetime_tag_present_and_none_parses_then_creation_time_returns_undef : Test(4) {
	my $self = shift;
	my %parse_map = (
		'2008:11:27 20:43:52' => sub { undef },
	);
	$self->_test_creation_time('jpg_no_0x9003_0x9004', \%parse_map, undef, 'undef');
}


sub when_no_datetime_tag_present_then_creation_time_returns_undef : Test(4) {
	my $self = shift;
	$self->_test_creation_time('jpg_no_0x9003_0x9004_0x0132', {}, undef, 'undef');
}

package App::MaMGal::Unit::ImageInfo::ImageInfo;
use strict;
use warnings;
use Carp 'verbose';
use File::stat;
use Test::More;
use Test::Exception;
use Test::Warn;
use base 'App::MaMGal::Unit::ImageInfo';
use lib 'testlib';
use App::MaMGal::TestHelper;

use vars '%ENV';
$ENV{MAMGAL_FORCE_IMAGEINFO} = 'App::MaMGal::ImageInfo::ImageInfo';
App::MaMGal::Unit::ImageInfo::ImageInfo->runtests unless defined caller;

1;



( run in 1.228 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )