tytanic_core/
dsl.rs

1//! Implementations for the test set DSL. See the language [reference] and
2//! [guide] for more info.
3//!
4//! [reference]: https://tingerrr.github.io/tytanic/reference/test-sets/index.html
5//! [guide]: https://tingerrr.github.io/tytanic/guides/test-sets.html
6
7use tytanic_filter::ast::Id;
8use tytanic_filter::eval::{self, Context, Error, Func, Set, Value};
9
10use crate::test::Test;
11
12impl eval::Test for Test {
13    fn id(&self) -> &str {
14        self.id().as_str()
15    }
16}
17
18/// Creates the default context used by tytanic, this contains bindings for the
19/// constructor functions in [`built_in`].
20pub fn context() -> Context<Test> {
21    type FuncPtr =
22        for<'a, 'b> fn(&'a Context<Test>, &'b [Value<Test>]) -> Result<Value<Test>, Error>;
23
24    let mut ctx = Context::new();
25
26    let functions = [
27        ("all", built_in::all_ctor as FuncPtr),
28        ("none", built_in::none_ctor),
29        ("skip", built_in::skip_ctor),
30        ("compile-only", built_in::compile_only_ctor),
31        ("ephemeral", built_in::ephemeral_ctor),
32        ("persistent", built_in::persistent_ctor),
33    ];
34
35    for (id, func) in functions {
36        ctx.bind(Id(id.into()), Value::Func(Func::new(func)));
37    }
38
39    ctx
40}
41
42/// Function definitions for the tytanic test set DSL default evaluation
43/// context.
44pub mod built_in {
45    use tytanic_filter::eval::{Context, Error, Func, Value};
46
47    use super::*;
48
49    /// The constructor function for the test set returned by [`all`].
50    pub fn all_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
51        Func::expect_no_args("all", ctx, args)?;
52        Ok(Value::Set(all()))
53    }
54
55    /// Constructor for the `all()` test set. A test set which contains _all_
56    /// tests.
57    pub fn all() -> Set<Test> {
58        Set::new(|_, _| Ok(true))
59    }
60
61    /// The constructor function for the test set returned by [`none`].
62    pub fn none_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
63        Func::expect_no_args("none", ctx, args)?;
64        Ok(Value::Set(none()))
65    }
66
67    /// Constructor for the `none()` test set. A test set which contains _no_
68    /// tests.
69    pub fn none() -> Set<Test> {
70        Set::new(|_, _| Ok(false))
71    }
72
73    /// The constructor function for the test set returned by [`skip`].
74    pub fn skip_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
75        Func::expect_no_args("skip", ctx, args)?;
76        Ok(Value::Set(skip()))
77    }
78
79    /// Constructs the `skip()` test set. A test set which contains all tests marked
80    /// with the `skip` annotation.
81    pub fn skip() -> Set<Test> {
82        Set::new(|_, test: &Test| Ok(test.is_skip()))
83    }
84
85    /// The constructor function for the test set returned by [`compile_only`].
86    pub fn compile_only_ctor(
87        ctx: &Context<Test>,
88        args: &[Value<Test>],
89    ) -> Result<Value<Test>, Error> {
90        Func::expect_no_args("compile-only", ctx, args)?;
91        Ok(Value::Set(compile_only()))
92    }
93
94    /// Constructs the `compile-only()` test set. A test set which contains all
95    /// `compile-only` tests.
96    pub fn compile_only() -> Set<Test> {
97        Set::new(|_, test: &Test| Ok(test.kind().is_compile_only()))
98    }
99
100    /// The constructor function for the test set returned by [`ephemeral`].
101    pub fn ephemeral_ctor(ctx: &Context<Test>, args: &[Value<Test>]) -> Result<Value<Test>, Error> {
102        Func::expect_no_args("ephemeral", ctx, args)?;
103        Ok(Value::Set(ephemeral()))
104    }
105
106    /// Constructs the `ephemeral()` test set. A test set which contains all
107    /// `ephemeral` tests.
108    pub fn ephemeral() -> Set<Test> {
109        Set::new(|_, test: &Test| Ok(test.kind().is_ephemeral()))
110    }
111
112    /// The constructor function for the test set returned by [`persistent`].
113    pub fn persistent_ctor(
114        ctx: &Context<Test>,
115        args: &[Value<Test>],
116    ) -> Result<Value<Test>, Error> {
117        Func::expect_no_args("persistent", ctx, args)?;
118        Ok(Value::Set(persistent()))
119    }
120
121    /// Constructs the `persistent()` test set. A test set which contains all
122    /// `persistent` tests.
123    pub fn persistent() -> Set<Test> {
124        Set::new(|_, test: &Test| Ok(test.kind().is_persistent()))
125    }
126}