diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2022-06-21 15:25:07 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-21 15:25:07 -0400 |
commit | 3455f16079c2ca676e092d2123b8895e68e1a3ee (patch) | |
tree | cccbca198d8f4e375b6e90b6415acffad532589a /test_util/src | |
parent | 7ad0d092281c040c5c5be4ba0415f3e7551201de (diff) |
chore(test_util): add new string assertion macros (#14928)
Diffstat (limited to 'test_util/src')
-rw-r--r-- | test_util/src/assertions.rs | 39 | ||||
-rw-r--r-- | test_util/src/lib.rs | 3 |
2 files changed, 41 insertions, 1 deletions
diff --git a/test_util/src/assertions.rs b/test_util/src/assertions.rs new file mode 100644 index 000000000..ecee14b1f --- /dev/null +++ b/test_util/src/assertions.rs @@ -0,0 +1,39 @@ +#[macro_export] +macro_rules! assert_ends_with { + ($left:expr, $right:expr $(,)?) => { + match (&$left, &$right) { + (actual, expected) => { + let actual = if expected.len() > actual.len() { + actual + } else { + &actual[actual.len() - expected.len()..] + }; + pretty_assertions::assert_eq!( + actual, + *expected, + "should end with expected." + ); + } + } + }; +} + +#[macro_export] +macro_rules! assert_contains { + ($string:expr, $($test:expr),+ $(,)?) => { + let string = &$string; // This might be a function call or something + if !($(string.contains($test))||+) { + panic!("{:?} does not contain any of {:?}", string, [$($test),+]); + } + } +} + +#[macro_export] +macro_rules! assert_not_contains { + ($string:expr, $($test:expr),+ $(,)?) => { + let string = &$string; // This might be a function call or something + if !($(!string.contains($test))||+) { + panic!("{:?} contained {:?}", string, [$($test),+]); + } + } +} diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index 1373f758d..8df222115 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -49,6 +49,7 @@ use tokio_rustls::rustls; use tokio_rustls::TlsAcceptor; use tokio_tungstenite::accept_async; +pub mod assertions; pub mod lsp; pub mod pty; mod temp_dir; @@ -1924,7 +1925,7 @@ pub fn test_pty2(args: &str, data: Vec<PtyData>) { println!("ECHO: {}", echo.escape_debug()); // Windows may also echo the previous line, so only check the end - assert!(normalize_text(&echo).ends_with(&normalize_text(s))); + assert_ends_with!(normalize_text(&echo), normalize_text(s)); } PtyData::Output(s) => { let mut line = String::new(); |