sourcetrait_testing 0.0.0-2

Structured testing with setup, teardown, and standardized fixture and temp directories
Documentation
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
use crate::*;

/// Configuraiton for a single unit or integration test.
///
/// It has a parent [TestModule] from which it may inherit configuration.
pub struct Test<'module,'func> {
    pub(crate) module: &'module TestModule,
    pub(crate) namepath: Namepath,
    pub(crate) temp_dir: Option<PathBuf>,
    pub(crate) fixture_dir: Option<PathBuf>,
    pub(crate) teardown_func: Option<Box<dyn FnOnce(&mut Test) + 'func>>,
}

impl std::fmt::Debug for Test<'_, '_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let teardown_fn_dbg = if self.teardown_func.is_some() {
            "Some(fn(...))"
        } else {
            "None"
        };
        
        f.debug_struct("Test")
            .field("namepath", &self.namepath)
            .field("module", &self.module)
            .field("temp_dir", &self.temp_dir)
            .field("fixture_dir", &self.fixture_dir)
            .field("teardown_func", &teardown_fn_dbg)
            .finish()
    }
}

impl<'module,'func> Test<'module,'func> {
    #[inline]
    pub fn as_testable(&self) -> Testable<'_, 'module, 'func> {
        Testable::Test(self)
    }
    
    /// The parent [TestModule] of this test.
    pub fn module(&self) -> &'module TestModule {
        &self.module
    }

    fn teardown(&mut self) {
        if let Some(teardown_fn) = self.teardown_func.take() {
            teardown_fn(self);
        }
    }
}

impl<'module,'func> Testing for Test<'module,'func> {
    fn fixture_dir(&self) -> &Path {
        &self.fixture_dir.as_ref()
            .context("Test `fixture dir` is not configured").unwrap()
    }
    
    #[inline]
    fn kind(&self) -> TestingKind {
        TestingKind::Test
    }
    
    fn namepath(&self) -> &Namepath {
        &self.namepath
    }

    fn temp_dir(&self) -> &Path {
        self.temp_dir.as_ref()
            .context("Test `temp dir` is not configured").unwrap()
    }
    
    fn use_case(&self)-> UseCase {
        self.module.use_case
    }
}

impl<'module,'func> Drop for Test<'module,'func> {
    fn drop(&mut self) {
        self.teardown();
    }
}

/// Constructs a [Test]
pub struct TestBuilder<'module,'func> {
    pub(crate) name: &'static str,
    pub(crate) module: &'module TestModule,
    pub(crate) using_temp_dir: bool,
    pub(crate) inherit_temp_dir: bool,
    pub(crate) using_fixture_dir: bool,
    pub(crate) inherit_fixture_dir: bool,
    pub(crate) setup_func: Option<Box<dyn FnOnce(&mut Test) + 'func>>,
    pub(crate) teardown_func: Option<Box<dyn FnOnce(&mut Test) + 'func>>,
}

impl<'module,'func> TestBuilder<'module,'func> {
    pub fn new(module: &'module TestModule, name: &'static str) -> Self {
        assert!(!name.contains("::") && !name.contains('/') && !name.contains('.'),
            "Test name should be a single non-delimited token.");

        Self {
            name,
            module,
            using_temp_dir: false,
            inherit_temp_dir: false,
            using_fixture_dir: false,
            inherit_fixture_dir: false,
            setup_func: None,
            teardown_func: None,
        }
    }

    /// Builds the test and initializes it.
    pub fn build(self) -> Test<'module,'func> {
        let namepath = Namepath::new_test(
            self.module.namepath.raw.package_name,
            self.module.use_case,
            self.module.namepath.raw().path,
            self.name)
            .expect("Invalid namepath for Test");

        let temp_dir = if self.using_temp_dir {
            Some(build_temp_dir(&namepath, &self.module.base_temp_dir()))
        } else if self.inherit_temp_dir {
            Some(self.module.temp_dir().to_owned())
        } else {
            None
        };

        let fixture_dir = if self.using_fixture_dir {
            Some(build_fixture_dir(&namepath))
        } else if self.inherit_fixture_dir {
            Some(self.module.fixture_dir().to_owned())
        } else {
            None
        };

        let mut test = Test {
            module: self.module,
            namepath,
            temp_dir,
            fixture_dir,
            teardown_func: self.teardown_func,
        };

        if let Some(setup_fn) = self.setup_func {
            setup_fn(&mut test);
        }

        test
    }

