diff options
Diffstat (limited to 'src/eval/test.rs')
-rw-r--r-- | src/eval/test.rs | 61 |
1 files changed, 41 insertions, 20 deletions
diff --git a/src/eval/test.rs b/src/eval/test.rs index 83749e0..c9e7630 100644 --- a/src/eval/test.rs +++ b/src/eval/test.rs | |||
@@ -1,32 +1,27 @@ | |||
1 | use super::*; | 1 | use super::*; |
2 | use crate::ast::*; | 2 | use crate::ast::*; |
3 | use std::io::Write; | ||
4 | use expect_test::{expect, Expect}; | 3 | use expect_test::{expect, Expect}; |
5 | 4 | ||
6 | fn gen_test(file: &str, program: &str, expected: Expect) { | 5 | fn gen_test(file: &str, program: &str, stdout: Expect, stderr: Expect) { |
7 | let language = tree_sitter_python::language(); | 6 | let language = tree_sitter_python::language(); |
8 | let mut parser = tree_sitter::Parser::new(); | 7 | let mut parser = tree_sitter::Parser::new(); |
9 | let _ = parser.set_language(&language); | 8 | let _ = parser.set_language(&language); |
10 | let tree = parser.parse(file, None).unwrap(); | 9 | let tree = parser.parse(file, None).unwrap(); |
11 | let program = ast::Program::new().with_file(program).unwrap(); | 10 | let program = ast::Program::new().with_file(program).unwrap(); |
12 | 11 | ||
13 | let mut output = Vec::new(); | 12 | let mut out = Vec::new(); |
14 | let result; | 13 | let mut err = Vec::new(); |
15 | 14 | ||
16 | { | 15 | Context::new(language) |
17 | let mut ctx = Context::new(language) | 16 | .with_input(file.to_owned()) |
18 | .with_input(file.to_owned()) | 17 | .with_tree(tree) |
19 | .with_tree(tree) | 18 | .with_program(program) |
20 | .with_program(program) | 19 | .with_output_stream(Box::new(&mut out) as Box<dyn io::Write>) |
21 | .with_output_stream(Box::new(&mut output) as Box<dyn io::Write>); | 20 | .with_error_stream(Box::new(&mut err) as Box<dyn io::Write>) |
21 | .eval(); | ||
22 | 22 | ||
23 | result = ctx.eval(); | 23 | stdout.assert_eq(&String::from_utf8(out).unwrap()); |
24 | } | 24 | stderr.assert_eq(&String::from_utf8(err).unwrap()); |
25 | |||
26 | if let Err(e) = result { | ||
27 | writeln!(output, "{e:?}").unwrap(); | ||
28 | } | ||
29 | expected.assert_eq(&String::from_utf8(output).unwrap()) | ||
30 | } | 25 | } |
31 | 26 | ||
32 | #[test] | 27 | #[test] |
@@ -201,6 +196,7 @@ fn list_1() { | |||
201 | } | 196 | } |
202 | ", | 197 | ", |
203 | expect!["[5]"], | 198 | expect!["[5]"], |
199 | expect![], | ||
204 | ); | 200 | ); |
205 | } | 201 | } |
206 | 202 | ||
@@ -214,6 +210,7 @@ fn list_2() { | |||
214 | } | 210 | } |
215 | ", | 211 | ", |
216 | expect!["3"], | 212 | expect!["3"], |
213 | expect![], | ||
217 | ); | 214 | ); |
218 | } | 215 | } |
219 | 216 | ||
@@ -229,6 +226,7 @@ fn list_3() { | |||
229 | } | 226 | } |
230 | "#, | 227 | "#, |
231 | expect!["true, false"], | 228 | expect!["true, false"], |
229 | expect![], | ||
232 | ); | 230 | ); |
233 | } | 231 | } |
234 | 232 | ||
@@ -256,6 +254,7 @@ fn list_4() { | |||
256 | [5, 4] | 254 | [5, 4] |
257 | [5] | 255 | [5] |
258 | "#]], | 256 | "#]], |
257 | expect![], | ||
259 | ); | 258 | ); |
260 | } | 259 | } |
261 | 260 | ||
@@ -274,6 +273,7 @@ fn list_5() { | |||
274 | false | 273 | false |
275 | true | 274 | true |
276 | "#]], | 275 | "#]], |
276 | expect![], | ||
277 | ); | 277 | ); |
278 | } | 278 | } |
279 | 279 | ||
@@ -291,6 +291,7 @@ fn string_1() { | |||
291 | FOO | 291 | FOO |
292 | foo | 292 | foo |
293 | "#]], | 293 | "#]], |
294 | expect![], | ||
294 | ); | 295 | ); |
295 | } | 296 | } |
296 | 297 | ||
@@ -320,6 +321,7 @@ fn string_2() { | |||
320 | FOO is upper? true | 321 | FOO is upper? true |
321 | FOO is lower? false | 322 | FOO is lower? false |
322 | "#]], | 323 | "#]], |
324 | expect![], | ||
323 | ); | 325 | ); |
324 | } | 326 | } |
325 | 327 | ||
@@ -337,6 +339,7 @@ fn string_3() { | |||
337 | a[3:5]: ` b` | 339 | a[3:5]: ` b` |
338 | a[2:9]: `o bar b` | 340 | a[2:9]: `o bar b` |
339 | "#]], | 341 | "#]], |
342 | expect![], | ||
340 | ); | 343 | ); |
341 | } | 344 | } |
342 | 345 | ||
@@ -349,9 +352,8 @@ fn string_4() { | |||
349 | println("a[9:20]: `", substr(a, 9, 20), "`"); | 352 | println("a[9:20]: `", substr(a, 9, 20), "`"); |
350 | } | 353 | } |
351 | "#, | 354 | "#, |
352 | expect![[r#" | 355 | expect!["a[9:20]: `"], |
353 | a[9:20]: `InvalidStringSlice { length: 11, start: 9, end: 20 } | 356 | expect![], |
354 | "#]], | ||
355 | ); | 357 | ); |
356 | } | 358 | } |
357 | 359 | ||
@@ -367,5 +369,24 @@ fn node_1() { | |||
367 | def foo(a, b): hello() | 369 | def foo(a, b): hello() |
368 | foo | 370 | foo |
369 | "#]], | 371 | "#]], |
372 | expect![], | ||
373 | ); | ||
374 | } | ||
375 | |||
376 | #[test] | ||
377 | fn analysis_1() { | ||
378 | gen_test( | ||
379 | "def foo(a, b): hello()", | ||
380 | r#" | ||
381 | enter (function) { } | ||
382 | enter ((function (name))) { } | ||
383 | enter ((function (name args))) { } | ||
384 | "#, | ||
385 | expect![], | ||
386 | expect![[r#" | ||
387 | [warning] the pattern `(function)` can be reduced to just `function` | ||
388 | [warning] the pattern `((function (name)))` can be reduced to just `(function name)` | ||
389 | [warning] the pattern `((function (name args)))` can be reduced to just `(function (name args))` | ||
390 | "#]], | ||
370 | ); | 391 | ); |
371 | } | 392 | } |