1 use super::*;
2
3 #[test]
test_line_index()4 fn test_line_index() {
5 let text = "hello\nworld";
6 let table = [
7 (00, 0, 0),
8 (01, 0, 1),
9 (05, 0, 5),
10 (06, 1, 0),
11 (07, 1, 1),
12 (08, 1, 2),
13 (10, 1, 4),
14 (11, 1, 5),
15 (12, 1, 6),
16 ];
17
18 let index = LineIndex::new(text);
19 for &(offset, line, col) in &table {
20 assert_eq!(index.line_col(offset.into()), LineCol { line, col });
21 }
22
23 let text = "\nhello\nworld";
24 let table = [(0, 0, 0), (1, 1, 0), (2, 1, 1), (6, 1, 5), (7, 2, 0)];
25 let index = LineIndex::new(text);
26 for &(offset, line, col) in &table {
27 assert_eq!(index.line_col(offset.into()), LineCol { line, col });
28 }
29 }
30
31 #[test]
test_char_len()32 fn test_char_len() {
33 assert_eq!('メ'.len_utf8(), 3);
34 assert_eq!('メ'.len_utf16(), 1);
35 }
36
37 #[test]
test_empty_index()38 fn test_empty_index() {
39 let col_index = LineIndex::new(
40 "
41 const C: char = 'x';
42 ",
43 );
44 assert_eq!(col_index.utf16_lines.len(), 0);
45 }
46
47 #[test]
test_single_char()48 fn test_single_char() {
49 let col_index = LineIndex::new(
50 "
51 const C: char = 'メ';
52 ",
53 );
54
55 assert_eq!(col_index.utf16_lines.len(), 1);
56 assert_eq!(col_index.utf16_lines[&1].len(), 1);
57 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
58
59 // UTF-8 to UTF-16, no changes
60 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
61
62 // UTF-8 to UTF-16
63 assert_eq!(col_index.utf8_to_utf16_col(1, 22.into()), 20);
64
65 // UTF-16 to UTF-8, no changes
66 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
67
68 // UTF-16 to UTF-8
69 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21));
70
71 let col_index = LineIndex::new("ab");
72 assert_eq!(col_index.utf16_to_utf8_col(0, 3), TextSize::from(5));
73 }
74
75 #[test]
test_string()76 fn test_string() {
77 let col_index = LineIndex::new(
78 "
79 const C: char = \"メ メ\";
80 ",
81 );
82
83 assert_eq!(col_index.utf16_lines.len(), 1);
84 assert_eq!(col_index.utf16_lines[&1].len(), 2);
85 assert_eq!(col_index.utf16_lines[&1][0], Utf16Char { start: 17.into(), end: 20.into() });
86 assert_eq!(col_index.utf16_lines[&1][1], Utf16Char { start: 21.into(), end: 24.into() });
87
88 // UTF-8 to UTF-16
89 assert_eq!(col_index.utf8_to_utf16_col(1, 15.into()), 15);
90
91 assert_eq!(col_index.utf8_to_utf16_col(1, 21.into()), 19);
92 assert_eq!(col_index.utf8_to_utf16_col(1, 25.into()), 21);
93
94 assert!(col_index.utf8_to_utf16_col(2, 15.into()) == 15);
95
96 // UTF-16 to UTF-8
97 assert_eq!(col_index.utf16_to_utf8_col(1, 15), TextSize::from(15));
98
99 // メ UTF-8: 0xE3 0x83 0xA1, UTF-16: 0x30E1
100 assert_eq!(col_index.utf16_to_utf8_col(1, 17), TextSize::from(17)); // first メ at 17..20
101 assert_eq!(col_index.utf16_to_utf8_col(1, 18), TextSize::from(20)); // space
102 assert_eq!(col_index.utf16_to_utf8_col(1, 19), TextSize::from(21)); // second メ at 21..24
103
104 assert_eq!(col_index.utf16_to_utf8_col(2, 15), TextSize::from(15));
105 }
106
107 #[test]
test_splitlines()108 fn test_splitlines() {
109 fn r(lo: u32, hi: u32) -> TextRange {
110 TextRange::new(lo.into(), hi.into())
111 }
112
113 let text = "a\nbb\nccc\n";
114 let line_index = LineIndex::new(text);
115
116 let actual = line_index.lines(r(0, 9)).collect::<Vec<_>>();
117 let expected = vec![r(0, 2), r(2, 5), r(5, 9)];
118 assert_eq!(actual, expected);
119
120 let text = "";
121 let line_index = LineIndex::new(text);
122
123 let actual = line_index.lines(r(0, 0)).collect::<Vec<_>>();
124 let expected = vec![];
125 assert_eq!(actual, expected);
126
127 let text = "\n";
128 let line_index = LineIndex::new(text);
129
130 let actual = line_index.lines(r(0, 1)).collect::<Vec<_>>();
131 let expected = vec![r(0, 1)];
132 assert_eq!(actual, expected)
133 }
134