Skip to content

Commit

Permalink
tests: add template render error and filter error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Nov 7, 2024
1 parent 675f10d commit 91c4ac4
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions tests/test_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,75 @@ Alice is a minor.
Bob is an adult.";
assert_eq!(got, expected);
}

#[test]
fn template_render_error() {
let wrk = Workdir::new("template_render_error");
wrk.create(
"data.csv",
vec![
svec!["name", "age"],
svec!["Alice", "25"],
svec!["Bob", "30"],
],
);

// Test invalid template syntax with default error message
let mut cmd = wrk.command("template");
cmd.arg("--template")
.arg("Hello {{name}, invalid syntax!")
.arg("data.csv");

wrk.assert_err(&mut *&mut cmd);
let got: String = wrk.output_stderr(&mut cmd);
let expected =
"syntax error: unexpected `}}`, expected end of variable block (in template:1)\n";
assert_eq!(got, expected);
}

#[test]
fn template_filter_error() {
let wrk = Workdir::new("template_filter_error");
wrk.create(
"data.csv",
vec![
svec!["name", "amount"],
svec!["Alice", "not_a_number"],
svec!["Bob", "123.45"],
],
);

// Test filter error with default error message
let mut cmd = wrk.command("template");
cmd.arg("--template")
.arg("{{name}}: {{amount|format_float(2)}}\n\n")
.arg("data.csv");

let got: String = wrk.stdout(&mut cmd);
let expected = "Alice: <FILTER_ERROR>\nBob: 123.45";
assert_eq!(got, expected);

// Test custom filter error message
let mut cmd = wrk.command("template");
cmd.arg("--template")
.arg("{{name}}: {{amount|format_float(2)}}\n\n")
.arg("--customfilter-error")
.arg("INVALID NUMBER")
.arg("data.csv");

let got: String = wrk.stdout(&mut cmd);
let expected = "Alice: INVALID NUMBER\nBob: 123.45";
assert_eq!(got, expected);

// Test empty string as filter error
let mut cmd = wrk.command("template");
cmd.arg("--template")
.arg("{{name}}: {{amount|format_float(2)}}\n\n")
.arg("--customfilter-error")
.arg("<empty string>")
.arg("data.csv");

let got: String = wrk.stdout(&mut cmd);
let expected = "Alice: \nBob: 123.45";
assert_eq!(got, expected);
}

0 comments on commit 91c4ac4

Please sign in to comment.