Skip to content

Commit

Permalink
Don't panic when outputting TOML arrays in test failures.
Browse files Browse the repository at this point in the history
TOML can't serialize arrays as the top level type.
Some of our test cases have TOML arrays as expected output.
Thus sometimes failed tests will panic when trying to report
expected and found output. Instead, we now manually "serialize"
TOML arrays.
  • Loading branch information
mikebenfield committed Oct 23, 2024
1 parent 114e215 commit 322f2f7
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions tests/test-framework/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ pub enum TestError {
MissingTestConfig,
}

/// Make a user-readable string representing this Value.
///
/// TOML only supports tables as the top level type,
/// so we handle arrays on our own.
fn toml_to_string(x: &Value) -> String {
if let Some(array) = x.as_array() {
let mut s = String::new();
for value in array.iter() {
s.push_str(&toml::to_string(value).expect("serialization failed"));
s.push('\n');
}
s
} else {
toml::to_string(x).expect("serialization failed")
}
}

impl fmt::Display for TestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let format_test =
Expand All @@ -50,8 +67,8 @@ impl fmt::Display for TestError {
"test #{}: {}expected\n{}\ngot\n{}",
index + 1,
format_test(test),
toml::to_string(&expected).expect("serialization failed"),
toml::to_string(&output).expect("serialization failed")
toml_to_string(expected),
toml_to_string(output),
)
}
TestError::PassedAndShouldntHave { test, index } => {
Expand Down

0 comments on commit 322f2f7

Please sign in to comment.