Skip to content

Commit

Permalink
add compatibility test for custom union ID feature
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 Feb 17, 2023
1 parent 8e5cfad commit 27a349a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
Cargo.lock
18 changes: 18 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
66 changes: 66 additions & 0 deletions tests/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
3 changes: 3 additions & 0 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod union_compatibility_test;

fn main() {}
73 changes: 73 additions & 0 deletions tests/src/union_compatibility_test.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
11 changes: 11 additions & 0 deletions tests/union_foo_0_7_3.mol
Original file line number Diff line number Diff line change
@@ -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,
}
7 changes: 7 additions & 0 deletions tests/union_foo_with_custom_id.mol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
array a2 [byte;3];
array a3 [byte;4];

union Foo_Only_Reserve_a2_and_a3{
a2 : 2,
a3 : 3,
}

0 comments on commit 27a349a

Please sign in to comment.