diff --git a/Cargo.lock b/Cargo.lock index de5b71b..3727b00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -120,6 +120,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "console" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" +dependencies = [ + "encode_unicode", + "lazy_static", + "libc", + "windows-sys 0.52.0", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -191,6 +203,12 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" +[[package]] +name = "encode_unicode" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" + [[package]] name = "encoding_rs" version = "0.8.34" @@ -564,6 +582,19 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "insta" +version = "1.39.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5" +dependencies = [ + "console", + "lazy_static", + "linked-hash-map", + "serde", + "similar", +] + [[package]] name = "ipnet" version = "2.9.0" @@ -585,12 +616,24 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + [[package]] name = "libc" version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1146,6 +1189,7 @@ version = "0.2.0" dependencies = [ "anyhow", "futures", + "insta", "regex", "reqwest", "scrapelect-filter-proc-macro", @@ -1160,7 +1204,6 @@ dependencies = [ name = "scrapelect-filter-proc-macro" version = "0.2.0" dependencies = [ - "insta", "quote", "syn 2.0.64", ] @@ -1284,6 +1327,12 @@ dependencies = [ "libc", ] +[[package]] +name = "similar" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e" + [[package]] name = "siphasher" version = "0.3.11" diff --git a/Cargo.toml b/Cargo.toml index 6ef7135..3984260 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,13 @@ url = "2.5" [workspace] members = [".", "filter-proc-macro"] + +[dev-dependencies] +insta = { version = "1.39.0", features = ["json"] } + +# compile insta and similar in release mode to improve testing times +[profile.dev.package.insta] +opt-level = 3 + +[profile.dev.package.similar] +opt-level = 3 diff --git a/examples/inputs/recurser.html b/examples/inputs/recurser.html new file mode 100644 index 0000000..20ee881 --- /dev/null +++ b/examples/inputs/recurser.html @@ -0,0 +1,6 @@ + + + + Cool link! + + diff --git a/examples/outputs/abc.json b/examples/outputs/abc.snap similarity index 63% rename from examples/outputs/abc.json rename to examples/outputs/abc.snap index d081400..f20a0ad 100644 --- a/examples/outputs/abc.json +++ b/examples/outputs/abc.snap @@ -1,3 +1,7 @@ +--- +source: src/interpreter/mod.rs +expression: result +--- { "header": { "link": { diff --git a/examples/outputs/attr.json b/examples/outputs/attr.snap similarity index 68% rename from examples/outputs/attr.json rename to examples/outputs/attr.snap index cecc0a0..505aaa7 100644 --- a/examples/outputs/attr.json +++ b/examples/outputs/attr.snap @@ -1,3 +1,7 @@ +--- +source: src/interpreter/mod.rs +expression: result +--- { "link": [ { diff --git a/examples/outputs/qualifiers.json b/examples/outputs/qualifiers.snap similarity index 91% rename from examples/outputs/qualifiers.json rename to examples/outputs/qualifiers.snap index 5e4ec0b..e4cb25e 100644 --- a/examples/outputs/qualifiers.json +++ b/examples/outputs/qualifiers.snap @@ -1,3 +1,7 @@ +--- +source: src/interpreter/mod.rs +expression: result +--- { "items": [ { diff --git a/examples/outputs/recurser.snap b/examples/outputs/recurser.snap new file mode 100644 index 0000000..23047a4 --- /dev/null +++ b/examples/outputs/recurser.snap @@ -0,0 +1,10 @@ +--- +source: src/interpreter/mod.rs +expression: result +--- +{ + "link": "https://example.com", + "new_page": { + "content": "Example Domain" + } +} diff --git a/examples/outputs/relative.json b/examples/outputs/relative.snap similarity index 52% rename from examples/outputs/relative.json rename to examples/outputs/relative.snap index 25e6547..1bb52a1 100644 --- a/examples/outputs/relative.json +++ b/examples/outputs/relative.snap @@ -1,3 +1,7 @@ +--- +source: src/interpreter/mod.rs +expression: result +--- { "href": "./relative2.html", "relative": "You found me!" diff --git a/examples/scrps/recurser.scrp b/examples/scrps/recurser.scrp new file mode 100644 index 0000000..27b05f7 --- /dev/null +++ b/examples/scrps/recurser.scrp @@ -0,0 +1,7 @@ +link: a { + href: $element | attrs() | take(key: "href"); +} | take(key: "href"); + +new_page: <$link> h1 { + content: $text; +}; diff --git a/filter-proc-macro/Cargo.toml b/filter-proc-macro/Cargo.toml index c237657..8f2bf94 100644 --- a/filter-proc-macro/Cargo.toml +++ b/filter-proc-macro/Cargo.toml @@ -1,7 +1,11 @@ [package] -name = "filter-proc-macro" +name = "scrapelect-filter-proc-macro" version = "0.2.0" edition = "2021" +authors = ["Max Carr "] +license = "Apache-2.0 OR MIT" +repository = "https://github.com/suaviloquence/scrapelect" +description = "proc-macros for making scrapelect Filters" [lib] proc-macro = true diff --git a/src/interpreter/mod.rs b/src/interpreter/mod.rs index a16f0b6..1cacdd5 100644 --- a/src/interpreter/mod.rs +++ b/src/interpreter/mod.rs @@ -346,9 +346,6 @@ mod tests { async fn integration_test(filename: &str) -> anyhow::Result<()> { let input = std::fs::read_to_string(format!("examples/inputs/{filename}.html"))?; let script = std::fs::read_to_string(format!("examples/scrps/{filename}.scrp"))?; - let output: serde_json::Value = serde_json::from_reader(std::fs::File::open(format!( - "examples/outputs/{filename}.json" - ))?)?; let (ast, head) = crate::frontend::Parser::new(&script) .parse() @@ -369,9 +366,15 @@ mod tests { .parse() .expect("parse URL failed"), ) - .await?; - let result = serde_json::to_value(result.0)?; - assert_eq!(output, result); + .await?.0; + + let mut settings = insta::Settings::clone_current(); + settings.set_snapshot_path("../../examples/outputs"); + settings.set_prepend_module_to_snapshot(false); + + settings.bind(|| { + insta::assert_json_snapshot!(filename, result); + }); Ok(()) } @@ -454,5 +457,6 @@ mod tests { attr, qualifiers, relative, + recurser, } }