Skip to content

Commit

Permalink
rust-rewrite: Moved test gating to features + insta testing.
Browse files Browse the repository at this point in the history
Added optional insta testing to be used during development. Snapshots
are not commited.
  • Loading branch information
schilkp committed Jul 3, 2024
1 parent aee9f94 commit dc4ff0d
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ jobs:
toolchain: stable
- uses: carlosperate/arm-none-eabi-gcc-action@v1
- name: Run generator tests.
run: REGINALD_TEST_C_FUNCPACK_ADDITIONAL_COMPILERS=arm-none-eabi-gcc cargo test --workspace -- --ignored --show-output
run: REGINALD_TEST_C_FUNCPACK_ADDITIONAL_COMPILERS=arm-none-eabi-gcc cargo test --workspace --features=test_gen_output -- --show-output
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions reginald_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ path = "src/main.rs"
required-features = ["cli"]

[features]
default = ["cli"]
cli = ["dep:clap", "dep:clap_complete", "dep:similar", "dep:console"]
default = ["cli"]
cli = ["dep:clap", "dep:clap_complete", "dep:similar", "dep:console"]
test_gen_output = []
test_insta = []

[dependencies]
reginald_utils = { path = "../reginald_utils", features = ["clap"] }
Expand All @@ -30,3 +32,4 @@ console = { version = "0.15.8", features = [], optional = true }
[dev-dependencies]
tempfile = "3.10.1"
pretty_assertions = "1.4.0"
insta = "1.39.0"
20 changes: 16 additions & 4 deletions reginald_codegen/src/builtin/c/funcpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,15 @@ fn generate_header(out: &mut dyn Write, inp: &Input) -> Result<(), Error> {
writeln!(out, "// clang-format off")?;
}

let output_file_name = inp
.output_file
.file_name()
.ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))?
.to_string_lossy();

// Doxy file comment:
writeln!(out, "/**")?;
writeln!(out, " * @file {}", inp.output_file.to_string_lossy())?;
writeln!(out, " * @file {output_file_name}")?;
writeln!(out, " * @brief {}", inp.map.name)?;
if let Some(input_file) = &inp.map.from_file {
writeln!(
Expand Down Expand Up @@ -288,8 +294,8 @@ fn generate_header(out: &mut dyn Write, inp: &Input) -> Result<(), Error> {

// Include guard
if inp.opts.include_guards {
writeln!(out, "#ifndef REGINALD_{}", c_macro(&inp.output_file.to_string_lossy()))?;
writeln!(out, "#define REGINALD_{}", c_macro(&inp.output_file.to_string_lossy()))?;
writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file_name))?;
writeln!(out, "#define REGINALD_{}", c_macro(&output_file_name))?;
}

// Includes
Expand All @@ -311,8 +317,14 @@ fn generate_footer(out: &mut dyn Write, inp: &Input) -> Result<(), Error> {
// Include guard:
writeln!(out)?;

let output_file_name = inp
.output_file
.file_name()
.ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))?
.to_string_lossy();

if inp.opts.include_guards {
writeln!(out, "#endif /* REGINALD_{} */", c_macro(&inp.output_file.to_string_lossy()))?;
writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file_name))?;
}

// Clang format:
Expand Down
19 changes: 15 additions & 4 deletions reginald_codegen/src/builtin/c/macromap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ fn generate_header(
writeln!(out, "// clang-format off")?;
}

let output_file_name = output_file
.file_name()
.ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))?
.to_string_lossy();

