Skip to content

Commit

Permalink
Use common generated VERSION_BREAKS for all generated test suites
Browse files Browse the repository at this point in the history
  • Loading branch information
ggiraldez committed Jul 17, 2024
1 parent 949fdb7 commit 4d2fce7
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 169 deletions.
9 changes: 2 additions & 7 deletions crates/codegen/testing/src/bindings_assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,18 @@ use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

use anyhow::{bail, Result};
use codegen_language_definition::model::Language;
use inflector::Inflector;
use infra_utils::codegen::CodegenFileSystem;
use infra_utils::paths::FileWalker;

use crate::common::{generate_mod_file, generate_unit_test_file};

pub fn generate_bindings_assertions_tests(
language: &Language,
data_dir: &Path,
output_dir: &Path,
) -> Result<()> {
pub fn generate_bindings_assertions_tests(data_dir: &Path, output_dir: &Path) -> Result<()> {
let tests = collect_assertion_tests(data_dir)?;

let mut fs = CodegenFileSystem::new(data_dir)?;

generate_mod_file(language, &mut fs, &output_dir.join("mod.rs"), &tests)?;
generate_mod_file(&mut fs, &output_dir.join("mod.rs"), &tests)?;

for (group_name, test_files) in &tests {
generate_unit_test_file(
Expand Down
9 changes: 2 additions & 7 deletions crates/codegen/testing/src/bindings_output.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use std::path::Path;

use anyhow::Result;
use codegen_language_definition::model::Language;
use inflector::Inflector;
use infra_utils::codegen::CodegenFileSystem;

use crate::common::{collect_snapshot_tests, generate_mod_file, generate_unit_test_file};

pub fn generate_bindings_output_tests(
language: &Language,
data_dir: &Path,
output_dir: &Path,
) -> Result<()> {
pub fn generate_bindings_output_tests(data_dir: &Path, output_dir: &Path) -> Result<()> {
let tests = collect_snapshot_tests(data_dir)?;

let mut fs = CodegenFileSystem::new(data_dir)?;

generate_mod_file(language, &mut fs, &output_dir.join("mod.rs"), &tests)?;
generate_mod_file(&mut fs, &output_dir.join("mod.rs"), &tests)?;

for (group_name, test_names) in &tests {
generate_unit_test_file(
Expand Down
35 changes: 4 additions & 31 deletions crates/codegen/testing/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,44 +50,17 @@ pub(crate) fn collect_snapshot_tests(
}

pub(crate) fn generate_mod_file(
language: &Language,
fs: &mut CodegenFileSystem,
mod_file_path: &Path,
parser_tests: &BTreeMap<String, BTreeSet<String>>,
) -> Result<()> {
let module_declarations_str =
parser_tests
.keys()
.fold(String::new(), |mut buffer, parser_name| {
writeln!(buffer, "mod {0};", parser_name.to_snake_case()).unwrap();
buffer
});

let version_breaks = language.collect_breaking_versions();
let version_breaks_len = version_breaks.len();
let version_breaks_str = version_breaks
.iter()
.fold(String::new(), |mut buffer, version| {
writeln!(
buffer,
"Version::new({}, {}, {}),",
version.major, version.minor, version.patch
)
.unwrap();
let contents = parser_tests
.keys()
.fold(String::new(), |mut buffer, parser_name| {
writeln!(buffer, "mod {0};", parser_name.to_snake_case()).unwrap();
buffer
});

let contents = format!(
"
use semver::Version;
{module_declarations_str}
pub const VERSION_BREAKS: [Version; {version_breaks_len}] = [
{version_breaks_str}
];
",
);

fs.write_file(mod_file_path, contents)
}

Expand Down
9 changes: 2 additions & 7 deletions crates/codegen/testing/src/cst_output.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use std::path::Path;

use anyhow::Result;
use codegen_language_definition::model::Language;
use inflector::Inflector;
use infra_utils::codegen::CodegenFileSystem;

use crate::common::{collect_snapshot_tests, generate_mod_file, generate_unit_test_file};

pub fn generate_cst_output_tests(
language: &Language,
data_dir: &Path,
output_dir: &Path,
) -> Result<()> {
pub fn generate_cst_output_tests(data_dir: &Path, output_dir: &Path) -> Result<()> {
let parser_tests = collect_snapshot_tests(data_dir)?;

let mut fs = CodegenFileSystem::new(data_dir)?;

generate_mod_file(language, &mut fs, &output_dir.join("mod.rs"), &parser_tests)?;
generate_mod_file(&mut fs, &output_dir.join("mod.rs"), &parser_tests)?;

for (parser_name, test_names) in &parser_tests {
generate_unit_test_file(
Expand Down
6 changes: 3 additions & 3 deletions crates/codegen/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ impl TestingGeneratorExtensions for Language {
generate_version_breaks(self, lang_def_dir, output_dir)
}
fn generate_bindings_assertions_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> {
generate_bindings_assertions_tests(self, data_dir, output_dir)
generate_bindings_assertions_tests(data_dir, output_dir)
}
fn generate_bindings_output_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> {
generate_bindings_output_tests(self, data_dir, output_dir)
generate_bindings_output_tests(data_dir, output_dir)
}
fn generate_cst_output_tests(&self, data_dir: &Path, output_dir: &Path) -> Result<()> {
generate_cst_output_tests(self, data_dir, output_dir)
generate_cst_output_tests(data_dir, output_dir)
}
}

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

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use slang_solidity::language::Language;
use slang_solidity::{bindings, diagnostic};

use super::assertions::{check_assertions, collect_assertions};
use super::generated::VERSION_BREAKS;
use crate::generated::VERSION_BREAKS;

pub fn run(group_name: &str, test_name: &str) -> Result<()> {
let file_name = format!("{test_name}.sol");
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use slang_solidity::cst::KindTypes;
use slang_solidity::language::Language;
use slang_solidity::parse_output::ParseOutput;

use super::generated::VERSION_BREAKS;
use crate::generated::VERSION_BREAKS;

pub fn run(group_name: &str, test_name: &str) -> Result<()> {
let test_dir = CargoWorkspace::locate_source_crate("solidity_testing_snapshots")?
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use slang_solidity::kinds::NonterminalKind;
use slang_solidity::language::Language;
use strum_macros::Display;

use crate::cst_output::generated::VERSION_BREAKS;
use crate::cst_output::renderer::render;
use crate::generated::VERSION_BREAKS;

#[derive(Display)]
#[strum(serialize_all = "kebab_case")]
Expand Down
1 change: 1 addition & 0 deletions crates/solidity/outputs/cargo/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ mod bindings_output;
mod bindings_rules;
mod cst_output;
mod doc_examples;
mod generated;
mod trivia;

0 comments on commit 4d2fce7

Please sign in to comment.