    /// Configures this test to use an existing fixture directory.
    /// The base path is defined by the parent Module or Group, with an existing subdirectory expected to be the name of this test.
    pub fn using_fixture_dir(mut self) -> Self {
        assert!(!self.inherit_fixture_dir, "Configuring both `inherit` and `using` for `fixture_dir` is ambiguous");
        self.using_fixture_dir = true;
        self
    }

    /// Configures the test to use a temporary directory.
    /// The base path is defined by the parent Module or Group, with a subdirectory created just for this test (by its name).
    pub fn using_temp_dir(mut self) -> Self {
        assert!(!self.inherit_temp_dir);
        if self.module.temp_dir.is_none() {
            panic!("Test cannot use a temporary directory unless its parent Module uses one");
        }

        self.using_temp_dir = true;
        self
    }

    /// Configures the test to use the exact same temporary directory as its parent Module or Group.
    /// A separate subdirectory will not be created for this test.
    pub fn inherit_temp_dir(mut self) -> Self {
        assert!(!self.using_temp_dir);
        if self.module.temp_dir.is_none() {
            panic!("Test cannot use a temporary directory unless its parent Module uses one");
        }

        self.inherit_temp_dir = true;
        self
    }

    /// Configures the test to use the exact same fixture directory as its parent Module or Group.
    /// A separate subdirectory for this test is not expected to exist.
    pub fn inherit_fixture_dir(mut self) -> Self {
        assert!(!self.using_fixture_dir);
        self.inherit_fixture_dir = true;
        self
    }

    /// Calls the provided function once on construction of the test.
    pub fn setup(mut self, func: impl FnOnce(&mut Test) + 'func) -> Self {
        self.setup_func = Some(Box::new(func));
        self
    }

    /// Calls the provided function once on destruction of the test.
    pub fn teardown(mut self, func: impl FnOnce(&mut Test) + 'func) -> Self {
        self.teardown_func = Some(Box::new(func));
        self
    }
}

pub struct TestWith<'module, 'func, H> {
    inner: Test<'module, 'func>,
    harness: &'module H,
}

impl<'module, 'func, H> TestWith<'module, 'func, H> {
    pub fn harness(&self) -> &H {
        self.harness
    }
}

impl<'module, 'func, H> Deref for TestWith<'module, 'func, H> {
    type Target = Test<'module, 'func>;
    
    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

pub struct TestBuilderWith<'module, 'func, H> {
    inner: TestBuilder<'module, 'func>,
    harness: &'module H,
}

impl<'module, 'func, H> TestBuilderWith<'module, 'func, H> {
    pub fn new(module: &'module ModuleWith<H>, name: &'static str) -> Self {
        let harness = module.harness();
        let inner = TestBuilder::new(module, name);
        Self { inner, harness }
    }
    
    pub fn using_fixture_dir(mut self) -> Self {
        self.inner = self.inner.using_fixture_dir();
        self
    }
    
    pub fn using_temp_dir(mut self) -> Self {
        self.inner = self.inner.using_fixture_dir();
        self
    }
    
    pub fn inherit_temp_dir(mut self) -> Self {
        self.inner = self.inner.inherit_fixture_dir();
        self
    }
    
    pub fn inherit_fixture_dir(mut self) -> Self {
        self.inner = self.inner.inherit_fixture_dir();
        self
    }
    
    pub fn teardown(mut self, func: impl FnOnce(&mut Test) + 'func) -> Self {
        self.inner = self.inner.teardown(func);
        self
    }
    
    pub fn build(self) -> TestWith<'module, 'func, H> {
        let inner = self.inner.build();
        let harness = self.harness;
        
        TestWith {
            inner,
            harness,
        }
    }
}

/// Constructs a [Test] using with a parent [TestModule] in scope named, "TESTING".
/// 
/// Requires that the test method is attributed with `#[tested]`.
/// 
/// ## Forms
/// - Bare:
///   - `test!()`
/// - Builder:
///   - `test!({ builder method calls ... })`
/// 
/// ## Examples
/// ### Bare
/// ```rust,ignore
/// let test = testing::test!()
/// ```
/// ### Builder
/// ```rust,ignore
/// let test = testing::test!({
///     .inherit_fixture_dir()
///     .inherit_temp_dir()
/// });
/// ```
#[macro_export]
macro_rules! test {
    ({$($b:tt)+}) => {
        $crate::TestBuilder::new(&TESTING, function_name!())
        $($b)+
            .build()
    };
    () => {
        $crate::TestBuilder::new(&TESTING, function_name!()).build()
    };
}

#[macro_export]
macro_rules! harness_test {
    ({$($b:tt)+}) => {
        $crate::TestBuilderWith::new(&TESTING, function_name!())
        $($b)+
            .build()
    };
    () => {
        $crate::TestBuilderWith::new(&TESTING, function_name!()).build()
    };
}

/// Constructs a [Test] using a custom ident for its parent [TestModule].
#[macro_export]
macro_rules! test_as {
    ($m:ident, {$($b:tt)+}) => {
        let builder = $crate::TestBuilder::new(&$m, function_name!());
        builder$($b)+
            .build()
    };
    ($m:ident) => {
        $crate::TestBuilder::new(&$m, function_name!()).build()
    };
}

impl<'t, 'tm, 'tf> Into<Testable<'t, 'tm, 'tf>> for &'t Test<'tm, 'tf> {
    fn into(self) -> Testable<'t, 'tm, 'tf> {
        self.as_testable()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::prelude::*;

