Aion

 view release on metacpan or  search on metacpan

t/aion/types.t  view on Meta::CPAN

# 
# # SYNOPSIS
# 
subtest 'SYNOPSIS' => sub { 
use Aion::Types;

BEGIN {
	subtype SpeakOfKitty => as StrMatch[qr/\bkitty\b/i],
		message { "Speak is'nt included kitty!" };
}

local ($::_g0 = do {"Kitty!" ~~ SpeakOfKitty}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '"Kitty!" ~~ SpeakOfKitty # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {"abc"    ~~ SpeakOfKitty}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '"abc"    ~~ SpeakOfKitty # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

eval {SpeakOfKitty->validate("abc", "This")}; local ($::_g0 = $@, $::_e0 = 'Speak is\'nt included kitty!'); ok defined($::_g0) && $::_g0 =~ /^${\quotemeta $::_e0}/, 'SpeakOfKitty->validate("abc", "This") # @-> Speak is\'nt included kitty!' or ::diag ...


BEGIN {
	subtype 'IntOrArrayRef[len, B]',
		as Int & Range[5, A]
			| ArrayRef[B & Len[A]];
}

local ($::_g0 = do {35 ~~ IntOrArrayRef[35, Tel]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '35 ~~ IntOrArrayRef[35, Tel] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {35 ~~ IntOrArrayRef[34, Tel]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '35 ~~ IntOrArrayRef[34, Tel] # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

local ($::_g0 = do {'+23456789'  ~~ Tel}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '\'+23456789\'  ~~ Tel # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {'+234567890' ~~ Tel}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '\'+234567890\' ~~ Tel # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {'+23456789'  ~~ (Tel & Len[9])}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '\'+23456789\'  ~~ (Tel & Len[9]) # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {'+234567890' ~~ (Tel & Len[9])}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '\'+234567890\' ~~ (Tel & Len[9]) # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

local ($::_g0 = do {['+23456789',  '+23456789'] ~~ IntOrArrayRef[9, Tel]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '[\'+23456789\',  \'+23456789\'] ~~ IntOrArrayRef[9, Tel] # -> 1' or ::diag ::_struct_diff($::_g...
local ($::_g0 = do {['+234567890', '+23456789'] ~~ IntOrArrayRef[9, Tel]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '[\'+234567890\', \'+23456789\'] ~~ IntOrArrayRef[9, Tel] # -> ""' or ::diag ::_struct_diff($::...

local ($::_g0 = do {"" ~~ IntOrArrayRef[8, Tel]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '"" ~~ IntOrArrayRef[8, Tel]  # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;


coerce IntOrArrayRef[35, Str], from Num, via { int($_ + .5) };

local ($::_g0 = do {IntOrArrayRef([35, Str])->coerce(5.5)}, $::_e0 = "6"); ::ok $::_g0 eq $::_e0, 'IntOrArrayRef([35, Str])->coerce(5.5) # => 6' or ::diag ::_string_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

local ($::_g0 = do {5.5 >> IntOrArrayRef[35, Str]}, $::_e0 = "6"); ::ok $::_g0 eq $::_e0, '5.5 >> IntOrArrayRef[35, Str] # => 6' or ::diag ::_string_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

local ($::_g0 = do {(Tel & Len[9]) < (Tel & Len[10])}, $::_e0 = "1"); ::ok $::_g0 eq $::_e0, '(Tel & Len[9]) < (Tel & Len[10]) # => 1' or ::diag ::_string_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

# 
# # DESCRIPTION
# 
# Этот модуль экспортирует подпрограммы:
# 
# * `subtype`, `as`, `init_where`, `where`, `awhere`, `message` — для создания валидаторов.
# * `SELF`, `ARGS`, `A`, `B`, `C`, `D`, `M`, `N` — для использования в валидаторах типа и его аргументов.
# * `coerce`, `from`, `via` — для создания конвертора значений из одного класса в другой.
# 
# Иерархия валидаторов:
# 

# Any
# 	Control
# 		Union[A, B...]
# 		Intersection[A, B...]
# 		Exclude[A...]
# 		Option[A]
# 		Wantarray[A, B]
# 	Item
# 		External[type]
# 		Bool
# 		BoolLike
# 		Enum[e...]
# 		Maybe[A]
# 		Undef
# 		Defined
# 			Value
# 				Version
# 				Str
# 					Uni
# 					Bin
# 					NonEmptyStr
# 					StartsWith[start]
# 					EndsWith[end]
# 					Email
# 					Tel
# 					Url
# 					Path
# 					Html
# 					StrDate
# 					StrDateTime
# 					StrMatch[regexp]
# 					PackageName
# 						ClassName
# 						RoleName
# 					Join[separator]
# 					Split[separator]
# 					StrRat
# 					Num
# 						PositiveNum
# 						Int
# 							PositiveInt
# 							Nat
# 			Ref
# 				Tied`[class]
# 				LValueRef
# 				FormatRef
# 				CodeRef
# 					NamedCode[subname]
# 					ProtoCode[prototype]
# 					ForwardRef
# 					ImplementRef
# 					Isa[A...]
# 				RegexpRef
# 				ValueRef`[A]
# 					ScalarRef`[A]
# 					RefRef`[A]
# 				GlobRef
# 					FileHandle
# 				ArrayRef`[A]
# 					Tuple[A...]
# 					CycleTuple[A...]
# 				HashRef`[A]
# 					Map[A => B]
# 					Dict[k => A, ...]

t/aion/types.t  view on Meta::CPAN


# Bool from Any — 1 or ""
local ($::_g0 = do {Bool->coerce([])}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'Bool->coerce([]) # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {Bool->coerce(0)}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'Bool->coerce(0)  # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

# 
# ## from ($type)
# 
# Синтаксический сахар для `coerce`.
# 
# ## via ($code)
# 
# Синтаксический сахар для `coerce`.
# 
# # ATTRIBUTES
# 
# ## :Isa (@signature)
# 
# Проверяет сигнатуру подпрограммы: аргументы и результаты.
# 
::done_testing; }; subtest ':Isa (@signature)' => sub { 
sub minint($$) : Isa(Int => Int => Int) {
	my ($x, $y) = @_;
	$x < $y? $x : $y
}

local ($::_g0 = do {minint 6, 5;}, $::_e0 = do {5}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'minint 6, 5; # -> 5' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
::like scalar do {eval {minint 5.5, 2}; $@}, qr{Arguments of method `minint` must have the type Tuple\[Int, Int\]\.}, 'eval {minint 5.5, 2}; $@ # ~> Arguments of method `minint` must have the type Tuple\[Int, Int\]\.'; undef $::_g0; undef $::_e0;

sub half($) : Isa(Int => Int) {
	my ($x) = @_;
	$x / 2
}

local ($::_g0 = do {half 4;}, $::_e0 = do {2}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'half 4; # -> 2' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
::like scalar do {eval {half 5}; $@}, qr{Return of method `half` must have the type Int. The it is 2.5}, 'eval {half 5}; $@ # ~> Return of method `half` must have the type Int. The it is 2.5'; undef $::_g0; undef $::_e0;

# 
# # TYPES
# 
# ## Any
# 
# Тип верхнего уровня в иерархии. Сопоставляет всё.
# 
# Нижний тип можно получить как `~Any`.
# 
# ## Control
# 
# Тип верхнего уровня в конструкторах иерархии создает новые типы из любых типов.
# 
# ## Union[A, B...]
# 
# Союз нескольких типов. Аналогичен оператору `$type1 | $type2`.
# 
::done_testing; }; subtest 'Union[A, B...]' => sub { 
local ($::_g0 = do {33  ~~ Union[Int, Ref]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '33  ~~ Union[Int, Ref] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {[]  ~~ Union[Int, Ref]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '[]  ~~ Union[Int, Ref] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {"a" ~~ Union[Int, Ref]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '"a" ~~ Union[Int, Ref] # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

# 
# ## Intersection[A, B...]
# 
# Пересечение нескольких типов. Аналогичен оператору `$type1 & $type2`.
# 
::done_testing; }; subtest 'Intersection[A, B...]' => sub { 
local ($::_g0 = do {15 ~~ Intersection[Int, StrMatch[/5/]]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '15 ~~ Intersection[Int, StrMatch[/5/]] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef ...

# 
# ## Exclude[A]
# 
# Исключение значения типа. Аналогичен оператору `~ $type`.
# 
::done_testing; }; subtest 'Exclude[A]' => sub { 
local ($::_g0 = do {-5  ~~ Exclude[PositiveInt]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '-5  ~~ Exclude[PositiveInt] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {"a" ~~ Exclude[PositiveInt]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '"a" ~~ Exclude[PositiveInt] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {5   ~~ Exclude[PositiveInt]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '5   ~~ Exclude[PositiveInt] # -> ""' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {5.5 ~~ Exclude[PositiveInt]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '5.5 ~~ Exclude[PositiveInt] # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

# 
# ## Option[A]
# 
# Дополнительные ключи в `Dict`.
# 
::done_testing; }; subtest 'Option[A]' => sub { 
local ($::_g0 = do {{a=>55} ~~ Dict[a=>Int, b => Option[Int]]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '{a=>55} ~~ Dict[a=>Int, b => Option[Int]]          # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef...
local ($::_g0 = do {{a=>55, b=>31} ~~ Dict[a=>Int, b => Option[Int]]}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '{a=>55, b=>31} ~~ Dict[a=>Int, b => Option[Int]]   # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0)...
local ($::_g0 = do {{a=>55, b=>31.5} ~~ Dict[a=>Int, b => Option[Int]]}, $::_e0 = do {""}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '{a=>55, b=>31.5} ~~ Dict[a=>Int, b => Option[Int]] # -> ""' or ::diag ::_struct_diff($::_g0, $::...

# 
# ## Wantarray[A, S]
# 
# Если подпрограмма возвращает разные значения в контексте массива и скаляра, то используется тип `Wantarray` с типом `A` для контекста массива...
# 
::done_testing; }; subtest 'Wantarray[A, S]' => sub { 
sub arr : Isa(PositiveInt => Wantarray[ArrayRef[PositiveInt], PositiveInt]) {
	my ($n) = @_;
	wantarray? 1 .. $n: $n
}

my @array = arr(3);
my $scalar = arr(3);

local ($::_g0 = do {\@array}, $::_e0 = do {[1,2,3]}); ::is_deeply $::_g0, $::_e0, '\@array # --> [1,2,3]' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {$scalar}, $::_e0 = do {3}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, '$scalar  # -> 3' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

# 
# ## Item
# 
# Тип верхнего уровня в иерархии скалярных типов.
# 
# ## External[type]
# 
# Правращает `type` в `Aion::Type`.
# 
# * Если `type` - `Aion::Type`, то возвращает его без изменений.
# * Если `type` строка, то оборачивает его в `Object`.
# * Если `type` можно вызвать, то оборачивает его в `Aion::Type->new(test => $type, ...)`. А если он имеет метод `coerce`, то будет его использовать для преобразова...
# 
::done_testing; }; subtest 'External[type]' => sub { 
local ($::_g0 = do {External['Aion']}, $::_e0 = do {Object['Aion']}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'External[\'Aion\'] # -> Object[\'Aion\']' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;
local ($::_g0 = do {External[sub { /^x/ }] ~~ 'xyz'}, $::_e0 = do {1}); ::ok defined($::_g0) == defined($::_e0) && $::_g0 eq $::_e0, 'External[sub { /^x/ }] ~~ \'xyz\' # -> 1' or ::diag ::_struct_diff($::_g0, $::_e0); undef $::_g0; undef $::_e0;

package MyInt {
	use overload "&{}" => sub {
		sub { /^[+-]?[0-9]+$/ }
	};



( run in 1.327 second using v1.01-cache-2.11-cpan-6aa56a78535 )