1
1
// Copyright (c) The datatest-stable Contributors
2
2
// SPDX-License-Identifier: MIT OR Apache-2.0
3
3
4
+ use std:: path:: Path ;
5
+
4
6
use crate :: { utils, Result } ;
5
7
use camino:: { Utf8Path , Utf8PathBuf } ;
6
8
use libtest_mimic:: { Arguments , Trial } ;
@@ -17,22 +19,22 @@ pub fn runner(requirements: &[Requirements]) {
17
19
18
20
#[ doc( hidden) ]
19
21
pub struct Requirements {
20
- test : fn ( & Utf8Path ) -> Result < ( ) > ,
22
+ test : TestFn ,
21
23
test_name : String ,
22
24
root : Utf8PathBuf ,
23
25
pattern : String ,
24
26
}
25
27
26
28
impl Requirements {
27
29
#[ doc( hidden) ]
28
- pub fn new (
29
- test : fn ( & Utf8Path ) -> Result < ( ) > ,
30
+ pub fn new < P : TestFnPath + ? Sized > (
31
+ test : fn ( & P ) -> Result < ( ) > ,
30
32
test_name : String ,
31
33
root : Utf8PathBuf ,
32
34
pattern : String ,
33
35
) -> Self {
34
36
Self {
35
- test,
37
+ test : P :: convert ( test ) ,
36
38
test_name,
37
39
root,
38
40
pattern,
@@ -52,7 +54,9 @@ impl Requirements {
52
54
let testfn = self . test ;
53
55
let name = utils:: derive_test_name ( & self . root , & path, & self . test_name ) ;
54
56
Some ( Trial :: test ( name, move || {
55
- ( testfn) ( & path) . map_err ( |err| format ! ( "{:?}" , err) . into ( ) )
57
+ testfn
58
+ . call ( & path)
59
+ . map_err ( |err| format ! ( "{:?}" , err) . into ( ) )
56
60
} ) )
57
61
} else {
58
62
None
@@ -71,3 +75,44 @@ impl Requirements {
71
75
tests
72
76
}
73
77
}
78
+
79
+ #[ derive( Clone , Copy ) ]
80
+ #[ doc( hidden) ]
81
+ pub enum TestFn {
82
+ Path ( fn ( & Path ) -> Result < ( ) > ) ,
83
+ Utf8Path ( fn ( & Utf8Path ) -> Result < ( ) > ) ,
84
+ }
85
+
86
+ mod private {
87
+ pub trait Sealed { }
88
+ }
89
+
90
+ #[ doc( hidden) ]
91
+ pub trait TestFnPath : private:: Sealed {
92
+ fn convert ( f : fn ( & Self ) -> Result < ( ) > ) -> TestFn ;
93
+ }
94
+
95
+ impl private:: Sealed for Path { }
96
+
97
+ impl TestFnPath for Path {
98
+ fn convert ( f : fn ( & Self ) -> Result < ( ) > ) -> TestFn {
99
+ TestFn :: Path ( f)
100
+ }
101
+ }
102
+
103
+ impl private:: Sealed for Utf8Path { }
104
+
105
+ impl TestFnPath for Utf8Path {
106
+ fn convert ( f : fn ( & Self ) -> Result < ( ) > ) -> TestFn {
107
+ TestFn :: Utf8Path ( f)
108
+ }
109
+ }
110
+
111
+ impl TestFn {
112
+ fn call ( & self , path : & Utf8Path ) -> Result < ( ) > {
113
+ match self {
114
+ TestFn :: Path ( f) => f ( path. as_ref ( ) ) ,
115
+ TestFn :: Utf8Path ( f) => f ( path) ,
116
+ }
117
+ }
118
+ }
0 commit comments