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