Skip to content

Commit

Permalink
Use insta for snapshot testing
Browse files Browse the repository at this point in the history
  • Loading branch information
suaviloquence committed Aug 4, 2024
1 parent f71627d commit 302c382
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 8 deletions.
51 changes: 50 additions & 1 deletion Cargo.lock

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

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions examples/inputs/recurser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<!doctype html>
<html>
<body>
<a href="https://example.com">Cool link!</a>
</body>
</html>
4 changes: 4 additions & 0 deletions examples/outputs/abc.json → examples/outputs/abc.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
source: src/interpreter/mod.rs
expression: result
---
{
"header": {
"link": {
Expand Down
4 changes: 4 additions & 0 deletions examples/outputs/attr.json → examples/outputs/attr.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
source: src/interpreter/mod.rs
expression: result
---
{
"link": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
source: src/interpreter/mod.rs
expression: result
---
{
"items": [
{
Expand Down
10 changes: 10 additions & 0 deletions examples/outputs/recurser.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: src/interpreter/mod.rs
expression: result
---
{
"link": "https://example.com",
"new_page": {
"content": "Example Domain"
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
---
source: src/interpreter/mod.rs
expression: result
---
{
"href": "./relative2.html",
"relative": "You found me!"
Expand Down
7 changes: 7 additions & 0 deletions examples/scrps/recurser.scrp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
link: a {
href: $element | attrs() | take(key: "href");
} | take(key: "href");

new_page: <$link> h1 {
content: $text;
};
6 changes: 5 additions & 1 deletion filter-proc-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
[package]
name = "filter-proc-macro"
name = "scrapelect-filter-proc-macro"
version = "0.2.0"
edition = "2021"
authors = ["Max Carr <[email protected]>"]
license = "Apache-2.0 OR MIT"
repository = "https://github.com/suaviloquence/scrapelect"
description = "proc-macros for making scrapelect Filters"

[lib]
proc-macro = true
Expand Down
16 changes: 10 additions & 6 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(())
}
Expand Down Expand Up @@ -454,5 +457,6 @@ mod tests {
attr,
qualifiers,
relative,
recurser,
}
}

0 comments on commit 302c382

Please sign in to comment.