From 27a349abdbeddda3e2972eb1c97e9aed18f12896 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Tue, 14 Feb 2023 07:17:51 +0800 Subject: [PATCH] add compatibility test for custom union ID feature Signed-off-by: Eval EXEC --- Makefile | 2 +- tests/.gitignore | 2 + tests/Cargo.toml | 18 +++++++ tests/build.rs | 66 ++++++++++++++++++++++++ tests/src/main.rs | 3 ++ tests/src/union_compatibility_test.rs | 73 +++++++++++++++++++++++++++ tests/union_foo_0_7_3.mol | 11 ++++ tests/union_foo_with_custom_id.mol | 7 +++ 8 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 tests/.gitignore create mode 100644 tests/Cargo.toml create mode 100644 tests/build.rs create mode 100644 tests/src/main.rs create mode 100644 tests/src/union_compatibility_test.rs create mode 100644 tests/union_foo_0_7_3.mol create mode 100644 tests/union_foo_with_custom_id.mol diff --git a/Makefile b/Makefile index c9375ba..8e39093 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ ci: make ci-examples ci-crates; \ echo "Success!" -RUST_DEV_PROJS = examples/ci-tests +RUST_DEV_PROJS = examples/ci-tests tests RUST_PROD_PROJS = bindings/rust tools/codegen tools/compiler RUST_PROJS = ${RUST_DEV_PROJS} ${RUST_PROD_PROJS} C_PROJS = examples/ci-tests diff --git a/tests/.gitignore b/tests/.gitignore new file mode 100644 index 0000000..a9d37c5 --- /dev/null +++ b/tests/.gitignore @@ -0,0 +1,2 @@ +target +Cargo.lock diff --git a/tests/Cargo.toml b/tests/Cargo.toml new file mode 100644 index 0000000..e3e8695 --- /dev/null +++ b/tests/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "tests" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[dev-dependencies] +codegen-0_7_3 = {package = "molecule-codegen", version = "0.7.3", features = ["compiler-plugin"]} +codegen-dev = {package = "molecule-codegen", path = "../tools/codegen", features = ["compiler-plugin"]} +molecule = "0.7.3" + +[build-dependencies] +codegen-0_7_3 = {package = "molecule-codegen", version = "0.7.3", features = ["compiler-plugin"]} +codegen-dev = {package = "molecule-codegen", path = "../tools/codegen", features = ["compiler-plugin"]} +molecule = "0.7.3" diff --git a/tests/build.rs b/tests/build.rs new file mode 100644 index 0000000..e332dae --- /dev/null +++ b/tests/build.rs @@ -0,0 +1,66 @@ +fn compile_schema_0_7_3(schema: &str) { + let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("0_7_3"); + std::fs::create_dir_all(&out_dir).unwrap(); + + let mut compiler = codegen_0_7_3::Compiler::new(); + compiler + .input_schema_file(schema) + .generate_code(codegen_0_7_3::Language::Rust) + .output_dir(out_dir) + .run() + .unwrap(); + println!("cargo:rerun-if-changed={}", schema); +} + +fn compile_schema_dev(schema: &str) { + let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("dev"); + std::fs::create_dir_all(&out_dir).unwrap(); + + let mut compiler = codegen_dev::Compiler::new(); + compiler + .input_schema_file(schema) + .generate_code(codegen_dev::Language::Rust) + .output_dir(out_dir) + .run() + .unwrap(); + println!("cargo:rerun-if-changed={}", schema); +} + +fn compile_intermediate_0_7_3(schema: &str) { + let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("0_7_3"); + std::fs::create_dir_all(&out_dir).unwrap(); + + let mut compiler = codegen_0_7_3::Compiler::new(); + compiler + .input_schema_file(schema) + .generate_intermediate(codegen_0_7_3::IntermediateFormat::JSON) + .output_dir(out_dir) + .run() + .unwrap(); + println!("cargo:rerun-if-changed={}", schema); +} + +fn compile_intermediate_dev(schema: &str) { + let out_dir = std::path::PathBuf::from(&std::env::var("OUT_DIR").unwrap()).join("dev"); + std::fs::create_dir_all(&out_dir).unwrap(); + + let mut compiler = codegen_dev::Compiler::new(); + compiler + .input_schema_file(schema) + .generate_intermediate(codegen_dev::IntermediateFormat::JSON) + .output_dir(out_dir) + .run() + .unwrap(); + println!("cargo:rerun-if-changed={}", schema); +} + +fn main() { + println!("cargo:rerun-if-changed=./union_foo_0_7_3.mol"); + println!("cargo:rerun-if-changed=./union_foo_with_custom_id.mol"); + + compile_intermediate_0_7_3("./union_foo_0_7_3.mol"); + compile_intermediate_dev("./union_foo_with_custom_id.mol"); + + compile_schema_0_7_3("./union_foo_0_7_3.mol"); + compile_schema_dev("./union_foo_with_custom_id.mol"); +} diff --git a/tests/src/main.rs b/tests/src/main.rs new file mode 100644 index 0000000..fd6491b --- /dev/null +++ b/tests/src/main.rs @@ -0,0 +1,3 @@ +mod union_compatibility_test; + +fn main() {} diff --git a/tests/src/union_compatibility_test.rs b/tests/src/union_compatibility_test.rs new file mode 100644 index 0000000..ad6b0d3 --- /dev/null +++ b/tests/src/union_compatibility_test.rs @@ -0,0 +1,73 @@ +#[cfg(test)] +mod tests { + use molecule::prelude::*; + + static UNION_FOO_0_7_3_JSON_INTERMEDIATE: &str = + include_str!(concat!(env!("OUT_DIR"), "/0_7_3/union_foo_0_7_3.json")); + + static UNION_FOO_DEV_JSON_INTERMEDIATE: &str = include_str!(concat!( + env!("OUT_DIR"), + "/dev/union_foo_with_custom_id.json" + )); + + #[test] + fn test_recover_0_7_3_intermediate_by_current_ir_recover() { + let format = codegen_dev::IntermediateFormat::JSON; + let ast_result = format.recover(UNION_FOO_0_7_3_JSON_INTERMEDIATE.as_bytes()); + assert!(ast_result.is_ok()); + } + + #[test] + fn test_recover_ir() { + let format = codegen_dev::IntermediateFormat::JSON; + let ast_result = format.recover(UNION_FOO_DEV_JSON_INTERMEDIATE.as_bytes()); + assert!(ast_result.is_ok()); + } + + mod union_foo_0_7_3 { + #![allow(clippy::all, dead_code)] + include!(concat!(env!("OUT_DIR"), "/0_7_3/union_foo_0_7_3.rs")); + } + + mod union_foo_dev { + #![allow(clippy::all, dead_code)] + include!(concat!(env!("OUT_DIR"), "/dev/union_foo_with_custom_id.rs")); + } + + #[test] + fn test_decode_0_7_3_generated_rust_bytes_by_current_version() { + let a2_0_7_3 = union_foo_0_7_3::A2::new_builder() + .nth0(Byte::from(17)) + .build(); + + let foo_0_7_3 = union_foo_0_7_3::Foo::new_builder() + .set(a2_0_7_3.clone()) + .build(); + let foo_0_7_3_slice = foo_0_7_3.as_slice(); + + let foo_dev_result = union_foo_dev::FooOnlyReserveA2AndA3::from_slice(foo_0_7_3_slice); + assert!(foo_dev_result.is_ok()); + let foo_dev = foo_dev_result.unwrap(); + + let foo_union_dev = foo_dev.to_enum(); + + if let union_foo_dev::FooOnlyReserveA2AndA3Union::A2(a2_dev) = foo_union_dev { + assert_eq!(a2_0_7_3.as_slice(), a2_dev.as_slice()); + } else { + panic!("foo_union_dev should be A2"); + } + } + + #[test] + fn test_decode_0_7_3_generated_deprecated_rust_bytes_by_current_version() { + let a0_0_7_3 = union_foo_0_7_3::A0::new_builder() + .nth0(Byte::from(133)) + .build(); + + let foo_0_7_3 = union_foo_0_7_3::Foo::new_builder().set(a0_0_7_3).build(); + let foo_0_7_3_slice = foo_0_7_3.as_slice(); + + let foo_dev_result = union_foo_dev::FooOnlyReserveA2AndA3::from_slice(foo_0_7_3_slice); + assert!(foo_dev_result.is_err()); + } +} diff --git a/tests/union_foo_0_7_3.mol b/tests/union_foo_0_7_3.mol new file mode 100644 index 0000000..332eea0 --- /dev/null +++ b/tests/union_foo_0_7_3.mol @@ -0,0 +1,11 @@ +array a0 [byte;1]; +array a1 [byte;2]; +array a2 [byte;3]; +array a3 [byte;4]; + +union Foo { + a0, + a1, + a2, + a3, +} diff --git a/tests/union_foo_with_custom_id.mol b/tests/union_foo_with_custom_id.mol new file mode 100644 index 0000000..2482e47 --- /dev/null +++ b/tests/union_foo_with_custom_id.mol @@ -0,0 +1,7 @@ +array a2 [byte;3]; +array a3 [byte;4]; + +union Foo_Only_Reserve_a2_and_a3{ + a2 : 2, + a3 : 3, +}