-
Notifications
You must be signed in to change notification settings - Fork 0
/
assign_test.go
131 lines (103 loc) · 3.65 KB
/
assign_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package jq
import "testing"
func TestAssignObjectAbs(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", obj{"b", 10}, "b", 20})
testOne(tb, NewAssign(Key("a"), Key("b")), b, off, obj{"a", 20, "b", 20})
}
func TestAssignObjectRel(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", obj{"b", 10}, "b", 20})
testOne(tb, NewUpdateAssign(Key("a"), Key("b")), b, off, obj{"a", 10, "b", 20})
}
func TestAssignArrayAbs(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", arr{nil, obj{"c", obj{"v", 10}}}, "v", 20})
testOne(tb, NewAssign(NewQuery("a", 1, "c"), Key("v")), b, off, obj{"a", arr{nil, obj{"c", 20}}, "v", 20})
}
func TestAssignArrayRel(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", arr{nil, obj{"c", obj{"v", 10}}}, "v", 20})
testOne(tb, NewUpdateAssign(NewQuery("a", 1, "c"), Key("v")), b, off, obj{"a", arr{nil, obj{"c", 10}}, "v", 20})
}
func TestAssignArrayIterAbs(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", arr{obj{"c", obj{"v", 5}}, obj{"c", obj{"v", 10}}}, "v", 20})
testOne(tb, NewAssign(NewQuery("a", Iter{}, "c"), Key("v")), b, off, obj{"a", arr{obj{"c", 20}, obj{"c", 20}}, "v", 20})
}
func TestAssignArrayIterRel(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", arr{obj{"c", obj{"v", 5}}, obj{"c", obj{"v", 10}}}, "v", 20})
testOne(tb, NewUpdateAssign(NewQuery("a", Iter{}, "c"), Key("v")), b, off, obj{"a", arr{obj{"c", 5}, obj{"c", 10}}, "v", 20})
}
func TestAssignLongArrayAbs(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(arr{
obj{"a", arr{1, 1, 1, 1}},
obj{"a", arr{2, 2, 2, 2, 2}},
obj{"a", arr{3, 3, 3, 3}},
// obj{"a", arr{4, 4, 4, 4, 4, 4}},
// obj{"a", arr{5, 5, 5, 5, 5}},
})
exp := b.appendVal(arr{
obj{"a", arr{0, 0, 0, 0}},
obj{"a", arr{0, 0, 0, 0, 0}},
obj{"a", arr{0, 0, 0, 0}},
// obj{"a", arr{0, 0, 0, 0, 0, 0}},
// obj{"a", arr{0, 0, 0, 0, 0}},
})
testOne(tb, NewAssign(NewQuery(Iter{}, "a", Iter{}), Zero), b, off, exp)
}
func TestAssignComma(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", 1, "b", 2})
exp := b.appendVal(obj{"a", 10, "b", 10})
testOne(tb, NewAssign(NewComma(Key("a"), Key("b")), NewLiteral(10)), b, off, exp)
}
func TestAssignCommaPipe(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", 1, "b", obj{"c", 2, "d", obj{"e", 3}}, "q", 4})
exp := b.appendVal(obj{"a", 10, "b", obj{"c", 10, "d", obj{"e", 10}}, "q", 10})
testOne(tb,
NewAssign(
NewComma(
Key("a"),
NewPipe(
Key("b"),
NewComma(
Key("c"),
NewPipe(Key("d"), Key("e")),
),
),
Key("q"),
),
NewLiteral(10),
),
b, off, exp,
)
}
func TestAssignSelect(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(arr{obj{"a", false, "v", 1}, obj{"a", true, "v", 2}, obj{"a", 1, "v", 3}, obj{"a", nil, "v", 4}})
exp := b.appendVal(arr{obj{"a", false, "v", 1}, obj{"a", true, "v", 10}, obj{"a", 1, "v", 10}, obj{"a", nil, "v", 4}})
testOne(tb, NewAssign(NewPipe(NewIter(), NewSelect(Key("a")), Key("v")), NewLiteral(10)), b, off, exp)
}
func TestAssignMulti(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", arr{1, 2}, "b", arr{"q", "w"}})
testIter(tb, NewAssign(NewQuery("a", Iter{}), NewQuery("b", Iter{})), b, off, []any{
obj{"a", arr{"q", "q"}, "b", arr{"q", "w"}},
obj{"a", arr{"w", "w"}, "b", arr{"q", "w"}},
})
}
func TestAssignNoneAbs(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", 1, "b", 2})
testIter(tb, NewAssign(Key("b"), None), b, off, nil)
}
func TestAssignNoneRel(tb *testing.T) {
b := NewBuffer()
off := b.appendVal(obj{"a", 1, "b", 2})
exp := b.appendVal(obj{"a", 1})
testOne(tb, NewUpdateAssign(Key("b"), None), b, off, exp)
}