summaryrefslogtreecommitdiff
path: root/cpan/IO-Compress/t/globmapper.t
blob: 10a4d8871629f8e4f2f8698837be42cbe1b291c7 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
BEGIN {
    if ($ENV{PERL_CORE}) {
	chdir 't' if -d 't';
	@INC = ("../lib", "lib/compress");
    }
}

use lib qw(t t/compress);
use strict ;
use warnings ;

use Test::More ;
use CompTestUtils;


BEGIN 
{ 
    plan(skip_all => "File::GlobMapper needs Perl 5.005 or better - you have
Perl $]" )
        if $] < 5.005 ;

    # use Test::NoWarnings, if available
    my $extra = 0 ;
    $extra = 1
        if eval { require Test::NoWarnings ;  import Test::NoWarnings; 1 };

    plan tests => 68 + $extra ;

    use_ok('File::GlobMapper') ; 
}

{
    title "Error Cases" ;

    my $gm;

    for my $delim ( qw/ ( ) { } [ ] / )
    {
        $gm = new File::GlobMapper("${delim}abc", '*.X');
        ok ! $gm, "  new failed" ;
        is $File::GlobMapper::Error, "Unmatched $delim in input fileglob", 
            "  catch unmatched $delim";
    }

    for my $delim ( qw/ ( ) [ ] / )
    {
        $gm = new File::GlobMapper("{${delim}abc}", '*.X');
        ok ! $gm, "  new failed" ;
        is $File::GlobMapper::Error, "Unmatched $delim in input fileglob", 
            "  catch unmatched $delim inside {}";
    }

    
}

{
    title "input glob matches zero files";

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;

    my $gm = new File::GlobMapper("$tmpDir/Z*", '*.X');
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 0, "  returned 0 maps";
    is_deeply $map, [], " zero maps" ;

    my $hash = $gm->getHash() ;
    is_deeply $hash, {}, "  zero maps" ;
}

{
    title 'test wildcard mapping of * in destination';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("$tmpDir/ab*.tmp", "*X");
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 3, "  returned 3 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp abc1.tmpX)],
          [map { "$tmpDir/$_" } qw(abc2.tmp abc2.tmpX)],
          [map { "$tmpDir/$_" } qw(abc3.tmp abc3.tmpX)],
        ], "  got mapping";

    my $hash = $gm->getHash() ;
    is_deeply $hash,
        { map { "$tmpDir/$_" } qw(abc1.tmp abc1.tmpX
                                  abc2.tmp abc2.tmpX
                                  abc3.tmp abc3.tmpX),
        }, "  got mapping";
}

{
    title 'no wildcards in input or destination';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("$tmpDir/abc2.tmp", "$tmpDir/abc2.tmp");
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 1, "  returned 1 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_.tmp" } qw(abc2 abc2)],
        ], "  got mapping";

    my $hash = $gm->getHash() ;
    is_deeply $hash,
        { map { "$tmpDir/$_.tmp" } qw(abc2 abc2),
        }, "  got mapping";
}

{
    title 'test wildcard mapping of {} in destination';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("$tmpDir/abc{1,3}.tmp", "*.X");
    #diag "Input pattern is $gm->{InputPattern}";
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 2, "  returned 2 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp abc1.tmp.X)],
          [map { "$tmpDir/$_" } qw(abc3.tmp abc3.tmp.X)],
        ], "  got mapping";

    $gm = new File::GlobMapper("$tmpDir/abc{1,3}.tmp", "$tmpDir/X.#1.X")
        or diag $File::GlobMapper::Error ;
    #diag "Input pattern is $gm->{InputPattern}";
    ok $gm, "  created GlobMapper object" ;

    $map = $gm->getFileMap() ;
    is @{ $map }, 2, "  returned 2 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp X.1.X)],
          [map { "$tmpDir/$_" } qw(abc3.tmp X.3.X)],
        ], "  got mapping";

}