writeln!(out, "/**")?;
writeln!(out, " * @file {}", output_file.to_string_lossy())?;
writeln!(out, " * @file {output_file_name}")?;
writeln!(out, " * @brief {}", map.name)?;
if let Some(input_file) = &map.from_file {
writeln!(
Expand All @@ -103,8 +108,8 @@ fn generate_header(
}
}
writeln!(out, " */")?;
writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file.to_string_lossy()))?;
writeln!(out, "#define REGINALD_{}", c_macro(&output_file.to_string_lossy()))?;
writeln!(out, "#ifndef REGINALD_{}", c_macro(&output_file_name))?;
writeln!(out, "#define REGINALD_{}", c_macro(&output_file_name))?;
writeln!(out)?;
writeln!(out, "#include <stdint.h>")?;
for include in &opts.add_include {
Expand Down Expand Up @@ -318,7 +323,13 @@ fn generate_layout_defines(out: &mut dyn Write, map: &RegisterMap, layout: &Layo

fn generate_footer(out: &mut dyn Write, output_file: &Path, opts: &GeneratorOpts) -> Result<(), Error> {
writeln!(out)?;
writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file.to_string_lossy()))?;

let output_file_name = output_file
.file_name()
.ok_or(Error::GeneratorError("Failed to extract file name from output path!".to_string()))?
.to_string_lossy();

writeln!(out, "#endif /* REGINALD_{} */", c_macro(&output_file_name))?;

if opts.clang_format_guard {
writeln!(out, "// clang-format on")?;
Expand Down
1 change: 1 addition & 0 deletions reginald_codegen/tests/generator_c_funcpack/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
artifacts/
.cache/
compile_commands.json
snapshots/
33 changes: 27 additions & 6 deletions reginald_codegen/tests/generator_c_funcpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ fn run_reginald(d: &TempDir, output_name: &str, opts: GeneratorOpts) {
// ==== Tests ==================================================================

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_c99() {
let d = tempdir().unwrap();

Expand All @@ -155,7 +155,7 @@ fn generator_c_funcpack_c99() {
}

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_c11() {
let d = tempdir().unwrap();

Expand All @@ -173,7 +173,28 @@ fn generator_c_funcpack_c11() {
}

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_insta"), ignore)]
fn generator_c_funcpack_c11_insta() {
let d = tempdir().unwrap();

run_reginald(
&d,
"out.h",
GeneratorOpts {
..GeneratorOpts::default()
},
);

test_generated_code(&d, &["-std=c11"], &[]);

let file = fs::read_to_string(d.path().join("out.h")).unwrap();
insta::assert_snapshot!(file);

finish_test(d);
}

#[test]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_defer_to_le() {
let d = tempdir().unwrap();

Expand All @@ -192,7 +213,7 @@ fn generator_c_funcpack_defer_to_le() {
}

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_defer_to_be() {
let d = tempdir().unwrap();

Expand All @@ -211,7 +232,7 @@ fn generator_c_funcpack_defer_to_be() {
}

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_split_header_source() {
let d = tempdir().unwrap();

Expand Down Expand Up @@ -243,7 +264,7 @@ fn generator_c_funcpack_split_header_source() {
}

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_funcpack_split_headers() {
let d = tempdir().unwrap();

Expand Down
1 change: 1 addition & 0 deletions reginald_codegen/tests/generator_c_macromap/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
artifacts/
snapshots/
25 changes: 24 additions & 1 deletion reginald_codegen/tests/generator_c_macromap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ fn finish_test(d: TempDir) {
// ==== Tests ==================================================================

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_c_macromap_c99() {
let d = tempdir().unwrap();

Expand All @@ -105,3 +105,26 @@ fn generator_c_macromap_c99() {

finish_test(d);
}

#[test]
#[cfg_attr(not(feature = "test_insta"), ignore)]
fn generator_c_macromap_c99_insta() {
let d = tempdir().unwrap();

gen::cmd(gen::Command {
input: TEST_MAP_FILE.to_owned(),
output: d.path().to_owned().join(PathBuf::from("out.h")),
overwrite_map_name: None,
verify: false,
generator: Generator::CMacromap(GeneratorOpts {
clang_format_guard: true,
add_include: vec![],
}),
})
.unwrap();

let file = fs::read_to_string(d.path().join("out.h")).unwrap();
insta::assert_snapshot!(file);

finish_test(d);
}
2 changes: 1 addition & 1 deletion reginald_codegen/tests/generator_rs_structs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn run_reginald(output_name: &str, opts: GeneratorOpts) {
// ==== Tests ==================================================================

#[test]
#[ignore]
#[cfg_attr(not(feature = "test_gen_output"), ignore)]
fn generator_rs_structs() {
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let test_proj = manifest_dir.join(PathBuf::from("tests/generator_rs_structs/test_proj/"));
Expand Down

0 comments on commit dc4ff0d

Please sign in to comment.