From: Shawn M Moore Date: Wed, 4 Jun 2008 04:10:09 +0000 (+0000) Subject: Tests for builder X-Git-Tag: 0.04~92 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b6e1128f0c67e71aef90e20b22068cda4f5df21a;p=gitmo%2FMouse.git Tests for builder --- diff --git a/t/023-builder.t b/t/023-builder.t index e69de29..275ae59 100644 --- a/t/023-builder.t +++ b/t/023-builder.t @@ -0,0 +1,38 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More tests => 6; + +my $builder_called = 0; + +do { + package Class; + use Mouse; + + has name => ( + is => 'rw', + isa => 'Str', + builder => '_build_name', + ); + + sub default_name { "Frank" } + sub _build_name { + my $self = shift; + ++$builder_called; + return uc $self->default_name; + }; +}; + +my $object = Class->new(name => "Bob"); +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"); +is($object->name, "Bill", "builder doesn't matter when we just set the value in writer"); +is($builder_called, 0, "builder not called in the setter"); +$builder_called = 0; + +my $object2 = Class->new; +is($object2->name, "FRANK", "builder called to provide the default value"); +is($builder_called, 1, "builder called ONCE to provide the default value"); + +# XXX: test clearer, lazy