]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_span/src/tests.rs
Rollup merge of #103653 - GuillaumeGomez:missing-impl-private-json, r=notriddle
[rust.git] / compiler / rustc_span / src / tests.rs
1 use super::*;
2
3 #[test]
4 fn test_lookup_line() {
5     let source = "abcdefghijklm\nabcdefghij\n...".to_owned();
6     let sf =
7         SourceFile::new(FileName::Anon(0), source, BytePos(3), SourceFileHashAlgorithm::Sha256);
8     sf.lines(|lines| assert_eq!(lines, &[BytePos(3), BytePos(17), BytePos(28)]));
9
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));
13
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));
17
18     assert_eq!(sf.lookup_line(BytePos(28)), Some(2));
19     assert_eq!(sf.lookup_line(BytePos(29)), Some(2));
20 }
21
22 #[test]
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);
31     }
32     check("", "", &[]);
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", &[]);
43 }