diff --git a/Makefile b/Makefile index c9375ba..b59d27f 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ ci: @set -eu; \ export RUSTFLAGS='-D warnings'; \ make fmt clippy; \ - make ci-examples ci-crates; \ + make cargo-test ci-examples ci-crates; \ echo "Success!" RUST_DEV_PROJS = examples/ci-tests @@ -40,6 +40,15 @@ clippy: cd - > /dev/null; \ done +cargo-test: + @set -eu; \ + for dir in ${RUST_PROJS}; do \ + cd "$${dir}"; \ + cargo test; \ + cd - > /dev/null; \ + done + + ci-msrv: @set -eu; \ for dir in ${RUST_PROD_PROJS}; do \ diff --git a/tools/codegen/Cargo.toml b/tools/codegen/Cargo.toml index 4c1202c..09803c9 100644 --- a/tools/codegen/Cargo.toml +++ b/tools/codegen/Cargo.toml @@ -29,6 +29,9 @@ serde = { version = "1.0.118", features = ["derive", "rc"], optional = true } serde_json = { version = "1.0.61", optional = true } serde_yaml = { version = "0.8.15", optional = true } +[dev-dependencies] +tempfile = "3.3.0" + [features] default = [] compiler-plugin = ["serde", "serde_json", "serde_yaml"] diff --git a/tools/codegen/src/ast/raw/utils.rs b/tools/codegen/src/ast/raw/utils.rs index dd5e020..d0f93a1 100644 --- a/tools/codegen/src/ast/raw/utils.rs +++ b/tools/codegen/src/ast/raw/utils.rs @@ -289,3 +289,72 @@ impl parser::Parser { Ok(()) } } + +#[cfg(test)] +mod tests { + use super::{parser, utils, SyntaxVersion}; + use std::io::Write; + + #[test] + fn test_default_syntax_version_should_be_1_0() { + use utils::ParserUtils; + // get path of file + let mut schema_file = tempfile::NamedTempFile::new().unwrap(); + let _ = schema_file.write(b"array uint32 [byte; 4];").unwrap(); + schema_file.flush().unwrap(); + + let file = schema_file.into_temp_path(); + + let ast = parser::Parser::preprocess(&file).unwrap(); + assert_eq!(ast.syntax_version, Some(SyntaxVersion { version: 1 })); + } + + #[test] + fn test_parse_syntax_version() { + use utils::ParserUtils; + // get path of file + let mut schema_file = tempfile::NamedTempFile::new().unwrap(); + let test_version = SyntaxVersion { version: 7 }; + schema_file + .write_fmt(format_args!("syntax = {};", test_version.version)) + .unwrap(); + let _ = schema_file.write(b"array uint32 [byte; 4];").unwrap(); + schema_file.flush().unwrap(); + + let file = schema_file.into_temp_path(); + + let ast = parser::Parser::preprocess(&file).unwrap(); + assert_eq!(ast.syntax_version, Some(test_version)); + } + + #[test] + #[should_panic] + // if A `syntax = 1` schema file imports a `syntax = 2` schema file, it should panic + fn test_different_syntax_version_should_panic() { + use utils::ParserUtils; + + let mut child_schema_file = tempfile::NamedTempFile::new().unwrap(); + child_schema_file + .write_fmt(format_args!("syntax = 2;")) + .unwrap(); + let _ = child_schema_file.write(b"array uint64 [byte; 8];").unwrap(); + child_schema_file.flush().unwrap(); + + let child_file = child_schema_file.into_temp_path(); + let child_file_path = child_file.to_str().unwrap(); + + let mut root_schema_file = tempfile::NamedTempFile::new().unwrap(); + root_schema_file + .write_fmt(format_args!("syntax = 1;",)) + .unwrap(); + root_schema_file + .write_fmt(format_args!("import {:?}", child_file_path)) + .unwrap(); + let _ = root_schema_file.write(b"array uint32 [byte; 4];").unwrap(); + root_schema_file.flush().unwrap(); + + let file = root_schema_file.into_temp_path(); + + parser::Parser::preprocess(&file).unwrap(); + } +}