all repos

clerk @ 1925839

missing tooling for ledger/hledger

clerk/internal/lsp/lsputil/utf16_test.go (view raw)

Oleksandr Smirnov Oleksandr Smirnov
olexsmir@gmail.com
improve lsputil tests, 2 months ago
1
package lsputil
2
3
import (
4
	"fmt"
5
	"testing"
6
	"unicode/utf8"
7
)
8
9
func TestUtf16Col(t *testing.T) {
10
	tests := map[string]struct {
11
		line   string
12
		offset int
13
		want   int
14
	}{
15
		"ascii":              {"hello", 3, 3},
16
		"cyrillic full line": {"Привіт", len("Привіт"), 6},
17
	}
18
	for name, tt := range tests {
19
		t.Run(name, func(t *testing.T) {
20
			if got := Utf16Col(tt.line, tt.offset); got != tt.want {
21
				t.Errorf("Utf16Col(%q, %d) = %d, want %d", tt.line, tt.offset, got, tt.want)
22
			}
23
		})
24
	}
25
}
26
27
func TestUtf16Len(t *testing.T) {
28
	tests := map[string]struct {
29
		content string
30
		offset  int
31
		end     int
32
		want    int
33
	}{
34
		"ascii":    {"hello world", 0, 5, 5},
35
		"cyrillic": {"Привіт світ", 0, len("Привіт"), 6},
36
	}
37
	for name, tt := range tests {
38
		t.Run(name, func(t *testing.T) {
39
			if got := Utf16Len(tt.content, tt.offset, tt.end); got != tt.want {
40
				t.Errorf("Utf16Len(%q, %d, %d) = %d, want %d", tt.content, tt.offset, tt.end, got, tt.want)
41
			}
42
		})
43
	}
44
}
45
46
func TestLineCol(t *testing.T) {
47
	tests := map[string]struct {
48
		content   string
49
		offset    int
50
		line, col int
51
	}{
52
		"basic":             {"hello\nworld", 11, 1, 5},
53
		"first line":        {"hello world", 5, 0, 5},
54
		"line start":        {"ab\ncd", 3, 1, 0},
55
		"newline ends line": {"ab\ncd", 2, 0, 2},
56
		"past blank line":   {"a\n\nb", 3, 2, 0},
57
		"blank line col":    {"a\n\nb", 2, 1, 0},
58
		"CRLF line start":   {"ab\r\ncd", 4, 1, 0},
59
		"crlf newline ends": {"ab\r\ncd", 2, 0, 2},
60
	}
61
	for name, tt := range tests {
62
		t.Run(name, func(t *testing.T) {
63
			line, col := LineCol(tt.content, tt.offset)
64
			if line != tt.line || col != tt.col {
65
				t.Errorf("LineCol(%q, %d) = (%d,%d), want (%d,%d)", tt.content, tt.offset, line, col, tt.line, tt.col)
66
			}
67
		})
68
	}
69
}
70
71
func TestOffset(t *testing.T) {
72
	tests := map[string]struct {
73
		content   string
74
		line, col int
75
		want      int
76
	}{
77
		"basic":        {"hello\nworld", 1, 5, 11},
78
		"past EOL col": {"ab\ncd", 0, 99, 2},
79
		"cyrillic":     {"Привіт😀", 0, 6, 12},
80
		"emoji":        {"Привіт😀", 0, 7, 16},
81
	}
82
	for name, tt := range tests {
83
		t.Run(name, func(t *testing.T) {
84
			if got := Offset(tt.content, tt.line, tt.col); got != tt.want {
85
				t.Errorf("Offset(%d,%d) = %d, want %d", tt.line, tt.col, got, tt.want)
86
			}
87
		})
88
	}
89
90
	t.Run("past EOF line", func(t *testing.T) {
91
		if got := Offset("ab\ncd", 5, 0); got != len("ab\ncd") {
92
			t.Errorf("Offset = %d, want %d", got, len("ab\ncd"))
93
		}
94
	})
95
}
96
97
func TestOffset_RoundTrip(t *testing.T) {
98
	content := "first\nПривіт світ\r\nlast\n"
99
100
	var boundaries []int
101
	for i := 0; i < len(content); {
102
		_, size := utf8.DecodeRuneInString(content[i:])
103
		if content[i] != '\n' || i <= 0 || content[i-1] != '\r' {
104
			boundaries = append(boundaries, i)
105
		}
106
		i += size
107
	}
108
109
	for _, off := range boundaries {
110
		t.Run(fmt.Sprintf("round trip at %d", off), func(t *testing.T) {
111
			line, col := LineCol(content, off)
112
			if got := Offset(content, line, col); got != off {
113
				t.Errorf("round trip at %d: Offset(LineCol(%d)) = %d", off, off, got)
114
			}
115
		})
116
	}
117
}
118
119
func TestPosition(t *testing.T) {
120
	content := "2024-01-15 Супермаркет\r\n  Витрати:Продукти  ¥50\n"
121
	for _, off := range []int{0, 10, len("2024-01-15 Супермаркет"), len(content)} {
122
		t.Run(fmt.Sprintf("round trip at %d", off), func(t *testing.T) {
123
			p := Position(content, off)
124
			if got := Offset(content, int(p.Line), int(p.Character)); got != off {
125
				t.Errorf("round trip of %d = %d (pos %d:%d), want identity", off, got, p.Line, p.Character)
126
			}
127
		})
128
	}
129
}