    static MODULE_BASIC: Module = module!(Unit);

    static MODULE_WITH_DIRS: Module = module!(Unit, {
            .using_fixture_dir()
            .using_temp_dir()
    });

    // Test parent Module should be bound.
    #[test] #[named]
    fn test_module() {
        let test = MODULE_BASIC.test_builder(function_name!()).build();
        assert_eq!(&*MODULE_BASIC.namepath(), test.module().namepath(),
            "Test parent Module should be bound.");
    }

    // Test name should be set.
    #[test] #[named]
    fn test_name() {
        let test = MODULE_BASIC.test_builder(function_name!()).build();
        assert_eq!("test_name", test.namepath().name(),
            "Test name should be set.");
    }

    // Test name should not contain namepath separator tokens: "::", '/', '.'
    #[test] #[should_panic]
    fn test_name_invalid() {
        MODULE_BASIC.test_builder("foo.bar").build();  // should panic
    }

    // Test with only a parent Module should have a namepath of: `Test::module().namepath()` / `Test::name()`
    // Test with a parent Group should have a namepath of: `Test::group().namepath()` / `Test::name()`
    #[test] #[named]
    fn test_namepath() {
        const EXPECTED_TEST_NAMEPATH: &'static str = "unit/test/test_namepath";
        let test = MODULE_BASIC.test_builder(function_name!()).build();
        assert_eq!(EXPECTED_TEST_NAMEPATH, test.namepath().to_string());
    }

    // Test not configured with a temp dir should panic when attempting to access it
    #[test] #[should_panic] #[named]
    fn test_temp_dir_unconfigured_access() {
        MODULE_BASIC.test_builder(function_name!())
            .build()
            .temp_dir();  // should panic
    }

    // Test should not allow configuration with `using_temp_dir()` if its parent Module is not using a temp dir.
    #[test] #[should_panic] #[named]
    fn test_temp_dir_using_unconfigured_module() {
        MODULE_BASIC.test_builder(function_name!())
            .using_temp_dir()  // should panic
            .build();
    }

    // Test should not allow configuration with `inherit_temp_dir()` if its parent Module is not using a temp dir.
    #[test] #[should_panic] #[named]
    fn test_temp_dir_inherited_unconfigured_module() {
        MODULE_BASIC.test_builder(function_name!())
            .inherit_temp_dir()  // should panic
            .build();
    }

    // Test configured with `using_tmp_dir()` should have a temp path of: `Module.tmp_dir()` + `Test.name()`
    // Test configured with `using_temp_dir()` should create the directory on construction if it does not exist.
    #[test] #[named]
    fn test_temp_dir_using() {
        let test = MODULE_WITH_DIRS.test_builder(function_name!())
            .using_temp_dir()
            .build();

        assert!(test.temp_dir().exists());
        assert_eq!(MODULE_WITH_DIRS.temp_dir().join("test_temp_dir_using"), test.temp_dir());
    }

    // Test configured to `inherit_temp_dir()` should have the same temp path as its parent.
    #[test] #[named]
    fn test_temp_dir_inherited() {
        let test = MODULE_WITH_DIRS.test_builder(function_name!())
            .inherit_temp_dir()
            .build();

        assert_eq!(MODULE_WITH_DIRS.temp_dir(), test.temp_dir(),
            "Test configured to `inherit_temp_dir()` should have the same temp path as its parent.");
    }

    // Test not configured with a fixture dir should panic when attempting to access it
    #[test] #[should_panic] #[named]
    fn test_fixture_dir_unconfigured_access() {
        MODULE_WITH_DIRS.test_builder(function_name!())
            .build()
            .fixture_dir(); // should panic
    }

