-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_test.go
56 lines (53 loc) · 1.65 KB
/
object_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
package djson
import "testing"
func TestObject_set(t *testing.T) {
// obj.set(k == "0" => 4)
obj := NewObject(
&pair{key: []byte{'0'}, val: Value{Type: ValueInt, Value: Int(int64(1))}},
&pair{key: []byte{'1'}, val: Value{Type: ValueInt, Value: Int(int64(2))}},
&pair{key: []byte{'2'}, val: Value{Type: ValueInt, Value: Int(int64(3))}},
)
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenIdentifier, Raw: []byte{'k'}},
{Type: TokenEqual},
{Type: TokenString, Raw: []byte{'0'}},
{Type: TokenReduction},
{Type: TokenNumber, Raw: []byte{'4'}},
{Type: TokenParenthesesClose},
}))
val, err := setObject(Value{Type: ValueArray, Value: obj}, scanner, NewContext())
if err != nil {
t.Fatal(err)
}
if val.Type != ValueObject {
t.Fatal("returned error")
}
val = val.Value.(Object).Get([]byte("0"))
if val.Type != ValueInt {
t.Fatal("set error")
}
if v, _ := val.Value.(Inter).Int(); v != 4 {
t.Fatal("set error")
}
}
func TestObject_del(t *testing.T) {
// obj.del(k == "0")
obj := NewObject(
&pair{key: []byte{'0'}, val: Value{Type: ValueInt, Value: Int(int64(1))}},
&pair{key: []byte{'1'}, val: Value{Type: ValueInt, Value: Int(int64(2))}},
&pair{key: []byte{'2'}, val: Value{Type: ValueInt, Value: Int(int64(3))}},
)
scanner := NewTokenScanner(NewLexMock([]*Token{
{Type: TokenIdentifier, Raw: []byte{'k'}},
{Type: TokenEqual},
{Type: TokenString, Raw: []byte{'0'}},
{Type: TokenParenthesesClose},
}))
val, err := delObject(Value{Type: ValueArray, Value: obj}, scanner, NewContext())
if err != nil {
t.Fatal(err)
}
if !(val.Type == ValueObject && val.Value.(Object).Total() == 2) {
t.Fatal("del object error")
}
}