-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_test.go
More file actions
70 lines (58 loc) · 1.58 KB
/
token_test.go
File metadata and controls
70 lines (58 loc) · 1.58 KB
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
package jsonpointer
import (
"errors"
"testing"
)
func TestParseToken(t *testing.T) {
t.Parallel()
type test struct {
tok string
field string
index int
}
tests := []test{
{"", "", -1},
{"a", "a", -1},
{"0", "0", 0},
{"1", "1", 1},
{"1a", "1a", -1},
{"01", "01", -1},
{"~0", "~", -1},
{"~1", "/", -1},
{"~01", "~1", -1},
{"~10", "/0", -1},
}
for _, test := range tests {
tok, err := parseToken(test.tok)
if err != nil {
t.Errorf("parseToken(%s) = %v, want <nil>", test.tok, err)
}
if tok.field != test.field || tok.index != test.index {
t.Errorf("parseToken(%s) = {%s, %d}, want {%s, %d}", test.tok, tok.field, tok.index, test.field, test.index)
}
tok, err = parseTokenBytes([]byte(test.tok))
if err != nil {
t.Errorf("parseTokenBytes(%s) = %v, want <nil>", test.tok, err)
}
if tok.field != test.field || tok.index != test.index {
t.Errorf("parseTokenBytes(%s) = {%s, %d}, want {%s, %d}", test.tok, tok.field, tok.index, test.field, test.index)
}
}
var terr *invalidTokenError
_, err := parseToken("~")
if !errors.As(err, &terr) {
t.Errorf("parseToken(~) = %v, want %v", err, &invalidTokenError{"~"})
}
_, err = parseTokenBytes([]byte("~"))
if !errors.As(err, &terr) {
t.Errorf("parseTokenBytes(~) = %v, want %v", err, &invalidTokenError{"~"})
}
_, err = parseToken("~2")
if !errors.As(err, &terr) {
t.Errorf("parseToken(~2) = %v, want %v", err, &invalidTokenError{"~2"})
}
_, err = parseTokenBytes([]byte("~2"))
if !errors.As(err, &terr) {
t.Errorf("parseTokenBytes(~2) = %v, want %v", err, &invalidTokenError{"~2"})
}
}