forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.rs
More file actions
104 lines (94 loc) · 3.99 KB
/
Copy patherrors.rs
File metadata and controls
104 lines (94 loc) · 3.99 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
95
96
97
98
99
100
101
102
103
104
use fe_common::diagnostics::diagnostics_string;
use fe_parser::grammar::{contracts, expressions, functions, module, types};
use fe_parser::{ParseResult, Parser};
use insta::assert_snapshot;
pub fn err_string<F, T>(test_name: &str, mut parse_fn: F, should_fail: bool, src: &str) -> String
where
F: FnMut(&mut Parser) -> ParseResult<T>,
T: std::fmt::Debug,
{
let mut files = fe_common::files::FileStore::new();
let id = files.add_file(test_name, src);
let mut parser = Parser::new(src, id);
if should_fail != parse_fn(&mut parser).is_err() {
panic!(
"expected parsing to {}fail",
if should_fail { "" } else { "not " }
);
}
diagnostics_string(&parser.diagnostics, &files)
}
macro_rules! test_parse_err {
($name:ident, $parse_fn:expr, $should_fail:expr, $src:expr) => {
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn $name() {
assert_snapshot!(err_string(stringify!($name), $parse_fn, $should_fail, $src));
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test::wasm_bindgen_test]
fn $name() {
let actual = err_string(stringify!($name), $parse_fn, $should_fail, $src);
let (_, expected) = include_str!(concat!(
"snapshots/cases__errors__",
stringify!($name),
".snap"
))
.rsplit_once("---\n")
.unwrap();
pretty_assertions::assert_eq!(actual.trim(), expected.trim());
}
};
}
// These tests use the insta crate. insta will automatically generate the
// snapshot file on the first run.
test_parse_err! { contract_invalid_version_requirement, module::parse_module, true, r#"
pragma 0.o
contract C:
pass
"#
}
test_parse_err! { contract_missing_version_requirement, module::parse_module, true, r#"
pragma
contract C:
pass
"#
}
test_parse_err! { contract_bad_name, contracts::parse_contract_def, true, "contract 1X:\n x: u8" }
test_parse_err! { contract_empty_body, module::parse_module, true, "contract X:\n \n \ncontract Y:\n x: u8" }
test_parse_err! { contract_field_after_def, module::parse_module, false, r#"
contract C:
def f():
pass
x: u8
"#
}
test_parse_err! { contract_pub_event, contracts::parse_contract_def, false, "contract C:\n pub event E:\n x: u8" }
test_parse_err! { contract_const_pub, contracts::parse_contract_def, false, "contract C:\n const pub x: u8" }
test_parse_err! { contract_const_fn, contracts::parse_contract_def, false, "contract C:\n const def f():\n pass" }
test_parse_err! { emit_no_args, functions::parse_stmt, true, "emit x" }
test_parse_err! { emit_expr, functions::parse_stmt, true, "emit x + 1" }
test_parse_err! { emit_bad_call, functions::parse_stmt, true, "emit MyEvent(1)()" }
test_parse_err! { expr_bad_prefix, expressions::parse_expr, true, "*x + 1" }
test_parse_err! { for_no_in, functions::parse_stmt, true, "for x:\n pass" }
test_parse_err! { fn_no_args, |par| functions::parse_fn_def(par, None), false, "def f:\n return 5" }
test_parse_err! { if_no_body, functions::parse_stmt, true, "if x:\nelse:\n x" }
test_parse_err! { import_bad_name, module::parse_simple_import, true, "import x as 123" }
test_parse_err! { module_bad_stmt, module::parse_module, true, "if x:\n y" }
test_parse_err! { module_nonsense, module::parse_module, true, "))" }
test_parse_err! { struct_bad_field_name, types::parse_struct_def, true, "struct f:\n pub def" }
test_parse_err! { stmt_vardecl_attr, functions::parse_stmt, true, "f.s : u" }
test_parse_err! { stmt_vardecl_tuple, functions::parse_stmt, true, "(a, x+1) : u256" }
test_parse_err! { stmt_vardecl_tuple_empty, functions::parse_stmt, true, "(a, ()) : u256" }
test_parse_err! { stmt_vardecl_subscript, functions::parse_stmt, true, "a[1] : u256" }
// assert_snapshot! doesn't like the invalid escape code
#[test]
fn string_invalid_escape() {
let err = err_string(
"string_invalid_escape",
expressions::parse_expr,
false,
r#""a string \c ""#,
);
assert_snapshot!(err);
}