-
Notifications
You must be signed in to change notification settings - Fork 0
/
array_test.go
82 lines (77 loc) · 1.91 KB
/
array_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
package djson
import "testing"
func TestArray_set(t *testing.T) {
// arr.set(k == 0 -> 4)
arr := NewArray(
IntValue(int64(1)),
IntValue(int64(2)),
IntValue(int64(3)),
)
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenIdentifier, Raw: []byte{'i'}},
{Type: TokenEqual},
{Type: TokenNumber, Raw: []byte{'0'}},
{Type: TokenReduction},
{Type: TokenNumber, Raw: []byte{'4'}},
{Type: TokenParenthesesClose},
}))
val, err := setArray(Value{Type: ValueArray, Value: arr}, scanner, NewContext())
if err != nil {
t.Fatal(err)
}
if val.Type != ValueArray {
t.Fatal("array set returned error")
}
val = val.Value.(Array).Get(0)
if val.Type != ValueInt {
t.Fatal("array set error")
}
if v, _ := val.Value.(Inter).Int(); v != 4 {
t.Fatal("array set error")
}
}
func TestArray_del(t *testing.T) {
// arr.del(i == 0)
arr := NewArray(
IntValue(int64(1)),
IntValue(int64(2)),
IntValue(int64(3)),
)
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenIdentifier, Raw: []byte{'i'}},
{Type: TokenEqual},
{Type: TokenNumber, Raw: []byte{'0'}},
{Type: TokenParenthesesClose},
}))
newArr, err := delArray(Value{Type: ValueArray, Value: arr}, scanner, NewContext())
if err != nil {
t.Fatal(err)
}
if !(newArr.Type == ValueArray && newArr.Value.(Array).Total() == 2) {
t.Fatal("del array error")
}
}
func TestArray_get(t *testing.T) {
// arr.filter(i > 1)
arr := NewArray(
IntValue(int64(1)),
IntValue(int64(2)),
IntValue(int64(3)),
)
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenIdentifier, Raw: []byte{'i'}},
{Type: TokenGreateThan},
{Type: TokenNumber, Raw: []byte{'1'}},
{Type: TokenParenthesesClose},
}))
val, err := filterArray(Value{Type: ValueArray, Value: arr}, scanner, NewContext())
if err != nil {
t.Fatal(err)
}
if val.Type != ValueArray {
t.Fatal("returned type error")
}
if val.Value.(Array).Total() != 1 {
t.Fatal("get error")
}
}