| 1 | #[cfg(test)] |
| 2 | #[allow(clippy::module_inception)] |
| 3 | pub mod ansi_test_utils { |
| 4 | use ansi_term; |
| 5 | |
| 6 | use crate::ansi; |
| 7 | use crate::config::Config; |
| 8 | use crate::delta::State; |
| 9 | use crate::paint; |
| 10 | use crate::style::Style; |
| 11 | |
| 12 | // Check if `output[line_number]` start with `expected_prefix` |
| 13 | // Then check if the first style in the line is the `expected_style` |
| 14 | pub fn assert_line_has_style( |
| 15 | output: &str, |
| 16 | line_number: usize, |
| 17 | expected_prefix: &str, |
| 18 | expected_style: &str, |
| 19 | config: &Config, |
| 20 | ) { |
| 21 | assert!(_line_has_style( |
| 22 | output, |
| 23 | line_number, |
| 24 | expected_prefix, |
| 25 | expected_style, |
| 26 | config, |
| 27 | false, |
| 28 | )); |
| 29 | } |
| 30 | |
| 31 | // Check if `output[line_number]` start with `expected_prefix` |
| 32 | // Then check if it contains the `expected_substring` with the corresponding `expected_style` |
| 33 | // If the line contains multiples times the `expected_style`, will only compare with the first |
| 34 | // item found |
| 35 | pub fn assert_line_contain_substring_style( |
| 36 | output: &str, |
| 37 | line_number: usize, |
| 38 | expected_prefix: &str, |
| 39 | expected_substring: &str, |
| 40 | expected_style: &str, |
| 41 | config: &Config, |
| 42 | ) { |
| 43 | assert_eq!( |
| 44 | expected_substring, |
| 45 | _line_get_substring_matching_style( |
| 46 | output, |
| 47 | line_number, |
| 48 | expected_prefix, |
| 49 | expected_style, |
| 50 | config, |
| 51 | ) |
| 52 | .unwrap() |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | // Check if `output[line_number]` start with `expected_prefix` |
| 57 | // Then check if the line does not contains the `expected_style` |
| 58 | pub fn assert_line_does_not_contain_substring_style( |
| 59 | output: &str, |
| 60 | line_number: usize, |
| 61 | expected_prefix: &str, |
| 62 | expected_style: &str, |
| 63 | config: &Config, |
| 64 | ) { |
| 65 | assert!(_line_get_substring_matching_style( |
| 66 | output, |
| 67 | line_number, |
| 68 | expected_prefix, |
| 69 | expected_style, |
| 70 | config, |
| 71 | ) |
| 72 | .is_none()); |
| 73 | } |
| 74 | |
| 75 | pub fn assert_line_does_not_have_style( |
| 76 | output: &str, |
| 77 | line_number: usize, |
| 78 | expected_prefix: &str, |
| 79 | expected_style: &str, |
| 80 | config: &Config, |
| 81 | ) { |
| 82 | assert!(!_line_has_style( |
| 83 | output, |
| 84 | line_number, |
| 85 | expected_prefix, |
| 86 | expected_style, |
| 87 | config, |
| 88 | false, |
| 89 | )); |
| 90 | } |
| 91 | |
| 92 | pub fn assert_line_has_4_bit_color_style( |
| 93 | output: &str, |
| 94 | line_number: usize, |
| 95 | expected_prefix: &str, |
| 96 | expected_style: &str, |
| 97 | config: &Config, |
| 98 | ) { |
| 99 | assert!(_line_has_style( |
| 100 | output, |
| 101 | line_number, |
| 102 | expected_prefix, |
| 103 | expected_style, |
| 104 | config, |
| 105 | true, |
| 106 | )); |
| 107 | } |
| 108 | |
| 109 | pub fn assert_line_has_no_color(output: &str, line_number: usize, expected_prefix: &str) { |
| 110 | let line = output.lines().nth(line_number).unwrap(); |
| 111 | let stripped_line = ansi::strip_ansi_codes(line); |
| 112 | assert!(stripped_line.starts_with(expected_prefix)); |
| 113 | assert_eq!(line, stripped_line); |
| 114 | } |
| 115 | |
| 116 | /// Assert that the specified line number of output (a) has, after stripping ANSI codes, a |
| 117 | /// substring starting at `substring_begin` equal to `expected_substring` and (b) in its raw |
| 118 | /// form contains a version of that substring syntax-highlighted according to |
| 119 | /// `language_extension`. |
| 120 | pub fn assert_line_has_syntax_highlighted_substring( |
| 121 | output: &str, |
| 122 | line_number: usize, |
| 123 | substring_begin: usize, |
| 124 | expected_substring: &str, |
| 125 | filename_for_highlighting: &str, |
| 126 | state: State, |
| 127 | config: &Config, |
| 128 | ) { |
| 129 | assert!( |
| 130 | filename_for_highlighting.contains('.'), |
| 131 | "expecting filename, not just a file extension" |
| 132 | ); |
| 133 | let line = output.lines().nth(line_number).unwrap(); |
| 134 | let substring_end = substring_begin + expected_substring.len(); |
| 135 | let substring = &ansi::strip_ansi_codes(line)[substring_begin..substring_end]; |
| 136 | assert_eq!(substring, expected_substring); |
| 137 | let painted_substring = paint_line(substring, filename_for_highlighting, state, config); |
| 138 | // remove trailing newline appended by paint::paint_lines. |
| 139 | assert!(line.contains(painted_substring.trim_end())); |
| 140 | } |
| 141 | |
| 142 | pub fn assert_has_color_other_than_plus_color(string: &str, config: &Config) { |
| 143 | let (string_without_any_color, string_with_plus_color_only) = |
| 144 | get_color_variants(string, config); |
| 145 | assert_ne!(string, string_without_any_color); |
| 146 | assert_ne!(string, string_with_plus_color_only); |
| 147 | } |
| 148 | |
| 149 | pub fn assert_has_plus_color_only(string: &str, config: &Config) { |
| 150 | let (string_without_any_color, string_with_plus_color_only) = |
| 151 | get_color_variants(string, config); |
| 152 | assert_ne!(string, string_without_any_color); |
| 153 | assert_eq!(string, string_with_plus_color_only); |
| 154 | } |
| 155 | |
| 156 | pub fn get_color_variants(string: &str, config: &Config) -> (String, String) { |
| 157 | let string_without_any_color = ansi::strip_ansi_codes(string); |
| 158 | let string_with_plus_color_only = config |
| 159 | .plus_style |
| 160 | .ansi_term_style |
| 161 | .paint(&string_without_any_color); |
| 162 | ( |
| 163 | string_without_any_color.to_string(), |
| 164 | string_with_plus_color_only.to_string(), |
| 165 | ) |
| 166 | } |
| 167 | |
| 168 | pub fn paint_line( |
| 169 | line: &str, |
| 170 | filename_for_highlighting: &str, |
| 171 | state: State, |
| 172 | config: &Config, |
| 173 | ) -> String { |
| 174 | let mut output_buffer = String::new(); |
| 175 | let mut unused_writer = Vec::<u8>::new(); |
| 176 | let mut painter = paint::Painter::new(&mut unused_writer, config); |
| 177 | let syntax_highlighted_style = Style { |
| 178 | is_syntax_highlighted: true, |
| 179 | ..Style::new() |
| 180 | }; |
| 181 | painter.set_syntax(Some(filename_for_highlighting)); |
| 182 | painter.set_highlighter(); |
| 183 | let lines = vec![(line.to_string(), state)]; |
| 184 | let syntax_style_sections = paint::get_syntax_style_sections_for_lines( |
| 185 | &lines, |
| 186 | painter.highlighter.as_mut(), |
| 187 | config, |
| 188 | ); |
| 189 | let diff_style_sections = vec![vec![(syntax_highlighted_style, lines[0].0.as_str())]]; |
| 190 | paint::Painter::paint_lines( |
| 191 | &lines, |
| 192 | &syntax_style_sections, |
| 193 | &diff_style_sections, |
| 194 | &[false], |
| 195 | &mut output_buffer, |
| 196 | config, |
| 197 | &mut None, |
| 198 | None, |
| 199 | paint::BgShouldFill::default(), |
| 200 | ); |
| 201 | output_buffer |
| 202 | } |
| 203 | |
| 204 | fn _line_extract<'a>(output: &'a str, line_number: usize, expected_prefix: &str) -> &'a str { |
| 205 | let line = output.lines().nth(line_number).unwrap(); |
| 206 | assert!(ansi::strip_ansi_codes(line).starts_with(expected_prefix)); |
| 207 | line |
| 208 | } |
| 209 | |
| 210 | fn _line_get_substring_matching_style<'a>( |
| 211 | output: &'a str, |
| 212 | line_number: usize, |
| 213 | expected_prefix: &str, |
| 214 | expected_style: &str, |
| 215 | config: &Config, |
| 216 | ) -> Option<&'a str> { |
| 217 | let line = _line_extract(output, line_number, expected_prefix); |
| 218 | let style = Style::from_str( |
| 219 | expected_style, |
| 220 | None, |
| 221 | None, |
| 222 | config.true_color, |
| 223 | config.git_config.as_ref(), |
| 224 | ); |
| 225 | style.get_matching_substring(line) |
| 226 | } |
| 227 | |
| 228 | fn _line_has_style( |
| 229 | output: &str, |
| 230 | line_number: usize, |
| 231 | expected_prefix: &str, |
| 232 | expected_style: &str, |
| 233 | config: &Config, |
| 234 | _4_bit_color: bool, |
| 235 | ) -> bool { |
| 236 | let line = _line_extract(output, line_number, expected_prefix); |
| 237 | let mut style = Style::from_str( |
| 238 | expected_style, |
| 239 | None, |
| 240 | None, |
| 241 | config.true_color, |
| 242 | config.git_config(), |
| 243 | ); |
| 244 | if _4_bit_color { |
| 245 | style.ansi_term_style.foreground = style |
| 246 | .ansi_term_style |
| 247 | .foreground |
| 248 | .map(ansi_term_fixed_foreground_to_4_bit_color); |
| 249 | } |
| 250 | style.is_applied_to(line) |
| 251 | } |
| 252 | |
| 253 | fn ansi_term_fixed_foreground_to_4_bit_color(color: ansi_term::Color) -> ansi_term::Color { |
| 254 | match color { |
| 255 | ansi_term::Color::Fixed(30) => ansi_term::Color::Black, |
| 256 | ansi_term::Color::Fixed(31) => ansi_term::Color::Red, |
| 257 | ansi_term::Color::Fixed(32) => ansi_term::Color::Green, |
| 258 | ansi_term::Color::Fixed(33) => ansi_term::Color::Yellow, |
| 259 | ansi_term::Color::Fixed(34) => ansi_term::Color::Blue, |
| 260 | ansi_term::Color::Fixed(35) => ansi_term::Color::Purple, |
| 261 | ansi_term::Color::Fixed(36) => ansi_term::Color::Cyan, |
| 262 | ansi_term::Color::Fixed(37) => ansi_term::Color::White, |
| 263 | color => panic!( |
| 264 | "Invalid 4-bit color: {:?}. \ |
| 265 | (Add bright color entries to this map if needed for tests.", |
| 266 | color |
| 267 | ), |
| 268 | } |
| 269 | } |
| 270 | } |