2424#![ warn( clippy:: print_stderr) ]
2525#![ warn( clippy:: print_stdout) ]
2626
27- pub use libtest2_harness:: Harness ;
28- pub use libtest2_harness:: RunError ;
29- pub use libtest2_harness:: RunResult ;
30- pub use libtest2_harness:: TestContext ;
31- pub use libtest2_harness:: TestKind ;
27+ pub use libtest_json:: RunMode ;
3228
33- use libtest2_harness:: Case ;
34- use libtest2_harness:: Source ;
29+ pub struct Harness {
30+ harness : libtest2_harness:: Harness ,
31+ }
32+
33+ impl Harness {
34+ pub fn with_args ( args : impl IntoIterator < Item = impl Into < std:: ffi:: OsString > > ) -> Self {
35+ Self {
36+ harness : libtest2_harness:: Harness :: with_args ( args) ,
37+ }
38+ }
39+
40+ pub fn with_env ( ) -> Self {
41+ Self {
42+ harness : libtest2_harness:: Harness :: with_env ( ) ,
43+ }
44+ }
45+
46+ pub fn case ( mut self , case : Trial ) -> Self {
47+ self . harness . case ( TrialCase { inner : case } ) ;
48+ self
49+ }
50+
51+ pub fn cases ( mut self , cases : impl IntoIterator < Item = Trial > ) -> Self {
52+ self . harness
53+ . cases ( cases. into_iter ( ) . map ( |c| TrialCase { inner : c } ) ) ;
54+ self
55+ }
56+
57+ pub fn main ( self ) -> ! {
58+ self . harness . main ( )
59+ }
60+ }
3561
3662pub struct Trial {
3763 name : String ,
3864 #[ allow( clippy:: type_complexity) ]
39- runner : Box < dyn Fn ( & TestContext ) -> Result < ( ) , RunError > + Send + Sync > ,
65+ runner : Box < dyn Fn ( TestContext < ' _ > ) -> Result < ( ) , RunError > + Send + Sync > ,
4066}
4167
4268impl Trial {
4369 pub fn test (
4470 name : impl Into < String > ,
45- runner : impl Fn ( & TestContext ) -> Result < ( ) , RunError > + Send + Sync + ' static ,
71+ runner : impl Fn ( TestContext < ' _ > ) -> Result < ( ) , RunError > + Send + Sync + ' static ,
4672 ) -> Self {
4773 Self {
4874 name : name. into ( ) ,
@@ -51,22 +77,71 @@ impl Trial {
5177 }
5278}
5379
54- impl Case for Trial {
80+ struct TrialCase {
81+ inner : Trial ,
82+ }
83+
84+ impl libtest2_harness:: Case for TrialCase {
5585 fn name ( & self ) -> & str {
56- & self . name
86+ & self . inner . name
5787 }
58- fn kind ( & self ) -> TestKind {
88+ fn kind ( & self ) -> libtest2_harness :: TestKind {
5989 Default :: default ( )
6090 }
61- fn source ( & self ) -> Option < & Source > {
91+ fn source ( & self ) -> Option < & libtest2_harness :: Source > {
6292 None
6393 }
64- fn exclusive ( & self , _: & TestContext ) -> bool {
94+ fn exclusive ( & self , _: & libtest2_harness :: TestContext ) -> bool {
6595 false
6696 }
6797
68- fn run ( & self , context : & TestContext ) -> Result < ( ) , RunError > {
69- ( self . runner ) ( context)
98+ fn run (
99+ & self ,
100+ context : & libtest2_harness:: TestContext ,
101+ ) -> Result < ( ) , libtest2_harness:: RunError > {
102+ ( self . inner . runner ) ( TestContext { inner : context } ) . map_err ( |e| e. inner )
103+ }
104+ }
105+
106+ pub type RunResult = Result < ( ) , RunError > ;
107+
108+ #[ derive( Debug ) ]
109+ pub struct RunError {
110+ inner : libtest2_harness:: RunError ,
111+ }
112+
113+ impl RunError {
114+ pub fn with_cause ( cause : impl std:: error:: Error + Send + Sync + ' static ) -> Self {
115+ Self {
116+ inner : libtest2_harness:: RunError :: with_cause ( cause) ,
117+ }
118+ }
119+
120+ pub fn fail ( cause : impl std:: fmt:: Display ) -> Self {
121+ Self {
122+ inner : libtest2_harness:: RunError :: fail ( cause) ,
123+ }
124+ }
125+ }
126+
127+ #[ derive( Debug ) ]
128+ pub struct TestContext < ' t > {
129+ inner : & ' t libtest2_harness:: TestContext ,
130+ }
131+
132+ impl < ' t > TestContext < ' t > {
133+ pub fn ignore ( & self ) -> Result < ( ) , RunError > {
134+ self . inner . ignore ( ) . map_err ( |e| RunError { inner : e } )
135+ }
136+
137+ pub fn ignore_for ( & self , reason : impl std:: fmt:: Display ) -> Result < ( ) , RunError > {
138+ self . inner
139+ . ignore_for ( reason)
140+ . map_err ( |e| RunError { inner : e } )
141+ }
142+
143+ pub fn current_mode ( & self ) -> RunMode {
144+ self . inner . current_mode ( )
70145 }
71146}
72147
0 commit comments