Skip to content

Commit 6cc43db

Browse files
committed
libsyntax_pos: Unconfigure tests during normal build
1 parent 5947db1 commit 6cc43db

File tree

7 files changed

+210
-211
lines changed

7 files changed

+210
-211
lines changed

src/libsyntax_pos/analyze_source_file.rs

Lines changed: 3 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use unicode_width::UnicodeWidthChar;
22
use super::*;
33

4+
#[cfg(test)]
5+
mod tests;
6+
47
/// Finds all newlines, multi-byte characters, and non-narrow characters in a
58
/// SourceFile.
69
///
@@ -271,155 +274,3 @@ fn analyze_source_file_generic(src: &str,
271274

272275
i - scan_len
273276
}
274-
275-
276-
277-
macro_rules! test {
278-
(case: $test_name:ident,
279-
text: $text:expr,
280-
source_file_start_pos: $source_file_start_pos:expr,
281-
lines: $lines:expr,
282-
multi_byte_chars: $multi_byte_chars:expr,
283-
non_narrow_chars: $non_narrow_chars:expr,) => (
284-
285-
#[test]
286-
fn $test_name() {
287-
288-
let (lines, multi_byte_chars, non_narrow_chars) =
289-
analyze_source_file($text, BytePos($source_file_start_pos));
290-
291-
let expected_lines: Vec<BytePos> = $lines
292-
.into_iter()
293-
.map(|pos| BytePos(pos))
294-
.collect();
295-
296-
assert_eq!(lines, expected_lines);
297-
298-
let expected_mbcs: Vec<MultiByteChar> = $multi_byte_chars
299-
.into_iter()
300-
.map(|(pos, bytes)| MultiByteChar {
301-
pos: BytePos(pos),
302-
bytes,
303-
})
304-
.collect();
305-
306-
assert_eq!(multi_byte_chars, expected_mbcs);
307-
308-
let expected_nncs: Vec<NonNarrowChar> = $non_narrow_chars
309-
.into_iter()
310-
.map(|(pos, width)| {
311-
NonNarrowChar::new(BytePos(pos), width)
312-
})
313-
.collect();
314-
315-
assert_eq!(non_narrow_chars, expected_nncs);
316-
})
317-
}
318-
319-
test!(
320-
case: empty_text,
321-
text: "",
322-
source_file_start_pos: 0,
323-
lines: vec![],
324-
multi_byte_chars: vec![],
325-
non_narrow_chars: vec![],
326-
);
327-
328-
test!(
329-
case: newlines_short,
330-
text: "a\nc",
331-
source_file_start_pos: 0,
332-
lines: vec![0, 2],
333-
multi_byte_chars: vec![],
334-
non_narrow_chars: vec![],
335-
);
336-
337-
test!(
338-
case: newlines_long,
339-
text: "012345678\nabcdef012345678\na",
340-
source_file_start_pos: 0,
341-
lines: vec![0, 10, 26],
342-
multi_byte_chars: vec![],
343-
non_narrow_chars: vec![],
344-
);
345-
346-
test!(
347-
case: newline_and_multi_byte_char_in_same_chunk,
348-
text: "01234β789\nbcdef0123456789abcdef",
349-
source_file_start_pos: 0,
350-
lines: vec![0, 11],
351-
multi_byte_chars: vec![(5, 2)],
352-
non_narrow_chars: vec![],
353-
);
354-
355-
test!(
356-
case: newline_and_control_char_in_same_chunk,
357-
text: "01234\u{07}6789\nbcdef0123456789abcdef",
358-
source_file_start_pos: 0,
359-
lines: vec![0, 11],
360-
multi_byte_chars: vec![],
361-
non_narrow_chars: vec![(5, 0)],
362-
);
363-
364-
test!(
365-
case: multi_byte_char_short,
366-
text: "aβc",
367-
source_file_start_pos: 0,
368-
lines: vec![0],
369-
multi_byte_chars: vec![(1, 2)],
370-
non_narrow_chars: vec![],
371-
);
372-
373-
test!(
374-
case: multi_byte_char_long,
375-
text: "0123456789abcΔf012345β",
376-
source_file_start_pos: 0,
377-
lines: vec![0],
378-
multi_byte_chars: vec![(13, 2), (22, 2)],
379-
non_narrow_chars: vec![],
380-
);
381-
382-
test!(
383-
case: multi_byte_char_across_chunk_boundary,
384-
text: "0123456789abcdeΔ123456789abcdef01234",
385-
source_file_start_pos: 0,
386-
lines: vec![0],
387-
multi_byte_chars: vec![(15, 2)],
388-
non_narrow_chars: vec![],
389-
);
390-
391-
test!(
392-
case: multi_byte_char_across_chunk_boundary_tail,
393-
text: "0123456789abcdeΔ....",
394-
source_file_start_pos: 0,
395-
lines: vec![0],
396-
multi_byte_chars: vec![(15, 2)],
397-
non_narrow_chars: vec![],
398-
);
399-
400-
test!(
401-
case: non_narrow_short,
402-
text: "0\t2",
403-
source_file_start_pos: 0,
404-
lines: vec![0],
405-
multi_byte_chars: vec![],
406-
non_narrow_chars: vec![(1, 4)],
407-
);
408-
409-
test!(
410-
case: non_narrow_long,
411-
text: "01\t3456789abcdef01234567\u{07}9",
412-
source_file_start_pos: 0,
413-
lines: vec![0],
414-
multi_byte_chars: vec![],
415-
non_narrow_chars: vec![(2, 4), (24, 0)],
416-
);
417-
418-
test!(
419-
case: output_offset_all,
420-
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
421-
source_file_start_pos: 1000,
422-
lines: vec![0 + 1000, 7 + 1000, 27 + 1000],
423-
multi_byte_chars: vec![(13 + 1000, 2), (29 + 1000, 2)],
424-
non_narrow_chars: vec![(2 + 1000, 4), (24 + 1000, 0)],
425-
);
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
11+
#[test]
12+
fn $test_name() {
13+
14+
let (lines, multi_byte_chars, non_narrow_chars) =
15+
analyze_source_file($text, BytePos($source_file_start_pos));
16+
17+
let expected_lines: Vec<BytePos> = $lines
18+
.into_iter()
19+
.map(|pos| BytePos(pos))
20+
.collect();
21+
22+
assert_eq!(lines, expected_lines);
23+
24+
let expected_mbcs: Vec<MultiByteChar> = $multi_byte_chars
25+
.into_iter()
26+
.map(|(pos, bytes)| MultiByteChar {
27+
pos: BytePos(pos),
28+
bytes,
29+
})
30+
.collect();
31+
32+
assert_eq!(multi_byte_chars, expected_mbcs);
33+
34+
let expected_nncs: Vec<NonNarrowChar> = $non_narrow_chars
35+
.into_iter()
36+
.map(|(pos, width)| {
37+
NonNarrowChar::new(BytePos(pos), width)
38+
})
39+
.collect();
40+
41+
assert_eq!(non_narrow_chars, expected_nncs);
42+
})
43+
}
44+
45+
test!(
46+
case: empty_text,
47+
text: "",
48+
source_file_start_pos: 0,
49+
lines: vec![],
50+
multi_byte_chars: vec![],
51+
non_narrow_chars: vec![],
52+
);
53+
54+
test!(
55+
case: newlines_short,
56+
text: "a\nc",
57+
source_file_start_pos: 0,
58+
lines: vec![0, 2],
59+
multi_byte_chars: vec![],
60+
non_narrow_chars: vec![],
61+
);
62+
63+
test!(
64+
case: newlines_long,
65+
text: "012345678\nabcdef012345678\na",
66+
source_file_start_pos: 0,
67+
lines: vec![0, 10, 26],
68+
multi_byte_chars: vec![],
69+
non_narrow_chars: vec![],
70+
);
71+
72+
test!(
73+
case: newline_and_multi_byte_char_in_same_chunk,
74+
text: "01234β789\nbcdef0123456789abcdef",
75+
source_file_start_pos: 0,
76+
lines: vec![0, 11],
77+
multi_byte_chars: vec![(5, 2)],
78+
non_narrow_chars: vec![],
79+
);
80+
81+
test!(
82+
case: newline_and_control_char_in_same_chunk,
83+
text: "01234\u{07}6789\nbcdef0123456789abcdef",
84+
source_file_start_pos: 0,
85+
lines: vec![0, 11],
86+
multi_byte_chars: vec![],
87+
non_narrow_chars: vec![(5, 0)],
88+
);
89+
90+
test!(
91+
case: multi_byte_char_short,
92+
text: "aβc",
93+
source_file_start_pos: 0,
94+
lines: vec![0],
95+
multi_byte_chars: vec![(1, 2)],
96+
non_narrow_chars: vec![],
97+
);
98+
99+
test!(
100+
case: multi_byte_char_long,
101+
text: "0123456789abcΔf012345β",
102+
source_file_start_pos: 0,
103+
lines: vec![0],
104+
multi_byte_chars: vec![(13, 2), (22, 2)],
105+
non_narrow_chars: vec![],
106+
);
107+
108+
test!(
109+
case: multi_byte_char_across_chunk_boundary,
110+
text: "0123456789abcdeΔ123456789abcdef01234",
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: multi_byte_char_across_chunk_boundary_tail,
119+
text: "0123456789abcdeΔ....",
120+
source_file_start_pos: 0,
121+
lines: vec![0],
122+
multi_byte_chars: vec![(15, 2)],
123+
non_narrow_chars: vec![],
124+
);
125+
126+
test!(
127+
case: non_narrow_short,
128+
text: "0\t2",
129+
source_file_start_pos: 0,
130+
lines: vec![0],
131+
multi_byte_chars: vec![],
132+
non_narrow_chars: vec![(1, 4)],
133+
);
134+
135+
test!(
136+
case: non_narrow_long,
137+
text: "01\t3456789abcdef01234567\u{07}9",
138+
source_file_start_pos: 0,
139+
lines: vec![0],
140+
multi_byte_chars: vec![],
141+
non_narrow_chars: vec![(2, 4), (24, 0)],
142+
);
143+
144+
test!(
145+
case: output_offset_all,
146+
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
147+
source_file_start_pos: 1000,
148+
lines: vec![0 + 1000, 7 + 1000, 27 + 1000],
149+
multi_byte_chars: vec![(13 + 1000, 2), (29 + 1000, 2)],
150+
non_narrow_chars: vec![(2 + 1000, 4), (24 + 1000, 0)],
151+
);

