1#!perl 2use strict; 3use warnings; 4 5# This test file exists to show that Time::Piece can be subclassed and that its 6# methods will return objects of the class on which they're called. 7 8use Test::More 'no_plan'; 9 10BEGIN { use_ok('Time::Piece'); } 11 12my $class = 'Time::Piece::Twin'; 13 14for my $method (qw(new localtime gmtime)) { 15 my $piece = $class->$method; 16 isa_ok($piece, $class, "timepiece made via $method"); 17} 18 19{ 20 my $piece = $class->strptime("2005-01-01", "%Y-%m-%d"); 21 isa_ok($piece, $class, "timepiece made via strptime"); 22} 23 24{ 25 my $piece = $class->new; 26 isa_ok($piece, $class, "timepiece made via new (again)"); 27 28 my $sum = $piece + 86_400; 29 isa_ok($sum, $class, "tomorrow via addition operator"); 30 31 my $diff = $piece - 86_400; 32 isa_ok($diff, $class, "yesterday via subtraction operator"); 33} 34 35{ 36 # let's verify that we can use gmtime from T::P without the export magic 37 my $piece = Time::Piece::gmtime; 38 isa_ok($piece, "Time::Piece", "object created via full-qualified gmtime"); 39 isnt(ref $piece, 'Time::Piece::Twin', "it's not a Twin"); 40} 41 42## below is our doppelgaenger package 43{ 44 package Time::Piece::Twin; 45 use base qw(Time::Piece); 46 # this package is identical, but will be ->isa('Time::Piece::Twin'); 47} 48 49{ 50 my $class = "Time::Piece::NumString"; 51 my $piece = $class->strptime ("2006", "%Y"); 52 is (2007 - $piece, 1, 53 "subtract attempts stringify for unrecognized objects."); 54} 55 56## Below is a package which only changes the stringify function. 57{ 58 package Time::Piece::NumString; 59 use base qw(Time::Piece); 60 use overload '""' => \&_stringify; 61 sub _stringify 62 { 63 my $self = shift; 64 return $self->strftime ("%Y"); 65 } 66} 67