diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 05f31b3..7eebbb2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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: diff --git a/Cargo.lock b/Cargo.lock index 9da5104..ea85f7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,15 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.12.1" @@ -206,6 +215,26 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "console" +version = "0.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d64e8af5551369d19cf50138de61f1c42074ab970f74e99be916646777f8fc87" +dependencies = [ + "encode_unicode", + "libc", + "windows-sys", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crossbeam-deque" version = "0.8.6" @@ -231,6 +260,16 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "dashmap" version = "5.5.3" @@ -250,6 +289,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "displaydoc" version = "0.2.5" @@ -261,6 +310,12 @@ dependencies = [ "syn", ] +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "equivalent" version = "1.0.2" @@ -389,6 +444,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.4.2" @@ -450,6 +515,7 @@ dependencies = [ "clap", "html5ever", "ignore", + "insta", "js-sys", "markup5ever_rcdom", "nom", @@ -618,6 +684,21 @@ dependencies = [ "serde_core", ] +[[package]] +name = "insta" +version = "1.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0f8fee8c926415c58d6ae43a08523a26faccb2323f5e6b644fe7dd4ef6b82" +dependencies = [ + "console", + "once_cell", + "pest", + "pest_derive", + "serde", + "similar", + "tempfile", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -837,6 +918,49 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "pest" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0848c601009d37dfa3430c4666e147e49cdcf1b92ecd3e63657d8a5f19da662" +dependencies = [ + "memchr", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f486f1ea21e6c10ed15d5a7c77165d0ee443402f0780849d1768e7d9d6fe77" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8040c4647b13b210a963c1ed407c1ff4fdfa01c31d6d2a098218702e6664f94f" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89815c69d36021a140146f26659a81d6c2afa33d216d736dd4be5381a7362220" +dependencies = [ + "pest", + "sha2", +] + [[package]] name = "phf" version = "0.13.1" @@ -1124,12 +1248,29 @@ dependencies = [ "syn", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" + [[package]] name = "siphasher" version = "1.0.3" @@ -1359,6 +1500,18 @@ dependencies = [ "once_cell", ] +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "ucd-trie" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" + [[package]] name = "unicode-ident" version = "1.0.24" @@ -1402,6 +1555,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wait-timeout" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index cc8a312..436675b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/diagnostic/format/tests/default.rs b/src/diagnostic/format/tests/default.rs index decfa24..0a916f3 100644 --- a/src/diagnostic/format/tests/default.rs +++ b/src/diagnostic/format/tests/default.rs @@ -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] @@ -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 - --> :1:1 - | -1 | 123invalid - | ^ -" - ); + insta::assert_snapshot!(output); } #[test] @@ -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] @@ -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); } diff --git a/src/diagnostic/format/tests/gitlab.rs b/src/diagnostic/format/tests/gitlab.rs index a2de9d1..05350e7 100644 --- a/src/diagnostic/format/tests/gitlab.rs +++ b/src/diagnostic/format/tests/gitlab.rs @@ -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] @@ -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] diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap new file mode 100644 index 0000000..e6422d3 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +error: parse error + --> :1:1 + | +1 | 123invalid + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap new file mode 100644 index 0000000..4e1072d --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W002]: Duplicate class 'text-red' + --> test.hsml:1:12 + | +1 | h1.text-red.text-red Hello + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap new file mode 100644 index 0000000..24d1231 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W001]: Duplicate attribute 'id' is not allowed + --> example.hsml:1:8 + | +1 | div#foo#bar + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap new file mode 100644 index 0000000..e98fac1 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W002]: Duplicate class 'foo' + --> test.hsml:1:7 + | +1 | h1.foo.foo Hello + | ^^^^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap new file mode 100644 index 0000000..7f4f1b9 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap @@ -0,0 +1,18 @@ +--- +source: src/diagnostic/format/tests/gitlab.rs +expression: parsed +--- +[ + { + "check_name": "hsml", + "description": "parse error", + "fingerprint": "f1051314d06cb78c", + "location": { + "lines": { + "begin": 1 + }, + "path": "test.hsml" + }, + "severity": "major" + } +] diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap new file mode 100644 index 0000000..9d4191d --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap @@ -0,0 +1,18 @@ +--- +source: src/diagnostic/format/tests/gitlab.rs +expression: parsed +--- +[ + { + "check_name": "W002", + "description": "Duplicate class 'foo'", + "fingerprint": "3b3deaac0f93890d", + "location": { + "lines": { + "begin": 1 + }, + "path": "example.hsml" + }, + "severity": "minor" + } +] diff --git a/src/parser/attribute/tests/node.rs b/src/parser/attribute/tests/node.rs index 2033e74..f186945 100644 --- a/src/parser/attribute/tests/node.rs +++ b/src/parser/attribute/tests/node.rs @@ -1,7 +1,6 @@ use crate::parser::{ HsmlNode, HsmlProcessContext, Span, attribute::node::{AttributeNode, attribute_node, attribute_nodes}, - comment::node::CommentNode, }; #[test] @@ -117,15 +116,8 @@ fn it_should_return_attribute_nodes_with_dev_comments() { ) .unwrap(); - assert_eq!( - attribute_nodes, - vec![ - HsmlNode::Comment(CommentNode::new_without_location(" comment 1", true)), - HsmlNode::Attribute(AttributeNode::new_without_location("key", Some("value"))), - HsmlNode::Comment(CommentNode::new_without_location(" comment 2", true)), - HsmlNode::Attribute(AttributeNode::new_without_location(":key2", Some("value2"))), - ] - ); + // Structure (the comment/attribute interleaving) is snapshotted; locations redacted. + insta::assert_json_snapshot!(attribute_nodes, { ".**.location" => "[location]" }); assert_eq!(*rest.fragment(), "\n"); } diff --git a/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap b/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap new file mode 100644 index 0000000..beb92c6 --- /dev/null +++ b/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap @@ -0,0 +1,30 @@ +--- +source: src/parser/attribute/tests/node.rs +expression: attribute_nodes +--- +[ + { + "type": "comment", + "text": " comment 1", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": "key", + "value": "value", + "location": "[location]" + }, + { + "type": "comment", + "text": " comment 2", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": ":key2", + "value": "value2", + "location": "[location]" + } +] diff --git a/src/parser/tag/tests/node.rs b/src/parser/tag/tests/node.rs index b0be186..1ff5ae5 100644 --- a/src/parser/tag/tests/node.rs +++ b/src/parser/tag/tests/node.rs @@ -1,10 +1,8 @@ use crate::parser::{ HsmlProcessContext, Span, - class::node::ClassNode, error::ErrorCode, id::node::IdNode, tag::node::{TagNode, tag_node}, - text::node::TextNode, }; #[test] @@ -26,27 +24,8 @@ fn it_should_return_tag_node_with_piped_text() { ) .unwrap(); - assert_eq!( - tag, - TagNode::without_location( - "p", - vec![], - Some(vec![ - ClassNode::new_without_location("text-lg"), - ClassNode::new_without_location("font-medium"), - ]), - None, - Some(TextNode { - text: String::from( - r#""Tailwind CSS is the only framework that I've seen scale -on large teams. It's easy to customize, adapts to any design, -and the build size is tiny.""# - ), - is_block: true, - }), - None, - ) - ); + // Structure is snapshotted; source locations are redacted (asserted separately). + insta::assert_json_snapshot!(tag, { ".**.location" => "[location]" }); assert_eq!(*rest.fragment(), "\n figcaption.font-medium"); } diff --git a/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap b/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap new file mode 100644 index 0000000..8b898e1 --- /dev/null +++ b/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap @@ -0,0 +1,25 @@ +--- +source: src/parser/tag/tests/node.rs +expression: tag +--- +{ + "tag": "p", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "text-lg", + "location": "[location]" + }, + { + "name": "font-medium", + "location": "[location]" + } + ], + "attributes": null, + "text": { + "text": "\"Tailwind CSS is the only framework that I've seen scale\non large teams. It's easy to customize, adapts to any design,\nand the build size is tiny.\"", + "isBlock": true + }, + "children": null +} diff --git a/tests/compiler.rs b/tests/compiler.rs index b7f4ab0..0150ce8 100644 --- a/tests/compiler.rs +++ b/tests/compiler.rs @@ -75,10 +75,7 @@ fn it_should_compile_parsed_content() { let html_content = compile(&ast, &HsmlCompileOptions::default()).unwrap(); - assert_eq!( - html_content, - r#"

Vite CJS Faker Demo

{{ fullName }}
"# - ); + insta::assert_snapshot!(html_content); assert_eq!(*rest.fragment(), ""); } @@ -111,12 +108,7 @@ figure.md:flex.bg-slate-100.rounded-xl.p-8.md:p-0.dark:bg-slate-800/10 let html_content = compile(&ast, &HsmlCompileOptions::default()).unwrap(); - assert_eq!( - html_content, - r#"

"Tailwind CSS is the only framework that I've seen scale -on large teams. It's easy to customize, adapts to any design, -and the build size is tiny."

Sarah Dayan
Staff Engineer, Algolia
"# - ); + insta::assert_snapshot!(html_content); assert_eq!(*rest.fragment(), ""); } @@ -160,13 +152,7 @@ fn it_should_compile_parsed_elk_status_content_component() { let html_content = compile(&ast, &HsmlCompileOptions::default()).unwrap(); - assert_eq!( - html_content, - r#"
"# - ); + insta::assert_snapshot!(html_content); assert_eq!(*rest.fragment(), ""); } @@ -559,10 +545,7 @@ fn it_should_compile_parsed_elk_main_content_component() { let html_content = compile(&ast, &HsmlCompileOptions::default()).unwrap(); - assert_eq!( - html_content, - r#"
"# - ); + insta::assert_snapshot!(html_content); assert_eq!(*rest.fragment(), ""); } diff --git a/tests/parse.rs b/tests/parse.rs index 74a2635..037f619 100644 --- a/tests/parse.rs +++ b/tests/parse.rs @@ -1,8 +1,13 @@ -use hsml::parser::{ - HsmlNode, RootNode, Span, attribute::node::AttributeNode, class::node::ClassNode, - comment::node::CommentNode, id::node::IdNode, parse::parse, tag::node::TagNode, - text::node::TextNode, -}; +use hsml::parser::{Span, parse::parse}; + +// AST structure is captured via JSON snapshots. The `.**.location` redaction +// drops source locations so these tests stay focused on structure — location +// correctness is asserted separately in the validator tests. +macro_rules! assert_ast_snapshot { + ($ast:expr) => { + insta::assert_json_snapshot!($ast, { ".**.location" => "[location]" }) + }; +} #[test] fn it_should_parse() { @@ -17,93 +22,7 @@ fn it_should_parse() { let (rest, root_node) = parse(Span::new(input)).unwrap(); - assert_eq!( - root_node, - RootNode { - nodes: vec![ - HsmlNode::Tag(TagNode::without_location( - "h1", - vec![], - Some(vec![ClassNode::new_without_location("text-red")]), - None, - Some(TextNode { - text: String::from("Vite CJS Faker Demo"), - is_block: false, - }), - None, - )), - HsmlNode::Tag(TagNode::without_location( - "div", - vec![], - Some(vec![ClassNode::new_without_location("card")]), - None, - None, - Some(vec![ - HsmlNode::Tag(TagNode::without_location( - "div", - vec![], - Some(vec![ClassNode::new_without_location("card__image")]), - None, - None, - Some(vec![HsmlNode::Tag(TagNode::without_location( - "img", - vec![], - None, - Some(vec![ - HsmlNode::Attribute(AttributeNode::new_without_location( - ":src", - Some("natureImageUrl"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - ":alt", - Some("'Background image for ' + fullName"), - )), - ]), - None, - None, - ))]), - )), - HsmlNode::Tag(TagNode::without_location( - "div", - vec![], - Some(vec![ClassNode::new_without_location("card__profile")]), - None, - None, - Some(vec![HsmlNode::Tag(TagNode::without_location( - "img", - vec![], - None, - Some(vec![ - HsmlNode::Attribute(AttributeNode::new_without_location( - ":src", - Some("avatarUrl"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - ":alt", - Some("'Avatar image of ' + fullName"), - )), - ]), - None, - None, - ))]), - )), - HsmlNode::Tag(TagNode::without_location( - "div", - vec![], - Some(vec![ClassNode::new_without_location("card__body")]), - None, - Some(TextNode { - text: String::from("{{ fullName }}"), - is_block: false, - }), - None, - )) - ]), - )), - ], - } - ); - + assert_ast_snapshot!(root_node); assert_eq!(*rest.fragment(), ""); } @@ -127,83 +46,7 @@ div let (rest, root_node) = parse(Span::new(input)).unwrap(); - assert_eq!( - root_node, - RootNode { - nodes: vec![ - HsmlNode::Comment(CommentNode::new_without_location( - " this is a root dev comment", - true - )), - HsmlNode::Comment(CommentNode::new_without_location( - " this is a root native comment (will get rendered)", - false - )), - HsmlNode::Tag(TagNode::without_location( - "div", - vec![], - None, - None, - None, - Some(vec![ - HsmlNode::Comment(CommentNode::new_without_location( - " this is a child comment", - true - )), - HsmlNode::Tag(TagNode::without_location( - "p", - vec![], - None, - None, - Some(TextNode { - text: String::from("another tag"), - is_block: false, - }), - None, - )), - HsmlNode::Comment(CommentNode::new_without_location( - " this is a child comment that gets rendered", - false - )), - HsmlNode::Tag(TagNode::without_location( - "img", - vec![], - None, - Some(vec![ - HsmlNode::Comment(CommentNode::new_without_location( - " supports attribute inline comments", - true - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - "src", - Some("/fancy-avatar.jpg"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - "alt", - Some("Fancy Avatar"), - )), - HsmlNode::Comment(CommentNode::new_without_location( - " the size of the image", - true - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - "width", - Some("384"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - "height", - Some("512"), - )), - ]), - None, - None, - )), - ]) - )) - ] - } - ); - + assert_ast_snapshot!(root_node); assert_eq!(*rest.fragment(), ""); } @@ -219,36 +62,7 @@ fn it_should_parse_wrapped_attributes() { let (rest, root_node) = parse(Span::new(input)).unwrap(); - assert_eq!( - root_node, - RootNode { - nodes: vec![HsmlNode::Tag(TagNode::without_location( - "img", - vec![], - Some(vec![ - ClassNode::new_without_location("rounded-full"), - ClassNode::new_without_location("mx-auto"), - ]), - Some(vec![ - HsmlNode::Attribute(AttributeNode::new_without_location( - "src", - Some("/fancy-avatar.jpg"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location( - "alt", - Some("A fancy avatar"), - )), - HsmlNode::Attribute(AttributeNode::new_without_location("width", Some("384"),)), - HsmlNode::Attribute( - AttributeNode::new_without_location("height", Some("512"),) - ), - ]), - None, - None, - ))], - } - ); - + assert_ast_snapshot!(root_node); assert_eq!(*rest.fragment(), ""); } @@ -260,22 +74,6 @@ fn it_should_parse_tag_with_multiple_ids() { let (rest, root_node) = parse(Span::new(input)).unwrap(); - assert_eq!( - root_node, - RootNode { - nodes: vec![HsmlNode::Tag(TagNode::without_location( - "div", - vec![ - IdNode::new_without_location("id1"), - IdNode::new_without_location("id2"), - ], - None, - None, - None, - None, - ))], - } - ); - + assert_ast_snapshot!(root_node); assert_eq!(*rest.fragment(), ""); } diff --git a/tests/snapshots/compiler__it_should_compile_parsed_content.snap b/tests/snapshots/compiler__it_should_compile_parsed_content.snap new file mode 100644 index 0000000..3e9a6ab --- /dev/null +++ b/tests/snapshots/compiler__it_should_compile_parsed_content.snap @@ -0,0 +1,5 @@ +--- +source: tests/compiler.rs +expression: html_content +--- +

Vite CJS Faker Demo

{{ fullName }}
diff --git a/tests/snapshots/compiler__it_should_compile_parsed_content_2.snap b/tests/snapshots/compiler__it_should_compile_parsed_content_2.snap new file mode 100644 index 0000000..15a3272 --- /dev/null +++ b/tests/snapshots/compiler__it_should_compile_parsed_content_2.snap @@ -0,0 +1,7 @@ +--- +source: tests/compiler.rs +expression: html_content +--- +

"Tailwind CSS is the only framework that I've seen scale +on large teams. It's easy to customize, adapts to any design, +and the build size is tiny."

Sarah Dayan
Staff Engineer, Algolia
diff --git a/tests/snapshots/compiler__it_should_compile_parsed_elk_main_content_component.snap b/tests/snapshots/compiler__it_should_compile_parsed_elk_main_content_component.snap new file mode 100644 index 0000000..040c21d --- /dev/null +++ b/tests/snapshots/compiler__it_should_compile_parsed_elk_main_content_component.snap @@ -0,0 +1,5 @@ +--- +source: tests/compiler.rs +expression: html_content +--- +
diff --git a/tests/snapshots/compiler__it_should_compile_parsed_elk_status_content_component.snap b/tests/snapshots/compiler__it_should_compile_parsed_elk_status_content_component.snap new file mode 100644 index 0000000..c7f74be --- /dev/null +++ b/tests/snapshots/compiler__it_should_compile_parsed_elk_status_content_component.snap @@ -0,0 +1,8 @@ +--- +source: tests/compiler.rs +expression: html_content +--- +
diff --git a/tests/snapshots/parse__it_should_parse.snap b/tests/snapshots/parse__it_should_parse.snap new file mode 100644 index 0000000..ff6a992 --- /dev/null +++ b/tests/snapshots/parse__it_should_parse.snap @@ -0,0 +1,138 @@ +--- +source: tests/parse.rs +expression: root_node +--- +{ + "nodes": [ + { + "type": "tag", + "tag": "h1", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "text-red", + "location": "[location]" + } + ], + "attributes": null, + "text": { + "text": "Vite CJS Faker Demo", + "isBlock": false + }, + "children": null + }, + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "card", + "location": "[location]" + } + ], + "attributes": null, + "text": null, + "children": [ + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "card__image", + "location": "[location]" + } + ], + "attributes": null, + "text": null, + "children": [ + { + "type": "tag", + "tag": "img", + "location": "[location]", + "ids": [], + "classes": null, + "attributes": [ + { + "type": "attribute", + "key": ":src", + "value": "natureImageUrl", + "location": "[location]" + }, + { + "type": "attribute", + "key": ":alt", + "value": "'Background image for ' + fullName", + "location": "[location]" + } + ], + "text": null, + "children": null + } + ] + }, + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "card__profile", + "location": "[location]" + } + ], + "attributes": null, + "text": null, + "children": [ + { + "type": "tag", + "tag": "img", + "location": "[location]", + "ids": [], + "classes": null, + "attributes": [ + { + "type": "attribute", + "key": ":src", + "value": "avatarUrl", + "location": "[location]" + }, + { + "type": "attribute", + "key": ":alt", + "value": "'Avatar image of ' + fullName", + "location": "[location]" + } + ], + "text": null, + "children": null + } + ] + }, + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "card__body", + "location": "[location]" + } + ], + "attributes": null, + "text": { + "text": "{{ fullName }}", + "isBlock": false + }, + "children": null + } + ] + } + ] +} diff --git a/tests/snapshots/parse__it_should_parse_tag_with_multiple_ids.snap b/tests/snapshots/parse__it_should_parse_tag_with_multiple_ids.snap new file mode 100644 index 0000000..8628ca5 --- /dev/null +++ b/tests/snapshots/parse__it_should_parse_tag_with_multiple_ids.snap @@ -0,0 +1,27 @@ +--- +source: tests/parse.rs +expression: root_node +--- +{ + "nodes": [ + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [ + { + "id": "id1", + "location": "[location]" + }, + { + "id": "id2", + "location": "[location]" + } + ], + "classes": null, + "attributes": null, + "text": null, + "children": null + } + ] +} diff --git a/tests/snapshots/parse__it_should_parse_with_comments.snap b/tests/snapshots/parse__it_should_parse_with_comments.snap new file mode 100644 index 0000000..846b789 --- /dev/null +++ b/tests/snapshots/parse__it_should_parse_with_comments.snap @@ -0,0 +1,103 @@ +--- +source: tests/parse.rs +expression: root_node +--- +{ + "nodes": [ + { + "type": "comment", + "text": " this is a root dev comment", + "isDev": true, + "location": "[location]" + }, + { + "type": "comment", + "text": " this is a root native comment (will get rendered)", + "isDev": false, + "location": "[location]" + }, + { + "type": "tag", + "tag": "div", + "location": "[location]", + "ids": [], + "classes": null, + "attributes": null, + "text": null, + "children": [ + { + "type": "comment", + "text": " this is a child comment", + "isDev": true, + "location": "[location]" + }, + { + "type": "tag", + "tag": "p", + "location": "[location]", + "ids": [], + "classes": null, + "attributes": null, + "text": { + "text": "another tag", + "isBlock": false + }, + "children": null + }, + { + "type": "comment", + "text": " this is a child comment that gets rendered", + "isDev": false, + "location": "[location]" + }, + { + "type": "tag", + "tag": "img", + "location": "[location]", + "ids": [], + "classes": null, + "attributes": [ + { + "type": "comment", + "text": " supports attribute inline comments", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": "src", + "value": "/fancy-avatar.jpg", + "location": "[location]" + }, + { + "type": "attribute", + "key": "alt", + "value": "Fancy Avatar", + "location": "[location]" + }, + { + "type": "comment", + "text": " the size of the image", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": "width", + "value": "384", + "location": "[location]" + }, + { + "type": "attribute", + "key": "height", + "value": "512", + "location": "[location]" + } + ], + "text": null, + "children": null + } + ] + } + ] +} diff --git a/tests/snapshots/parse__it_should_parse_wrapped_attributes.snap b/tests/snapshots/parse__it_should_parse_wrapped_attributes.snap new file mode 100644 index 0000000..8f6663f --- /dev/null +++ b/tests/snapshots/parse__it_should_parse_wrapped_attributes.snap @@ -0,0 +1,52 @@ +--- +source: tests/parse.rs +expression: root_node +--- +{ + "nodes": [ + { + "type": "tag", + "tag": "img", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "rounded-full", + "location": "[location]" + }, + { + "name": "mx-auto", + "location": "[location]" + } + ], + "attributes": [ + { + "type": "attribute", + "key": "src", + "value": "/fancy-avatar.jpg", + "location": "[location]" + }, + { + "type": "attribute", + "key": "alt", + "value": "A fancy avatar", + "location": "[location]" + }, + { + "type": "attribute", + "key": "width", + "value": "384", + "location": "[location]" + }, + { + "type": "attribute", + "key": "height", + "value": "512", + "location": "[location]" + } + ], + "text": null, + "children": null + } + ] +}