Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore 'skip' tests #23

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ serde_json = "1.0.117"
[dev-dependencies]
test-case = "3.3.1"
test-generator = "0.3.1"

[build-dependencies]
glob = "0.3"
72 changes: 70 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
use glob::glob;
use std::env;
use std::fs;
use std::io::Write;
use std::path::Path;

fn main() {
println!("cargo:rerun-if-changed=tests/testsuite/groups/*/*.json");
println!("cargo:rerun-if-changed=tests/testsuite/groups");
let out_dir = env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("generated_tests.rs");
let mut file = fs::File::create(dest_path).unwrap();

let resources = get_test_resources("tests/testsuite/**/*.json");

for resource in resources {
if resource.contains("/skip/") {
writeln!(
file,
r#"
#[test]
#[ignore]
fn test_{}() {{
test_case("{}");
}}
"#,
sanitize_filename(&resource),
resource
)
.unwrap();
} else {
writeln!(
file,
r#"
#[test]
fn test_{}() {{
test_case("{}");
}}
"#,
sanitize_filename(&resource),
resource
)
.unwrap();
}
}
}

fn get_test_resources(pattern: &str) -> Vec<String> {
glob(pattern)
.expect("Failed to read glob pattern")
.filter_map(Result::ok)
.filter(|path| !path.to_string_lossy().contains("datasets")) // Exclude datasets folder
.map(|path| path.to_string_lossy().into_owned())
.collect()
}

fn sanitize_filename(filename: &str) -> String {
let mut sanitized = String::new();
let mut prev_was_underscore = false;

for c in filename.chars() {
if c.is_alphanumeric() {
if prev_was_underscore {
sanitized.push('_');
prev_was_underscore = false;
}
sanitized.push(c.to_ascii_lowercase());
} else {
prev_was_underscore = true;
}
}

sanitized
}
22 changes: 2 additions & 20 deletions tests/testsuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,11 @@
extern crate test_generator;

use bumpalo::Bump;
use jsonata_rs::{ArrayFlags, JsonAta, Value};
use std::fs;
use std::path;
use test_generator::test_resources;

use jsonata_rs::{ArrayFlags, JsonAta, Value};

const SKIP: &[&str] = &[
// The order of object properties in the output is not deterministic,
// so string comparison fails. If we were using something like a BTreeMap
// or an IndexedMap then running these would be possible.
"tests/testsuite/groups/function-string/case018.json",
"tests/testsuite/groups/function-string/case027.json",
"tests/testsuite/groups/function-string/case028.json",
];

#[test_resources("tests/testsuite/groups/*/*.json")]
fn t(resource: &str) {
if SKIP.iter().any(|&s| s == resource) {
return;
}

test_case(resource);
}
include!(concat!(env!("OUT_DIR"), "/generated_tests.rs"));

fn test_case(resource: &str) {
let arena = Bump::new();
Expand Down