-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_test.go
71 lines (67 loc) · 1.88 KB
/
string_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
package djson
import (
"bytes"
"testing"
)
func TestString_Index(t *testing.T) {
// "hello world".index("world")
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenString, Raw: []byte("hello world")},
{Type: TokenDot},
{Type: TokenIdentifier, Raw: []byte("index")},
{Type: TokenParenthesesOpen},
{Type: TokenString, Raw: []byte("world")},
{Type: TokenParenthesesClose},
}))
stmt := NewStmtExecutor(scanner, NewContext())
if err := stmt.Execute(); err != nil {
t.Fatal(err)
}
if stmt.value.Type != ValueInt {
t.Fatal("index error")
}
v, _ := stmt.value.Value.(Inter).Int()
if v != 6 {
t.Fatal("index error")
}
}
func TestString_Match(t *testing.T) {
// "hello world".match("world$")
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenString, Raw: []byte("hello world")},
{Type: TokenDot},
{Type: TokenIdentifier, Raw: []byte("match")},
{Type: TokenParenthesesOpen},
{Type: TokenString, Raw: []byte("world$")},
{Type: TokenParenthesesClose},
}))
stmt := NewStmtExecutor(scanner, NewContext())
if err := stmt.Execute(); err != nil {
t.Fatal(err)
}
if !(stmt.value.Type == ValueBool && stmt.value.Value.(bool)) {
t.Fatal("match error")
}
}
func TestString_sub(t *testing.T) {
// "hello world".sub([0, 4])
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenString, Raw: []byte("hello world")},
{Type: TokenDot},
{Type: TokenIdentifier, Raw: []byte("sub")},
{Type: TokenParenthesesOpen},
{Type: TokenBracketsOpen},
{Type: TokenNumber, Raw: []byte{'0'}},
{Type: TokenComma},
{Type: TokenNumber, Raw: []byte{'4'}},
{Type: TokenBracketsClose},
{Type: TokenParenthesesClose},
}))
stmt := NewStmtExecutor(scanner, NewContext())
if err := stmt.Execute(); err != nil {
t.Fatal(err)
}
if !(stmt.value.Type == ValueString && bytes.Equal(stmt.value.Value.(String).Bytes(), []byte("hello"))) {
t.Fatal("match error")
}
}