{
    title 'test wildcard mapping of multiple * to #';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("$tmpDir/*b(*).tmp", "$tmpDir/X-#2-#1-X");
    ok $gm, "  created GlobMapper object" 
        or diag $File::GlobMapper::Error ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 3, "  returned 3 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp X-c1-a-X)],
          [map { "$tmpDir/$_" } qw(abc2.tmp X-c2-a-X)],
          [map { "$tmpDir/$_" } qw(abc3.tmp X-c3-a-X)],
        ], "  got mapping";
}

{
    title 'test wildcard mapping of multiple ? to #';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("$tmpDir/?b(*).tmp", "$tmpDir/X-#2-#1-X");
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 3, "  returned 3 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp X-c1-a-X)],
          [map { "$tmpDir/$_" } qw(abc2.tmp X-c2-a-X)],
          [map { "$tmpDir/$_" } qw(abc3.tmp X-c3-a-X)],
        ], "  got mapping";
}

{
    title 'test wildcard mapping of multiple ?,* and [] to #';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $gm = new File::GlobMapper("./$tmpDir/?b[a-z]*.tmp", "./$tmpDir/X-#3-#2-#1-X");
    ok $gm, "  created GlobMapper object" ;

    #diag "Input pattern is $gm->{InputPattern}";
    my $map = $gm->getFileMap() ;
    is @{ $map }, 3, "  returned 3 maps";
    is_deeply $map,
        [ [map { "./$tmpDir/$_" } qw(abc1.tmp X-1-c-a-X)],
          [map { "./$tmpDir/$_" } qw(abc2.tmp X-2-c-a-X)],
          [map { "./$tmpDir/$_" } qw(abc3.tmp X-3-c-a-X)],
        ], "  got mapping";
}

{
    title 'input glob matches a file multiple times';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch "$tmpDir/abc.tmp";

    my $gm = new File::GlobMapper("$tmpDir/{a*,*c}.tmp", '*.X');
    ok $gm, "  created GlobMapper object" ;

    my $map = $gm->getFileMap() ;
    is @{ $map }, 1, "  returned 1 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc.tmp abc.tmp.X)], ], "  got mapping";

    my $hash = $gm->getHash() ;
    is_deeply $hash,
        { map { "$tmpDir/$_" } qw(abc.tmp abc.tmp.X) }, "  got mapping";

}

{
    title 'multiple input files map to one output file';

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc def) ;

    my $gm = new File::GlobMapper("$tmpDir/*.tmp", "$tmpDir/fred");
    ok ! $gm, "  did not create GlobMapper object" ;

    is $File::GlobMapper::Error, 'multiple input files map to one output file', "  Error is expected" ;

    #my $map = $gm->getFileMap() ;
    #is @{ $map }, 1, "  returned 1 maps";
    #is_deeply $map,
    #[ [map { "$tmpDir/$_" } qw(abc1 abc.X)], ], "  got mapping";
}

{
    title "globmap" ;

    my $tmpDir = 'td';
    my $lex = new LexDir $tmpDir;
    mkdir $tmpDir, 0777 ;

    touch map { "$tmpDir/$_.tmp" } qw( abc1 abc2 abc3 ) ;

    my $map = File::GlobMapper::globmap("$tmpDir/*b*.tmp", "$tmpDir/X-#2-#1-X");
    ok $map, "  got map" 
        or diag $File::GlobMapper::Error ;

    is @{ $map }, 3, "  returned 3 maps";
    is_deeply $map,
        [ [map { "$tmpDir/$_" } qw(abc1.tmp X-c1-a-X)],
          [map { "$tmpDir/$_" } qw(abc2.tmp X-c2-a-X)],
          [map { "$tmpDir/$_" } qw(abc3.tmp X-c3-a-X)],
        ], "  got mapping";
}

# TODO
# test each of the wildcard metacharacters can be mapped to the output filename
#
#   ~ [] {} . *

# input & output glob with no wildcards is ok
# input with no wild or output with no wild is bad
# input wild has concatenated *'s
# empty string for either both from & to
# escaped chars within [] and {}, including the chars []{}
# escaped , within {}
# missing ] and missing }
# {} and {,} are special cases
# {ab*,de*}
# {abc,{},{de,f}} => abc {} de f