From: Stevan Little Date: Thu, 20 Mar 2008 16:14:18 +0000 (+0000) Subject: adding the test X-Git-Tag: 0_55~268 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=638585e1cd1b5b604ca229cf71656a343320cd83;p=gitmo%2FMoose.git adding the test --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 30fcf2e..5c310be 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -868,7 +868,7 @@ and it certainly wouldn't have this name ;P =item The basis of the TypeContraints module was Rob Kinyon's idea originally, I just ran with it. -=item Thanks to mst & chansen and the whole #moose poose for all the +=item Thanks to mst & chansen and the whole #moose posse for all the early ideas/feature-requests/encouragement/bug-finding. =item Thanks to David "Theory" Wheeler for meta-discussions and spelling fixes. diff --git a/t/000_recipes/004_recipe.t b/t/000_recipes/004_recipe.t index 1f9bbc0..44bfa13 100644 --- a/t/000_recipes/004_recipe.t +++ b/t/000_recipes/004_recipe.t @@ -8,7 +8,7 @@ use Test::More; BEGIN { eval "use Regexp::Common; use Locale::US;"; plan skip_all => "Regexp::Common & Locale::US required for this test" if $@; - plan tests => 81; + plan tests => 66; } use Test::Exception; @@ -146,13 +146,7 @@ lives_ok { last_name => 'Little', title => 'Senior Developer', address => Address->new(city => 'Madison', state => 'CT') - ), - Employee->new( - first_name => 'Rob', - last_name => 'Kinyon', - title => 'Developer', - address => Address->new(city => 'Marysville', state => 'OH') - ), + ), ] }); } '... created the entire company successfully'; @@ -166,7 +160,7 @@ is($ii->address->city, 'Manhasset', '... got the right city'); is($ii->address->state, 'NY', '... got the right state'); is($ii->address->zip_code, 11030, '... got the zip code'); -is($ii->get_employee_count, 4, '... got the right employee count'); +is($ii->get_employee_count, 3, '... got the right employee count'); # employee #1 @@ -222,24 +216,6 @@ isa_ok($ii->employees->[2]->address, 'Address'); is($ii->employees->[2]->address->city, 'Madison', '... got the right city'); is($ii->employees->[2]->address->state, 'CT', '... got the right state'); -# employee #4 - -isa_ok($ii->employees->[3], 'Employee'); -isa_ok($ii->employees->[3], 'Person'); - -is($ii->employees->[3]->first_name, 'Rob', '... got the right first name'); -is($ii->employees->[3]->last_name, 'Kinyon', '... got the right last name'); -ok(!$ii->employees->[3]->has_middle_initial, '... got middle initial'); -is($ii->employees->[3]->middle_initial, undef, '... got the right middle initial value'); -is($ii->employees->[3]->full_name, 'Rob Kinyon, Developer', '... got the right full name'); -is($ii->employees->[3]->title, 'Developer', '... got the right title'); -is($ii->employees->[3]->company, $ii, '... got the right company'); -ok(isweak($ii->employees->[3]->{company}), '... the company is a weak-ref'); - -isa_ok($ii->employees->[3]->address, 'Address'); -is($ii->employees->[3]->address->city, 'Marysville', '... got the right city'); -is($ii->employees->[3]->address->state, 'OH', '... got the right state'); - # create new company my $new_company = Company->new(name => 'Infinity Interactive International'); diff --git a/t/200_examples/008_record_set_iterator.t b/t/200_examples/008_record_set_iterator.t new file mode 100644 index 0000000..990f2a8 --- /dev/null +++ b/t/200_examples/008_record_set_iterator.t @@ -0,0 +1,129 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 9; +use Test::Exception; + +BEGIN { + use_ok('Moose'); +} + +{ + package Record; + use Moose; + + has 'first_name' => (is => 'ro', isa => 'Str'); + has 'last_name' => (is => 'ro', isa => 'Str'); + + package RecordSet; + use Moose; + + has 'data' => ( + is => 'ro', + isa => 'ArrayRef[Record]', + default => sub { [] }, + ); + + has 'index' => ( + is => 'rw', + isa => 'Int', + default => sub { 0 }, + ); + + sub next { + my $self = shift; + my $i = $self->index; + $self->index($i + 1); + return $self->data->[$i]; + } + + package RecordSetIterator; + use Moose; + + has 'record_set' => ( + is => 'rw', + isa => 'RecordSet', + ); + + # list the fields you want to + # fetch from the current record + my @fields = Record->meta->get_attribute_list; + + has 'current_record' => ( + is => 'rw', + isa => 'Record', + lazy => 1, + default => sub { + my $self = shift; + $self->record_set->next() # grab the first one + }, + trigger => sub { + my $self = shift; + # whenever this attribute is + # updated, it will clear all + # the fields for you. + $self->$_() for map { '_clear_' . $_ } @fields; + } + ); + + # define the attributes + # for all the fields. + for my $field (@fields) { + has $field => ( + is => 'ro', + isa => 'Any', + lazy => 1, + default => sub { + my $self = shift; + # fetch the value from + # the current record + $self->current_record->$field(); + }, + # make sure they have a clearer .. + clearer => ('_clear_' . $field) + ); + } + + sub get_next_record { + my $self = shift; + $self->current_record($self->record_set->next()); + } +} + +my $rs = RecordSet->new( + data => [ + Record->new(first_name => 'Bill', last_name => 'Smith'), + Record->new(first_name => 'Bob', last_name => 'Jones'), + Record->new(first_name => 'Jim', last_name => 'Johnson'), + ] +); +isa_ok($rs, 'RecordSet'); + +my $rsi = RecordSetIterator->new(record_set => $rs); +isa_ok($rsi, 'RecordSetIterator'); + +is($rsi->first_name, 'Bill', '... got the right first name'); +is($rsi->last_name, 'Smith', '... got the right last name'); + +$rsi->get_next_record; + +is($rsi->first_name, 'Bob', '... got the right first name'); +is($rsi->last_name, 'Jones', '... got the right last name'); + +$rsi->get_next_record; + +is($rsi->first_name, 'Jim', '... got the right first name'); +is($rsi->last_name, 'Johnson', '... got the right last name'); + + + + + + + + + + +