19
19
package path
20
20
21
21
import (
22
+ "fmt"
22
23
"reflect"
23
24
"runtime"
24
25
"testing"
25
26
)
26
27
28
+ func testEachOS (t * testing.T , list []OS , fn func (t * testing.T , os OS )) {
29
+ for _ , os := range list {
30
+ t .Run (fmt .Sprintf ("OS=%s" , os ), func (t * testing.T ) {
31
+ fn (t , os )
32
+ })
33
+ }
34
+ }
35
+
27
36
type PathTest struct {
28
37
path , result string
29
38
}
@@ -101,8 +110,8 @@ var wincleantests = []PathTest{
101
110
}
102
111
103
112
func TestClean (t * testing.T ) {
104
- tests := cleantests
105
- for _ , os := range [] OS { Unix , Windows , Plan9 } {
113
+ testEachOS ( t , [] OS { Unix , Windows , Plan9 }, func ( t * testing. T , os OS ) {
114
+ tests := append ([] PathTest {}, cleantests ... ) // TODO: replace with slices.Clone
106
115
if os == Windows {
107
116
for i := range tests {
108
117
tests [i ].result = FromSlash (tests [i ].result , os )
@@ -132,7 +141,7 @@ func TestClean(t *testing.T) {
132
141
t .Errorf ("Clean(%q): %v allocs, want zero" , test .result , allocs )
133
142
}
134
143
}
135
- }
144
+ })
136
145
}
137
146
138
147
func TestFromAndToSlash (t * testing.T ) {
@@ -185,7 +194,7 @@ var winsplitlisttests = []SplitListTest{
185
194
}
186
195
187
196
func TestSplitList (t * testing.T ) {
188
- for _ , os := range []OS {Unix , Windows , Plan9 } {
197
+ testEachOS ( t , []OS {Unix , Windows , Plan9 }, func ( t * testing. T , os OS ) {
189
198
sep := getOS (os ).ListSeparator
190
199
191
200
tests := []SplitListTest {
@@ -201,7 +210,7 @@ func TestSplitList(t *testing.T) {
201
210
t .Errorf ("SplitList(%#q, %q) = %#q, want %#q" , test .list , os , l , test .result )
202
211
}
203
212
}
204
- }
213
+ })
205
214
}
206
215
207
216
type SplitTest struct {
@@ -230,21 +239,20 @@ var winsplittests = []SplitTest{
230
239
}
231
240
232
241
func TestSplit (t * testing.T ) {
233
- for _ , os := range []OS {Windows , Unix } {
234
- var splittests []SplitTest
235
- splittests = unixsplittests
242
+ testEachOS (t , []OS {Unix , Windows }, func (t * testing.T , os OS ) {
243
+ tests := unixsplittests
236
244
if os == Windows {
237
- splittests = append (splittests , winsplittests ... )
245
+ tests = append (tests , winsplittests ... )
238
246
}
239
- for _ , test := range splittests {
247
+ for _ , test := range tests {
240
248
pair := Split (test .path , os )
241
249
d , f := pair [0 ], pair [1 ]
242
250
if d != test .dir || f != test .file {
243
251
t .Errorf ("Split(%q, %q) = %q, %q, want %q, %q" ,
244
252
test .path , os , d , f , test .dir , test .file )
245
253
}
246
254
}
247
- }
255
+ })
248
256
}
249
257
250
258
type JoinTest struct {
@@ -308,17 +316,18 @@ var winjointests = []JoinTest{
308
316
}
309
317
310
318
func TestJoin (t * testing.T ) {
311
- for _ , os := range []OS {Unix , Windows } {
319
+ testEachOS (t , []OS {Unix , Windows }, func (t * testing.T , os OS ) {
320
+ tests := jointests
312
321
if os == Windows {
313
- jointests = append (jointests , winjointests ... )
322
+ tests = append (tests , winjointests ... )
314
323
}
315
- for _ , test := range jointests {
324
+ for _ , test := range tests {
316
325
expected := FromSlash (test .path , os )
317
326
if p := Join (test .elem , os ); p != expected {
318
327
t .Errorf ("join(%q, %q) = %q, want %q" , test .elem , os , p , expected )
319
328
}
320
329
}
321
- }
330
+ })
322
331
}
323
332
324
333
type ExtTest struct {
@@ -334,13 +343,13 @@ var exttests = []ExtTest{
334
343
}
335
344
336
345
func TestExt (t * testing.T ) {
337
- for _ , os := range []OS {Unix , Windows } {
346
+ testEachOS ( t , []OS {Unix , Windows }, func ( t * testing. T , os OS ) {
338
347
for _ , test := range exttests {
339
348
if x := Ext (test .path , os ); x != test .ext {
340
349
t .Errorf ("Ext(%q, %q) = %q, want %q" , test .path , os , x , test .ext )
341
350
}
342
351
}
343
- }
352
+ })
344
353
}
345
354
346
355
var basetests = []PathTest {
@@ -370,7 +379,7 @@ var winbasetests = []PathTest{
370
379
371
380
func TestBase (t * testing.T ) {
372
381
tests := basetests
373
- for _ , os := range []OS {Unix , Windows } {
382
+ testEachOS ( t , []OS {Unix , Windows }, func ( t * testing. T , os OS ) {
374
383
if os == Windows {
375
384
// make unix tests work on windows
376
385
for i := range tests {
@@ -384,7 +393,7 @@ func TestBase(t *testing.T) {
384
393
t .Errorf ("Base(%q, %q) = %q, want %q" , test .path , os , s , test .result )
385
394
}
386
395
}
387
- }
396
+ })
388
397
}
389
398
390
399
var dirtests = []PathTest {
@@ -415,7 +424,7 @@ var windirtests = []PathTest{
415
424
}
416
425
417
426
func TestDir (t * testing.T ) {
418
- for _ , os := range []OS {Unix , Windows } {
427
+ testEachOS ( t , []OS {Unix , Windows }, func ( t * testing. T , os OS ) {
419
428
tests := dirtests
420
429
if os == Windows {
421
430
// make unix tests work on windows
@@ -430,7 +439,7 @@ func TestDir(t *testing.T) {
430
439
t .Errorf ("Dir(%q, %q) = %q, want %q" , test .path , os , s , test .result )
431
440
}
432
441
}
433
- }
442
+ })
434
443
}
435
444
436
445
type IsAbsTest struct {
@@ -465,7 +474,7 @@ var winisabstests = []IsAbsTest{
465
474
}
466
475
467
476
func TestIsAbs (t * testing.T ) {
468
- for _ , os := range []OS {Unix , Windows } {
477
+ testEachOS ( t , []OS {Unix , Windows }, func ( t * testing. T , os OS ) {
469
478
var tests []IsAbsTest
470
479
if os == Windows {
471
480
tests = append (tests , winisabstests ... )
@@ -491,7 +500,7 @@ func TestIsAbs(t *testing.T) {
491
500
t .Errorf ("IsAbs(%q, %q) = %v, want %v" , test .path , os , r , test .isAbs )
492
501
}
493
502
}
494
- }
503
+ })
495
504
}
496
505
497
506
type RelTests struct {
@@ -552,7 +561,7 @@ var winreltests = []RelTests{
552
561
}
553
562
554
563
func TestRel (t * testing.T ) {
555
- for _ , os := range []OS {Unix , Windows } {
564
+ testEachOS ( t , []OS {Unix , Windows }, func ( t * testing. T , os OS ) {
556
565
tests := append ([]RelTests {}, reltests ... )
557
566
if os == Windows {
558
567
for i := range tests {
@@ -575,7 +584,7 @@ func TestRel(t *testing.T) {
575
584
t .Errorf ("Rel(%q, %q, %q)=%q, want %q" , test .root , test .path , os , got , test .want )
576
585
}
577
586
}
578
- }
587
+ })
579
588
}
580
589
581
590
type VolumeNameTest struct {
@@ -609,14 +618,10 @@ var volumenametests = []VolumeNameTest{
609
618
}
610
619
611
620
func TestVolumeName (t * testing.T ) {
612
- for _ , os := range []OS {Unix , Windows } {
613
- if os != Windows {
614
- return
615
- }
616
- for _ , v := range volumenametests {
617
- if vol := VolumeName (v .path , os ); vol != v .vol {
618
- t .Errorf ("VolumeName(%q, %q)=%q, want %q" , v .path , os , vol , v .vol )
619
- }
621
+ os := Windows
622
+ for _ , v := range volumenametests {
623
+ if vol := VolumeName (v .path , os ); vol != v .vol {
624
+ t .Errorf ("VolumeName(%q, %q)=%q, want %q" , v .path , os , vol , v .vol )
620
625
}
621
626
}
622
627
}
0 commit comments