Skip to content
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
25 changes: 25 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,31 @@ cargo clippy --all-targets --all-features
cargo test
```

#### Snapshot tests

Some tests use [`insta`](https://insta.rs) snapshots instead of inline `assert_eq!`.
We reach for snapshots when the expected value is large or tedious to maintain by hand:

- Parsed ASTs (`tests/parse.rs`) — snapshotted as JSON with source locations redacted,
so these tests stay focused on structure. Location correctness is asserted separately
in the validator tests.
- Long compiled HTML output (the bigger cases in `tests/compiler.rs`).
- Multi-line rendered diagnostics (`src/diagnostic/format/tests/default.rs`).

Short, behavior-defining checks stay as plain `assert_eq!` — they read as documentation
and force you to think about the exact expected value.

When a snapshot test fails (or you add a new one), review and accept the change with
[`cargo-insta`](https://insta.rs/docs/cli/) (`cargo install cargo-insta`):

```sh
cargo insta test --review # run tests, then interactively review pending snapshots
cargo insta accept # accept all pending snapshots without review
```

Always eyeball the diff before accepting — a snapshot is only as trustworthy as the
review that produced it. Committed snapshots live in `**/snapshots/*.snap`.

### Run

HSML provides several subcommands:
Expand Down
159 changes: 159 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@ predicates = "=3.1.4"
tempfile = "=3.27.0"

[dev-dependencies]
insta = { version = "=1.48.0", features = ["json", "redactions"] }
js-sys = "=0.3.99"
wasm-bindgen-test = "=0.3.72"
44 changes: 4 additions & 40 deletions src/diagnostic/format/tests/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ fn it_should_format_warning_with_source_context() {
let source = "div#foo#bar";
let output = DefaultFormatter.format(&[diag], Some(source));

assert_eq!(
output,
"\
warning[W001]: Duplicate attribute 'id' is not allowed
--> example.hsml:1:8
|
1 | div#foo#bar
| ^
"
);
insta::assert_snapshot!(output);
}

#[test]
Expand All @@ -47,16 +38,7 @@ fn it_should_format_error_without_code() {
let source = "123invalid";
let output = DefaultFormatter.format(&[diag], Some(source));

assert_eq!(
output,
"\
error: parse error
--> <input>:1:1
|
1 | 123invalid
| ^
"
);
insta::assert_snapshot!(output);
}

#[test]
Expand Down Expand Up @@ -96,16 +78,7 @@ fn it_should_format_warning() {
let source = "h1.text-red.text-red Hello";
let output = DefaultFormatter.format(&[diag], Some(source));

assert_eq!(
output,
"\
warning[W002]: Duplicate class 'text-red'
--> test.hsml:1:12
|
1 | h1.text-red.text-red Hello
| ^
"
);
insta::assert_snapshot!(output);
}

#[test]
Expand Down Expand Up @@ -151,14 +124,5 @@ fn it_should_underline_span_range() {
let source = "h1.foo.foo Hello";
let output = DefaultFormatter.format(&[diag], Some(source));

assert_eq!(
output,
"\
warning[W002]: Duplicate class 'foo'
--> test.hsml:1:7
|
1 | h1.foo.foo Hello
| ^^^^
"
);
insta::assert_snapshot!(output);
}
13 changes: 2 additions & 11 deletions src/diagnostic/format/tests/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ fn it_should_format_warning_as_codeclimate_issue() {

let output = GitlabFormatter.format(&[diag], None);
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
let arr = parsed.as_array().unwrap();

assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["description"], "Duplicate class 'foo'");
assert_eq!(arr[0]["check_name"], "W002");
assert_eq!(arr[0]["severity"], "minor");
assert_eq!(arr[0]["location"]["path"], "example.hsml");
assert_eq!(arr[0]["location"]["lines"]["begin"], 1);
assert!(arr[0]["fingerprint"].is_string());
insta::assert_json_snapshot!(parsed);
}

#[test]
Expand All @@ -47,10 +40,8 @@ fn it_should_format_error_as_major_severity() {

let output = GitlabFormatter.format(&[diag], None);
let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();
let arr = parsed.as_array().unwrap();

assert_eq!(arr[0]["severity"], "major");
assert_eq!(arr[0]["check_name"], "hsml");
insta::assert_json_snapshot!(parsed);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: src/diagnostic/format/tests/default.rs
expression: output
---
error: parse error
--> <input>:1:1
|
1 | 123invalid
| ^
Loading