]> git.lizzy.rs Git - rust.git/blob - src/librustc_span/analyze_source_file/tests.rs
Rollup merge of #68511 - tmiasko:ignore-license, r=alexcrichton
[rust.git] / src / librustc_span / analyze_source_file / tests.rs
1 use super::*;
2
3 macro_rules! test {
4     (case: $test_name:ident,
5      text: $text:expr,
6      source_file_start_pos: $source_file_start_pos:expr,
7      lines: $lines:expr,
8      multi_byte_chars: $multi_byte_chars:expr,
9      non_narrow_chars: $non_narrow_chars:expr,) => {
10         #[test]
11         fn $test_name() {
12             let (lines, multi_byte_chars, non_narrow_chars) =
13                 analyze_source_file($text, BytePos($source_file_start_pos));
14
15             let expected_lines: Vec<BytePos> = $lines.into_iter().map(|pos| BytePos(pos)).collect();
16
17             assert_eq!(lines, expected_lines);
18
19             let expected_mbcs: Vec<MultiByteChar> = $multi_byte_chars
20                 .into_iter()
21                 .map(|(pos, bytes)| MultiByteChar { pos: BytePos(pos), bytes })
22                 .collect();
23
24             assert_eq!(multi_byte_chars, expected_mbcs);
25
26             let expected_nncs: Vec<NonNarrowChar> = $non_narrow_chars
27                 .into_iter()
28                 .map(|(pos, width)| NonNarrowChar::new(BytePos(pos), width))
29                 .collect();
30
31             assert_eq!(non_narrow_chars, expected_nncs);
32         }
33     };
34 }
35
36 test!(
37     case: empty_text,
38     text: "",
39     source_file_start_pos: 0,
40     lines: vec![],
41     multi_byte_chars: vec![],
42     non_narrow_chars: vec![],
43 );
44
45 test!(
46     case: newlines_short,
47     text: "a\nc",
48     source_file_start_pos: 0,
49     lines: vec![0, 2],
50     multi_byte_chars: vec![],
51     non_narrow_chars: vec![],
52 );
53
54 test!(
55     case: newlines_long,
56     text: "012345678\nabcdef012345678\na",
57     source_file_start_pos: 0,
58     lines: vec![0, 10, 26],
59     multi_byte_chars: vec![],
60     non_narrow_chars: vec![],
61 );
62
63 test!(
64     case: newline_and_multi_byte_char_in_same_chunk,
65     text: "01234β789\nbcdef0123456789abcdef",
66     source_file_start_pos: 0,
67     lines: vec![0, 11],
68     multi_byte_chars: vec![(5, 2)],
69     non_narrow_chars: vec![],
70 );
71
72 test!(
73     case: newline_and_control_char_in_same_chunk,
74     text: "01234\u{07}6789\nbcdef0123456789abcdef",
75     source_file_start_pos: 0,
76     lines: vec![0, 11],
77     multi_byte_chars: vec![],
78     non_narrow_chars: vec![(5, 0)],
79 );
80
81 test!(
82     case: multi_byte_char_short,
83     text: "aβc",
84     source_file_start_pos: 0,
85     lines: vec![0],
86     multi_byte_chars: vec![(1, 2)],
87     non_narrow_chars: vec![],
88 );
89
90 test!(
91     case: multi_byte_char_long,
92     text: "0123456789abcΔf012345β",
93     source_file_start_pos: 0,
94     lines: vec![0],
95     multi_byte_chars: vec![(13, 2), (22, 2)],
96     non_narrow_chars: vec![],
97 );
98
99 test!(
100     case: multi_byte_char_across_chunk_boundary,
101     text: "0123456789abcdeΔ123456789abcdef01234",
102     source_file_start_pos: 0,
103     lines: vec![0],
104     multi_byte_chars: vec![(15, 2)],
105     non_narrow_chars: vec![],
106 );
107
108 test!(
109     case: multi_byte_char_across_chunk_boundary_tail,
110     text: "0123456789abcdeΔ....",
111     source_file_start_pos: 0,
112     lines: vec![0],
113     multi_byte_chars: vec![(15, 2)],
114     non_narrow_chars: vec![],
115 );
116
117 test!(
118     case: non_narrow_short,
119     text: "0\t2",
120     source_file_start_pos: 0,
121     lines: vec![0],
122     multi_byte_chars: vec![],
123     non_narrow_chars: vec![(1, 4)],
124 );
125
126 test!(
127     case: non_narrow_long,
128     text: "01\t3456789abcdef01234567\u{07}9",
129     source_file_start_pos: 0,
130     lines: vec![0],
131     multi_byte_chars: vec![],
132     non_narrow_chars: vec![(2, 4), (24, 0)],
133 );
134
135 test!(
136     case: output_offset_all,
137     text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
138     source_file_start_pos: 1000,
139     lines: vec![0 + 1000, 7 + 1000, 27 + 1000],
140     multi_byte_chars: vec![(13 + 1000, 2), (29 + 1000, 2)],
141     non_narrow_chars: vec![(2 + 1000, 4), (24 + 1000, 0)],
142 );