File: basic.t

package info (click to toggle)
libmoosex-types-path-tiny-perl 0.012-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 276; makefile: 2
file content (173 lines) | stat: -rw-r--r-- 4,105 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use strict;
use warnings;
use Test::More 0.96;
use File::Temp 0.18;
use File::pushd qw/tempd/;
use Path::Tiny;

{

    package Foo;
    use Moose;
    use MooseX::Types::Path::Tiny qw/Path File Dir/;

    has a_path => ( is => 'ro', isa => Path, coerce => 1 );
    has a_file => ( is => 'ro', isa => File, coerce => 1 );
    has a_dir  => ( is => 'ro', isa => Dir,  coerce => 1 );
}

{

    package AbsFoo;
    use Moose;
    use MooseX::Types::Path::Tiny qw/AbsPath AbsFile AbsDir/;

    has a_path => ( is => 'ro', isa => AbsPath, coerce => 1 );
    has a_file => ( is => 'ro', isa => AbsFile, coerce => 1 );
    has a_dir  => ( is => 'ro', isa => AbsDir,  coerce => 1 );
}

my $tf = File::Temp->new;
my $td = File::Temp->newdir;

my @cases = (
    # Path
    {
        label    => "coerce string to Path",
        absolute => 0,
        attr     => "a_path",
        input    => "./foo",
    },
    {
        label    => "coerce object to Path",
        absolute => 0,
        attr     => "a_path",
        input    => $tf,
    },
    {
        label    => "coerce array ref to Path",
        absolute => 0,
        attr     => "a_path",
        input    => [qw/foo bar/],
    },
    # AbsPath
    {
        label    => "coerce string to AbsPath",
        absolute => 1,
        attr     => "a_path",
        input    => "./foo",
    },
    {
        label    => "coerce Path to AbsPath",
        absolute => 1,
        attr     => "a_path",
        input    => path($tf),
    },
    {
        label    => "coerce object to AbsPath",
        absolute => 1,
        attr     => "a_path",
        input    => $tf,
    },
    {
        label    => "coerce array ref to AbsPath",
        absolute => 1,
        attr     => "a_path",
        input    => [qw/foo bar/],
    },
    # File
    {
        label    => "coerce string to File",
        absolute => 0,
        attr     => "a_file",
        input    => "$tf",
    },
    {
        label    => "coerce object to File",
        absolute => 0,
        attr     => "a_file",
        input    => $tf,
    },
    {
        label    => "coerce array ref to File",
        absolute => 0,
        attr     => "a_file",
        input    => [$tf],
    },
    # Dir
    {
        label    => "coerce string to Dir",
        absolute => 0,
        attr     => "a_dir",
        input    => "$td",
    },
    {
        label    => "coerce object to Dir",
        absolute => 0,
        attr     => "a_dir",
        input    => $td,
    },
    {
        label    => "coerce array ref to Dir",
        absolute => 0,
        attr     => "a_dir",
        input    => [$td],
    },
    # AbsFile
    {
        label    => "coerce string to AbsFile",
        absolute => 1,
        attr     => "a_file",
        input    => "$tf",
    },
    {
        label    => "coerce object to AbsFile",
        absolute => 1,
        attr     => "a_file",
        input    => $tf,
    },
    {
        label    => "coerce array ref to AbsFile",
        absolute => 1,
        attr     => "a_file",
        input    => [$tf],
    },
    # AbsDir
    {
        label    => "coerce string to AbsDir",
        absolute => 1,
        attr     => "a_dir",
        input    => "$td",
    },
    {
        label    => "coerce object to AbsDir",
        absolute => 1,
        attr     => "a_dir",
        input    => $td,
    },
    {
        label    => "coerce array ref to AbsDir",
        absolute => 1,
        attr     => "a_dir",
        input    => [$td],
    },
);

for my $c (@cases) {
    subtest $c->{label} => sub {
        my $wd       = tempd;
        my $class    = $c->{absolute} ? "AbsFoo" : "Foo";
        my $attr     = $c->{attr};
        my $input    = $c->{input};
        my $expected = path( ref $input eq 'ARRAY' ? @$input : $input );
        $expected = $expected->absolute if $c->{absolute};

        my $obj = eval { $class->new( $attr => $input ); };
        is( $@, '', "object created without exception" );
        isa_ok( $obj->$attr, "Path::Tiny", $attr );
        is( $obj->$attr, $expected, "$attr set correctly" );
    };
}

done_testing;
# COPYRIGHT