4 fn test_lookup_line() {
5 let source = "abcdefghijklm\nabcdefghij\n...".to_owned();
7 SourceFile::new(FileName::Anon(0), source, BytePos(3), SourceFileHashAlgorithm::Sha256);
8 assert_eq!(sf.lines.as_slice(), &[BytePos(3), BytePos(17), BytePos(28)]);
10 assert_eq!(sf.lookup_line(BytePos(0)), None);
11 assert_eq!(sf.lookup_line(BytePos(3)), Some(0));
12 assert_eq!(sf.lookup_line(BytePos(4)), Some(0));
14 assert_eq!(sf.lookup_line(BytePos(16)), Some(0));
15 assert_eq!(sf.lookup_line(BytePos(17)), Some(1));
16 assert_eq!(sf.lookup_line(BytePos(18)), Some(1));
18 assert_eq!(sf.lookup_line(BytePos(28)), Some(2));
19 assert_eq!(sf.lookup_line(BytePos(29)), Some(2));
23 fn test_normalize_newlines() {
24 fn check(before: &str, after: &str, expected_positions: &[u32]) {
25 let mut actual = before.to_string();
26 let mut actual_positions = vec![];
27 normalize_newlines(&mut actual, &mut actual_positions);
28 let actual_positions: Vec<_> = actual_positions.into_iter().map(|nc| nc.pos.0).collect();
29 assert_eq!(actual.as_str(), after);
30 assert_eq!(actual_positions, expected_positions);
33 check("\n", "\n", &[]);
34 check("\r", "\r", &[]);
35 check("\r\r", "\r\r", &[]);
36 check("\r\n", "\n", &[1]);
37 check("hello world", "hello world", &[]);
38 check("hello\nworld", "hello\nworld", &[]);
39 check("hello\r\nworld", "hello\nworld", &[6]);
40 check("\r\nhello\r\nworld\r\n", "\nhello\nworld\n", &[1, 7, 13]);
41 check("\r\r\n", "\r\n", &[2]);
42 check("hello\rworld", "hello\rworld", &[]);