lib/FP/Path/t.pm

filter: Statementstmt 100% 251 / 251 Branchbran 61.1% 11 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 113 / 113 Podpod 0.0% 0 / 6 totaltotal 96.6% 375 / 388 CC 9 SCAR 1.4
CC
9
Cov
97%
path_split_first_segment
3.0
t_equal
2.0
t_str_clean
2.0
CRAP 1.2
« lib/PXML.pm ↑ Summary lib/Chj/IO/PipelessCommand.pm »
1 #
2 # Copyright (c) 2011-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Path::t
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 run by test suite
19  
20 =head1 NOTE
21  
22 This is alpha software! Read the status section in the package README
23 or on the L<website|http://functional-perl.org/>.
24  
25 =cut
26  
27 package FP::Path::t;
28  
29 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use FP::Path "path";
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use FP::Equal;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 path("a/b/C")->add(path("d/e"), 0)->string
40 }
41 1 'a/b/C/d/e';
42 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
43 1 path("a/b/C")->add(path("../d/e"), 0)->string
44 }
45 1 'a/b/C/../d/e';
46 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 1 path("a/b/C")->add(path("../d/e"), 1)->string
48 }
49 1 'a/b/d/e';
50 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 1 path("a/b/C")->add(path("/d/e"), 1)->string
52 }
53 1 '/d/e';
54  
55 1 my $p = path("a/../b/C")->add(path("../d/../e"), 0);
56 1 ▶TEST { $p->string } 'a/../b/C/../d/../e';
Subroutine __ANON__: called CC=1 SCAR=0.0
57 1 ▶TEST { $p->xclean->string } 'b/e';
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶TEST { equal($p->xclean, $p) } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 ▶TEST { equal($p->xclean, $p->xclean) } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
60  
61 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
62 1 path("a/.././b/C")->add(path("../d/./../e"), 0)->string
63 }
64 1 'a/../b/C/../d/../e'; # 'add' does an implicit clean_dot; should it be
65 # implemented differently?
66  
67 1 ▶TEST { (path "hello//world/you")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 "hello/world/you";
69 1 ▶TEST { (path "/hello//world/you")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
70 1 "/hello/world/you";
71 1 ▶TEST { (path "/hello//world/you/")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 "/hello/world/you/";
73 1 ▶TEST { (path "/")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
74 1 "/";
75 1 ▶TEST { (path ".")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
76 1 ".";
77 1 ▶TEST { (path "./")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
78 1 "./";
79 1 ▶TEST { (path "./")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
80 1 "./";
81 1 ▶TEST { (path "./..")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
82 1 "./..";
83 1 ▶TEST { (path "./..")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
84 1 "..";
85  
86 1 ▶TEST { (path "./foo/../bar/.//baz/.")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 "foo/../bar/baz/";
88 1 ▶TEST { (path "")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
89  
90 # XX should this be an error?
91 1 '.';
92  
93 1 ▶TEST { (path ".")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
94 1 ".";
95 1 ▶TEST { (path ".")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
96 1 './';
97  
98 1 ▶TEST { (path "/")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
99 1 "/";
100 1 ▶TEST { (path "/")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1 "/";
102 1 ▶TEST { (path "/.")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
103 1 "/";
104 1 ▶TEST { (path "/./")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
105 1 "/";
106 1 ▶TEST { (path "/./")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
107 1 "/./";
108 1 ▶TEST { (path "/.")->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
109 1 "/.";
110  
111 1 ▶TEST { (path "/.")->contains_dotdot }
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 '';
113 1 ▶TEST { (path "foo/bar/../baz")->contains_dotdot }
Subroutine __ANON__: called CC=1 SCAR=0.0
114 1 "1";
115 1 ▶TEST { (path "../baz")->contains_dotdot }
Subroutine __ANON__: called CC=1 SCAR=0.0
116 1 "1";
117 1 ▶TEST { (path "baz/..")->contains_dotdot }
Subroutine __ANON__: called CC=1 SCAR=0.0
118 1 "1";
119 1 ▶TEST { (path "baz/..")->clean_dot->contains_dotdot }
Subroutine __ANON__: called CC=1 SCAR=0.0
120 1 "1";
121  
122 1 ▶TEST_EXCEPTION { path(".")->clean_dot->dirname }
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 q{can't take dirname of empty path};
124 1 ▶TEST { path("foo")->clean_dot->dirname->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
125 1 './';
126 1 ▶TEST { path("foo/bar")->clean_dot->dirname->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
127 1 'foo/';
128 1 ▶TEST { path("/bar")->clean_dot->dirname->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
129 1 '/';
130 1 ▶TEST_EXCEPTION { path("")->dirname }
Subroutine __ANON__: called CC=1 SCAR=0.0
131 1 q{can't take dirname of empty path};
132  
133 1 ▶TEST { path(".")->clean_dot->has_endslash }
Subroutine __ANON__: called CC=1 SCAR=0.0
134 1 1;
135 1 ▶TEST { path(".")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
136 1 './';
137  
138 #ok
139 1 ▶TEST { path("")->clean_dot->has_endslash }
Subroutine __ANON__: called CC=1 SCAR=0.0
140 1 0;
141 1 ▶TEST { path("")->clean_dot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1 '.';
143  
144 #h
145  
146 1 ▶TEST { path("/foo")->to_relative->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
147 1 'foo';
148 1 ▶TEST { path("/")->to_relative->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
149 1 './';
150 1 ▶TEST_EXCEPTION { path("")->to_relative->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
151 1 q{is already relative};
152 1 ▶TEST { path("/foo/")->to_relative->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
153 1 'foo/';
154  
155 1 ▶use FP::DumperEqual;
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 1 dumperequal(path("/"), path("//"), path("///"))
159 }
160 1 1;
161  
162 # invalid segments:
163  
164 1 ▶use FP::List;
Subroutine BEGIN: called CC=1 SCAR=0.0
165  
166 1 ▶TEST_EXCEPTION { path("/foo")->add_segment("") }
Subroutine __ANON__: called CC=1 SCAR=0.0
167 1 "segments cannot be the empty string";
168 1 ▶TEST_EXCEPTION { path("/foo")->add_segment("bar/") }
Subroutine __ANON__: called CC=1 SCAR=0.0
169 1 'segment contains slash: \'bar/\'';
170 1 ▶TEST_EXCEPTION { FP::Path->new(list("/foo"), 1, 1)->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
171 1 ▶($FP::Path::use_costly_typing
Branch: 1 branch
BranchTrueFalseTotal
$FP::Path::use_costly_typing ? : 0 1 1
172 ? 'unacceptable value for field \'rsegments\': list(\'/foo\')'
173 : 'segment contains slash: \'/foo\'');
174  
175 # equal:
176  
177 7 ▶sub t_equal {
Subroutine t_equal: called CC=2 SCAR=7.1
Pod undocumented
178 7 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 7 0 7
179 7 my ($a, $b) = @_;
180 7 equal(path($a), path($b))
181 }
182  
183 1 ▶TEST { t_equal "/foo", "/foo" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
184 1 ▶TEST { t_equal "/foo", "foo" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
185 1 ▶TEST { t_equal "/foo", "/foo/" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
186 1 ▶TEST { t_equal "/foo", "/bar" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
187 1 ▶TEST { t_equal "/", "/" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
188 1 ▶TEST { t_equal "/foo/..", "/" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
189 1 ▶TEST { t_equal "/foo", "/foo/bar" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
190  
191 # test booleanization (!!) in equal method
192 1 ▶TEST {
Subroutine __ANON__: called CC=1 SCAR=0.0
193 1 my $p = path("/foo");
194 1 equal $p, $p->has_endslash_set(0)
195 }
196 1 1;
197  
198 10 ▶sub t_str_clean {
Subroutine t_str_clean: called CC=2 SCAR=7.1
Pod undocumented
199 10 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 10 0 10
200 10 my ($a) = @_;
201 10 path($a)->clean_dot->xclean_dotdot;
202 }
203  
204 5 ▶sub t_equals_clean {
Subroutine t_equals_clean: called CC=2 SCAR=7.1
Pod undocumented
205 5 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 5 0 5
206 5 my ($a, $b) = @_;
207 5 equal(t_str_clean($a), t_str_clean($b));
208 }
209  
210 1 ▶TEST { t_equals_clean "/foo", "/foo" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
211 1 ▶TEST { t_equals_clean "/foo", "foo" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
212 1 ▶TEST { t_equals_clean "/foo/bar/..", "/foo" } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
213 1 ▶TEST { t_equals_clean "/foo/bar/..", "/foo/" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
214 1 ▶TEST { t_equals_clean "/foo/..", "/" } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
215  
216 # split and resplit:
217  
218 11 ▶sub path_split_first_segment {
Subroutine path_split_first_segment: called CC=3 SCAR=11.0
Pod undocumented
219 11 my ($str, $clean) = @_;
220 11 my $p = path $str;
221 11 ▶ if (my @v = ($clean ? $p->xclean : $p)->perhaps_split_first_segment) {
Branch: 2 branches
BranchTrueFalseTotal
if (my(@v) = ($clean ? $p->xclean : $p)->perhaps_split_first_segment) { } 9 2 11
$clean ? : 1 10 11
222 9 [map { $_->string } @v]
223 } else {
224 2 "unsplittable"
225 }
226 }
227  
228 1 ▶TEST { path_split_first_segment "/foo/bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
229 1 ["/foo/", "bar"];
230 1 ▶TEST { path_split_first_segment "/foo/bar/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
231 1 ["/foo/", "bar/"];
232 1 ▶TEST { path_split_first_segment "/foo/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
233 1 ["/foo/", "./"];
234 1 ▶TEST { path_split_first_segment "/foo" }
Subroutine __ANON__: called CC=1 SCAR=0.0
235 1 ["/foo", "."];
236 1 ▶TEST { path_split_first_segment "/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
237 1 "unsplittable";
238 1 ▶TEST { path_split_first_segment "./foo/bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
239 1 ["./", "foo/bar"]; # ok? what you get for not cleaning.
240 1 ▶TEST { path_split_first_segment "foo/bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
241 1 ["foo/", "bar"];
242 1 ▶TEST { path_split_first_segment "foo/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
243 1 ["foo/", "./"];
244 1 ▶TEST { path_split_first_segment "foo" }
Subroutine __ANON__: called CC=1 SCAR=0.0
245 1 ["foo", "."];
246  
247 # (BTW isn't it stupid that ./ and . do both exist? Ok, some kinds of
248 # paths might treat "." as non-directory filename? But then it would
249 # fail anyway. XX)
250 1 ▶TEST { path_split_first_segment "." }
Subroutine __ANON__: called CC=1 SCAR=0.0
251 1 [".", "."]; # odd of course, but that's what you get for not cleaning?
252 1 ▶TEST { path_split_first_segment ".", 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
253 1 "unsplittable";
254  
255 1 ▶use FP::List qw(unfold);
Subroutine BEGIN: called CC=1 SCAR=0.0
256 1 ▶use FP::Array qw(array_is_null array_map);
Subroutine BEGIN: called CC=1 SCAR=0.0
257 1 ▶use FP::Ops qw(the_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
258  
259 6 ▶sub tupleify {
Subroutine tupleify: called CC=2 SCAR=7.1
Pod undocumented
260 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
261 6 my ($f) = @_;
262 12 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
263 12 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 12 0 12
264 12 [&$f(@{ $_[0] })]
265 }
266 6 }
267  
268 6 ▶sub all_splits {
Subroutine all_splits: called CC=2 SCAR=7.0
Pod undocumented
269 6 my ($str, $clean) = @_;
270 6 my $p = path $str;
271 6 ▶ my $p0 = ($clean ? $p->xclean : $p);
Branch: 1 branch
BranchTrueFalseTotal
$clean ? : 0 6 6
272  
273 unfold( # ending predicate
274 *array_is_null,
275  
276 # mapping function
277 12 ▶ sub { array_map the_method("string"), $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
278  
279 # stepping function
280 6 tupleify(the_method("perhaps_resplit_next_segment")),
281  
282 # seed value
283 [$p0->perhaps_split_first_segment]
284 )->array
285 }
286  
287 1 ▶TEST { all_splits "/foo/bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
288 1 [['/foo/', 'bar'], ['/foo/bar', '.']];
289 1 ▶TEST { all_splits "/foo/./bar" }
Subroutine __ANON__: called CC=1 SCAR=0.0
290 1 [['/foo/', './bar'], ['/foo/./', 'bar'], ['/foo/./bar', '.']];
291 1 ▶TEST { all_splits "/foo" }
Subroutine __ANON__: called CC=1 SCAR=0.0
292 1 [['/foo', '.']];
293  
294 # Note that the end cases above have a left part that does *not* have
295 # an end slash (it inherited the setting from the right part). Is this
296 # ok? The right side in this case is rather fake; and XX re-appending
297 # might fail in some algorithms! But what else would be better?
298  
299 # It's unambiguous when the right hand argument has_end_slash==1:
300  
301 1 ▶TEST { all_splits "/foo/bar/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
302 1 [['/foo/', 'bar/'], ['/foo/bar/', './']];
303 1 ▶TEST { all_splits "/foo/./bar/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
304 1 [['/foo/', './bar/'], ['/foo/./', 'bar/'], ['/foo/./bar/', './']];
305 1 ▶TEST { all_splits "/foo/" }
Subroutine __ANON__: called CC=1 SCAR=0.0
306 1 [['/foo/', './']];
307  
308 # XX rules-based testing rules?:
309  
310 # - if a path is absolute, the cleaned path is always absolute, too?
311  
312 # previously: why?
313 # TEST { path("/..")->xclean_dotdot->string }
314 # '/';
315 # TEST { path("/../..")->xclean_dotdot->string }
316 # '..';
317 # TEST_EXCEPTION { path("..")->xclean_dotdot->string }
318 # 'can\'t take \'..\' of root directory';
319 # TEST_EXCEPTION { path("../..")->xclean_dotdot->string }
320 # 'can\'t take \'..\' of root directory';
321  
322 1 ▶TEST_EXCEPTION { path("..")->xclean_dotdot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
323 1 'can\'t take \'..\' of root directory'; # ".."; ?
324 1 ▶TEST_EXCEPTION { path("../..")->xclean_dotdot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
325 1 'can\'t take \'..\' of root directory'; # "../.."; ?
326 1 ▶TEST_EXCEPTION { path("/..")->xclean_dotdot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
327 1 'can\'t take \'..\' of root directory';
328 1 ▶TEST_EXCEPTION { path("/../..")->xclean_dotdot->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
329 1 'can\'t take \'..\' of root directory';
330  
331 1 ▶TEST_EXCEPTION { path("../foo")->xclean->string }
Subroutine __ANON__: called CC=1 SCAR=0.0
332 1 "can't take '..' of root directory";
333  
334 # should .. be allowed at the beginning? But then all of the above are
335 # ok, too, just translate into a number of ../ at the beginning. ->
336 # XX See scm libs re chroot / leaving root.
337  
338 # - does cleaning a path that ends in /. leve it with has_endslash
339 # set?
340  
341 1 ▶TEST { path("foo/.")->has_endslash } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
342 1 ▶TEST { path("foo/.")->xclean->has_endslash } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
343 1 ▶TEST { path("/.")->has_endslash } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
344 1 ▶TEST { path("/.")->xclean->has_endslash } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
345  
346 1 ▶TEST { path("foo/..")->has_endslash } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
347 1 ▶TEST { path("foo/..")->xclean->has_endslash } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
348  
349 1 ▶TEST { path("foo/bar/..")->has_endslash } '';
Subroutine __ANON__: called CC=1 SCAR=0.0
350 1 ▶TEST { path("foo/bar/..")->xclean->has_endslash } 1;
Subroutine __ANON__: called CC=1 SCAR=0.0
351  
352 1 1
« lib/PXML.pm ↑ Summary lib/Chj/IO/PipelessCommand.pm »