json formatter spec tests
MODIFY
crates/formatter/tests/spec_test.rs
crates/formatter/tests/spec_test.rs
use rome_formatter::{format_str, FormatOptions};
use std::fs;
-use std::path::PathBuf;
+use std::path::Path;
/// Tests that format a given input file, results in the expected formatted output.
///
@@ -13,45 +13,25 @@ use std::path::PathBuf;
///
/// * `json/null` -> input: `tests/specs/json/null.json`, expected output: `tests/specs/json/null.expected.json`
/// * `null` -> input: `tests/specs/null.json`, expected output: `tests/specs/null.expected.json`
-pub fn spec_test(spec_name: &str) {
- let spec_input_file = spec_file_name(spec_name, "json");
+pub fn run(spec_input_file: &str, expected_file: &str) {
+ let spec_input_file = Path::new(spec_input_file);
+ let expected_file = Path::new(expected_file);
assert!(
spec_input_file.is_file(),
- "The input '{}' for the spec '{}' must exist and be a file.",
- spec_name,
+ "The input '{}' must exist and be a file.",
spec_input_file.display()
);
- let expected_file = spec_file_name(spec_name, "expected.json");
assert!(
expected_file.is_file(),
- "The expected output '{}' for the spec '{}' must exist and be a file.",
+ "The expected output '{}' must exist and be a file.",
expected_file.display(),
- spec_name
);
let spec_content = fs::read_to_string(spec_input_file).unwrap();
-
let result = format_str(spec_content.as_str(), FormatOptions::default());
-
let expected_output = fs::read_to_string(expected_file).unwrap();
- assert_eq!(&expected_output, result.code());
-}
-
-fn spec_file_name(spec_name: &str, extension: &str) -> PathBuf {
- let specs_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
- .join("tests")
- .join("specs");
- assert!(
- specs_dir.is_dir(),
- "The directory '{}' containing the specification files doesn't exist or isn't a directory.",
- specs_dir.display()
- );
-
- let base_spec_path =
- PathBuf::from(spec_name.replace('/', std::path::MAIN_SEPARATOR.to_string().as_str()));
-
- specs_dir.join(base_spec_path.with_extension(extension))
+ assert_eq!(&expected_output, result.code());
}

MODIFY
crates/formatter/tests/spec_tests.rs
crates/formatter/tests/spec_tests.rs
mod spec_test;
-#[test]
-pub fn json_boolean() {
- spec_test::spec_test("json/boolean");
+mod formatter {
+ mod json {
+ use crate::spec_test;
+ tests_macros::gen_tests! {"tests/specs/json/*.json", spec_test::run}
}
-
-#[test]
-pub fn json_null() {
- spec_test::spec_test("json/null");
-}
-
-#[test]
-pub fn json_string() {
- spec_test::spec_test("json/string");
-}
-
-#[test]
-pub fn json_number() {
- spec_test::spec_test("json/number");
-}
-
-#[test]
-pub fn json_single_line() {
- spec_test::spec_test("json/single_line");
-}
-
-#[test]
-pub fn json_array() {
- spec_test::spec_test("json/array");
-}
-
-#[test]
-pub fn json_key_value() {
- spec_test::spec_test("json/key_value");
-}
-
-#[test]
-pub fn json_multi_line() {
- spec_test::spec_test("json/multi_line");
-}
-
-#[test]
-pub fn json_int1() {
- spec_test::spec_test("json/int1");
}

MODIFY
crates/tests_macros/Readme.md
crates/tests_macros/Readme.md
A set of utilities that automatically generate unit tests from files.
+## How to install
+
+```
+[dev-dependencies]
+tests_macros = { path = "../tests_macros" }
+```
+
## Usage
First argument: glob that will passed to https://github.com/gilnaa/globwalk. Crate's cargo.toml will be the base directory. To pattern format see here: https://git-scm.com/docs/gitignore#_pattern_format
-Second argument: method that will be called.
+Second argument: method that will be called with full path to each file.
-> The test name is the `snake_case` version of the file name.
+One suggestion to organize tests is to put the macro inside a module.
```rust
+mod some_mod {
tests_macros::gen_tests!{"tests/*.{js,json}", run_test}
-fn run_test<S: AsRef<str> + std::fmt::Debug>(a: S, b: S) {
- println!("{:?} {:?}", a, b);
+ // input_file and expected_file are full paths
+ fn run_test(input_file: &str, expected_file: &str) {
+ println!("{:?} {:?}", input_file, expected_file);
+ }
}
```
-This will generate the following for each file:
+Test name is the "snake case" version of the file name.
+this will generate the following for each file:
```rust
#[test]
-pub fn some_test()
+pub fn somefilename()
{
- let test_file = "<SOMEDIR>/tests/sometest.txt";
- let test_expected_file = "<SOMEDIR>/tests/sometest.expected.txt";
+ let test_file = "<crate's cargo.toml full path>/tests/sometest.txt";
+ let test_expected_file = "<crate's cargo.toml full path>/tests/sometest.expected.txt";
run_test(test_file, test_expected_file);
}
```
+
+## How to run
+
+```
+> cargo test // all tests in all crates
+> cargo test -p crate-name // all tests of one crate
+> cargo test -p crate-name -- some_mod:: // all tests of one crate and one module
+> cargo test -p crate-name -- some_mod::somefilename // just one test
+```

json formatter spec tests (#1694)