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
//! # Test Utilities
//!
//! For now, mostly just helpers for running "ui tests", or executing forth code at
//! test time.
//!
//! ## UI Tests
//!
//! Generally, forth code provided as a str will have one of the following things
//! for each line:
//!
//! * Configuration values for the VM, specified as "frontmatter comments".
//!   These must appear before any other non-comment lines. Currently accepted:
//!     * `( data_stack_elems USIZE )`
//!     * `( return_stack_elems USIZE )`
//!     * `( control_stack_elems USIZE )`
//!     * `( input_buf_elems USIZE )`
//!     * `( output_buf_elems USIZE )`
//!     * `( dict_buf_elems USIZE )`
//! * Comment lines. These are any lines just containing a `( ... )` style forth comment.
//! * Successful input lines, starting with `> ...`.
//! * Successful output lines, starting with `< ...`.
//!     * Any successful input line can have zero or more output lines
//!     * If *no* input lines are specified, ANY successful output is accepted/ignored.
//! * Unsuccessful input lines, starting with `x ...`.
//!     * This line is expected to cause an "exception" - basically `process_line` returns
//!       an `Err()`.
//!     * There is no way to specify which error yet
//!     * Unsuccessful input lines may not have any successful output
//!
//! These ui-tests can also be run as doctests (see below), and doctests can be run
//! in miri.
//!
//! ### Example
//!
//! This is a forth ui-test doctest. It will be run with `cargo test --all-features`.
//!
//! ```rust
//! # use forth3::testutil::blocking_runtest;
//! #
//! # blocking_runtest(r#"
//! ( specify VM settings with frontmatter )
//! ( data_stack_elems 1 )
//!
//! ( specify input with no output )
//! > : star 42 emit ;
//!
//! ( specify input and output )
//! > star
//! < *ok.
//!
//! ( specify lines that cause exceptions/errors )
//! x starb
//! # "#)
//! ```

use crate::{
    leakbox::{LBForth, LBForthParams},
    Error, Forth,
};

/// Run the given forth ui test against ALL enabled forth VMs
///
/// A helper for calling blocking + async runtest functions. This is a good
/// default to use for unit tests.
pub fn all_runtest(contents: &str) {
    blocking_runtest(contents);
    #[cfg(feature = "async")]
    async_blockon_runtest(contents);
}

/// Run the given forth ui test against the default forth vm
///
/// Does accept any/all/none of the following configuration frontmatter (see above
/// for listing of frontmatter kinds)
pub fn blocking_runtest(contents: &str) {
    let tokd = tokenize(contents, true).unwrap();
    let mut forth = LBForth::from_params(tokd.settings, (), Forth::FULL_BUILTINS);
    blocking_steps_with(tokd.steps.as_slice(), &mut forth.forth);
}

/// Run the given forth ui-test against the given forth vm.
///
/// Does not accept ui-tests with frontmatter configuration (will panic)
pub fn blocking_runtest_with<T>(forth: &mut Forth<T>, contents: &str) {
    let tokd = tokenize(contents, false).unwrap();
    blocking_steps_with(tokd.steps.as_slice(), forth);
}

/// Run the given forth ui test against the async forth vm
///
/// Does accept any/all/none of the following configuration frontmatter (see above
/// for listing of frontmatter kinds). Provides no actual async builtins, and will
/// panic if the provided dispatcher is called for some reason
#[cfg(feature = "async")]
pub fn async_blockon_runtest(contents: &str) {
    use crate::{
        dictionary::{AsyncBuiltinEntry, AsyncBuiltins},
        fastr::FaStr,
        leakbox::AsyncLBForth,
    };

    struct TestAsyncDispatcher;
    impl<'forth> AsyncBuiltins<'forth, ()> for TestAsyncDispatcher {
        type Future = core::future::Ready<Result<(), Error>>;
        const BUILTINS: &'static [AsyncBuiltinEntry<()>] = &[];
        fn dispatch_async(&self, _id: &FaStr, _forth: &'forth mut Forth<()>) -> Self::Future {
            unreachable!("no async builtins should be called in this test")
        }
    }

    let tokd = tokenize(contents, true).unwrap();
    let mut forth =
        AsyncLBForth::from_params(tokd.settings, (), Forth::FULL_BUILTINS, TestAsyncDispatcher);
    async_blockon_runtest_with(&mut forth.forth, contents);
}

/// Like `async_blockon_runtest`, but with provided context + dispatcher
#[cfg(feature = "async")]
pub fn async_blockon_runtest_with_dispatcher<T, D>(context: T, dispatcher: D, contents: &str)
where
    T: 'static,
    D: for<'forth> crate::dictionary::AsyncBuiltins<'forth, T>,
{
    use crate::leakbox::AsyncLBForth;

    let tokd = tokenize(contents, true).unwrap();
    let mut forth =
        AsyncLBForth::from_params(tokd.settings, context, Forth::FULL_BUILTINS, dispatcher);
    async_blockon_runtest_with(&mut forth.forth, contents);
}

