-
Notifications
You must be signed in to change notification settings - Fork 65
/
position_test.go
88 lines (81 loc) · 2.76 KB
/
position_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package parse
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/tdewolff/test"
)
func TestPosition(t *testing.T) {
var newlineTests = []struct {
offset int
buf string
line int
col int
}{
// \u2028, \u2029, and \u2318 are three bytes long
{0, "x", 1, 1},
{1, "xx", 1, 2},
{2, "x\nx", 2, 1},
{2, "\n\nx", 3, 1},
{3, "\nxxx", 2, 3},
{2, "\r\nx", 2, 1},
{1, "\rx", 2, 1},
{3, "\u2028x", 2, 1},
{3, "\u2029x", 2, 1},
// edge cases
{0, "", 1, 1},
{2, "x", 1, 2},
{0, "\nx", 1, 1},
{1, "\r\ny", 1, 1},
{-1, "x", 1, 1},
{0, "\x00a", 1, 1},
{2, "a\x00\n", 1, 3},
// unicode
{1, "x\u2028x", 1, 2},
{2, "x\u2028x", 1, 2},
{3, "x\u2028x", 1, 2},
{0, "x\u2318x", 1, 1},
{1, "x\u2318x", 1, 2},
{2, "x\u2318x", 1, 2},
{3, "x\u2318x", 1, 2},
{4, "x\u2318x", 1, 3},
}
for _, tt := range newlineTests {
t.Run(fmt.Sprint(tt.buf, " ", tt.offset), func(t *testing.T) {
r := bytes.NewBufferString(tt.buf)
line, col, _ := Position(r, tt.offset)
test.T(t, line, tt.line, "line")
test.T(t, col, tt.col, "column")
})
}
}
func TestPositionContext(t *testing.T) {
var newlineTests = []struct {
offset int
buf string
context string
}{
{10, "0123456789@123456789012345678901234567890123456789012345678901234567890123456789", "0123456789@1234567890123456789012345678901234567890123456..."}, // 80 characters -> 60 characters
{40, "0123456789012345678901234567890123456789@123456789012345678901234567890123456789", "...01234567890123456789@12345678901234567890..."},
{60, "012345678901234567890123456789012345678901234567890123456789@12345678901234567890", "...78901234567890123456789@12345678901234567890"},
{60, "012345678901234567890123456789012345678901234567890123456789@12345678901234567890123", "...01234567890123456789@12345678901234567890123"},
{60, "012345678901234567890123456789012345678901234567890123456789@123456789012345678901234", "...01234567890123456789@12345678901234567890..."},
{60, "0123456789012345678901234567890123456789ÎÎÎÎÎÎÎÎÎÎ@123456789012345678901234567890", "...0123456789ÎÎÎÎÎÎÎÎÎÎ@12345678901234567890..."},
{60, "012345678901234567890123456789012345678912456780123456789@12345678901234567890", "...789·12·45678·0123456789@12345678901234567890"},
}
for _, tt := range newlineTests {
t.Run(fmt.Sprint(tt.buf, " ", tt.offset), func(t *testing.T) {
r := bytes.NewBufferString(tt.buf)
_, _, context := Position(r, tt.offset)
i := strings.IndexByte(context, '\n')
pointer := context[i+1:]
context = context[:i]
test.T(t, context[7:], tt.context)
// check if @ and ^ are at the same position
j := strings.IndexByte(context, '@')
k := strings.IndexByte(pointer, '^')
test.T(t, len([]rune(pointer[:k])), len([]rune(context[:j])))
})
}
}