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