summaryrefslogtreecommitdiff
path: root/src/syntax/mod.rs
blob: 70935e7969d3dfe7bc403af5c992cdd072ff8442 (plain) (blame)
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
//! Syntax trees, parsing and tokenization.

pub mod decoration;
pub mod parsing;
pub mod span;
pub mod tokens;
pub mod tree;

#[cfg(test)]
mod tests {
    use super::span;
    use crate::prelude::*;
    use std::fmt::Debug;

    /// Assert that expected and found are equal, printing both and panicking
    /// and the source of their test case if they aren't.
    ///
    /// When `cmp_spans` is false, spans are ignored.
    pub fn check<T>(src: &str, exp: T, found: T, cmp_spans: bool)
    where
        T: Debug + PartialEq,
    {
        span::set_cmp(cmp_spans);
        let equal = exp == found;
        span::set_cmp(true);

        if !equal {
            println!("source:   {:?}", src);
            if cmp_spans {
                println!("expected: {:#?}", exp);
                println!("found:    {:#?}", found);
            } else {
                println!("expected: {:?}", exp);
                println!("found:    {:?}", found);
            }
            panic!("test failed");
        }
    }

    pub fn s<T>(sl: usize, sc: usize, el: usize, ec: usize, v: T) -> Spanned<T> {
        Spanned::new(v, Span::new(Pos::new(sl, sc), Pos::new(el, ec)))
    }

    // Enables tests to optionally specify spans.
    impl<T> From<T> for Spanned<T> {
        fn from(t: T) -> Self {
            Spanned::zero(t)
        }
    }
}