feat(testing-framework): prevalidate AIR script (#365)

Try to parse the annotated AIR script with the standard parser to catch
to catch errors early with better error messages.

We do it in the `TestExecutor` only to make testing framwork parser
tests simplier.  For user experience, it doesn't matter.
This commit is contained in:
Ivan Boldyrev 2022-10-12 09:44:59 +03:00 committed by GitHub
parent a60b61e1a1
commit 22fac9329e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

1
Cargo.lock generated
View File

@ -184,6 +184,7 @@ dependencies = [
name = "air-testing-framework"
version = "0.1.0"
dependencies = [
"air-parser",
"air-test-utils",
"itertools",
"maplit",

View File

@ -15,6 +15,7 @@ path = "src/lib.rs"
[dependencies]
air-test-utils = { path = "../air-lib/test-utils" }
air-parser = { path = "../air-lib/air-parser" }
itertools = "0.10.4"
strum = { version="0.24.1", features=["derive"] }

View File

@ -42,6 +42,9 @@ impl TestExecutor {
extra_peers: impl IntoIterator<Item = PeerId>,
annotated_air_script: &str,
) -> Result<Self, String> {
// validate the AIR script with the standard parser first
air_parser::parse(annotated_air_script)?;
let mut sexp = Sexp::from_str(annotated_air_script)?;
let mut walker = Transformer::new();
walker.transform(&mut sexp);
@ -504,4 +507,26 @@ mod tests {
ExecutionTrace::from(vec![scalar_number(1), request_sent_by("peer1"),]),
)
}
#[test]
fn test_invalid_air() {
let res = TestExecutor::new(
TestRunParameters::from_init_peer_id("init_peer_id"),
vec![],
std::iter::empty(),
r#"(seq
(call "peer1" ("service" "func") [1 22] arg) ; behaviour=echo
)
"#,
);
assert!(res.is_err());
// TestExecutor doesn't implement Debug, so we have to unpack the error this way:
if let Err(err) = res {
assert_eq!(
err,
"error: \n ┌─ script.air:3:1\n\n3 │ )\n │ ^ expected \"(\"\n\n"
);
}
}
}