From: Paul Driver Date: Tue, 8 Apr 2008 16:55:39 +0000 (+0000) Subject: All unit tests passing with refactored stuff, documentation updated significantly. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=720fa35be1e2cb679dc0d146f2cef1d459cb4b5e;p=gitmo%2FMooseX-AttributeHelpers.git All unit tests passing with refactored stuff, documentation updated significantly. --- diff --git a/lib/MooseX/AttributeHelpers.pm b/lib/MooseX/AttributeHelpers.pm index 094248f..041edb6 100644 --- a/lib/MooseX/AttributeHelpers.pm +++ b/lib/MooseX/AttributeHelpers.pm @@ -76,6 +76,10 @@ Common numerical operations. Methods for incrementing and decrementing a counter attribute. +=item L + +Common string operations. + =item L Common methods for hash references. @@ -88,6 +92,14 @@ Common methods for array references. Common list methods for array references. +=item L + +Mathematical bags. + +=item L + +Hashes with no change methods. + =back =head1 CAVEAT diff --git a/lib/MooseX/AttributeHelpers/Collection.pm b/lib/MooseX/AttributeHelpers/Collection.pm index 2edd4a7..4f3da1a 100644 --- a/lib/MooseX/AttributeHelpers/Collection.pm +++ b/lib/MooseX/AttributeHelpers/Collection.pm @@ -21,23 +21,9 @@ MooseX::AttributeHelpers::Collection - Base class for all collection type helper =head1 DESCRIPTION -Documentation to come. - -=head1 METHODS - -=over 4 - -=item B - -=item B - -=item B - -=item B - -=item B - -=back +This is currently a sort of placeholder for future functionality. All +Collection helpers are encouraged to inherit from it, in case it does +something in the future. =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/Counter.pm b/lib/MooseX/AttributeHelpers/Counter.pm index d1d42f1..4626f7f 100644 --- a/lib/MooseX/AttributeHelpers/Counter.pm +++ b/lib/MooseX/AttributeHelpers/Counter.pm @@ -57,8 +57,9 @@ MooseX::AttributeHelpers::Counter =head1 DESCRIPTION -This module provides a simple counter attribute, which can be -incremented and decremeneted. +This module provides a simple counter attribute, which can be incremented and +decremented. It is important to note that all those methods do in place +modification of the value stored in the attribute. If your attribute definition does not include any of I, I, I or I but does use the C metaclass, @@ -68,32 +69,10 @@ above. This allows for a very basic counter definition: has 'foo' => (metaclass => 'Counter'); $obj->inc_foo; -=head1 METHODS - -=over 4 - -=item B - =head1 PROVIDED METHODS -It is important to note that all those methods do in place -modification of the value stored in the attribute. - -=over 4 - -=item I - -Increments the value stored in this slot by 1. - -=item I - -Decrements the value stored in this slot by 1. - -=item I - -Resets the value stored in this slot to it's default value. - -=back +The methods for this metaclass are provided by +L. =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm index 7366081..5668623 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Array.pm @@ -1,5 +1,6 @@ package MooseX::AttributeHelpers::MethodProvider::Array; use Moose::Role; +use MooseX::AttributeHelpers::Collection::TypeCheck; our $VERSION = '0.05'; our $AUTHORITY = 'cpan:STEVAN'; @@ -8,112 +9,67 @@ with 'MooseX::AttributeHelpers::MethodProvider::List'; sub push : method { my ($attr, $reader, $writer) = @_; - - if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { - my $container_type_constraint = $attr->type_constraint->type_parameter; - return sub { - my $instance = CORE::shift; - $container_type_constraint->check($_) - || confess "Value " . ($_||'undef') . " did not pass container type constraint" - foreach @_; - CORE::push @{$reader->($instance)} => @_; - }; - } - else { - return sub { - my $instance = CORE::shift; - CORE::push @{$reader->($instance)} => @_; - }; - } + return type_check($attr, sub {@_[1,$#_]}, sub { + my $self = shift; + CORE::push(@{ $reader->($self) }, @_); + }); } sub pop : method { my ($attr, $reader, $writer) = @_; - return sub { - CORE::pop @{$reader->($_[0])} - }; + return sub { CORE::pop(@{ $reader->($_[0]) }) }; } sub unshift : method { my ($attr, $reader, $writer) = @_; - if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { - my $container_type_constraint = $attr->type_constraint->type_parameter; - return sub { - my $instance = CORE::shift; - $container_type_constraint->check($_) - || confess "Value " . ($_||'undef') . " did not pass container type constraint" - foreach @_; - CORE::unshift @{$reader->($instance)} => @_; - }; - } - else { - return sub { - my $instance = CORE::shift; - CORE::unshift @{$reader->($instance)} => @_; - }; - } + return type_check($attr, sub {@_[1,$#_]}, sub { + my $self = shift; + CORE::unshift(@{ $reader->($self) }, @_); + }); } sub shift : method { my ($attr, $reader, $writer) = @_; return sub { - CORE::shift @{$reader->($_[0])} + CORE::shift(@{ $reader->($_[0]) }); }; } sub get : method { my ($attr, $reader, $writer) = @_; return sub { - $reader->($_[0])->[$_[1]] + my $self = shift; + return @{ $reader->($self) }[@_]; }; } sub set : method { my ($attr, $reader, $writer) = @_; - if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { - my $container_type_constraint = $attr->type_constraint->type_parameter; - return sub { - ($container_type_constraint->check($_[2])) - || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; - $reader->($_[0])->[$_[1]] = $_[2] - }; - } - else { - return sub { - $reader->($_[0])->[$_[1]] = $_[2] - }; - } + return type_check($attr, sub {@_[2,$#_]}, sub { + my ($self, $index, @values) = @_; + my @indexes = (ref $index eq 'ARRAY' ? @$index : ($index)); + @{ $reader->($self) }[@indexes] = @values; + }); } sub clear : method { my ($attr, $reader, $writer) = @_; - return sub { - @{$reader->($_[0])} = () - }; + return sub { @{ $reader->($_[0]) } = () }; } sub delete : method { my ($attr, $reader, $writer) = @_; return sub { - CORE::splice @{$reader->($_[0])}, $_[1], 1; - } + CORE::splice(@{ $reader->($_[0]) }, $_[1], $_[2] || 1); + }; } sub insert : method { my ($attr, $reader, $writer) = @_; - if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { - my $container_type_constraint = $attr->type_constraint->type_parameter; - return sub { - ($container_type_constraint->check($_[2])) - || confess "Value " . ($_[2]||'undef') . " did not pass container type constraint"; - CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2]; - }; - } - else { - return sub { - CORE::splice @{$reader->($_[0])}, $_[1], 0, $_[2]; - }; - } + return type_contraint($attr, sub {@_[2,$#_]}, sub { + my ($self, $index, @values) = @_; + CORE::splice(@{ $reader->($self) }, $index, 0, @values); + }); } 1; @@ -131,38 +87,56 @@ MooseX::AttributeHelpers::MethodProvider::Array This is a role which provides the method generators for L. -=head1 METHODS +=head1 PROVIDED METHODS + +This module consumes L, and so +provides all of its methods as well. All methods work when multiple indexes +are supplied - special cases are noted. =over 4 -=item B +=item B -=back +Behaves just like indexing an arrayref: returns the items indexed by the +supplied arguments (i.e. C<$self->get_my_stuff(1,2,3)> means +C<@{$aref}[1,2,3]>). -=head1 PROVIDED METHODS +=item B -This module also consumes the B method providers, to -see those provied methods, refer to that documentation. +=item B -=over 4 - -=item B +This is just like assigning to an arrayref, except that an arrayref lets you +assign multiple indexes at once with no strange syntax. You can do that with +this set as well, but the first argument should be an arrayref of the keys you +want to assign to. (e.g. C<$self->set_aref([1,2,3], qw(foo bar baz))>) =item B -=item B +L -=item B +=item B + +L =item B -=item B +L + +=item B + +L =item B -=item B +Deletes all items from the array. + +=item B + +Deletes $length (default: 1) items from the array at $index. + +=item B -=item B +Inserts @items into list at $index. =back diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Bag.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Bag.pm index 2bc0daa..6ea512f 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Bag.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Bag.pm @@ -34,42 +34,25 @@ MooseX::AttributeHelpers::MethodProvider::Bag =head1 DESCRIPTION This is a role which provides the method generators for -L. - -This role is composed from the -L role. - -=head1 METHODS - -=over 4 - -=item B - -=back +L. It also consumes +L, and thus provides all +of its methods asw well. =head1 PROVIDED METHODS =over 4 -=item B - =item B -=item B - -=item B - -=item B - -=item B +Remove the supplied key from the bag. =item B -=item B +Adds one to the value stored at the supplied key. -=item B +=item B -=item B +Sets the value at the supplied key to zero. =back diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm index 8a9b09d..2dce27c 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Counter.pm @@ -35,23 +35,21 @@ MooseX::AttributeHelpers::MethodProvider::Counter This is a role which provides the method generators for L. -=head1 METHODS +=head1 PROVIDED METHODS =over 4 -=item B +=item I -=back +Increments the value stored in this slot by 1. -=head1 PROVIDED METHODS - -=over 4 +=item I -=item B +Decrements the value stored in this slot by 1. -=item B +=item I -=item B +Resets the value stored in this slot to its default value. =back diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm index 6e08454..2ab57a4 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Hash.pm @@ -1,5 +1,6 @@ package MooseX::AttributeHelpers::MethodProvider::Hash; use Moose::Role; +use MooseX::AttributeHelpers::Collection::TypeCheck; our $VERSION = '0.04'; our $AUTHORITY = 'cpan:STEVAN'; @@ -8,45 +9,22 @@ with 'MooseX::AttributeHelpers::MethodProvider::ImmutableHash'; sub set : method { my ($attr, $reader, $writer) = @_; - if ($attr->has_type_constraint && $attr->type_constraint->isa('Moose::Meta::TypeConstraint::Parameterized')) { - my $container_type_constraint = $attr->type_constraint->type_parameter; - return sub { - my ( $self, @kvp ) = @_; - - my ( @keys, @values ); - - while ( @kvp ) { - my ( $key, $value ) = ( shift(@kvp), shift(@kvp) ); - ($container_type_constraint->check($value)) - || confess "Value " . ($value||'undef') . " did not pass container type constraint"; - push @keys, $key; - push @values, $value; + type_check( + $attr, + sub { + my ($self, %pairs) = @_; + return (values %pairs); + }, + sub { + my ($self, @pairs) = @_; + my $hash = $reader->($self); + while (@pairs) { + my $key = shift(@pairs); + my $value = shift(@pairs); + $hash->{$key} = $value; } - - if ( @values > 1 ) { - @{ $reader->($self) }{@keys} = @values; - } else { - $reader->($self)->{$keys[0]} = $values[0]; - } - }; - } - else { - return sub { - if ( @_ == 3 ) { - $reader->($_[0])->{$_[1]} = $_[2] - } else { - my ( $self, @kvp ) = @_; - my ( @keys, @values ); - - while ( @kvp ) { - push @keys, shift @kvp; - push @values, shift @kvp; - } - - @{ $reader->($_[0]) }{@keys} = @values; - } - }; - } + }, + ); } sub clear : method { @@ -75,18 +53,9 @@ MooseX::AttributeHelpers::MethodProvider::Hash =head1 DESCRIPTION This is a role which provides the method generators for -L. - -This role is composed from the -L role. - -=head1 METHODS - -=over 4 - -=item B - -=back +L. It consumes +L, and thus +provides all its methods as wel. =head1 PROVIDED METHODS @@ -94,23 +63,20 @@ L role. =item B -=item B +Returns the number of items in the hash. -=item B +=item B -=item B +Deletes the specified keys from the hash. -=item B - -=item B +=item B -=item B +Deletes all keys from the hash. =item B -=item B - -=item B +Sets the specified keys to the specified values. You can specify several of +these at once, in key => value order. =back diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm index 1cf5123..ede39d8 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/ImmutableHash.pm @@ -6,18 +6,14 @@ our $AUTHORITY = 'cpan:STEVAN'; sub exists : method { my ($attr, $reader, $writer) = @_; - return sub { CORE::exists $reader->($_[0])->{$_[1]} ? 1 : 0 }; + return sub { CORE::exists $reader->($_[0])->{$_[1]} }; } sub get : method { my ($attr, $reader, $writer) = @_; - return sub { - if ( @_ == 2 ) { - $reader->($_[0])->{$_[1]} - } else { - my ( $self, @keys ) = @_; - @{ $reader->($self) }{@keys} - } + return sub { + my ($self, @keys) = @_; + @{ $reader->($self) }{@keys} }; } @@ -46,11 +42,22 @@ sub count : method { return sub { scalar CORE::keys %{$reader->($_[0])} }; } +# Deprecated. The author was thinking backwardsly when this was written. sub empty : method { my ($attr, $reader, $writer) = @_; return sub { scalar CORE::keys %{$reader->($_[0])} ? 1 : 0 }; } +sub is_empty : method { + my ($attr, $reader, $writer) = @_; + return sub { CORE::keys %{$reader->($_[0])} == 0 }; +} + +sub has_items : method { + my ($attr, $reader, $writer) = @_; + return sub { CORE::keys %{$reader->($_[0])} > 0 }; +} + 1; __END__ @@ -66,32 +73,49 @@ MooseX::AttributeHelpers::MethodProvider::ImmutableHash This is a role which provides the method generators for L. -=head1 METHODS +=head1 PROVIDED METHODS =over 4 -=item B +=item B -=back +Returns the number of items in the hash. -=head1 PROVIDED METHODS +=item B -=over 4 +DEPRECATED. This was a misleading name for what it does (returns a boolean +indicating whether the hash is NOT empty), but we're keeping it for backwards +compatibility. Do not use it in new code. Use is_empty or has_items instead, +depending on what you meant. -=item B +=item B -=item B +Returns a boolean which is true if and only if the hash has no items in it. + +=item B + +Returns a boolean which is true if and only if the hash has at least one item. =item B -=item B +L + +=item B + +Gets the values specified by @keys from the hash. =item B +L + =item B +L + =item B +Returns a list of arrayrefs, each of which is a key => value pair mapping. + =back =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm index 949981a..bb3b5d5 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/List.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/List.pm @@ -6,16 +6,23 @@ our $AUTHORITY = 'cpan:STEVAN'; sub count : method { my ($attr, $reader, $writer) = @_; - return sub { - scalar @{$reader->($_[0])} - }; + return sub { scalar @{$reader->($_[0])} }; } +# Deprecated. The author was thinking backwardsly when this was written. sub empty : method { my ($attr, $reader, $writer) = @_; - return sub { - scalar @{$reader->($_[0])} ? 1 : 0 - }; + return sub { scalar @{$reader->($_[0])} ? 1 : 0 }; +} + +sub is_empty : method { + my ($attr, $reader, $writer) = @_; + return sub { @{ $reader->($_[0]) } == 0 }; +} + +sub has_items : method { + my ($attr, $reader, $writer) = @_; + return sub { @{ $reader->($_[0]) } > 0 }; } sub find : method { @@ -60,28 +67,41 @@ MooseX::AttributeHelpers::MethodProvider::List This is a role which provides the method generators for L. -=head1 METHODS +=head1 PROVIDED METHODS =over 4 -=item B +=item B -=back +Returns the number of items in the list. -=head1 PROVIDED METHODS +=item B -=over 4 +DEPRECATED. This was a misleading name for what it does (returns a boolean +indicating whether the list is NOT empty), but we're keeping it for backwards +compatibility. Do not use it in new code. Use is_empty or has_items instead, +depending on what you meant. -=item B +=item B -=item B +Returns a boolean which is true if and only if the list has no items in it. + +=item B -=item B +Returns a boolean which is true if and only if the list has at least one item. + +=item B + +Returns the first item in the list that satisfies $predicate. =item B +L + =item B +L + =back =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm b/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm index 9699619..a7929d0 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/Number.pm @@ -12,6 +12,7 @@ my %ops = ( div => '/', mod => '%', ); + foreach my $method (keys %ops) { my $s = $ops{$method}; @@ -48,26 +49,25 @@ L. =head1 PROVIDED METHODS -It is important to note that all those methods do in place modification of the -value stored in the attribute. All methods but 'set' are plain mathematical -operators, as in $current_value = $current_value I$argument, where I is -the operator listed next to the method name. +All methods but 'set' are plain mathematical operators, as in +C<$current_value = $current_value OP $argument> +where OP is the operator listed next to the method name. =over 4 -=item B: + +=item B + -=item B: - +=item B - -=item B: * +=item B * -=item B
: / +=item B
/ -=item B: % +=item B % -=item B: |$val|, or $val = abs($value). +=item B |$val|, or $val = abs($value). -=item B: +=item B A way to set the value instead of 'setter' or 'is => "rw"'. This method is provided for convenience. diff --git a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm index 4e4ee82..98d981f 100644 --- a/lib/MooseX/AttributeHelpers/MethodProvider/String.pm +++ b/lib/MooseX/AttributeHelpers/MethodProvider/String.pm @@ -86,33 +86,48 @@ MooseX::AttributeHelpers::MethodProvider::String This is a role which provides the method generators for L. -=head1 METHODS +=head1 PROVIDED METHODS + +It should be noted that all methods modify attribute values in place. =over 4 -=item B +=item I -=back +Increments the value stored in this slot using the magical string autoincrement +operator. Note that Perl doesn't provide analogeous behavior in C<-->, so +C is not available. -=head1 PROVIDED METHODS +=item I C<$string> -=over 4 +Append a string, like C<.=>. + +=item I C<$string> + +Prepend a string. + +=item I C<$pattern> C<$replacement> + +Performs a regexp substitution (L). There is no way to provide the +C flag, but code references will be accepted for the replacement, causing +the regex to be modified with a single C. C can be applied using the +C operator. -=item B +=item I C<$pattern> -=item B +Like I but without the replacement. Provided mostly for completeness. -=item B +=item C -=item B +L -=item B +=item C -=item B +L -=item B +=item C -=item B +Sets the string to the empty string (not the value passed to C). =back diff --git a/lib/MooseX/AttributeHelpers/Number.pm b/lib/MooseX/AttributeHelpers/Number.pm index cc90c41..19861c3 100644 --- a/lib/MooseX/AttributeHelpers/Number.pm +++ b/lib/MooseX/AttributeHelpers/Number.pm @@ -55,12 +55,13 @@ MooseX::AttributeHelpers::Number =head1 DESCRIPTION This provides a simple numeric attribute, which supports most of the -basic math operations. +basic math operations. It is important to note that all operations modify the +value of the attribute in place. =head1 METHOD PROVIDER The methods for this metaclass are provided by -L. +L. =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/String.pm b/lib/MooseX/AttributeHelpers/String.pm index b966697..7ef8123 100644 --- a/lib/MooseX/AttributeHelpers/String.pm +++ b/lib/MooseX/AttributeHelpers/String.pm @@ -68,59 +68,10 @@ above. This allows for a very basic attribute definition: has 'foo' => (metaclass => 'String'); $obj->append_foo; -=head1 METHODS - -=over 4 - -=item B - -=back - =head1 PROVIDED METHODS -It is important to note that all those methods do in place -modification of the value stored in the attribute. - -=over 4 - -=item I - -Increments the value stored in this slot using the magical string autoincrement -operator. Note that Perl doesn't provide analogeous behavior in C<-->, so -C is not available. - -=item I C<$string> - -Append a string, like C<.=>. - -=item I C<$string> - -Prepend a string. - -=item I C<$pattern> C<$replacement> - -Performs a regexp substitution (L). There is no way to provide the -C flag, but code references will be accepted for the replacement, causing -the regex to be modified with a single C. C can be applied using the -C operator. - -=item I C<$pattern> - -Like I but without the replacement. Provided mostly for completeness. - -=item C - -L - -=item C - -L - -=item C - -Sets the string to the empty string (not the value passed to C). - -=back +The methods for this metaclass are provided by +L. =head1 BUGS diff --git a/lib/MooseX/AttributeHelpers/Sugar.pm b/lib/MooseX/AttributeHelpers/Sugar.pm index 2809f17..f5ec33b 100644 --- a/lib/MooseX/AttributeHelpers/Sugar.pm +++ b/lib/MooseX/AttributeHelpers/Sugar.pm @@ -78,7 +78,8 @@ exported. =item B The following parameters are accepted, and are used to override methods in -the base class (see its documentation for details). +the base class (see L for +details). =item B I @@ -92,7 +93,7 @@ the base class (see its documentation for details). =back -=head SHORTCUT +=head1 SHORTCUT For ease of use of the generated metaclasses, if you pass in a "shortcut" parameter to define_attribute_helper, a class at diff --git a/t/002_basic_array.t b/t/002_basic_array.t index 27520ee..450b2bf 100644 --- a/t/002_basic_array.t +++ b/t/002_basic_array.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 51; +use Test::More tests => 54; use Test::Exception; BEGIN { @@ -20,15 +20,16 @@ BEGIN { isa => 'ArrayRef[Int]', default => sub { [] }, provides => { - 'push' => 'add_options', - 'pop' => 'remove_last_option', - 'shift' => 'remove_first_option', - 'unshift' => 'insert_options', - 'get' => 'get_option_at', - 'set' => 'set_option_at', - 'count' => 'num_options', - 'empty' => 'has_options', - 'clear' => 'clear_options', + 'push' => 'add_options', + 'pop' => 'remove_last_option', + 'shift' => 'remove_first_option', + 'unshift' => 'insert_options', + 'get' => 'get_option_at', + 'set' => 'set_option_at', + 'count' => 'num_options', + 'is_empty' => 'no_options', + 'has_items' => 'has_options', + 'clear' => 'clear_options', } ); } @@ -46,6 +47,7 @@ can_ok($stuff, $_) for qw[ num_options clear_options has_options + no_options ]; is_deeply($stuff->options, [10, 12], '... got options'); @@ -58,7 +60,7 @@ is($stuff->remove_first_option, 10, '... removed the last option'); is_deeply($stuff->options, [], '... no options anymore'); -ok(!$stuff->has_options, '... no options'); +ok($stuff->no_options, '... no options'); is($stuff->num_options, 0, '... got no options'); lives_ok { @@ -67,7 +69,7 @@ lives_ok { is_deeply($stuff->options, [1, 2, 3], '... got options now'); -ok($stuff->has_options, '... no options'); +ok($stuff->has_options, '... have options'); is($stuff->num_options, 3, '... got 3 options'); is($stuff->get_option_at(0), 1, '... get option at index 0'); @@ -112,6 +114,16 @@ is($stuff->get_option_at(0), 20, '... get option at index 0'); $stuff->clear_options; is_deeply( $stuff->options, [], "... clear options" ); +lives_ok { + $stuff->set_option_at([0..2], 10, 20, 30); +} '... set multiple options'; + +is_deeply( + [$stuff->get_option_at(0..2)], + [10,20,30], + '... and got what we set' +); + ## check some errors dies_ok { @@ -136,15 +148,16 @@ my $options = $stuff->meta->get_attribute('options'); isa_ok($options, 'MooseX::AttributeHelpers::Collection::Array'); is_deeply($options->provides, { - 'push' => 'add_options', - 'pop' => 'remove_last_option', - 'shift' => 'remove_first_option', - 'unshift' => 'insert_options', - 'get' => 'get_option_at', - 'set' => 'set_option_at', - 'count' => 'num_options', - 'empty' => 'has_options', - 'clear' => 'clear_options', -}, '... got the right provies mapping'); + 'push' => 'add_options', + 'pop' => 'remove_last_option', + 'shift' => 'remove_first_option', + 'unshift' => 'insert_options', + 'get' => 'get_option_at', + 'set' => 'set_option_at', + 'count' => 'num_options', + 'is_empty' => 'no_options', + 'has_items' => 'has_options', + 'clear' => 'clear_options', +}, '... got the right provides mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); diff --git a/t/003_basic_hash.t b/t/003_basic_hash.t index 24a6b3f..8f31e83 100644 --- a/t/003_basic_hash.t +++ b/t/003_basic_hash.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 32; +use Test::More tests => 33; use Test::Exception; BEGIN { @@ -21,12 +21,13 @@ BEGIN { isa => 'HashRef[Str]', default => sub { {} }, provides => { - 'set' => 'set_option', - 'get' => 'get_option', - 'empty' => 'has_options', - 'count' => 'num_options', - 'clear' => 'clear_options', - 'delete' => 'delete_option', + 'set' => 'set_option', + 'get' => 'get_option', + 'has_items' => 'has_options', + 'is_empty' => 'no_options', + 'count' => 'num_options', + 'clear' => 'clear_options', + 'delete' => 'delete_option', } ); } @@ -38,12 +39,13 @@ can_ok($stuff, $_) for qw[ set_option get_option has_options + no_options num_options delete_option clear_options ]; -ok(!$stuff->has_options, '... we have no options'); +ok($stuff->no_options, '... we have no options'); is($stuff->num_options, 0, '... we have no options'); is_deeply($stuff->options, {}, '... no options yet'); diff --git a/t/005_basic_list.t b/t/005_basic_list.t index bb8d10f..ac50393 100644 --- a/t/005_basic_list.t +++ b/t/005_basic_list.t @@ -20,11 +20,11 @@ BEGIN { isa => 'ArrayRef[Int]', default => sub { [] }, provides => { - 'count' => 'num_options', - 'empty' => 'has_options', - 'map' => 'map_options', - 'grep' => 'filter_options', - 'find' => 'find_option', + 'count' => 'num_options', + 'has_items' => 'has_options', + 'map' => 'map_options', + 'grep' => 'filter_options', + 'find' => 'find_option', } ); } @@ -65,11 +65,11 @@ my $options = $stuff->meta->get_attribute('options'); isa_ok($options, 'MooseX::AttributeHelpers::Collection::List'); is_deeply($options->provides, { - 'map' => 'map_options', - 'grep' => 'filter_options', - 'find' => 'find_option', - 'count' => 'num_options', - 'empty' => 'has_options', -}, '... got the right provies mapping'); + 'map' => 'map_options', + 'grep' => 'filter_options', + 'find' => 'find_option', + 'count' => 'num_options', + 'has_items' => 'has_options', +}, '... got the right provides mapping'); is($options->type_constraint->type_parameter, 'Int', '... got the right container type'); diff --git a/t/006_basic_bag.t b/t/006_basic_bag.t index a90bbc9..69fcbe8 100644 --- a/t/006_basic_bag.t +++ b/t/006_basic_bag.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 18; +use Test::More tests => 19; use Test::Exception; BEGIN { @@ -19,11 +19,12 @@ BEGIN { metaclass => 'Collection::Bag', is => 'ro', provides => { - 'add' => 'add_word', - 'get' => 'get_count_for', - 'empty' => 'has_any_words', - 'count' => 'num_words', - 'delete' => 'delete_word', + 'add' => 'add_word', + 'get' => 'get_count_for', + 'has_items' => 'has_any_words', + 'is_empty' => 'has_no_words', + 'count' => 'num_words', + 'delete' => 'delete_word', } ); } @@ -35,11 +36,12 @@ can_ok($stuff, $_) for qw[ add_word get_count_for has_any_words + has_no_words num_words delete_word ]; -ok(!$stuff->has_any_words, '... we have no words'); +ok($stuff->has_no_words, '... we have no words'); is($stuff->num_words, 0, '... we have no words'); lives_ok {