From: Guillermo Roditi Date: Mon, 2 Feb 2009 20:05:27 +0000 (+0000) Subject: restructuring X-Git-Tag: 0.001003~13 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Types-Common.git;a=commitdiff_plain;h=ac73ab52030a0e37e78c715d880d92cdb8f4b0ed restructuring --- diff --git a/lib/MooseX/Types/Common.pm b/lib/MooseX/Types/Common.pm index 651d011..8bf9726 100644 --- a/lib/MooseX/Types/Common.pm +++ b/lib/MooseX/Types/Common.pm @@ -5,64 +5,6 @@ use warnings; our $VERSION = '0.001000'; -use MooseX::Types -declare => [ - qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr - PositiveNum PositiveInt NegativeNum NegativeInt SingleDigit) -]; - -use MooseX::Types::Moose qw/Str Num Int Object/; - -subtype SimpleStr, - as Str, - where { (length($_) <= 255) && ($_ !~ m/\n/) }, - message { "Must be a single line of no more than 255 chars" }; - -subtype NonEmptySimpleStr, - as SimpleStr, - where { length($_) > 0 }, - message { "Must be a non-empty single line of no more than 255 chars" }; - -# XXX duplicating constraint msges since moose only uses last message -subtype Password, - as NonEmptySimpleStr, - where { length($_) > 3 }, - message { "Must be between 4 and 255 chars" }; - -subtype StrongPassword, - as Password, - where { (length($_) > 7) && (m/[^a-zA-Z]/) }, - message {"Must be between 8 and 255 chars, and contain a non-alpha char" }; - -subtype NonEmptyStr, - as Str, - where { length($_) > 0 }, - message { "Must not be empty" }; - -subtype PositiveNum, - as Num, - where { $_ >= 0 }, - message { "Must be a positive number" }; - -subtype PositiveInt, - as Int, - where { $_ >= 0 }, - message { "Must be a positive integer" }; - -subtype NegativeNum, - as Num, - where { $_ <= 0 }, - message { "Must be a negative number" }; - -subtype NegativeInt, - as Int, - where { $_ <= 0 }, - message { "Must be a negative integer" }; - -subtype SingleDigit, - as PositiveInt, - where { $_ <= 9 }, - message { "Must be a single digit" }; - 1; =head1 NAME @@ -71,49 +13,34 @@ MooseX::Types::Common =head1 SYNOPSIS - use MooseX::Types::Common qw/SimpleStr/; + use MooseX::Types::Common::String qw/SimpleStr/; has short_str => (is => 'rw', isa => SimpleStr); ... #this will fail $object->short_str("string\nwith\nbreaks"); -=head1 DESCRIPTION - -A set of commonly-used type constraints that do not ship with Moose by default. - -=over - -=item * SimpleStr - -A Str with no new-line characters. - -=item * NonEmptySimpleStr - -Does what it says on the tin. - -=item * Password -=item * StrongPassword -=item * NonEmptyStr + use MooseX::Types::Common::Numeric qw/PositiveInt/; + has count => (is => 'rw', isa => PositiveInt); -=item * PositiveNum - -=item * PositiveInt - -=item * NegativeNum - -=item * Int + ... + #this will fail + $object->count(-33); -=item * SingleDigit +=head1 DESCRIPTION -=back +A set of commonly-used type constraints that do not ship with Moose by default. =head1 SEE ALSO =over +=item * L + +=item * L + =item * L =item * L diff --git a/lib/MooseX/Types/Common/Numeric.pm b/lib/MooseX/Types/Common/Numeric.pm new file mode 100644 index 0000000..17127a2 --- /dev/null +++ b/lib/MooseX/Types/Common/Numeric.pm @@ -0,0 +1,89 @@ +package MooseX::Types::Common::Numeric; + +use strict; +use warnings; + +our $VERSION = '0.001000'; + +use MooseX::Types -declare => [ + qw(PositiveNum PositiveInt NegativeNum NegativeInt SingleDigit) +]; + +use MooseX::Types::Moose qw/Num Int/; + +subtype PositiveNum, + as Num, + where { $_ >= 0 }, + message { "Must be a positive number" }; + +subtype PositiveInt, + as Int, + where { $_ >= 0 }, + message { "Must be a positive integer" }; + +subtype NegativeNum, + as Num, + where { $_ <= 0 }, + message { "Must be a negative number" }; + +subtype NegativeInt, + as Int, + where { $_ <= 0 }, + message { "Must be a negative integer" }; + +subtype SingleDigit, + as PositiveInt, + where { $_ <= 9 }, + message { "Must be a single digit" }; + + +1; + +__END__; + + +=head1 NAME + +MooseX::Types::Common::Numeric + +=head1 SYNOPSIS + + use MooseX::Types::Common::Numeric qw/PositiveInt/; + has count => (is => 'rw', isa => PositiveInt); + + ... + #this will fail + $object->count(-33); + +=head1 DESCRIPTION + +A set of commonly-used numeric type constraints that do not ship with Moose by +default. + +=over + +=item * PositiveNum + +=item * PositiveInt + +=item * NegativeNum + +=item * Int + +=item * SingleDigit + +=back + +=head1 SEE ALSO + +=over + +=item * L + +=back + +=head1 AUTHORS + +Please see:: L + +=cut diff --git a/lib/MooseX/Types/Common/String.pm b/lib/MooseX/Types/Common/String.pm new file mode 100644 index 0000000..708f666 --- /dev/null +++ b/lib/MooseX/Types/Common/String.pm @@ -0,0 +1,91 @@ +package MooseX::Types::Common::String; + +use strict; +use warnings; + +our $VERSION = '0.001000'; + +use MooseX::Types -declare => [ + qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr) +]; + +use MooseX::Types::Moose qw/Str/; + +subtype SimpleStr, + as Str, + where { (length($_) <= 255) && ($_ !~ m/\n/) }, + message { "Must be a single line of no more than 255 chars" }; + +subtype NonEmptySimpleStr, + as SimpleStr, + where { length($_) > 0 }, + message { "Must be a non-empty single line of no more than 255 chars" }; + +# XXX duplicating constraint msges since moose only uses last message +subtype Password, + as NonEmptySimpleStr, + where { length($_) > 3 }, + message { "Must be between 4 and 255 chars" }; + +subtype StrongPassword, + as Password, + where { (length($_) > 7) && (m/[^a-zA-Z]/) }, + message {"Must be between 8 and 255 chars, and contain a non-alpha char" }; + +subtype NonEmptyStr, + as Str, + where { length($_) > 0 }, + message { "Must not be empty" }; + + +1; + +=head1 NAME + +MooseX::Types::Common::String + +=head1 SYNOPSIS + + use MooseX::Types::Common::String qw/SimpleStr/; + has short_str => (is => 'rw', isa => SimpleStr); + + ... + #this will fail + $object->short_str("string\nwith\nbreaks"); + +=head1 DESCRIPTION + +A set of commonly-used string type constraints that do not ship with Moose by +default. + +=over + +=item * SimpleStr + +A Str with no new-line characters. + +=item * NonEmptySimpleStr + +Does what it says on the tin. + +=item * Password + +=item * StrongPassword + +=item * NonEmptyStr + +=back + +=head1 SEE ALSO + +=over + +=item * L + +=back + +=head1 AUTHORS + +Please see:: L + +=cut diff --git a/t/00-load.t b/t/00-load.t index b004273..e51d270 100644 --- a/t/00-load.t +++ b/t/00-load.t @@ -2,6 +2,8 @@ use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 3; use_ok('MooseX::Types::Common'); +use_ok('MooseX::Types::Common::String'); +use_ok('MooseX::Types::Common::Numeric'); diff --git a/t/01-common.t b/t/01-common.t deleted file mode 100644 index 355be72..0000000 --- a/t/01-common.t +++ /dev/null @@ -1,63 +0,0 @@ -#! /usr/bin/perl -w - -use strict; -use warnings; -use Test::More tests => 26; -use Test::Exception; - -{ - package FooTest; - use Moose; - use MooseX::Types::Common ( - qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr), - qw(PositiveNum PositiveInt NegativeInt NegativeNum SingleDigit) - ); - - has simplestr => ( is => 'rw', isa => SimpleStr); - has nestr => ( is => 'rw', isa => NonEmptyStr); - has nesimplestr => ( is => 'rw', isa => NonEmptySimpleStr); - has password => ( is => 'rw', isa => Password); - has strongpassword => ( is => 'rw', isa => StrongPassword); - - has digit => ( is => 'rw', isa => SingleDigit); - has posnum => ( is => 'rw', isa => PositiveNum); - has posint => ( is => 'rw', isa => PositiveInt); - has negnum => ( is => 'rw', isa => NegativeNum); - has negint => ( is => 'rw', isa => NegativeInt); -} - -my $ins = FooTest->new; - -lives_ok { $ins->simplestr('') } 'SimpleStr'; -lives_ok { $ins->simplestr('good string') } 'SimpleStr 2'; -dies_ok { $ins->simplestr("bad\nstring") } 'SimpleStr 3'; -dies_ok { $ins->simplestr(join('', ("long string" x 25))) } 'SimpleStr 4'; - -dies_ok { $ins->nestr('') } 'NonEmptyStr'; -lives_ok { $ins->nestr('good string') } 'NonEmptyStr 2'; -lives_ok { $ins->nestr("bad\nstring") } 'NonEmptyStr 3'; -lives_ok { $ins->nestr(join('', ("long string" x 25))) } 'NonEmptyStr 4'; - -lives_ok { $ins->nesimplestr('good str') } 'NonEmptySimplrStr '; -dies_ok { $ins->nesimplestr('') } 'NonEmptyStr 2'; - -dies_ok { $ins->password('no') } 'Password'; -lives_ok { $ins->password('okay') } 'Password 2'; - -dies_ok { $ins->strongpassword('notokay') } 'StrongPassword'; -lives_ok { $ins->strongpassword('83773r_ch01c3') } 'StrongPassword 2'; - -dies_ok { $ins->digit(100); } 'SingleDigit'; -lives_ok { $ins->digit(1); } 'SingleDigit 2'; - -dies_ok { $ins->posint(-100); } 'PositiveInt'; -dies_ok { $ins->posint(100.885); } 'PositiveInt 2'; -lives_ok { $ins->posint(100); } 'PositiveInt 3'; -lives_ok { $ins->posnum(100.885); } 'PositiveNum'; -dies_ok { $ins->posnum(-100.885); } 'PositiveNum 2'; - -dies_ok { $ins->negint(100); } 'NegativeInt'; -dies_ok { $ins->negint(-100.885); } 'NegativeInt 2'; -lives_ok { $ins->negint(-100); } 'NegativeInt 3'; -lives_ok { $ins->negnum(-100.885); } 'NegativeNum'; -dies_ok { $ins->negnum(100.885); } 'NegativeNum 2'; diff --git a/t/01-string.t b/t/01-string.t new file mode 100644 index 0000000..c69f4a3 --- /dev/null +++ b/t/01-string.t @@ -0,0 +1,41 @@ +#! /usr/bin/perl -w + +use strict; +use warnings; +use Test::More tests => 14; +use Test::Exception; + +{ + package FooTest; + use Moose; + use MooseX::Types::Common ( + qw(SimpleStr NonEmptySimpleStr Password StrongPassword NonEmptyStr), + ); + + has simplestr => ( is => 'rw', isa => SimpleStr); + has nestr => ( is => 'rw', isa => NonEmptyStr); + has nesimplestr => ( is => 'rw', isa => NonEmptySimpleStr); + has password => ( is => 'rw', isa => Password); + has strongpassword => ( is => 'rw', isa => StrongPassword); +} + +my $ins = FooTest->new; + +lives_ok { $ins->simplestr('') } 'SimpleStr'; +lives_ok { $ins->simplestr('good string') } 'SimpleStr 2'; +dies_ok { $ins->simplestr("bad\nstring") } 'SimpleStr 3'; +dies_ok { $ins->simplestr(join('', ("long string" x 25))) } 'SimpleStr 4'; + +dies_ok { $ins->nestr('') } 'NonEmptyStr'; +lives_ok { $ins->nestr('good string') } 'NonEmptyStr 2'; +lives_ok { $ins->nestr("bad\nstring") } 'NonEmptyStr 3'; +lives_ok { $ins->nestr(join('', ("long string" x 25))) } 'NonEmptyStr 4'; + +lives_ok { $ins->nesimplestr('good str') } 'NonEmptySimplrStr '; +dies_ok { $ins->nesimplestr('') } 'NonEmptyStr 2'; + +dies_ok { $ins->password('no') } 'Password'; +lives_ok { $ins->password('okay') } 'Password 2'; + +dies_ok { $ins->strongpassword('notokay') } 'StrongPassword'; +lives_ok { $ins->strongpassword('83773r_ch01c3') } 'StrongPassword 2'; diff --git a/t/02-numeric.t b/t/02-numeric.t new file mode 100644 index 0000000..0e7a946 --- /dev/null +++ b/t/02-numeric.t @@ -0,0 +1,37 @@ +#! /usr/bin/perl -w + +use strict; +use warnings; +use Test::More tests => 12; +use Test::Exception; + +{ + package FooTest; + use Moose; + use MooseX::Types::Common ( + qw(PositiveNum PositiveInt NegativeInt NegativeNum SingleDigit) + ); + + has digit => ( is => 'rw', isa => SingleDigit); + has posnum => ( is => 'rw', isa => PositiveNum); + has posint => ( is => 'rw', isa => PositiveInt); + has negnum => ( is => 'rw', isa => NegativeNum); + has negint => ( is => 'rw', isa => NegativeInt); +} + +my $ins = FooTest->new; + +dies_ok { $ins->digit(100); } 'SingleDigit'; +lives_ok { $ins->digit(1); } 'SingleDigit 2'; + +dies_ok { $ins->posint(-100); } 'PositiveInt'; +dies_ok { $ins->posint(100.885); } 'PositiveInt 2'; +lives_ok { $ins->posint(100); } 'PositiveInt 3'; +lives_ok { $ins->posnum(100.885); } 'PositiveNum'; +dies_ok { $ins->posnum(-100.885); } 'PositiveNum 2'; + +dies_ok { $ins->negint(100); } 'NegativeInt'; +dies_ok { $ins->negint(-100.885); } 'NegativeInt 2'; +lives_ok { $ins->negint(-100); } 'NegativeInt 3'; +lives_ok { $ins->negnum(-100.885); } 'NegativeNum'; +dies_ok { $ins->negnum(100.885); } 'NegativeNum 2';