forked from slint-ui/slint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_tests.rs
319 lines (278 loc) · 9.72 KB
/
syntax_tests.rs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright © SixtyFPS GmbH <[email protected]>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
//! This test is trying to compile all the *.slint files in the sub directories and check that compilation
//! errors are properly reported
//!
//! The .slint files can have comments like this:
//! ```ignore
//! hi ho
//! // ^error{some_regexp}
//! ```
//!
//! Meaning that there must an error following with an error message for that regular expression in the position
//! on the line above at the column pointed by the caret.
//! If there are two carets: ` ^^error{some_regexp}` then it means two line above, and so on with more carets.
//! `^warning{regexp}` is also supported.
use i_slint_compiler::ComponentSelection;
use std::path::{Path, PathBuf};
#[test]
fn syntax_tests() -> std::io::Result<()> {
use rayon::prelude::*;
if let Some(specific_test) = std::env::args()
.skip(1)
.skip_while(|arg| arg.starts_with("--") || arg == "syntax_tests")
.next()
{
let mut path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push(specific_test);
assert!(process_file(&path)?);
return Ok(());
}
let mut test_entries = Vec::new();
for entry in std::fs::read_dir(format!("{}/tests/syntax", env!("CARGO_MANIFEST_DIR")))? {
let entry = entry?;
if entry.file_type().map_or(false, |f| f.is_dir()) {
let path = entry.path();
for test_entry in path.read_dir()? {
let test_entry = test_entry?;
let path = test_entry.path();
if let Some(ext) = path.extension() {
if ext == "60" || ext == "slint" {
test_entries.push(path);
}
}
}
}
}
let success = test_entries
.par_iter()
.try_fold(
|| true,
|mut success, path| {
success &= process_file(path)?;
Ok::<bool, std::io::Error>(success)
},
)
.try_reduce(|| true, |success, result| Ok(success & result))?;
assert!(success);
Ok(())
}
fn process_file(path: &std::path::Path) -> std::io::Result<bool> {
let source = std::fs::read_to_string(path)?;
std::panic::catch_unwind(|| process_file_source(path, source, false)).unwrap_or_else(|err| {
println!("Panic while processing {}: {:?}", path.display(), err);
Ok(false)
})
}
fn process_diagnostics(
compile_diagnostics: &i_slint_compiler::diagnostics::BuildDiagnostics,
path: &Path,
source: &str,
_silent: bool,
) -> std::io::Result<bool> {
let mut success = true;
let path = canonical(path);
let mut diags = compile_diagnostics
.iter()
.filter(|d| {
canonical(
d.source_file()
.unwrap_or_else(|| panic!("{path:?}: Error without a source file {d:?}",)),
) == path
})
.collect::<Vec<_>>();
let lines = source
.bytes()
.enumerate()
.filter_map(|(i, c)| if c == b'\n' { Some(i) } else { None })
.collect::<Vec<usize>>();
// Find expected errors in the file. The first caret (^) points to the expected column. The number of
// carets refers to the number of lines to go back. This is useful when one line of code produces multiple
// errors or warnings.
let re = regex::Regex::new(r"\n *//[^\n\^]*(\^+)(error|warning)\{([^\n]*)\}").unwrap();
for m in re.captures_iter(source) {
let line_begin_offset = m.get(0).unwrap().start();
let column = m.get(1).unwrap().start() - line_begin_offset;
let lines_to_source = m.get(1).unwrap().as_str().len();
let warning_or_error = m.get(2).unwrap().as_str();
let rx = m.get(3).unwrap().as_str();
let r = match regex::Regex::new(rx) {
Err(e) => {
eprintln!("{:?}: Invalid regexp {:?} : {:?}", path, rx, e);
return Ok(false);
}
Ok(r) => r,
};
let mut line_counter = 0;
let mut line_offset = source[..line_begin_offset].rfind('\n').unwrap_or(0);
let offset = loop {
line_counter += 1;
if line_counter >= lines_to_source {
break line_offset + column;
}
if let Some(o) = source[..line_offset].rfind('\n') {
line_offset = o;
} else {
break 1;
};
};
let expected_diag_level = match warning_or_error {
"warning" => i_slint_compiler::diagnostics::DiagnosticLevel::Warning,
"error" => i_slint_compiler::diagnostics::DiagnosticLevel::Error,
_ => panic!("Unsupported diagnostic level {}", warning_or_error),
};
match diags.iter().position(|e| {
let (l, c) = e.line_column();
let o = lines.get(l.wrapping_sub(2)).unwrap_or(&0) + c;
o == offset && r.is_match(e.message()) && e.level() == expected_diag_level
}) {
Some(idx) => {
diags.remove(idx);
}
None => {
success = false;
println!(
"{:?}: {} not found at offset {}: {:?}",
path, warning_or_error, offset, rx
);
}
}
}
// Ignore deprecated warning about old syntax, because our tests still use the old syntax a lot
diags.retain(|d| !(d.message().contains("':='") && d.message().contains("deprecated")));
if !diags.is_empty() {
println!("{:?}: Unexpected errors/warnings: {:#?}", path, diags);
#[cfg(feature = "display-diagnostics")]
if !_silent {
let mut to_report = i_slint_compiler::diagnostics::BuildDiagnostics::default();
for d in diags {
to_report.push_compiler_error(d.clone());
}
to_report.print();
}
success = false;
}
Ok(success)
}
fn canonical(path: &Path) -> PathBuf {
path.canonicalize().unwrap_or_else(|_| path.to_owned())
}
fn process_file_source(
path: &std::path::Path,
source: String,
silent: bool,
) -> std::io::Result<bool> {
let mut parse_diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
let syntax_node =
i_slint_compiler::parser::parse(source.clone(), Some(path), &mut parse_diagnostics);
let has_parse_error = parse_diagnostics.has_errors();
let mut compiler_config = i_slint_compiler::CompilerConfiguration::new(
i_slint_compiler::generator::OutputFormat::Interpreter,
);
compiler_config.embed_resources = i_slint_compiler::EmbedResourcesKind::OnlyBuiltinResources;
compiler_config.enable_experimental = true;
compiler_config.style = Some("fluent".into());
compiler_config.components_to_generate =
if source.contains("config:generate_all_exported_windows") {
ComponentSelection::ExportedWindows
} else {
// Otherwise we'd have lots of warnings about not inheriting Window
ComponentSelection::LastExported
};
let compile_diagnostics = if !parse_diagnostics.has_errors() {
let (_, build_diags, _) = spin_on::spin_on(i_slint_compiler::compile_syntax_node(
syntax_node.clone(),
parse_diagnostics,
compiler_config.clone(),
));
build_diags
} else {
parse_diagnostics
};
let mut success = true;
success &= process_diagnostics(&compile_diagnostics, path, &source, silent)?;
for p in &compile_diagnostics.all_loaded_files {
let source = if p.is_absolute() {
std::fs::read_to_string(p)?
} else {
// probably std-widgets.slint
String::new()
};
success &= process_diagnostics(&compile_diagnostics, p, &source, silent)?;
}
if has_parse_error {
// Still try to compile to make sure it doesn't panic
spin_on::spin_on(i_slint_compiler::compile_syntax_node(
syntax_node,
compile_diagnostics,
compiler_config,
));
}
Ok(success)
}
#[test]
/// Test that this actually fail when it should
fn self_test() -> std::io::Result<()> {
let fake_path = std::path::Path::new("fake.slint");
let process = |str: &str| process_file_source(fake_path, str.into(), true);
// this should succeed
assert!(process(
r#"
export Foo := Rectangle { x: 0px; }
"#
)?);
// unless we expected an error
assert!(!process(
r#"
export Foo := Rectangle { x: 0px; }
// ^error{i want an error}
"#
)?);
// An error should fail
assert!(!process(
r#"
export Foo := Rectangle foo { x:0px; }
"#
)?);
// An error with the proper comment should pass
assert!(process(
r#"
Foo := Rectangle foo { x:0px; }
// ^error{expected '\{'}
"#
)?);
// But not if it is at the wrong position
assert!(!process(
r#"
Foo := Rectangle foo { x:0px; }
// ^error{expected '\{'}
"#
)?);
// or the wrong line
assert!(!process(
r#"
Foo := Rectangle foo { x:0px; }
// ^error{expected '\{'}
"#
)?);
// or the wrong message
assert!(!process(
r#"
Foo := Rectangle foo { x:0px; }
// ^error{foo_bar}
"#
)?);
// or the wrong line because two carets
assert!(!process(
r#"
Foo := Rectangle foo { x:0px; }
// ^^error{expected '\{'}
"#
)?);
// Even on windows, it should work
assert!(process(
"\r\nFoo := Rectangle foo { x:0px; }\r\n// ^error{expected '\\{'}\r\n"
)?);
Ok(())
}