    // Test should not allow configuration with `using_fixture_dir()` if its parent Module is not using a fixture dir.
    #[test] #[should_panic] #[named]
    fn test_fixture_dir_using_unconfigured_module() {
        MODULE_BASIC.test_builder(function_name!())
            .using_fixture_dir()  // should panic
            .build();
    }

    // Test should not allow configuration with `inherit_fixture_dir()` if its parent Module is not using a fixture dir.
    #[test] #[should_panic] #[named]
    fn test_fixture_dir_inherited_unconfigured_module() {
        MODULE_BASIC.test_builder(function_name!())
            .inherit_fixture_dir()  // should panic
            .build();
    }

    // Test configured with `using_fixture_dir()` should have a path of: `Module::fixture_dir()` + `Test::name()`
    // Fixture path should exist for Test configured as `using_fixture_dir()` with a parent Module.
    // Test configured with `using_fixture_dir()` should have a path of: `Group::fixture_dir()` + `Test::name()`
    // Fixture path should exist for Test configured as `using_fixture_dir()` with a parent Module.
     #[test] #[named]
    fn test_fixture_dir_using() {
        let test = MODULE_WITH_DIRS.test_builder(function_name!())
            .using_fixture_dir()
            .build();

        assert_eq!(MODULE_WITH_DIRS.fixture_dir().join("test_fixture_dir_using"), test.fixture_dir(),
            "Test configured with `using_fixture_dir()` should have a path of: `Module::fixture_dir()` + `Test::name()`");
        assert!(test.fixture_dir().exists(),
            "Fixture path should exist for Test configured as `using_fixture_dir()`");
    }

    // Test configured to `inherit_fixture_dir()` should have a fixture path that is the same as its Module.
    // Fixture path should exist for Test configured to `inherit_fixture_dir()` from Module
    // Test configured to `inherit_fixture_dir()` should have a fixture path that is the same as its Group.
    // Fixture path should exist for Test configured to `inherit_fixture_dir()` from Group
    #[test] #[named]
    fn test_fixture_dir_inherited() {
        let test = MODULE_WITH_DIRS.test_builder(function_name!())
            .inherit_fixture_dir()
            .build();

        assert_eq!(MODULE_WITH_DIRS.fixture_dir(), test.fixture_dir(),
            "Test configured to `inherit_fixture_dir()` should have a fixture path that is the same as its Module.");
        assert!(test.fixture_dir().exists(),
            "Fixture path should exist for Test configured to `inherit_fixture_dir()` from Module");
    }

    // SAFETY: This can only be called once, by `test_setup_function()`. Not thread safe.
    static mut SETUP_FUNC_CALLED: bool = false;
    fn setup_func(_test: &mut Test) {
        unsafe {
            SETUP_FUNC_CALLED = true;
        }
    }

    // Test setup function should be ran on construction.
    #[test] #[named]
    fn test_setup_function() {
        let _testgroup = MODULE_BASIC.test_builder(function_name!())
            .setup(setup_func)
            .build();

        unsafe {
            assert!(SETUP_FUNC_CALLED,
                "Test setup function should be ran on construction.");
        }
    }

    // Test setup closure should be ran on construction.
    #[test] #[named]
    fn test_setup_closure() {
        let mut setup_closure_called = false;
        MODULE_BASIC.test_builder(function_name!())
            .setup(|_| {
                setup_closure_called = true;
            })
            .build();

        assert!(setup_closure_called,
            "Test setup closure should be ran on construction.");
    }

    // unsafe: This can only be called once, by `test_setup_function()`. Not thread safe.
    static mut TEARDOWN_FUNC_CALLED: bool = false;
    fn teardown_func(_group: &mut Test) {
        unsafe {
            TEARDOWN_FUNC_CALLED = true;
        }
    }

    // Test teardown function should be ran on destruction.
    #[test] #[named]
    fn test_teardown_function() {
        {
            MODULE_BASIC.test_builder(function_name!())
            .teardown(teardown_func)
            .build();
        }

        unsafe {
            assert!(TEARDOWN_FUNC_CALLED,
                "Test teardown function should be ran on destruction.");
        }
    }

    // Test teardown closure should be ran on destruction.
    #[test] #[named]
    fn test_teardown_closure() {
        let mut teardown_closure_called = false;
        {
            MODULE_BASIC.test_builder(function_name!())
                .teardown(|_| {
                    teardown_closure_called = true;
                })
                .build();
        }

        assert!(teardown_closure_called,
            "Test teardown closure should be ran on destruction.");
    }
}