Bump copyright_year in dist.ini
[gitmo/MooseX-Params-Validate.git] / t / 010_overloaded.t
1 use Test::More;
2 use strict;
3 use warnings;
4
5 {
6     package Foo;
7
8     use Moose;
9     use MooseX::Params::Validate;
10     use overload (
11         qw{""} => 'to_string',
12     );
13
14     has 'id' => (
15         is      => 'ro',
16         isa     => 'Str',
17         default => '1.10.100',
18     );
19
20     sub to_string {
21         my ( $self, %args ) = validated_hash(
22             \@_,
23             padded => {
24                 isa      => 'Bool',
25                 optional => 1,
26                 default  => 0,
27             },
28         );
29
30         # 1.10.100 => 0001.0010.0100
31         my $id
32             = $args{padded}
33             ? join(
34             '.',
35             map { sprintf( "%04d", $_ ) } split( /\./, $self->id )
36             )
37             : $self->id;
38
39         return $id;
40     }
41 }
42
43 isa_ok( my $foo = Foo->new(), 'Foo', 'new' );
44
45 is( $foo->id, '1.10.100', 'id' );
46
47 is( $foo->to_string, '1.10.100', 'to_string' );
48
49 is(
50     $foo->to_string( padded => 1 ), '0001.0010.0100',
51     'to_string( padded => 1 )'
52 );
53
54 done_testing();