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
|
use std::fmt::Debug;
use super::func::FuncHeader;
use super::expr::{Expr, Tuple, Object};
use super::span::{Span, Spanned};
use super::tokens::Token;
use super::*;
/// Check whether the expected and found results for the given source code
/// match by the comparison function, and print them out otherwise.
pub fn check<T>(src: &str, exp: T, found: T, spans: bool)
where T: Debug + PartialEq + SpanlessEq {
let cmp = if spans { PartialEq::eq } else { SpanlessEq::spanless_eq };
if !cmp(&exp, &found) {
println!("source: {:?}", src);
println!("expected: {:#?}", exp);
println!("found: {:#?}", found);
panic!("test failed");
}
}
/// Create a vector of optionally spanned expressions from a list description.
///
/// # Examples
/// When you want to add span information to the items, the format is as
/// follows.
/// ```
/// spanned![(0:0, 0:5, "hello"), (0:5, 0:3, "world")]
/// ```
/// The span information can simply be omitted to create a vector with items
/// that are spanned with dummy zero spans.
macro_rules! spanned {
(item ($sl:tt:$sc:tt, $el:tt:$ec:tt, $v:expr)) => ({
#[allow(unused_imports)]
use $crate::syntax::span::{Position, Span, Spanned};
Spanned {
span: Span::new(
Position::new($sl, $sc),
Position::new($el, $ec)
),
v: $v
}
});
(vec $(($sl:tt:$sc:tt, $el:tt:$ec:tt, $v:expr)),* $(,)?) => {
(vec![$(spanned![item ($sl:$sc, $el:$ec, $v)]),*], true)
};
(vec $($v:expr),* $(,)?) => {
(vec![$($crate::syntax::test::zspan($v)),*], false)
};
}
/// Span an element with a zero span.
pub fn zspan<T>(v: T) -> Spanned<T> {
Spanned { v, span: Span::ZERO }
}
function! {
/// Most functions in the tests are parsed into the debug function for easy
/// inspection of arguments and body.
#[derive(Debug, Clone, PartialEq)]
pub struct DebugFn {
pub header: FuncHeader,
pub body: Option<SyntaxModel>,
}
parse(header, body, ctx, f) {
let cloned = header.clone();
header.args.pos.items.clear();
header.args.key.pairs.clear();
DebugFn {
header: cloned,
body: body!(opt: body, ctx, f),
}
}
layout(self, ctx, errors) { vec![] }
}
/// Compares elements by only looking at values and ignoring spans.
pub trait SpanlessEq<Rhs=Self> {
fn spanless_eq(&self, other: &Rhs) -> bool;
}
impl<T: SpanlessEq> SpanlessEq for Vec<Spanned<T>> {
fn spanless_eq(&self, other: &Vec<Spanned<T>>) -> bool {
self.len() == other.len()
&& self.iter().zip(other).all(|(x, y)| x.v.spanless_eq(&y.v))
}
}
impl SpanlessEq for SyntaxModel {
fn spanless_eq(&self, other: &SyntaxModel) -> bool {
self.nodes.spanless_eq(&other.nodes)
}
}
impl SpanlessEq for Node {
fn spanless_eq(&self, other: &Node) -> bool {
fn downcast<'a>(func: &'a (dyn Model + 'static)) -> &'a DebugFn {
func.downcast::<DebugFn>().expect("not a debug fn")
}
match (self, other) {
(Node::Model(a), Node::Model(b)) => {
downcast(a.as_ref()).spanless_eq(downcast(b.as_ref()))
}
(a, b) => a == b,
}
}
}
impl SpanlessEq for DebugFn {
fn spanless_eq(&self, other: &DebugFn) -> bool {
self.header.name.v == other.header.name.v
&& self.header.args.pos.spanless_eq(&other.header.args.pos)
&& self.header.args.key.spanless_eq(&other.header.args.key)
}
}
impl SpanlessEq for Expr {
fn spanless_eq(&self, other: &Expr) -> bool {
match (self, other) {
(Expr::Tuple(a), Expr::Tuple(b)) => a.spanless_eq(b),
(Expr::Object(a), Expr::Object(b)) => a.spanless_eq(b),
(a, b) => a == b,
}
}
}
impl SpanlessEq for Tuple {
fn spanless_eq(&self, other: &Tuple) -> bool {
self.items.len() == other.items.len()
&& self.items.iter().zip(&other.items)
.all(|(x, y)| x.v.spanless_eq(&y.v))
}
}
impl SpanlessEq for Object {
fn spanless_eq(&self, other: &Object) -> bool {
self.pairs.len() == other.pairs.len()
&& self.pairs.iter().zip(&other.pairs)
.all(|(x, y)| x.key.v == y.key.v && x.value.v.spanless_eq(&y.value.v))
}
}
/// Implement `SpanlessEq` by just forwarding to `PartialEq`.
macro_rules! forward {
($type:ty) => {
impl SpanlessEq for $type {
fn spanless_eq(&self, other: &$type) -> bool {
self == other
}
}
};
}
forward!(String);
forward!(Token<'_>);
forward!(Decoration);
|