-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer_test.go
98 lines (78 loc) · 2.17 KB
/
tokenizer_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
89
90
91
92
93
94
95
96
97
98
package habari
import (
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
"testing"
)
func testEnclosedDetection(t *testing.T, tkns []*token) {
for idx, tkn := range tkns {
if tkn.getValue() == "ENCLOSED" {
assert.Truef(t, tkn.isEnclosed(), "expected token to be enclosed at index %d", idx)
}
if tkn.getValue() == "ABC" {
assert.Falsef(t, tkn.isEnclosed(), "expected token not to be enclosed at index %d", idx)
}
}
}
func TestEnclosedDetection(t *testing.T) {
ret := tokenize("[ENCLOSED] ABC ABC ABC (ENCLOSED) [ENCLOSED]")
test := tokens{}
test.setTokens(ret)
testEnclosedDetection(t, ret)
ret = tokenize("[ENCLOSED (ENCLOSED] ABC ABC [ENCLOSED]")
testEnclosedDetection(t, ret)
ret = tokenize("[ENCLOSED (ENCLOSED) ENCLOSED ENCLOSED [ENCLOSED]")
testEnclosedDetection(t, ret)
ret = tokenize("[ENCLOSED] (ENCLOSED) ABC ABC [ABC ABC")
testEnclosedDetection(t, ret)
}
func TestTokenizeCategories(t *testing.T) {
input := "[enclosed] abc-abc.mkv"
tkns := tokenize(input)
categories := []tokenCategory{
tokenCatOpeningBracket, // [
tokenCatUnknown, // enclosed
tokenCatClosingBracket, // ]
tokenCatDelimiter, // " "
tokenCatUnknown, // abc
tokenCatSeparator, // -
tokenCatUnknown, // abc
tokenCatDelimiter, // .
tokenCatUnknown, // mkv
}
assert.Equal(t, len(tkns), len(categories))
for idx, tkn := range tkns {
assert.Equal(t, tkn.Category, categories[idx])
if tkn.Category != categories[idx] {
spew.Dump(tkn)
}
}
}
func TestTokenizeKinds(t *testing.T) {
input := "01 ABC 01v3 2024 1080p"
tkns := tokenize(input)
kinds := []tokenKind{
tokenKindNumber,
tokenKindCharacter,
tokenKindWord,
tokenKindCharacter,
tokenKindNumberLike,
tokenKindCharacter,
tokenKindYear,
tokenKindCharacter,
tokenKindPossibleVideoRes,
}
assert.Equal(t, len(tkns), len(kinds))
for idx, tkn := range tkns {
assert.Equal(t, tkn.Kind, kinds[idx])
if tkn.Kind != kinds[idx] {
spew.Dump(tkn)
}
}
}
func TestIsMatchingClosingBracket(t *testing.T) {
ret := isMatchingClosingBracket("[", "]")
assert.True(t, ret)
ret = isMatchingClosingBracket("[", ")")
assert.False(t, ret)
}