grammar correction in test descriptions
[gitmo/Moose.git] / t / 010_basics / 012_rebless.t
CommitLineData
af3c1f96 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b10dde3a 7use Test::Fatal;
af3c1f96 8use Scalar::Util 'blessed';
9
7ff56534 10use Moose::Util::TypeConstraints;
af3c1f96 11
12subtype 'Positive'
13 => as 'Num'
14 => where { $_ > 0 };
15
16{
17 package Parent;
18 use Moose;
19
20 has name => (
21 is => 'rw',
22 isa => 'Str',
23 );
24
25 has lazy_classname => (
26 is => 'ro',
27 lazy => 1,
28 default => sub { "Parent" },
29 );
30
31 has type_constrained => (
32 is => 'rw',
33 isa => 'Num',
34 default => 5.5,
35 );
36
37 package Child;
38 use Moose;
39 extends 'Parent';
40
41 has '+name' => (
42 default => 'Junior',
43 );
44
45 has '+lazy_classname' => (
46 default => sub { "Child" },
47 );
48
49 has '+type_constrained' => (
50 isa => 'Int',
51 default => 100,
52 );
53}
54
55my $foo = Parent->new;
56my $bar = Parent->new;
57
58is(blessed($foo), 'Parent', 'Parent->new gives a Parent object');
59is($foo->name, undef, 'No name yet');
60is($foo->lazy_classname, 'Parent', "lazy attribute initialized");
b10dde3a 61is( exception { $foo->type_constrained(10.5) }, undef, "Num type constraint for now.." );
af3c1f96 62
63# try to rebless, except it will fail due to Child's stricter type constraint
aba63d2c 64like( exception { Child->meta->rebless_instance($foo) }, qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 10\.5/, '... this failed because of type check' );
65like( exception { Child->meta->rebless_instance($bar) }, qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 5\.5/, '... this failed because of type check' );
af3c1f96 66
67$foo->type_constrained(10);
68$bar->type_constrained(5);
69
a4154081 70Child->meta->rebless_instance($foo);
71Child->meta->rebless_instance($bar);
af3c1f96 72
73is(blessed($foo), 'Child', 'successfully reblessed into Child');
74is($foo->name, 'Junior', "Child->name's default came through");
75
76is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized");
77is($bar->lazy_classname, 'Child', "lazy attribute just now initialized");
78
aba63d2c 79like( exception { $foo->type_constrained(10.5) }, qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 10\.5/, '... this failed because of type check' );
a28e50e4 80
81done_testing;