Regenerate test files
[gitmo/Mouse.git] / t-failing / 010_basics / 012_rebless.t
CommitLineData
60ad2cb7 1#!/usr/bin/perl
fde8e43f 2# This is automatically generated by author/import-moose-test.pl.
3# DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4use t::lib::MooseCompat;
60ad2cb7 5
6use strict;
7use warnings;
8
fde8e43f 9use Test::More;
10$TODO = q{Mouse is not yet completed};
60ad2cb7 11use Test::Exception;
12use Scalar::Util 'blessed';
13
14use Mouse::Util::TypeConstraints;
15
16subtype 'Positive'
17 => as 'Num'
18 => where { $_ > 0 };
19
20{
21 package Parent;
22 use Mouse;
23
24 has name => (
25 is => 'rw',
26 isa => 'Str',
27 );
28
29 has lazy_classname => (
30 is => 'ro',
31 lazy => 1,
32 default => sub { "Parent" },
33 );
34
35 has type_constrained => (
36 is => 'rw',
37 isa => 'Num',
38 default => 5.5,
39 );
40
41 package Child;
42 use Mouse;
43 extends 'Parent';
44
45 has '+name' => (
46 default => 'Junior',
47 );
48
49 has '+lazy_classname' => (
50 default => sub { "Child" },
51 );
52
53 has '+type_constrained' => (
54 isa => 'Int',
55 default => 100,
56 );
57}
58
59my $foo = Parent->new;
60my $bar = Parent->new;
61
62is(blessed($foo), 'Parent', 'Parent->new gives a Parent object');
63is($foo->name, undef, 'No name yet');
64is($foo->lazy_classname, 'Parent', "lazy attribute initialized");
65lives_ok { $foo->type_constrained(10.5) } "Num type constraint for now..";
66
67# try to rebless, except it will fail due to Child's stricter type constraint
68throws_ok { Child->meta->rebless_instance($foo) }
fde8e43f 69qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 10\.5/,
60ad2cb7 70'... this failed cause of type check';
71throws_ok { Child->meta->rebless_instance($bar) }
fde8e43f 72qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 5\.5/,
60ad2cb7 73'... this failed cause of type check';;
74
75$foo->type_constrained(10);
76$bar->type_constrained(5);
77
78Child->meta->rebless_instance($foo);
79Child->meta->rebless_instance($bar);
80
81is(blessed($foo), 'Child', 'successfully reblessed into Child');
82is($foo->name, 'Junior', "Child->name's default came through");
83
84is($foo->lazy_classname, 'Parent', "lazy attribute was already initialized");
85is($bar->lazy_classname, 'Child', "lazy attribute just now initialized");
86
87throws_ok { $foo->type_constrained(10.5) }
fde8e43f 88qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 10\.5/,
60ad2cb7 89'... this failed cause of type check';
fde8e43f 90
91done_testing;