forked from diegoholiveira/jsonlogic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrays_test.go
111 lines (79 loc) · 1.95 KB
/
arrays_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
package jsonlogic
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFilterParseTheSubjectFromFirstPosition(t *testing.T) {
var parsed interface{}
err := json.Unmarshal([]byte(`[
[1,2,3,4,5],
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}
result := filter(parsed, nil)
var expected interface{}
json.Unmarshal([]byte(`[1,3,5]`), &expected) // nolint:errcheck
assert.Equal(t, expected, result)
}
func TestFilterParseTheSubjectFromNullValue(t *testing.T) {
var parsed interface{}
err := json.Unmarshal([]byte(`[
null,
{"%":[{"var":""},2]}
]`), &parsed)
if err != nil {
panic(err)
}
result := filter(parsed, nil)
var expected interface{}
json.Unmarshal([]byte(`[]`), &expected) // nolint:errcheck
assert.Equal(t, expected, result)
}
func TestReduceSkipNullValues(t *testing.T) {
var parsed interface{}
err := json.Unmarshal([]byte(`[
[1,2,null,4,5],
{"+":[{"var":"current"}, {"var":"accumulator"}]},
0
]`), &parsed)
if err != nil {
panic(err)
}
result := reduce(parsed, nil)
var expected interface{}
json.Unmarshal([]byte(`12`), &expected) // nolint:errcheck
assert.Equal(t, expected, result)
}
func TestReduceBoolValues(t *testing.T) {
var parsed interface{}
err := json.Unmarshal([]byte(`[
[true,false,true,null],
{"or":[{"var":"current"}, {"var":"accumulator"}]},
false
]`), &parsed)
if err != nil {
panic(err)
}
result := reduce(parsed, nil)
var expected interface{}
json.Unmarshal([]byte(`true`), &expected) // nolint:errcheck
assert.Equal(t, expected, result)
}
func TestReduceStringValues(t *testing.T) {
var parsed interface{}
err := json.Unmarshal([]byte(`[
["a",null,"b"],
{"cat":[{"var":"current"}, {"var":"accumulator"}]},
""
]`), &parsed)
if err != nil {
panic(err)
}
result := reduce(parsed, nil)
var expected interface{}
json.Unmarshal([]byte(`"ba"`), &expected) // nolint:errcheck
assert.Equal(t, expected, result)
}