From: Shawn M Moore Date: Tue, 10 Jun 2008 02:42:17 +0000 (+0000) Subject: Make sure that predicate plays well with builder X-Git-Tag: 0.04~67 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5d5a3b5ddd7ebaa29520ab3fdb23da94e6d67d85;p=gitmo%2FMouse.git Make sure that predicate plays well with builder --- diff --git a/t/023-builder.t b/t/023-builder.t index 2dff1dd..a89ee0b 100644 --- a/t/023-builder.t +++ b/t/023-builder.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 20; +use Test::More tests => 30; my $builder_called = 0; my $lazy_builder_called = 0; @@ -11,9 +11,10 @@ do { use Mouse; has name => ( - is => 'rw', - isa => 'Str', - builder => '_build_name', + is => 'rw', + isa => 'Str', + builder => '_build_name', + predicate => 'has_name', ); sub _build_name { @@ -23,11 +24,12 @@ do { }; has age => ( - is => 'ro', - isa => 'Int', - builder => '_build_age', - lazy => 1, - clearer => 'clear_age', + is => 'ro', + isa => 'Int', + builder => '_build_age', + lazy => 1, + clearer => 'clear_age', + predicate => 'has_age', ); sub default_age { 20 } @@ -41,6 +43,7 @@ do { # eager builder my $object = Class->new(name => "Bob"); +ok($object->has_name, "predicate: value from constructor"); is($builder_called, 0, "builder not called in the constructor when we pass a value"); is($object->name, "Bob", "builder doesn't matter when we just set the value in constructor"); $object->name("Bill"); @@ -49,29 +52,39 @@ is($builder_called, 0, "builder not called in the setter"); $builder_called = 0; my $object2 = Class->new; +ok($object2->has_name, "predicate: value from eager builder"); is($object2->name, "FRANK", "builder called to provide the default value"); is($builder_called, 1, "builder called ONCE to provide the default value"); # lazy builder my $object3 = Class->new; is($lazy_builder_called, 0, "lazy builder not called yet"); +ok(!$object3->has_age, "predicate: no age yet"); is($object3->age, 20, "lazy builder value"); +ok($object3->has_age, "predicate: have value after get"); is($lazy_builder_called, 1, "lazy builder called on get"); is($object3->age, 20, "lazy builder value"); is($lazy_builder_called, 1, "lazy builder not called on subsequent gets"); +ok($object3->has_age, "predicate: have value after subsequent gets"); +$lazy_builder_called = 0; $object3->clear_age; -is($lazy_builder_called, 1, "lazy builder not called on clear"); +ok(!$object3->has_age, "predicate: no value after clear"); +is($lazy_builder_called, 0, "lazy builder not called on clear"); is($object3->age, 20, "lazy builder value"); -is($lazy_builder_called, 2, "lazy builder called on get after clear"); +ok($object3->has_age, "predicate: have value after clear and get"); +is($lazy_builder_called, 1, "lazy builder called on get after clear"); -$lazy_builder_called = 0 ; +$lazy_builder_called = 0; my $object4 = Class->new(age => 50); +ok($object4->has_age, "predicate: have value from constructor"); is($lazy_builder_called, 0, "lazy builder not called yet"); is($object4->age, 50, "value from constructor"); is($lazy_builder_called, 0, "lazy builder not called if value is from constructor"); $object4->clear_age; +ok(!$object4->has_age, "predicate: no value after clear"); is($lazy_builder_called, 0, "lazy builder not called on clear"); is($object4->age, 20, "lazy builder value"); +ok($object4->has_age, "predicate: have value after clear and get"); is($lazy_builder_called, 1, "lazy builder called on get after clear");