oxedyne/fe2o3/fe2o3_text/tests/highlight.rs
9.3 KiB, 7 runs
created by r1870400018:1117, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_text::{ |
| 2 | Text, |
| 3 | highlight::{ |
| 4 | Highlight, |
| 5 | }, |
| 6 | lines::{ |
| 7 | LineRange, |
| 8 | TextLines, |
| 9 | }, |
| 10 | }; |
| 11 | |
| 12 | use oxedyne_fe2o3_core::{ |
| 13 | prelude::*, |
| 14 | test::test_it, |
| 15 | }; |
| 16 | use oxedyne_fe2o3_geom::{ |
| 17 | dim::Coord, |
| 18 | }; |
| 19 | |
| 20 | |
| 21 | #[derive(Clone, Debug, Default)] |
| 22 | enum TextType { |
| 23 | #[default] |
| 24 | A, |
| 25 | } |
| 26 | |
| 27 | #[allow(dead_code)] |
| 28 | #[derive(Clone, Debug)] |
| 29 | enum HighlightData<'a> { |
| 30 | X(&'a str), |
| 31 | } |
| 32 | |
| 33 | impl<'a> Default for HighlightData<'a> { |
| 34 | fn default() -> Self { |
| 35 | Self::X("") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | pub fn test_highlight(filter: &'static str) -> Outcome<()> { |
| 40 | |
| 41 | res!(test_it(filter, &["Highlights 000", "all", "highlight"], || { |
| 42 | let original_text = [ |
| 43 | // 00000000001111111111222 |
| 44 | // 01234567890123456789012 |
| 45 | "This is the first line.", |
| 46 | "Make your selection:", |
| 47 | " a. Do this", |
| 48 | " c. Do that other", |
| 49 | " d. Do nothing", |
| 50 | ]; |
| 51 | let mut tlines = TextLines::<TextType, HighlightData>::default(); |
| 52 | tlines.append_text_line( |
| 53 | Text::new(original_text[0], None), |
| 54 | Some(Highlight::new( |
| 55 | LineRange::new(0usize, (12usize, 16)), |
| 56 | 13, |
| 57 | None, |
| 58 | )), |
| 59 | ); |
| 60 | tlines.append_text_line( |
| 61 | Text::new(original_text[1], None), |
| 62 | None, |
| 63 | ); |
| 64 | tlines.append_text_line( |
| 65 | Text::new(original_text[2], Some(TextType::A)), |
| 66 | Some(Highlight::new( |
| 67 | LineRange::new(0usize, (9usize, 15)), |
| 68 | 0, |
| 69 | Some(HighlightData::X("this")), |
| 70 | )), |
| 71 | ); |
| 72 | tlines.append_text_line( |
| 73 | Text::new(original_text[3], Some(TextType::A)), |
| 74 | Some(Highlight::new( |
| 75 | LineRange::new(0usize, (9usize, 21)), |
| 76 | 0, |
| 77 | Some(HighlightData::X("that other")), |
| 78 | )), |
| 79 | ); |
| 80 | tlines.append_text_line( |
| 81 | Text::new(original_text[4], Some(TextType::A)), |
| 82 | Some(Highlight::new( |
| 83 | LineRange::new(0usize, (9usize, 18)), |
| 84 | 0, |
| 85 | Some(HighlightData::X("nothing")), |
| 86 | )), |
| 87 | ); |
| 88 | |
| 89 | // When each text was added, the associated highlight line should have incremented. |
| 90 | let expected_text = original_text.clone(); |
| 91 | let expected_highlight_lines = [0usize, 2, 3, 4]; |
| 92 | test!("Original text lines:"); |
| 93 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 94 | for (i, line) in tlines.lines.iter().enumerate() { |
| 95 | test!("{:02}: '{}'", i, line); |
| 96 | req!(expected_text[i], line.txt.as_str()); |
| 97 | } |
| 98 | test!("Original highlights:"); |
| 99 | if let Some(highlighter) = &tlines.highlighter { |
| 100 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 101 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 102 | test!("{:?}", highlight); |
| 103 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Break first line inside the associated highlight. This should delete the highlight and |
| 108 | // increment the line for the remainder. |
| 109 | tlines.enter_new_line(&mut Coord::from((14usize, 0))); |
| 110 | let expected_text = [ |
| 111 | "This is the fi", |
| 112 | "rst line.", |
| 113 | "Make your selection:", |
| 114 | " a. Do this", |
| 115 | " c. Do that other", |
| 116 | " d. Do nothing", |
| 117 | ]; |
| 118 | let expected_highlight_lines = [3, 4, 5]; |
| 119 | test!("Break first line:"); |
| 120 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 121 | for (i, line) in tlines.lines.iter().enumerate() { |
| 122 | test!("{:02}: '{}'", i, line); |
| 123 | req!(expected_text[i], line.txt.as_str()); |
| 124 | } |
| 125 | test!("Resulting highlights:"); |
| 126 | if let Some(highlighter) = &tlines.highlighter { |
| 127 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 128 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 129 | test!("{:?}", highlight); |
| 130 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Rejoin the first line inside the associated highlight. This should delete the highlight and |
| 135 | // increment the line for the remainder. |
| 136 | tlines.delete_char(&mut Coord::from((14usize, 0))); |
| 137 | let expected_text = original_text.clone(); |
| 138 | let expected_highlight_lines = [2, 3, 4]; |
| 139 | test!("Rejoin first line:"); |
| 140 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 141 | for (i, line) in tlines.lines.iter().enumerate() { |
| 142 | test!("{:02}: '{}'", i, line); |
| 143 | req!(expected_text[i], line.txt.as_str()); |
| 144 | } |
| 145 | test!("Resulting highlights:"); |
| 146 | if let Some(highlighter) = &tlines.highlighter { |
| 147 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 148 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 149 | test!("{:?}", highlight); |
| 150 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Insert a new line of text. |
| 155 | // 00000000001111111111222 |
| 156 | // 01234567890123456789012 |
| 157 | let new_txt = " b. Forgot this"; |
| 158 | tlines.insert_text_line( |
| 159 | 2, |
| 160 | Text::new(new_txt, Some(TextType::A)), |
| 161 | Some(Highlight::new( |
| 162 | LineRange::new(0usize, (9usize, 19)), |
| 163 | 0, |
| 164 | Some(HighlightData::X("forgot this")), |
| 165 | )), |
| 166 | ); |
| 167 | let expected_text = [ |
| 168 | "This is the first line.", |
| 169 | "Make your selection:", |
| 170 | " a. Do this", |
| 171 | new_txt, |
| 172 | " c. Do that other", |
| 173 | " d. Do nothing", |
| 174 | ]; |
| 175 | let expected_highlight_lines = [2, 3, 4, 5]; |
| 176 | test!("Insert new line:"); |
| 177 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 178 | for (i, line) in tlines.lines.iter().enumerate() { |
| 179 | test!("{:02}: '{}'", i, line); |
| 180 | req!(expected_text[i], line.txt.as_str()); |
| 181 | } |
| 182 | test!("Resulting highlights:"); |
| 183 | if let Some(highlighter) = &tlines.highlighter { |
| 184 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 185 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 186 | test!("{:?}", highlight); |
| 187 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Delete the inserted line. |
| 192 | tlines.remove_line(3); |
| 193 | let expected_text = original_text; |
| 194 | let expected_highlight_lines = [2, 3, 4]; |
| 195 | test!("Delete inserted line:"); |
| 196 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 197 | for (i, line) in tlines.lines.iter().enumerate() { |
| 198 | test!("{:02}: '{}'", i, line); |
| 199 | req!(expected_text[i], line.txt.as_str()); |
| 200 | } |
| 201 | test!("Resulting highlights:"); |
| 202 | if let Some(highlighter) = &tlines.highlighter { |
| 203 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 204 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 205 | test!("{:?}", highlight); |
| 206 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Append another TextLines. |
| 211 | let mut tlines2 = TextLines::<TextType, HighlightData>::default(); |
| 212 | let original_text2 = [ |
| 213 | // 00000000001111111111222 |
| 214 | // 01234567890123456789012 |
| 215 | " e. One more thing", |
| 216 | " f. And another", |
| 217 | ]; |
| 218 | tlines2.append_text_line( |
| 219 | Text::new(original_text2[0], None), |
| 220 | Some(Highlight::new( |
| 221 | LineRange::new(0usize, (9usize, 22)), |
| 222 | 0, |
| 223 | Some(HighlightData::X("one more thing")), |
| 224 | )), |
| 225 | ); |
| 226 | tlines2.append_text_line( |
| 227 | Text::new(original_text2[1], None), |
| 228 | Some(Highlight::new( |
| 229 | LineRange::new(0usize, (9usize, 19)), |
| 230 | 0, |
| 231 | Some(HighlightData::X("and another")), |
| 232 | )), |
| 233 | ); |
| 234 | tlines.append_text_lines(tlines2); |
| 235 | let mut expected_text = original_text.to_vec(); |
| 236 | for line in original_text2 { |
| 237 | expected_text.push(line); |
| 238 | } |
| 239 | let expected_highlight_lines = [2, 3, 4, 5, 6]; |
| 240 | test!("Append another TextLines:"); |
| 241 | req!(expected_text.len(), tlines.len(), "(L: expected, R: actual)"); |
| 242 | for (i, line) in tlines.lines.iter().enumerate() { |
| 243 | test!("{:02}: '{}'", i, line); |
| 244 | req!(expected_text[i], line.txt.as_str()); |
| 245 | } |
| 246 | test!("Resulting highlights:"); |
| 247 | if let Some(highlighter) = &tlines.highlighter { |
| 248 | req!(expected_highlight_lines.len(), highlighter.len(), "(L: expected, R: actual)"); |
| 249 | for (i, highlight) in highlighter.ranges.iter().enumerate() { |
| 250 | test!("{:?}", highlight); |
| 251 | req!(expected_highlight_lines[i], highlight.range.line, "(L: expected, R: actual)"); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | Ok(()) |
| 256 | })); |
| 257 | |
| 258 | |
| 259 | Ok(()) |
| 260 | } |