Skip to content

Commit

Permalink
add cargo test for parsing syntax version
Browse files Browse the repository at this point in the history
Signed-off-by: Eval EXEC <execvy@gmail.com>
  • Loading branch information
eval-exec committed Jan 20, 2023
1 parent 84f30f0 commit 86682ea
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down
3 changes: 3 additions & 0 deletions tools/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
69 changes: 69 additions & 0 deletions tools/codegen/src/ast/raw/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 86682ea

Please sign in to comment.