From: Guillermo Roditi Date: Fri, 30 Jan 2009 01:06:42 +0000 (+0000) Subject: initial checkin X-Git-Tag: 0.001003~14 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Types-Common.git;a=commitdiff_plain;h=3379cc45bfe75897006b72725753c9984add7afd initial checkin --- 3379cc45bfe75897006b72725753c9984add7afd diff --git a/Changes b/Changes new file mode 100644 index 0000000..b0ab4aa --- /dev/null +++ b/Changes @@ -0,0 +1,2 @@ +0.001000 + - Initial release. This is a spin off from Reaction. \ No newline at end of file diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..2c2667d --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,16 @@ +#!/usr/bin/perl -w +use strict; +use inc::Module::Install; + +name 'MooseX-Types-Common'; +license 'perl'; +all_from 'lib/MooseX/Types/Common.pm'; + +# prereqs +requires 'Moose' => '0.39'; +requires 'MooseX::Types' => '0.04'; + +# things the tests need +test_requires 'Test::More' => '0.62'; + +WriteAll; diff --git a/README b/README new file mode 100644 index 0000000..a0afb3d --- /dev/null +++ b/README @@ -0,0 +1,5 @@ +perl Makefile.PL +make +make test +sudo make install +make clean \ No newline at end of file diff --git a/lib/MooseX/Types/Common.pm b/lib/MooseX/Types/Common.pm new file mode 100644 index 0000000..651d011 --- /dev/null +++ b/lib/MooseX/Types/Common.pm @@ -0,0 +1,145 @@ +package MooseX::Types::Common; + +use strict; +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 + +MooseX::Types::Common + +=head1 SYNOPSIS + + use MooseX::Types::Common 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 + +=item * PositiveNum + +=item * PositiveInt + +=item * NegativeNum + +=item * Int + +=item * SingleDigit + +=back + +=head1 SEE ALSO + +=over + +=item * L + +=item * L + +=back + +=head1 AUTHORS + +This distribution was extracted from the L code base by Guillermo +Roditi (groditi). + +The original authors of this library are: + +=over 4 + +=item * Matt S. Trout + +=item * K. J. Cheetham + +=item * Guillermo Roditi + +=back + +=head1 LICENSE + +This library is free software, you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/t/00-load.t b/t/00-load.t new file mode 100644 index 0000000..b004273 --- /dev/null +++ b/t/00-load.t @@ -0,0 +1,7 @@ +#! /usr/bin/perl -w + +use strict; +use warnings; +use Test::More tests => 1; + +use_ok('MooseX::Types::Common'); diff --git a/t/01-common.t b/t/01-common.t new file mode 100644 index 0000000..355be72 --- /dev/null +++ b/t/01-common.t @@ -0,0 +1,63 @@ +#! /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';