Skip to content

Commit

Permalink
Merge pull request #545 from epage/snapbox
Browse files Browse the repository at this point in the history
chore: Upgrade to snapbox 0.6
  • Loading branch information
epage committed May 28, 2024
2 parents ad3160b + cc0101d commit c44ec2a
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 451 deletions.
667 changes: 262 additions & 405 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ serde_derive = "1.0"
serde_json = "1.0"
regex = "1.10"
criterion = "0.5"
snapbox = "0.5.9"
snapbox = "0.6.5"

[[bench]]
name = "liquid"
Expand Down
2 changes: 1 addition & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ liquid-derive = { version = "^0.26.4", path = "../derive", optional = true }

[dev-dependencies]
serde_yaml = "0.8"
snapbox = "0.5.9"
snapbox = "0.6.5"

[features]
default = []
Expand Down
114 changes: 102 additions & 12 deletions crates/core/src/model/value/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,27 +662,66 @@ impl<'de> serde::de::SeqAccess<'de> for ArrayDeserializer<'de> {
mod test {
use std::f64;

use snapbox::assert_data_eq;
use snapbox::prelude::*;
use snapbox::str;

#[test]
pub fn serialize_num() {
let actual = crate::model::Value::scalar(1f64);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n1.0");
assert_data_eq!(
actual.trim(),
str![[r#"
---
1.0
"#]]
.raw()
);

let actual = crate::model::Value::scalar(-100f64);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n-100.0");
assert_data_eq!(
actual.trim(),
str![[r#"
---
-100.0
"#]]
.raw()
);

let actual = crate::model::Value::scalar(3.14e_10f64);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n31400000000.0");
assert_data_eq!(
actual.trim(),
str![[r#"
---
31400000000.0
"#]]
.raw()
);

let actual = crate::model::Value::scalar(f64::NAN);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n.nan");
assert_data_eq!(
actual.trim(),
str![[r#"
---
.nan
"#]]
.raw()
);

let actual = crate::model::Value::scalar(f64::INFINITY);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n.inf");
assert_data_eq!(
actual.trim(),
str![[r#"
---
.inf
"#]]
.raw()
);
}

#[test]
Expand All @@ -703,11 +742,25 @@ mod test {
pub fn serialize_bool() {
let actual = crate::model::Value::scalar(true);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\ntrue");
assert_data_eq!(
actual.trim(),
str![[r#"
---
true
"#]]
.raw()
);

let actual = crate::model::Value::scalar(false);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\nfalse");
assert_data_eq!(
actual.trim(),
str![[r#"
---
false
"#]]
.raw()
);
}

#[test]
Expand All @@ -723,7 +776,14 @@ mod test {
pub fn serialize_nil() {
let actual = crate::model::Value::Nil;
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n~");
assert_data_eq!(
actual.trim(),
str![[r#"
---
~
"#]]
.raw()
);
}

#[test]
Expand All @@ -742,15 +802,36 @@ mod test {
pub fn serialize_str() {
let actual = crate::model::Value::scalar("Hello");
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\nHello");
assert_data_eq!(
actual.trim(),
str![[r#"
---
Hello
"#]]
.raw()
);

let actual = crate::model::Value::scalar("10");
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n\"10\"");
assert_data_eq!(
actual.trim(),
str![[r#"
---
"10"
"#]]
.raw()
);

let actual = crate::model::Value::scalar("false");
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n\"false\"");
assert_data_eq!(
actual.trim(),
str![[r#"
---
"false"
"#]]
.raw()
);
}

#[test]
Expand All @@ -774,7 +855,16 @@ mod test {
];
let actual = crate::model::Value::Array(actual);
let actual = serde_yaml::to_string(&actual).unwrap();
snapbox::assert_eq(actual.trim(), "---\n- 1.0\n- true\n- \"true\"");
assert_data_eq!(
actual.trim(),
str![[r#"
---
- 1.0
- true
- "true"
"#]]
.raw()
);
}

#[test]
Expand Down
14 changes: 3 additions & 11 deletions tests/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::fs::File;
use std::io::Read;

use liquid::*;
use snapbox::assert_data_eq;

pub type Partials = liquid::partials::EagerCompiler<liquid::partials::InMemorySource>;

fn compare_by_file(name: &str, globals: &Object) {
let input_file = format!("tests/fixtures/input/{}.txt", name);
let output_file = format!("tests/fixtures/output/{}.txt", name);
let output_file = std::path::PathBuf::from(format!("tests/fixtures/output/{}.txt", name));

let mut partials = Partials::empty();
partials.add("tests/fixtures/input/example.txt", r#"{{'whooo' | size}}{%comment%}What happens{%endcomment%} {%if num < numTwo%}wat{%else%}wot{%endif%} {%if num > numTwo%}wat{%else%}wot{%endif%}
Expand All @@ -27,13 +25,7 @@ fn compare_by_file(name: &str, globals: &Object) {

let output = template.render(globals).unwrap();

let mut comp = String::new();
File::open(output_file)
.unwrap()
.read_to_string(&mut comp)
.unwrap();

snapbox::assert_eq(&comp, output);
assert_data_eq!(output, snapbox::Data::read_from(&output_file, None).raw());
}

#[test]
Expand Down
17 changes: 7 additions & 10 deletions tests/multithreading.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::fs::File;
use std::io::Read;
use std::sync::Arc;
use std::thread;

use snapbox::assert_data_eq;

#[test]
pub fn pass_between_threads() {
let input_file = "tests/fixtures/input/example.txt";
Expand All @@ -18,21 +18,18 @@ pub fn pass_between_threads() {
let v = vec![(5f64, 6f64), (20f64, 10f64)];
for (counter, (num1, num2)) in v.into_iter().enumerate() {
let template = Arc::clone(&template);
let output_file = format!("tests/fixtures/output/example_mt{}.txt", counter + 1);
let output_file = std::path::PathBuf::from(format!(
"tests/fixtures/output/example_mt{}.txt",
counter + 1
));
handles.push(thread::spawn(move || {
let globals = liquid::object!({
"num": num1,
"numTwo": num2,
});
let output = template.render(&globals).unwrap();

let mut comp = String::new();
File::open(&output_file)
.unwrap_or_else(|_| panic!("Expected output file does not exist: {}", output_file))
.read_to_string(&mut comp)
.unwrap_or_else(|_| panic!("Failed to read file: {}", output_file));

snapbox::assert_eq(&comp, output);
assert_data_eq!(output, snapbox::Data::read_from(&output_file, None).raw());
}));
}

Expand Down
13 changes: 3 additions & 10 deletions tests/parse_file.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use liquid::*;
use std::fs::File;
use std::io::Read;
use snapbox::assert_data_eq;

fn compare_by_file(name: &str, globals: &Object) {
let input_file = format!("tests/fixtures/input/{}.txt", name);
let output_file = format!("tests/fixtures/output/{}.txt", name);
let output_file = std::path::PathBuf::from(format!("tests/fixtures/output/{}.txt", name));

let template = ParserBuilder::with_stdlib()
.build()
Expand All @@ -14,13 +13,7 @@ fn compare_by_file(name: &str, globals: &Object) {

let output = template.render(globals).unwrap();

let mut comp = String::new();
File::open(output_file)
.unwrap()
.read_to_string(&mut comp)
.unwrap();

snapbox::assert_eq(&comp, output);
assert_data_eq!(output, snapbox::Data::read_from(&output_file, None).raw());
}

#[test]
Expand Down
4 changes: 3 additions & 1 deletion tests/syntax.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use liquid::*;
use snapbox::assert_data_eq;
use snapbox::data::IntoData;

fn compare(input: &str, expected: &str) {
let input = input.replace('…', " ");
Expand All @@ -12,7 +14,7 @@ fn compare(input: &str, expected: &str) {

let output = template.render(&Object::default()).unwrap();

snapbox::assert_eq(expected, output);
assert_data_eq!(output, expected.raw());
}

#[test]
Expand Down

0 comments on commit c44ec2a

Please sign in to comment.