summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Oram <dev@mitmaro.ca>2022-01-19 09:23:56 -0330
committerTim Oram <dev@mitmaro.ca>2022-01-19 10:14:29 -0330
commit7cce533e689495ec165987be542d4eaf26590e16 (patch)
treecfdded2c9e1b5c716e8843980e627c9d210ed003
parent7795c287c77fb9b7118555c10768dca2cdf4ace8 (diff)
Add matchers to assert_rendered_output
Add AddLine, StartsWith and EndsWith matchers to the assert_rendered_output macro.
-rw-r--r--src/view/CHANGELOG.md17
-rw-r--r--src/view/src/testutil.rs66
2 files changed, 72 insertions, 11 deletions
diff --git a/src/view/CHANGELOG.md b/src/view/CHANGELOG.md
new file mode 100644
index 0000000..977bf42
--- /dev/null
+++ b/src/view/CHANGELOG.md
@@ -0,0 +1,17 @@
+# Change Log
+All notable changes to this project will be documented in this file.
+
+The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
+
+See the [migration guide](migration-guide.md) for migrating between major versions.
+
+## 1.1.0 - Unreleased
+
+### Added
+
+- Support for `AnyLine`, `StartsWith` and `EndsWith` on `assert_rendered_output` macro.
+
+## 1.0.0 - 2021-07-05
+
+### Added
+- Initial release
diff --git a/src/view/src/testutil.rs b/src/view/src/testutil.rs
index 51113a0..22ab6d8 100644
--- a/src/view/src/testutil.rs
+++ b/src/view/src/testutil.rs
@@ -9,16 +9,24 @@ use display::DisplayColor;
use super::{action::ViewAction, render_slice::RenderAction, view_data::ViewData, view_line::ViewLine, ViewSender};
const STARTS_WITH: &str = "{{StartsWith}}";
-
-/// Allow any line for a rendered line
-pub const ANY_LINE: &str = "{{ANY}}";
+const ENDS_WITH: &str = "{{EndsWith}}";
+const ANY_LINE: &str = "{{Any}}";
/// Assert the rendered output from a `ViewData`.
#[macro_export]
macro_rules! render_line {
+ (AnyLine) => {{
+ concat!("{{Any}}")
+ }};
+ (AnyLine $count:expr) => {{
+ concat!("{{Any(", $count, ")}}")
+ }};
(StartsWith $line:expr) => {{
concat!("{{StartsWith}}", $line)
}};
+ (EndsWith $line:expr) => {{
+ concat!("{{EndsWith}}", $line)
+ }};
}
/// Options for the `assert_rendered_output!` macro
@@ -160,6 +168,27 @@ fn render_view_data(view_data: &ViewData) -> Vec<String> {
lines
}
+fn expand_expected(expected: &[String]) -> Vec<String> {
+ expected
+ .iter()
+ .flat_map(|f| {
+ if f.starts_with("{{Any(") && f.ends_with(")}}") {
+ let lines = f
+ .replace("{{Any(", "")
+ .replace(")}}", "")
+ .as_str()
+ .parse::<u32>()
+ .unwrap_or_else(|_| panic!("Expected {} to have integer line count", f));
+ vec![String::from(ANY_LINE); lines as usize]
+ }
+ else {
+ vec![f.clone()]
+ }
+ })
+ .collect::<Vec<String>>()
+}
+
+#[allow(clippy::indexing_slicing, clippy::string_slice)]
pub(crate) fn _assert_rendered_output(options: AssertRenderOptions, actual: &[String], expected: &[String]) {
let mut mismatch = false;
let mut error_output = vec![
@@ -170,10 +199,6 @@ pub(crate) fn _assert_rendered_output(options: AssertRenderOptions, actual: &[St
];
for (expected_line, output_line) in expected.iter().zip(actual.iter()) {
- if expected_line == ANY_LINE {
- error_output.push(String::from("Matching *"));
- continue;
- }
let output = if options.ignore_trailing_whitespace {
output_line.trim_end()
}
@@ -184,15 +209,33 @@ pub(crate) fn _assert_rendered_output(options: AssertRenderOptions, actual: &[St
let mut e = replace_invisibles(expected_line);
let o = replace_invisibles(output);
+ if expected_line == ANY_LINE {
+ error_output.push(format!(" {}", o));
+ continue;
+ }
+
+ if expected_line.starts_with(ENDS_WITH) {
+ e = e.replace(ENDS_WITH, "");
+ if output.ends_with(&expected_line.replace(ENDS_WITH, "")) {
+ error_output.push(format!(" {}", o));
+ }
+ else {
+ mismatch = true;
+ error_output.push(format!("-EndsWith {}", e));
+ error_output.push(format!("+ {}", &o[o.len() - e.len() + 2..]));
+ }
+ continue;
+ }
+
if expected_line.starts_with(STARTS_WITH) {
- e = expected_line.replace(STARTS_WITH, "");
- if output.starts_with(&e) {
+ e = e.replace(STARTS_WITH, "");
+ if output.starts_with(&expected_line.replace(STARTS_WITH, "")) {
error_output.push(format!(" {}", o));
}
else {
mismatch = true;
error_output.push(format!("-StartsWith {}", e));
- error_output.push(format!("+ {}", o));
+ error_output.push(format!("+ {}", &o[0..=e.len()]));
}
continue;
}
@@ -234,7 +277,8 @@ pub(crate) fn _assert_rendered_output(options: AssertRenderOptions, actual: &[St
#[inline]
pub fn _assert_rendered_output_from_view_data(view_data: &ViewData, expected: &[String], options: AssertRenderOptions) {
let output = render_view_data(view_data);
- _assert_rendered_output(options, &output, expected);
+
+ _assert_rendered_output(options, &output, &expand_expected(expected));
}
/// Assert the rendered output from a `ViewData`.