Skip to content

Commit a011b6c

Browse files
committed
fix: correct existing tests for WideChar in lib 'line-index' and add more tests
1 parent 9a3167e commit a011b6c

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

lib/line-index/src/tests.rs

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{LineIndex, TextSize, WideChar};
1+
use crate::{LineCol, LineIndex, TextSize, WideChar, WideEncoding, WideLineCol};
22

33
macro_rules! test {
44
(
@@ -102,7 +102,7 @@ test!(
102102
case: multi_byte_with_new_lines,
103103
text: "01\t345\n789abcΔf01234567\u{07}9\nbcΔf",
104104
lines: vec![7, 27],
105-
multi_byte_chars: vec![(1, (13, 15)), (2, (29, 31))],
105+
multi_byte_chars: vec![(1, (6, 8)), (2, (2, 4))],
106106
);
107107

108108
test!(
@@ -118,3 +118,27 @@ test!(
118118
lines: vec![16],
119119
multi_byte_chars: vec![],
120120
);
121+
122+
#[test]
123+
fn test_try_line_col() {
124+
let text = "\n\n\n\n\n宽3456";
125+
assert_eq!(&text[5..8], "宽");
126+
assert_eq!(&text[11..12], "6");
127+
let line_index = LineIndex::new(text);
128+
let before_6 = TextSize::from(11);
129+
let line_col = line_index.try_line_col(before_6);
130+
assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
131+
}
132+
133+
#[test]
134+
fn test_to_wide() {
135+
let text = "\n\n\n\n\n宽3456";
136+
assert_eq!(&text[5..8], "宽");
137+
assert_eq!(&text[11..12], "6");
138+
let line_index = LineIndex::new(text);
139+
let before_6 = TextSize::from(11);
140+
let line_col = line_index.try_line_col(before_6);
141+
assert_eq!(line_col, Some(LineCol { line: 5, col: 6 }));
142+
let wide_line_col = line_index.to_wide(WideEncoding::Utf16, line_col.unwrap());
143+
assert_eq!(wide_line_col, Some(WideLineCol { line: 5, col: 4 }));
144+
}

0 commit comments

Comments
 (0)