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
//! A simple framework for authoring conformance test suites.

#![deny(missing_docs)]

pub use self::test_case_results::*;
pub use self::test_suite::*;
pub use self::visualizer::*;

mod test_case_results;
mod test_suite;
mod visualizer;

/// Run a test suite.
pub fn run_test_suite<TypeToTest, TypeToTestCreator>(
    type_to_test_creator: TypeToTestCreator,
    suite: TestSuite<TypeToTest>,
) -> TestCaseResults
where
    TypeToTestCreator: Fn(&Box<dyn TestCase<TypeToTest>>) -> TypeToTest,
{
    let mut results = Vec::with_capacity(suite.test_cases.len());

    for test_case in suite.test_cases {
        let type_to_test = type_to_test_creator(&test_case);

        let test_case_result = test_case.run(type_to_test);
        results.push(test_case_result);
    }

    TestCaseResults::new(suite.title, suite.description, results)
}

/// Used to indicate a type that can be used to create test cases to test some aspect of a
/// Renderer.
pub trait TestCase<TypeToTest> {
    /// Ok if the test case was successful, otherwise an error message is returned.
    fn run(self: Box<Self>, type_to_test: TypeToTest) -> TestCaseResult;

    /// Useful for downcasting.
    ///
    /// ```
    /// # struct Foo;
    /// # impl Foo {
    /// fn as_any(&self) -> &dyn std::any::Any {
    ///     self
    /// }
    /// #}
    /// ```
    fn as_any(&self) -> &dyn std::any::Any;
}