2019-08-01 00:20:23 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lookup_line() {
|
2021-07-05 00:00:00 +00:00
|
|
|
let source = "abcdefghijklm\nabcdefghij\n...".to_owned();
|
2024-06-22 07:27:59 +00:00
|
|
|
let mut sf = SourceFile::new(
|
|
|
|
FileName::Anon(Hash64::ZERO),
|
|
|
|
source,
|
|
|
|
SourceFileHashAlgorithm::Sha256,
|
|
|
|
Some(SourceFileHashAlgorithm::Sha256),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-09-03 10:15:35 +00:00
|
|
|
sf.start_pos = BytePos(3);
|
2023-08-31 20:12:47 +00:00
|
|
|
assert_eq!(sf.lines(), &[RelativeBytePos(0), RelativeBytePos(14), RelativeBytePos(25)]);
|
2019-08-01 00:20:23 +00:00
|
|
|
|
2023-09-03 10:15:35 +00:00
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(0)), Some(0));
|
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(1)), Some(0));
|
2019-08-01 00:20:23 +00:00
|
|
|
|
2023-09-03 10:15:35 +00:00
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(13)), Some(0));
|
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(14)), Some(1));
|
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(15)), Some(1));
|
2019-08-01 00:20:23 +00:00
|
|
|
|
2023-09-03 10:15:35 +00:00
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(25)), Some(2));
|
|
|
|
assert_eq!(sf.lookup_line(RelativeBytePos(26)), Some(2));
|
2019-08-01 00:20:23 +00:00
|
|
|
}
|
2019-08-14 12:35:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_normalize_newlines() {
|
2019-10-03 00:55:31 +00:00
|
|
|
fn check(before: &str, after: &str, expected_positions: &[u32]) {
|
2019-08-14 12:35:12 +00:00
|
|
|
let mut actual = before.to_string();
|
2019-10-03 00:55:31 +00:00
|
|
|
let mut actual_positions = vec![];
|
|
|
|
normalize_newlines(&mut actual, &mut actual_positions);
|
|
|
|
let actual_positions: Vec<_> = actual_positions.into_iter().map(|nc| nc.pos.0).collect();
|
2019-08-14 12:35:12 +00:00
|
|
|
assert_eq!(actual.as_str(), after);
|
2019-10-03 00:55:31 +00:00
|
|
|
assert_eq!(actual_positions, expected_positions);
|
2019-08-14 12:35:12 +00:00
|
|
|
}
|
2019-10-03 00:55:31 +00:00
|
|
|
check("", "", &[]);
|
|
|
|
check("\n", "\n", &[]);
|
|
|
|
check("\r", "\r", &[]);
|
|
|
|
check("\r\r", "\r\r", &[]);
|
|
|
|
check("\r\n", "\n", &[1]);
|
|
|
|
check("hello world", "hello world", &[]);
|
|
|
|
check("hello\nworld", "hello\nworld", &[]);
|
|
|
|
check("hello\r\nworld", "hello\nworld", &[6]);
|
|
|
|
check("\r\nhello\r\nworld\r\n", "\nhello\nworld\n", &[1, 7, 13]);
|
|
|
|
check("\r\r\n", "\r\n", &[2]);
|
|
|
|
check("hello\rworld", "hello\rworld", &[]);
|
2019-08-14 12:35:12 +00:00
|
|
|
}
|
2024-06-03 06:57:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_trim() {
|
|
|
|
let span = |lo: usize, hi: usize| {
|
|
|
|
Span::new(BytePos::from_usize(lo), BytePos::from_usize(hi), SyntaxContext::root(), None)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Various positions, named for their relation to `start` and `end`.
|
|
|
|
let well_before = 1;
|
|
|
|
let before = 3;
|
|
|
|
let start = 5;
|
|
|
|
let mid = 7;
|
|
|
|
let end = 9;
|
|
|
|
let after = 11;
|
|
|
|
let well_after = 13;
|
|
|
|
|
|
|
|
// The resulting span's context should be that of `self`, not `other`.
|
|
|
|
let other = span(start, end).with_ctxt(SyntaxContext::from_u32(999));
|
|
|
|
|
2024-06-03 07:00:10 +00:00
|
|
|
// Test cases for `trim_end`.
|
|
|
|
|
|
|
|
assert_eq!(span(well_before, before).trim_end(other), Some(span(well_before, before)));
|
|
|
|
assert_eq!(span(well_before, start).trim_end(other), Some(span(well_before, start)));
|
|
|
|
assert_eq!(span(well_before, mid).trim_end(other), Some(span(well_before, start)));
|
|
|
|
assert_eq!(span(well_before, end).trim_end(other), Some(span(well_before, start)));
|
|
|
|
assert_eq!(span(well_before, after).trim_end(other), Some(span(well_before, start)));
|
|
|
|
|
|
|
|
assert_eq!(span(start, mid).trim_end(other), None);
|
|
|
|
assert_eq!(span(start, end).trim_end(other), None);
|
|
|
|
assert_eq!(span(start, after).trim_end(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(mid, end).trim_end(other), None);
|
|
|
|
assert_eq!(span(mid, after).trim_end(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(end, after).trim_end(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(after, well_after).trim_end(other), None);
|
|
|
|
|
2024-06-03 06:57:17 +00:00
|
|
|
// Test cases for `trim_start`.
|
|
|
|
|
|
|
|
assert_eq!(span(after, well_after).trim_start(other), Some(span(after, well_after)));
|
|
|
|
assert_eq!(span(end, well_after).trim_start(other), Some(span(end, well_after)));
|
|
|
|
assert_eq!(span(mid, well_after).trim_start(other), Some(span(end, well_after)));
|
|
|
|
assert_eq!(span(start, well_after).trim_start(other), Some(span(end, well_after)));
|
|
|
|
assert_eq!(span(before, well_after).trim_start(other), Some(span(end, well_after)));
|
|
|
|
|
|
|
|
assert_eq!(span(mid, end).trim_start(other), None);
|
|
|
|
assert_eq!(span(start, end).trim_start(other), None);
|
|
|
|
assert_eq!(span(before, end).trim_start(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(start, mid).trim_start(other), None);
|
|
|
|
assert_eq!(span(before, mid).trim_start(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(before, start).trim_start(other), None);
|
|
|
|
|
|
|
|
assert_eq!(span(well_before, before).trim_start(other), None);
|
|
|
|
}
|