substrait_explain/
fixtures.rs

1use crate::extensions::SimpleExtensions;
2use crate::extensions::simple::ExtensionKind;
3use crate::parser::{MessageParseError, ScopedParse};
4use crate::textify::foundation::{ErrorAccumulator, ErrorList};
5use crate::textify::{ErrorQueue, OutputOptions, Scope, ScopedContext, Textify};
6
7pub struct TestContext {
8    pub options: OutputOptions,
9    pub extensions: SimpleExtensions,
10}
11
12impl Default for TestContext {
13    fn default() -> Self {
14        Self::new()
15    }
16}
17
18impl TestContext {
19    pub fn new() -> Self {
20        Self {
21            options: OutputOptions::default(),
22            extensions: SimpleExtensions::new(),
23        }
24    }
25
26    pub fn with_options(mut self, options: OutputOptions) -> Self {
27        self.options = options;
28        self
29    }
30
31    pub fn with_uri(mut self, anchor: u32, uri: &str) -> Self {
32        self.extensions
33            .add_extension_uri(uri.to_string(), anchor)
34            .unwrap();
35        self
36    }
37
38    pub fn with_function(mut self, uri: u32, anchor: u32, name: impl Into<String>) -> Self {
39        assert!(self.extensions.find_uri(uri).is_ok());
40        self.extensions
41            .add_extension(ExtensionKind::Function, uri, anchor, name.into())
42            .unwrap();
43        self
44    }
45
46    pub fn with_type(mut self, uri: u32, anchor: u32, name: impl Into<String>) -> Self {
47        assert!(self.extensions.find_uri(uri).is_ok());
48        self.extensions
49            .add_extension(ExtensionKind::Type, uri, anchor, name.into())
50            .unwrap();
51        self
52    }
53
54    pub fn with_type_variation(mut self, uri: u32, anchor: u32, name: impl Into<String>) -> Self {
55        assert!(self.extensions.find_uri(uri).is_ok());
56        self.extensions
57            .add_extension(ExtensionKind::TypeVariation, uri, anchor, name.into())
58            .unwrap();
59        self
60    }
61
62    pub fn scope<'e, E: ErrorAccumulator>(&'e self, errors: &'e E) -> impl Scope + 'e {
63        ScopedContext::new(&self.options, errors, &self.extensions)
64    }
65
66    pub fn textify<T: Textify>(&self, t: &T) -> (String, ErrorList) {
67        let errors = ErrorQueue::default();
68        let mut output = String::new();
69
70        let scope = self.scope(&errors);
71        t.textify(&scope, &mut output).unwrap();
72
73        let evec = errors.into_iter().collect();
74        (output, ErrorList(evec))
75    }
76
77    pub fn textify_no_errors<T: Textify>(&self, t: &T) -> String {
78        let (s, errs) = self.textify(t);
79        assert!(errs.is_empty(), "{} Errors: {}", errs.0.len(), errs.0[0]);
80        s
81    }
82
83    pub fn parse<T: ScopedParse>(&self, input: &str) -> Result<T, MessageParseError> {
84        T::parse(&self.extensions, input)
85    }
86}