Repository navigation
Expand file tree
/
Copy pathrunner.rs
More file actions
94 lines (81 loc) · 2.23 KB
/
Copy pathrunner.rs
File metadata and controls
94 lines (81 loc) · 2.23 KB
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
use compy_lang::ast::Module;
use compy_lang::evaluate;
use compy_lang::parser::Parser;
use regex::Regex;
use std::fs;
use std::path::Path;
use util::eval_module_from_file;
mod util;
#[test]
fn basics() {
run("tests/basics.txt");
}
#[test]
fn functions() {
run("tests/functions.txt");
}
#[test]
fn binding() {
run("tests/binding.txt");
}
#[test]
fn blocks() {
eval_module_from_file("tests/blocks.txt");
}
#[test]
fn arrays() {
eval_module_from_file("tests/arrays.txt");
}
#[test]
fn records() {
run("tests/records.txt");
}
#[test]
fn datastructures() {
eval_module_from_file("tests/data-structures.txt");
}
#[test]
fn queries() {
eval_module_from_file("tests/queries.txt");
}
#[test]
fn integration() {
eval_module_from_file("tests/integration.txt");
}
#[test]
fn modules() {
eval_module_from_file("tests/modules.txt");
}
// Parses a test cases file and executes the tests.
fn run(path: &str) {
let expect_pattern = Regex::new("# *Expect: *(.+)").unwrap();
let path = Path::new("").join(path);
let input_owned = fs::read_to_string(&path).unwrap();
let mut input = input_owned.as_str();
while let Some(mat) = expect_pattern.find(input) {
// Extract expected output.
let expected_value = expect_pattern.captures(input).unwrap().get(1).unwrap().as_str();
println!("Expect: {}", expected_value);
// Parse code under test.
// The code starts immediately after the expectation line.
let code_start = mat.end() + 1;
let code_end = input[code_start..].find('\n').unwrap();
input = &input[code_start..];
let code = &input[..code_end];
let mut p = Parser::new(code);
let tree = p.parse_some().unwrap();
println!("{} => {}", code, expected_value);
// Execute code and compare result.
match evaluate(&Module { expression: tree }) {
Ok(result) => assert_eq!(result.to_string(), expected_value),
Err(e) => assert!(
e.to_string().contains(expected_value),
"expected {}, was {}",
expected_value,
e
),
}
// Advance input to the end of the code that was parsed.
input = &input[code_end..];
}
}