Skip to content

Commit fde4549

Browse files
committed
(tests) ref: standardise tests for lexer and parser
1 parent 7a28108 commit fde4549

File tree

4 files changed

+45
-87
lines changed

4 files changed

+45
-87
lines changed

tests/files.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
mod files {
2+
use std::fs;
3+
4+
use c_parser::*;
5+
6+
const PREFIX: &str = "./tests/data/";
7+
8+
#[expect(clippy::unwrap_used)]
9+
fn test_file(file: &str, parser_works: bool) {
10+
let path = format!("{PREFIX}{file}.c");
11+
let content = fs::read_to_string(&path).unwrap();
12+
let mut location = Location::from(path.clone());
13+
let files: &[(String, &str)] = &[(path, &content)];
14+
let tokens = lex_file(&content, &mut location).unwrap_or_display(files, "lexer");
15+
if parser_works {
16+
let _tree = parse_tokens(tokens).unwrap_or_display(files, "parser");
17+
}
18+
}
19+
20+
#[test]
21+
fn escape() {
22+
test_file("escape", false);
23+
}
24+
25+
#[test]
26+
fn general() {
27+
test_file("general", false);
28+
}
29+
30+
#[test]
31+
fn operators() {
32+
test_file("general", false);
33+
}
34+
35+
#[test]
36+
fn no_control_flow() {
37+
test_file("no-control-flow", true);
38+
}
39+
}

tests/lex_files.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/parse_files.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use c_parser::*;
22

3-
fn test_parser_on_string(content: &str, output: &str) {
3+
fn test_string(content: &str, output: &str) {
44
let files = &[(String::new(), content)];
55
let mut location = Location::from(String::new());
66
let tokens = lex_file(content, &mut location).unwrap_or_display(files, "lexer");
@@ -12,7 +12,7 @@ fn test_parser_on_string(content: &str, output: &str) {
1212
);
1313
}
1414

15-
// fn test_parser_error_on_string(content: &str, output: &str) {
15+
// fn test_string_error(content: &str, output: &str) {
1616
// let files = &[(String::new(), content)];
1717
// let mut location = Location::from(String::new());
1818
// let tokens = lex_file(content, &mut location).unwrap_or_display(files,
@@ -24,21 +24,21 @@ fn test_parser_on_string(content: &str, output: &str) {
2424
// );
2525
// }
2626

27-
macro_rules! test_string {
27+
macro_rules! make_string_tests {
2828
($($name:ident: $input:expr => $output:expr)*) => {
2929
mod parser_string {
3030
$(
3131
#[test]
3232
fn $name() {
33-
super::test_parser_on_string($input, $output)
33+
super::test_string($input, $output)
3434
}
3535
)*
3636
}
3737

3838
};
3939
}
4040

41-
test_string!(
41+
make_string_tests!(
4242

4343
unary_binary:
4444
"a + b * c - d / e % f + g - h * i + j % k * l ^ !m++ & n | o || p && q"
@@ -138,7 +138,7 @@ keywords_attributes_functions:
138138
"[((int main)°()), [(const int volatile static short thread_local y), (static_assert°((((sizeof°((x = 2))) + 1) == 2))), \u{2205} ]..]"
139139
);
140140

141-
// macro_rules! test_string_error {
141+
// macro_rules! make_string_error_tests {
142142
// ($($name:ident: $input:expr => $output:expr)*) => {
143143
// mod parser_string_error {
144144
// $(
@@ -151,5 +151,3 @@ keywords_attributes_functions:
151151

152152
// };
153153
// }
154-
155-
// test_string_error!();

0 commit comments

Comments
 (0)