datatest_stable/
macros.rs

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
// Copyright (c) The datatest-stable Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

/// `datatest-stable` test harness entry point. Should be declared in the test module.
///
/// Also, `harness` should be set to `false` for that test module in `Cargo.toml` (see [Configuring
/// a target](https://doc.rust-lang.org/cargo/reference/manifest.html#configuring-a-target)).
#[macro_export]
macro_rules! harness {
    ( $( { $($args:tt)* } ),+ $(,)* ) => {
        fn main() -> ::std::process::ExitCode {
            let mut requirements = Vec::new();
            use $crate::data_source_kinds::*;
            use $crate::test_kinds::*;

            $(
                $crate::harness_collect!(@gather_test requirements, { $($args)*, } => { });
            )+

            $crate::runner(&requirements)
        }
    };
    ( $( $name:path, $root:expr, $pattern:expr ),+ $(,)* ) => {
        // This is the old format with datatest-stable 0.2. Print a nice message
        // in this case.
        const _: () = {
            compile_error!(
concat!(r"this format is no longer supported -- please switch to specifying as:

datatest_stable::harness! {
",
    $(concat!("    { test = ", stringify!($name), ", root = ", stringify!($root), ", pattern = ", stringify!($pattern), " },\n"),)+
r"}

note: patterns are now evaluated relative to the provided root, not to the crate root
"));
        };
    }
}

#[macro_export]
#[doc(hidden)]
macro_rules! harness_collect {
    // Gather `test`
    (@gather_test
        $requirements:expr,
        // Note: here and below, rest always ends with at least 1 comma
        { test = $test:path, $($rest:tt)* } =>
        { }
    ) => {
        $crate::harness_collect!(@gather_root
            $requirements,
            { $($rest)* } =>
            { test = $test, }
        );
    };

    // `test` not found
    (@gather_test
        $requirements:expr,
        { $key:ident $($rest:tt)* } =>
        { }
    ) => {
        compile_error!(concat!("expected `test`, found `", stringify!($key), "`"));
    };

    // No remaining arguments
    (@gather_test
        $requirements:expr,
        { $(,)* } =>
        { }
    ) => {
        compile_error!("expected `test`, but ran out of arguments");
    };

    // Something that isn't an identifier
    (@gather_test
        $requirements:expr,
        { $($rest:tt)* } =>
        { }
    ) => {
        compile_error!(concat!("expected `test`, found non-identifier token: (rest: ", stringify!($($rest)*), ")"));
    };

    // Gather `root`
    (@gather_root
        $requirements:expr,
        { root = $root:expr, $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        $crate::harness_collect!(@gather_pattern
            $requirements,
            { $($rest)* } =>
            { $($collected)* root = $root, }
        );
    };

    // `root` not found
    (@gather_root
        $requirements:expr,
        { $key:ident $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("expected `root`, found `", stringify!($key), "`"));
    };

    // No remaining arguments
    (@gather_root
        $requirements:expr,
        { $(,)* } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("expected `root`, but ran out of arguments (collected: ", stringify!($($collected)*), ")"));
    };

    // Something that isn't an identifier
    (@gather_root
        $requirements:expr,
        { $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("expected `root`, found non-identifier token (rest: ", stringify!($($rest)*), ")"));
    };

    // Gather pattern
    (@gather_pattern
        $requirements:expr,
        { pattern = $pattern:expr, $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        $crate::harness_collect!(@finish
            $requirements,
            { $($rest)* } =>
            { $($collected)* pattern = $pattern, }
        );
    };

    // `pattern` not found
    (@gather_pattern
        $requirements:expr,
        { $key:ident $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("expected `pattern`, found `", stringify!($key), "`"));
    };

    // `pattern` not found: no remaining arguments
    (@gather_pattern
        $requirements:expr,
        { $(,)* } =>
        { $($collected:tt)* }
    ) => {
        $crate::harness_collect!(@finish
            $requirements,
            { } =>
            { $($collected)* pattern = ".*", }
        );
    };

    // Something that isn't an identifier
    (@gather_pattern
        $requirements:expr,
        { $($rest:tt)* } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("expected `pattern`, found non-identifier token (rest: ", stringify!($($rest)*), ")"));
    };

    // Finish - no more arguments allowed
    (@finish
        $requirements:expr,
        { $(,)* } =>
        { test = $test:path, root = $root:expr, pattern = $pattern:expr, }
    ) => {
        $requirements.push(
            $crate::Requirements::new(
                $test.kind().resolve($test),
                stringify!($test).to_string(),
                $root.resolve_data_source(),
                $pattern.to_string()
            )
        );
    };

    // Finish - unexpected extra arguments
    (@finish
        $requirements:expr,
        { $($unexpected:tt)+ } =>
        { $($collected:tt)* }
    ) => {
        compile_error!(concat!("unexpected extra arguments: ", stringify!($($unexpected)+)));
    };
}