|
| 1 | +//! Collection of assertions and assertion-related helpers. |
| 2 | +
|
| 3 | +#[cfg(test)] |
| 4 | +mod tests; |
| 5 | + |
| 6 | +use std::panic; |
| 7 | +use std::path::Path; |
| 8 | + |
| 9 | +use crate::{fs, regex}; |
| 10 | + |
| 11 | +/// Assert that `actual` is equal to `expected`. |
| 12 | +#[track_caller] |
| 13 | +pub fn assert_equals<A: AsRef<str>, E: AsRef<str>>(actual: A, expected: E) { |
| 14 | + let actual = actual.as_ref(); |
| 15 | + let expected = expected.as_ref(); |
| 16 | + |
| 17 | + if actual != expected { |
| 18 | + eprintln!("=== ACTUAL TEXT ==="); |
| 19 | + eprintln!("{}", actual); |
| 20 | + eprintln!("=== EXPECTED ==="); |
| 21 | + eprintln!("{}", expected); |
| 22 | + panic!("expected text does not match actual text"); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +struct SearchDetails<'assertion_name, 'haystack, 'query> { |
| 27 | + assertion_name: &'assertion_name str, |
| 28 | + haystack: &'haystack str, |
| 29 | + query_kind: &'static str, |
| 30 | + query: &'query str, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'assertion_name, 'haystack, 'query> SearchDetails<'assertion_name, 'haystack, 'query> { |
| 34 | + fn dump(&self) { |
| 35 | + eprintln!("{}:", self.assertion_name); |
| 36 | + eprintln!("=== HAYSTACK ==="); |
| 37 | + eprintln!("{}", self.haystack); |
| 38 | + eprintln!("=== {} ===", self.query_kind); |
| 39 | + eprintln!("{}", self.query); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Assert that `haystack` contains `needle`. |
| 44 | +#[track_caller] |
| 45 | +pub fn assert_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) { |
| 46 | + let haystack = haystack.as_ref(); |
| 47 | + let needle = needle.as_ref(); |
| 48 | + if !haystack.contains(needle) { |
| 49 | + SearchDetails { |
| 50 | + assertion_name: "assert_contains", |
| 51 | + haystack, |
| 52 | + query_kind: "NEEDLE", |
| 53 | + query: needle, |
| 54 | + } |
| 55 | + .dump(); |
| 56 | + panic!("needle was not found in haystack"); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Assert that `haystack` does not contain `needle`. |
| 61 | +#[track_caller] |
| 62 | +pub fn assert_not_contains<H: AsRef<str>, N: AsRef<str>>(haystack: H, needle: N) { |
| 63 | + let haystack = haystack.as_ref(); |
| 64 | + let needle = needle.as_ref(); |
| 65 | + if haystack.contains(needle) { |
| 66 | + SearchDetails { |
| 67 | + assertion_name: "assert_not_contains", |
| 68 | + haystack, |
| 69 | + query_kind: "NEEDLE", |
| 70 | + query: needle, |
| 71 | + } |
| 72 | + .dump(); |
| 73 | + panic!("needle was unexpectedly found in haystack"); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Assert that `haystack` contains the regex `pattern`. |
| 78 | +#[track_caller] |
| 79 | +pub fn assert_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, pattern: N) { |
| 80 | + let haystack = haystack.as_ref(); |
| 81 | + let pattern = pattern.as_ref(); |
| 82 | + let re = regex::Regex::new(pattern).unwrap(); |
| 83 | + if !re.is_match(haystack) { |
| 84 | + SearchDetails { |
| 85 | + assertion_name: "assert_contains_regex", |
| 86 | + haystack, |
| 87 | + query_kind: "REGEX", |
| 88 | + query: pattern, |
| 89 | + } |
| 90 | + .dump(); |
| 91 | + panic!("regex was not found in haystack"); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +/// Assert that `haystack` does not contain the regex `pattern`. |
| 96 | +#[track_caller] |
| 97 | +pub fn assert_not_contains_regex<H: AsRef<str>, N: AsRef<str>>(haystack: H, pattern: N) { |
| 98 | + let haystack = haystack.as_ref(); |
| 99 | + let pattern = pattern.as_ref(); |
| 100 | + let re = regex::Regex::new(pattern).unwrap(); |
| 101 | + if re.is_match(haystack) { |
| 102 | + SearchDetails { |
| 103 | + assertion_name: "assert_not_contains_regex", |
| 104 | + haystack, |
| 105 | + query_kind: "REGEX", |
| 106 | + query: pattern, |
| 107 | + } |
| 108 | + .dump(); |
| 109 | + panic!("regex was unexpectedly found in haystack"); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Assert that `haystack` contains regex `pattern` an `expected_count` number of times. |
| 114 | +#[track_caller] |
| 115 | +pub fn assert_count_is<H: AsRef<str>, N: AsRef<str>>( |
| 116 | + expected_count: usize, |
| 117 | + haystack: H, |
| 118 | + pattern: N, |
| 119 | +) { |
| 120 | + let haystack = haystack.as_ref(); |
| 121 | + let pattern = pattern.as_ref(); |
| 122 | + |
| 123 | + let actual_count = haystack.matches(pattern).count(); |
| 124 | + if expected_count != actual_count { |
| 125 | + let count_fmt = format!( |
| 126 | + "assert_count_is (expected_count = {expected_count}, actual_count = {actual_count})" |
| 127 | + ); |
| 128 | + SearchDetails { assertion_name: &count_fmt, haystack, query_kind: "REGEX", query: pattern } |
| 129 | + .dump(); |
| 130 | + panic!( |
| 131 | + "regex did not appear {expected_count} times in haystack (expected_count = \ |
| 132 | + {expected_count}, actual_count = {actual_count})" |
| 133 | + ); |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/// Assert that all files in `dir1` exist and have the same content in `dir2` |
| 138 | +pub fn assert_dirs_are_equal(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) { |
| 139 | + let dir2 = dir2.as_ref(); |
| 140 | + fs::read_dir_entries(dir1, |entry_path| { |
| 141 | + let entry_name = entry_path.file_name().unwrap(); |
| 142 | + if entry_path.is_dir() { |
| 143 | + assert_dirs_are_equal(&entry_path, &dir2.join(entry_name)); |
| 144 | + } else { |
| 145 | + let path2 = dir2.join(entry_name); |
| 146 | + let file1 = fs::read(&entry_path); |
| 147 | + let file2 = fs::read(&path2); |
| 148 | + |
| 149 | + // We don't use `assert_eq!` because they are `Vec<u8>`, so not great for display. |
| 150 | + // Why not using String? Because there might be minified files or even potentially |
| 151 | + // binary ones, so that would display useless output. |
| 152 | + assert!( |
| 153 | + file1 == file2, |
| 154 | + "`{}` and `{}` have different content", |
| 155 | + entry_path.display(), |
| 156 | + path2.display(), |
| 157 | + ); |
| 158 | + } |
| 159 | + }); |
| 160 | +} |
0 commit comments