summaryrefslogtreecommitdiffstats
path: root/gnu/usr.bin/perl/cpan/Time-Piece/t/06subclass.t
blob: a72cfb89acc8f30be40107bcf530ece65e792711 (plain) (blame)
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
#!perl
use strict;
use warnings;

# This test file exists to show that Time::Piece can be subclassed and that its
# methods will return objects of the class on which they're called.

use Test::More 'no_plan';

use lib "t/lib";
use Time::Piece::Twin;

BEGIN { use_ok('Time::Piece'); }

my $class = 'Time::Piece::Twin';

for my $method (qw(new localtime gmtime)) {
  my $piece = $class->$method;
  isa_ok($piece, $class, "timepiece made via $method");
}

{
  my $piece = $class->strptime("2005-01-01", "%Y-%m-%d");
  isa_ok($piece, $class, "timepiece made via strptime");
}

{
  my $piece = $class->new;
  isa_ok($piece, $class, "timepiece made via new (again)");

  my $sum = $piece + 86_400;
  isa_ok($sum, $class, "tomorrow via addition operator");

  my $diff = $piece - 86_400;
  isa_ok($diff, $class, "yesterday via subtraction operator");
}

{
  my $g = $class->gmtime;
  my $l = $class->localtime;

  #via clone
  my $l_clone = $class->new($l);
  isa_ok($l_clone, $class, 'custom localtime via clone');
  cmp_ok("$l_clone", 'eq', "$l", 'Clones match');

  #via clone with gmtime
  my $g_clone = $class->new($g);
  isa_ok($g_clone, $class, 'custom gmtime via clone');
  cmp_ok("$g_clone", 'eq', "$g", 'Clones match');
}

{
  # let's verify that we can use gmtime from T::P without the export magic
  my $piece = Time::Piece::gmtime;
  isa_ok($piece, "Time::Piece", "object created via full-qualified gmtime");
  isnt(ref $piece, 'Time::Piece::Twin', "it's not a Twin");
}



{
  my $class = "Time::Piece::NumString";
  my $piece = $class->strptime ("2006", "%Y");
  is (2007 - $piece, 1,
      "subtract attempts stringify for unrecognized objects.");
}

## Below is a package which only changes the stringify function.
{
  package Time::Piece::NumString;
  use base qw(Time::Piece);
  use overload '""' => \&_stringify;
  sub _stringify
  {
    my $self = shift;
    return $self->strftime ("%Y");
  }
}