Skip to content

Commit f1576e7

Browse files
committed
Improve tests
1 parent 1afa047 commit f1576e7

3 files changed

Lines changed: 46 additions & 113 deletions

File tree

crates/plotnik-lib/src/lib.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -203,48 +203,6 @@ impl<'a> Query<'a> {
203203
out
204204
}
205205

206-
/// Snapshot of CST structure (without trivia, with errors).
207-
pub fn snapshot_cst(&self) -> String {
208-
let mut out = self.format_cst();
209-
if !self.errors.is_empty() {
210-
out.push_str("---\n");
211-
out.push_str(&self.render_errors());
212-
out.push('\n');
213-
}
214-
out
215-
}
216-
217-
/// Snapshot of AST structure (with errors).
218-
pub fn snapshot_ast(&self) -> String {
219-
let mut out = self.format_ast();
220-
if !self.errors.is_empty() {
221-
out.push_str("---\n");
222-
out.push_str(&self.render_errors());
223-
out.push('\n');
224-
}
225-
out
226-
}
227-
228-
/// Snapshot of CST structure (with trivia, with errors).
229-
pub fn snapshot_cst_raw(&self) -> String {
230-
let mut out = self.format_cst_raw();
231-
if !self.errors.is_empty() {
232-
out.push_str("---\n");
233-
out.push_str(&self.render_errors());
234-
out.push('\n');
235-
}
236-
out
237-
}
238-
239-
/// Snapshot of symbol references (with errors).
240-
pub fn snapshot_refs(&self) -> String {
241-
let mut out = self.format_refs();
242-
if !self.errors.is_empty() {
243-
out.push_str("---\n");
244-
out.push_str(&self.render_errors());
245-
}
246-
out
247-
}
248206

