clerk/internal/lsp/lsputil/utf16_test.go (view raw)
| 1 | package lsputil |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestUtf16Col_Basic(t *testing.T) { |
| 6 | col := Utf16Col("hello", 3) |
| 7 | if col != 3 { |
| 8 | t.Errorf("Utf16Col for ASCII = %d, want 3", col) |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | func TestUtf16Col_Cyrillic(t *testing.T) { |
| 13 | // "Привет" is 6 Cyrillic chars = 12 bytes, 6 UTF-16 code units |
| 14 | line := "Привет" |
| 15 | col := Utf16Col(line, len(line)) |
| 16 | if col != 6 { |
| 17 | t.Errorf("Utf16Col for 'Привет' = %d, want 6", col) |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | func TestUtf16Len_ASCII(t *testing.T) { |
| 22 | l := Utf16Len("hello world", 0, 5) |
| 23 | if l != 5 { |
| 24 | t.Errorf("Utf16Len = %d, want 5", l) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | func TestUtf16Len_Cyrillic(t *testing.T) { |
| 29 | // "Привет" = 6 UTF-16 code units |
| 30 | l := Utf16Len("Привет мир", 0, 12) |
| 31 | if l != 6 { |
| 32 | t.Errorf("Utf16Len for 'Привет' = %d, want 6", l) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | func TestLineCol_Basic(t *testing.T) { |
| 37 | line, col := LineCol("hello\nworld", 11) |
| 38 | if line != 1 || col != 5 { |
| 39 | t.Errorf("LineCol = (%d,%d), want (1,5)", line, col) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestLineCol_FirstLine(t *testing.T) { |
| 44 | line, col := LineCol("hello world", 5) |
| 45 | if line != 0 || col != 5 { |
| 46 | t.Errorf("LineCol = (%d,%d), want (0,5)", line, col) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestLineCol_LineStart(t *testing.T) { |
| 51 | // offset at the first char of line 1 must be (1,0), not the tail of line 0 |
| 52 | line, col := LineCol("ab\ncd", 3) |
| 53 | if line != 1 || col != 0 { |
| 54 | t.Errorf("LineCol(3) = (%d,%d), want (1,0)", line, col) |
| 55 | } |
| 56 | // newline char itself belongs to the line it ends |
| 57 | line, col = LineCol("ab\ncd", 2) |
| 58 | if line != 0 || col != 2 { |
| 59 | t.Errorf("LineCol(2) = (%d,%d), want (0,2)", line, col) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestLineCol_BlankLine(t *testing.T) { |
| 64 | // "a\n\nb": line 1 is blank; offset 3 is the start of line 2 |
| 65 | line, col := LineCol("a\n\nb", 3) |
| 66 | if line != 2 || col != 0 { |
| 67 | t.Errorf("LineCol(3) = (%d,%d), want (2,0)", line, col) |
| 68 | } |
| 69 | line, col = LineCol("a\n\nb", 2) |
| 70 | if line != 1 || col != 0 { |
| 71 | t.Errorf("LineCol(2) = (%d,%d), want (1,0)", line, col) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestLineCol_CRLF(t *testing.T) { |
| 76 | line, col := LineCol("ab\r\ncd", 4) |
| 77 | if line != 1 || col != 0 { |
| 78 | t.Errorf("LineCol(4) = (%d,%d), want (1,0)", line, col) |
| 79 | } |
| 80 | line, col = LineCol("ab\r\ncd", 2) |
| 81 | if line != 0 || col != 2 { |
| 82 | t.Errorf("LineCol(2) = (%d,%d), want (0,2)", line, col) |
| 83 | } |
| 84 | } |