src/libsyntax_pos/lib.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ use std::hash::{Hasher, Hash};
4242
use std::ops::{Add, Sub};
4343
use std::path::PathBuf;
4444

45+
#[cfg(test)]
46+
mod tests;
47+
4548
pub struct Globals {
4649
symbol_interner: Lock<symbol::Interner>,
4750
span_interner: Lock<span_encoding::SpanInterner>,
@@ -1420,25 +1423,3 @@ fn lookup_line(lines: &[BytePos], pos: BytePos) -> isize {
14201423
Err(line) => line as isize - 1
14211424
}
14221425
}
1423-
1424-
#[cfg(test)]
1425-
mod tests {
1426-
use super::{lookup_line, BytePos};
1427-
1428-
#[test]
1429-
fn test_lookup_line() {
1430-
1431-
let lines = &[BytePos(3), BytePos(17), BytePos(28)];
1432-
1433-
assert_eq!(lookup_line(lines, BytePos(0)), -1);
1434-
assert_eq!(lookup_line(lines, BytePos(3)), 0);
1435-
assert_eq!(lookup_line(lines, BytePos(4)), 0);
1436-
1437-
assert_eq!(lookup_line(lines, BytePos(16)), 0);
1438-
assert_eq!(lookup_line(lines, BytePos(17)), 1);
1439-
assert_eq!(lookup_line(lines, BytePos(18)), 1);
1440-
1441-
assert_eq!(lookup_line(lines, BytePos(28)), 2);
1442-
assert_eq!(lookup_line(lines, BytePos(29)), 2);
1443-
}
1444-
}

0 commit comments

Comments
 (0)