-
Notifications
You must be signed in to change notification settings - Fork 52
/
arrays_test.go
79 lines (61 loc) · 1.59 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
package jsonlogic_test
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/diegoholiveira/jsonlogic/v3"
)
func TestFilterParseTheSubjectFromFirstPosition(t *testing.T) {
rule := strings.NewReader(`{"filter": [
[1,2,3,4,5],
{"%":[{"var":""},2]}
]}`)
var result bytes.Buffer
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[1,3,5]`, result.String())
}
func TestFilterParseTheSubjectFromNullValue(t *testing.T) {
rule := strings.NewReader(`{"filter": [
null,
{"%":[{"var":""},2]}
]}`)
var result bytes.Buffer
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `[]`, result.String())
}
func TestReduceSkipNullValues(t *testing.T) {
rule := strings.NewReader(`{"reduce": [
[1,2,null,4,5],
{"+":[{"var":"current"}, {"var":"accumulator"}]},
0
]}`)
var result bytes.Buffer
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `12`, result.String())
}
func TestReduceBoolValues(t *testing.T) {
rule := strings.NewReader(`{"reduce": [
[true,false,true,null],
{"or":[{"var":"current"}, {"var":"accumulator"}]},
false
]}`)
var result bytes.Buffer
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `true`, result.String())
}
func TestReduceStringValues(t *testing.T) {
rule := strings.NewReader(`{"reduce": [
["a",null,"b"],
{"cat":[{"var":"current"}, {"var":"accumulator"}]},
""
]}`)
var result bytes.Buffer
err := jsonlogic.Apply(rule, nil, &result)
assert.Nil(t, err)
assert.JSONEq(t, `"ba"`, result.String())
}