zinkc_filetests/
lib.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
#![deny(missing_docs)]
//! Zink filetests

include!(concat!(env!("OUT_DIR"), "/tests.rs"));

/// A wat test
#[derive(Clone)]
pub struct Test {
    /// The module name
    pub module: String,
    /// The test name
    pub name: String,
    /// The test source
    pub wasm: Vec<u8>,
}

#[cfg(test)]
impl Test {
    /// Compile test to evm bytecode.
    pub fn compile(&self) -> anyhow::Result<()> {
        tracing_subscriber::fmt()
            .with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
            .without_time()
            .compact()
            .try_init()
            .ok();

        let Test { module, name, wasm } = self;
        tracing::info!("Compiling {module}::{name}");

        let compiler = zinkc::Compiler::default();
        // TODO: after #248
        if name == "fibonacci" {
            return Ok(());
        }
        compiler.compile(&wasm)?;
        Ok(())
    }
}

/// Generate tests for different modules.
#[allow(clippy::crate_in_macro_def)]
#[macro_export]
macro_rules! impl_tests {
    (
        tests: $tests:tt,
        modules: [$($mod:expr),+]
    ) => {
        $(
            impl_tests!(@module $mod, $tests);
        )*
    };
    (@module  $module:tt, [$($test:ident),+]) => {
        paste::paste! {
            mod [< $module >] {
                $(
                    #[test]
                    fn $test() -> anyhow::Result<()> {
                        crate::$test($module)
                    }
                )*
            }
        }
    }
}