/// Like `async_blockon_runtest`, but with provided async vm
#[cfg(feature = "async")]
pub fn async_blockon_runtest_with<T, D>(forth: &mut crate::AsyncForth<T, D>, contents: &str)
where
    T: 'static,
    D: for<'forth> crate::dictionary::AsyncBuiltins<'forth, T>,
{
    let tokd = tokenize(contents, false).unwrap();
    for Step {
        ref input,
        output: ref outcome,
    } in tokd.steps
    {
        #[cfg(not(miri))]
        println!("> {input}");
        forth.input_mut().fill(input).unwrap();
        let res = futures::executor::block_on(forth.process_line());
        check_output(res, outcome, forth.output().as_str());
        forth.output_mut().clear();
    }
}

fn check_output(res: Result<(), Error>, outcome: &Outcome, output: &str) {
    #[cfg(not(miri))]
    println!("< {output}");
    match (res, outcome) {
        (Ok(()), Outcome::OkAnyOutput) => {}
        (Ok(()), Outcome::OkWithOutput(exp)) => {
            let act_lines = output.lines().collect::<Vec<&str>>();
            assert_eq!(act_lines.len(), exp.len());
            act_lines.iter().zip(exp.iter()).for_each(|(a, e)| {
                assert_eq!(a.trim_end(), e.trim_end());
            })
        }
        (Err(_e), Outcome::FatalError) => {}
        (res, exp) => {
            eprintln!("Error!");
            eprintln!("Expected: {exp:?}");
            eprintln!("Got: {res:?}");
            if res.is_ok() {
                eprintln!("Output:\n{}", output);
            }
            panic!();
        }
    }
}

// Runs the given steps against the given forth VM.
//
// Panics on any mismatch
fn blocking_steps_with<T>(steps: &[Step], forth: &mut Forth<T>) {
    for Step {
        input,
        output: outcome,
    } in steps
    {
        #[cfg(not(miri))]
        println!("> {input}");
        forth.input.fill(input).unwrap();
        let res = forth.process_line();
        check_output(res, outcome, forth.output.as_str());
        forth.output.clear();
    }
}

#[derive(Debug)]
enum Outcome {
    OkAnyOutput,
    OkWithOutput(Vec<String>),
    FatalError,
}

#[derive(Debug)]
struct Step {
    input: String,
    output: Outcome,
}

#[derive(Default, Debug)]
struct Tokenized {
    settings: LBForthParams,
    steps: Vec<Step>,
}

fn tokenize(contents: &str, allow_frontmatter: bool) -> Result<Tokenized, ()> {
    let mut output = Tokenized::default();
    let mut frontmatter_done = !allow_frontmatter;

    for line in contents.lines() {
        let (tok, remain) = if let Some(t) = line.trim_start().split_once(' ') {
            t
        } else {
            continue;
        };

        match tok {
            ">" => {
                frontmatter_done = true;
                output.steps.push(Step {
                    input: remain.to_string(),
                    output: Outcome::OkAnyOutput,
                });
            }
            "<" => {
                frontmatter_done = true;
                let cur_step = output.steps.last_mut().unwrap();
                let expected_out = remain.to_string();
                match &mut cur_step.output {
                    Outcome::OkAnyOutput => {
                        cur_step.output = Outcome::OkWithOutput(vec![expected_out]);
                    }
                    Outcome::OkWithOutput(o) => {
                        o.push(remain.to_string());
                    }
                    Outcome::FatalError => panic!("Fatal error can't set output"),
                }
            }
            "x" => {
                frontmatter_done = true;
                output.steps.push(Step {
                    input: remain.to_string(),
                    output: Outcome::FatalError,
                });
            }
            "(" => {
                let mut split = remain.split_whitespace();
                let mut is_comment = false;
                match split.next() {
                    Some("data_stack_elems") => {
                        output.settings.data_stack_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some("return_stack_elems") => {
                        output.settings.return_stack_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some("control_stack_elems") => {
                        output.settings.control_stack_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some("input_buf_elems") => {
                        output.settings.input_buf_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some("output_buf_elems") => {
                        output.settings.output_buf_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some("dict_buf_elems") => {
                        output.settings.dict_buf_elems =
                            split.next().unwrap().parse::<usize>().unwrap();
                    }
                    Some(_) => {
                        is_comment = true;
                    }
                    _ => panic!(),
                }
                if !is_comment {
                    assert!(!frontmatter_done, "Unexpected frontmatter settings!");
                    assert_eq!(Some(")"), split.next());
                }
            }
            _ => {}
        }
    }

    Ok(output)
}