249207
fn format_tree(node: &SyntaxNode, indent: usize, out: &mut String, include_trivia: bool) {
250208
use std::fmt::Write;

crates/plotnik-lib/src/resolve.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ mod tests {
214214
fn single_definition() {
215215
let input = "Expr = (expression)";
216216
let query = Query::new(input);
217-
insta::assert_snapshot!(query.snapshot_refs(), @"Expr");
217+
assert!(query.is_valid());
218+
insta::assert_snapshot!(query.format_refs(), @"Expr");
218219
}
219220

220221
#[test]
@@ -226,7 +227,8 @@ mod tests {
226227
"#};
227228

228229
let query = Query::new(input);
229-
insta::assert_snapshot!(query.snapshot_refs(), @r"
230+
assert!(query.is_valid());
231+
insta::assert_snapshot!(query.format_refs(), @r"
230232
Decl
231233
Expr
232234
Stmt
@@ -241,7 +243,8 @@ mod tests {
241243
"#};
242244

243245
let query = Query::new(input);
244-
insta::assert_snapshot!(query.snapshot_refs(), @r"
246+
assert!(query.is_valid());
247+
insta::assert_snapshot!(query.format_refs(), @r"
245248
Call -> Expr
246249
Expr
247250
");
@@ -252,9 +255,8 @@ mod tests {
252255
let input = "Call = (call_expression function: (Undefined))";
253256

254257
let query = Query::new(input);
255-
insta::assert_snapshot!(query.snapshot_refs(), @r"
256-
Call -> Undefined
257-
---
258+
assert!(!query.is_valid());
259+
insta::assert_snapshot!(query.render_errors(), @r"
258260
error: undefined reference: `Undefined`
259261
|
260262
1 | Call = (call_expression function: (Undefined))
@@ -267,7 +269,8 @@ mod tests {
267269
let input = "Expr = [(identifier) (call (Expr))]";
268270

269271
let query = Query::new(input);
270-
insta::assert_snapshot!(query.snapshot_refs(), @"Expr -> Expr");
272+
assert!(query.is_valid());
273+
insta::assert_snapshot!(query.format_refs(), @"Expr -> Expr");
271274
}
272275

273276
#[test]
@@ -278,10 +281,8 @@ mod tests {
278281
"#};
279282

280283
let query = Query::new(input);
281-
insta::assert_snapshot!(query.snapshot_refs(), @r"
282-
A -> B
283-
B -> A
284-
---
284+
assert!(!query.is_valid());
285+
insta::assert_snapshot!(query.render_errors(), @r"
285286
error: recursive pattern can never match: cycle `B` → `A` → `B` has no escape path
286287
|
287288
1 | A = (foo (B))
@@ -302,9 +303,8 @@ mod tests {
302303
"#};
303304

304305
let query = Query::new(input);
305-
insta::assert_snapshot!(query.snapshot_refs(), @r"
306-
Expr
307-
---
306+
assert!(!query.is_valid());
307+
insta::assert_snapshot!(query.render_errors(), @r"
308308
error: duplicate definition: `Expr`
309309
|
310310
2 | Expr = (other)
@@ -320,7 +320,8 @@ mod tests {
320320
"#};
321321

322322
let query = Query::new(input);
323-
insta::assert_snapshot!(query.snapshot_refs(), @r"
323+
assert!(query.is_valid());
324+
insta::assert_snapshot!(query.format_refs(), @r"
324325
Expr
325326
Value -> Expr
326327
");
@@ -334,7 +335,8 @@ mod tests {
334335
"#};
335336

336337
let query = Query::new(input);
337-
insta::assert_snapshot!(query.snapshot_refs(), @r"
338+
assert!(query.is_valid());
339+
insta::assert_snapshot!(query.format_refs(), @r"
338340
Expr
339341
Pair -> Expr
340342
");
@@ -348,7 +350,8 @@ mod tests {
348350
"#};
349351

350352
let query = Query::new(input);
351-
insta::assert_snapshot!(query.snapshot_refs(), @r"
353+
assert!(query.is_valid());
354+
insta::assert_snapshot!(query.format_refs(), @r"
352355
Expr
353356
List -> Expr
354357
");
@@ -362,7 +365,8 @@ mod tests {
362365
"#};
363366

364367
let query = Query::new(input);
365-
insta::assert_snapshot!(query.snapshot_refs(), @r"
368+
assert!(query.is_valid());
369+
insta::assert_snapshot!(query.format_refs(), @r"
366370
Expr
367371
Named -> Expr
368372
");
@@ -376,16 +380,17 @@ mod tests {
376380
"#};
377381

378382
let query = Query::new(input);
379-
insta::assert_snapshot!(query.snapshot_refs(), @"Expr");
383+
assert!(query.is_valid());
384+
insta::assert_snapshot!(query.format_refs(), @"Expr");
380385
}
381386

382387
#[test]
383388
fn entry_point_undefined_reference() {
384389
let input = "(call function: (Unknown))";
385390

386391
let query = Query::new(input);
387-
insta::assert_snapshot!(query.snapshot_refs(), @r"
388-
---
392+
assert!(!query.is_valid());
393+
insta::assert_snapshot!(query.render_errors(), @r"
389394
error: undefined reference: `Unknown`
390395
|
391396
1 | (call function: (Unknown))
@@ -397,7 +402,8 @@ mod tests {
397402
fn no_definitions() {
398403
let input = "(identifier)";
399404
let query = Query::new(input);
400-
insta::assert_snapshot!(query.snapshot_refs(), @"");
405+
assert!(query.is_valid());
406+
insta::assert_snapshot!(query.format_refs(), @"");
401407
}
402408

403409
#[test]
@@ -410,7 +416,8 @@ mod tests {
410416
"#};
411417

412418
let query = Query::new(input);
413-
insta::assert_snapshot!(query.snapshot_refs(), @r"
419+
assert!(query.is_valid());
420+
insta::assert_snapshot!(query.format_refs(), @r"
414421
A
415422
B -> A
416423
C -> B
@@ -423,8 +430,8 @@ mod tests {
423430
let input = "(foo (X) (Y) (Z))";
424431

425432
let query = Query::new(input);
426-
insta::assert_snapshot!(query.snapshot_refs(), @r"
427-
---
433+
assert!(!query.is_valid());
434+
insta::assert_snapshot!(query.render_errors(), @r"
428435
error: undefined reference: `X`
429436
|
430437
1 | (foo (X) (Y) (Z))

crates/plotnik-lib/src/validate.rs

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ mod tests {
127127
#[test]
128128
fn tagged_alternation_valid() {
129129
let query = Query::new("[A: (a) B: (b)]");
130-
insta::assert_snapshot!(query.snapshot_ast(), @r"
130+
assert!(query.is_valid());
131+
insta::assert_snapshot!(query.format_ast(), @r"
131132
Root
132133
Def
133134
Alt
@@ -141,7 +142,8 @@ mod tests {
141142
#[test]
142143
fn untagged_alternation_valid() {
143144
let query = Query::new("[(a) (b)]");
144-
insta::assert_snapshot!(query.snapshot_ast(), @r"
145+
assert!(query.is_valid());
146+
insta::assert_snapshot!(query.format_ast(), @r"
145147
Root
146148
Def
147149
Alt
@@ -155,15 +157,8 @@ mod tests {
155157
#[test]
156158
fn mixed_alternation_tagged_first() {
157159
let query = Query::new("[A: (a) (b)]");
158-
insta::assert_snapshot!(query.snapshot_ast(), @r"
159-
Root
160-
Def
161-
Alt
162-
Branch A:
163-
Tree a
164-
Branch
165-
Tree b
166-
---
160+
assert!(!query.is_valid());
161+
insta::assert_snapshot!(query.render_errors(), @r"
167162
error: mixed tagged and untagged branches in alternation
168163
|
169164
1 | [A: (a) (b)]
@@ -183,15 +178,8 @@ mod tests {
183178
]
184179
"#,
185180
);
186-
insta::assert_snapshot!(query.snapshot_ast(), @r"
187-
Root
188-
Def
189-
Alt
190-
Branch
191-
Tree a
192-
Branch B:
193-
Tree b
194-
---
181+
assert!(!query.is_valid());
182+
insta::assert_snapshot!(query.render_errors(), @r"
195183
error: mixed tagged and untagged branches in alternation
196184
|
197185
3 | (a)
@@ -204,16 +192,8 @@ mod tests {
204192
#[test]
205193
fn nested_mixed_alternation() {
206194
let query = Query::new("(call [A: (a) (b)])");
207-
insta::assert_snapshot!(query.snapshot_ast(), @r"
208-
Root
209-
Def
210-
Tree call
211-
Alt
212-
Branch A:
213-
Tree a
214-
Branch
215-
Tree b
216-
---
195+
assert!(!query.is_valid());
196+
insta::assert_snapshot!(query.render_errors(), @r"
217197
error: mixed tagged and untagged branches in alternation
218198
|
219199
1 | (call [A: (a) (b)])
@@ -226,21 +206,8 @@ mod tests {
226206
#[test]
227207
fn multiple_mixed_alternations() {
228208
let query = Query::new("(foo [A: (a) (b)] [C: (c) (d)])");
229-
insta::assert_snapshot!(query.snapshot_ast(), @r"
230-
Root
231-
Def
232-
Tree foo
233-
Alt
234-
Branch A:
235-
Tree a
236-
Branch
237-
Tree b
238-
Alt
239-
Branch C:
240-
Tree c
241-
Branch
242-
Tree d
243-
---
209+
assert!(!query.is_valid());
210+
insta::assert_snapshot!(query.render_errors(), @r"
244211
error: mixed tagged and untagged branches in alternation
245212
|
246213
1 | (foo [A: (a) (b)] [C: (c) (d)])
@@ -259,7 +226,8 @@ mod tests {
259226
#[test]
260227
fn single_branch_no_error() {
261228
let query = Query::new("[A: (a)]");
262-
insta::assert_snapshot!(query.snapshot_ast(), @r"
229+
assert!(query.is_valid());
230+
insta::assert_snapshot!(query.format_ast(), @r"
263231
Root
264232
Def
265233
Alt

0 commit comments

Comments
 (0)