-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpressions_test.go
91 lines (82 loc) · 4.54 KB
/
expressions_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
package regex2json_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/regex2json"
)
type ExpValue struct {
Expression string
Value string
}
var Tests = []struct { //nolint: gochecknoglobals
Exps []ExpValue
Expected string
Errors []string
}{
{[]ExpValue{{"foobar", "x"}}, `{"foobar":"x"}`, []string{}},
{[]ExpValue{{"foo", "x"}, {"bar", "y"}}, `{"bar":"y","foo":"x"}`, []string{}},
{[]ExpValue{{"nested__foo", "x"}, {"nested__bar", "y"}}, `{"nested":{"bar":"y","foo":"x"}}`, []string{}},
{[]ExpValue{{"nested__foo", "x"}, {"nested__foo", "y"}}, `{"nested":{"foo":"x"}}`, []string{`nested__foo: value already exist`}},
{[]ExpValue{{"foo___array", "x"}, {"foo___array", "y"}}, `{"foo":["x","y"]}`, []string{}},
{[]ExpValue{{"foo___array", "x"}, {"foo", "y"}}, `{"foo":["x","y"]}`, []string{}},
{[]ExpValue{{"foo", "x"}, {"foo___array", "y"}}, `{"foo":["x","y"]}`, []string{}},
{[]ExpValue{{"nested__foo___array", "x"}, {"nested__foo___array", "y"}}, `{"nested":{"foo":["x","y"]}}`, []string{}},
{[]ExpValue{{"foobar___bool", "true"}}, `{"foobar":true}`, []string{}},
{[]ExpValue{{"foobar___int", "42"}}, `{"foobar":42}`, []string{}},
{[]ExpValue{{"foobar___float", "42.1"}}, `{"foobar":42.1}`, []string{}},
{[]ExpValue{{"foobar___null", ""}}, `{"foobar":null}`, []string{}},
{[]ExpValue{{"foobar___optional", ""}}, ``, []string{}},
{[]ExpValue{{"nested__foo___array___optional", ""}, {"nested__foo___array___optional", "y"}}, `{"nested":{"foo":["y"]}}`, []string{}},
{[]ExpValue{{"nested__foo___array___null", ""}, {"nested__foo___array___optional", "y"}}, `{"nested":{"foo":[null,"y"]}}`, []string{}},
{[]ExpValue{{"nested__foo___array___object__a", "x"}, {"nested__foo___array___object__b", "y"}}, `{"nested":{"foo":[{"a":"x"},{"b":"y"}]}}`, []string{}},
{[]ExpValue{{"nested__foo___array___object__a", "x"}, {"nested__foo___object__b", "y"}}, `{"nested":{"foo":[{"a":"x","b":"y"}]}}`, []string{}},
{[]ExpValue{{"nested__foo___object__b", "y"}, {"nested__foo___array___object__a", "x"}}, `{"nested":{"foo":[{"a":"x","b":"y"}]}}`, []string{}},
{[]ExpValue{{"foo", "x"}, {"foo___array___optional", ""}}, `{"foo":["x"]}`, []string{}},
{[]ExpValue{{"foo", "x"}, {"foo___array___int", "1"}}, `{"foo":["x",1]}`, []string{}},
{[]ExpValue{{"foo___array___int", "1"}, {"foo", "x"}}, `{"foo":[1,"x"]}`, []string{}},
{[]ExpValue{{"foo__bar", "x"}, {"foo___array___int", "1"}}, `{"foo":[{"bar":"x"},1]}`, []string{}},
{[]ExpValue{{"foo__bar", "x"}, {"foo___int", "1"}}, `{"foo":{"bar":"x"}}`, []string{`foo: type mismatch`}},
{[]ExpValue{{"foo___array___optional", ""}, {"foo", "x"}}, `{"foo":["x"]}`, []string{}},
{[]ExpValue{{"foo___array___int", "1"}, {"foo__bar", "x"}}, `{"foo":[1,{"bar":"x"}]}`, []string{}},
{[]ExpValue{{"foo___time__UnixDate", "Fri Jun 9 22:21:17 CEST 2023"}}, `{"foo":"2023-06-09T20:21:17.000Z"}`, []string{}},
{[]ExpValue{{"foo___time__UnixDate__DateTime", "Fri Jun 9 22:21:17 CEST 2023"}}, `{"foo":"2023-06-09 20:21:17"}`, []string{}},
{[]ExpValue{{"foo___time__UnixDate__DateTime__UTC__UTC", "Fri Jun 9 22:21:17 CEST 2023"}}, `{"foo":"2023-06-09 20:21:17"}`, []string{}},
{[]ExpValue{{"foo___time__UnixDate__DateTime", "Fri Jun 9 22:21:17 MST 2023"}}, `{"foo":"2023-06-10 05:21:17"}`, []string{}},
{[]ExpValue{{"foo___time__UnixDate__DateTime__Europe_Ljubljana", "Fri Jun 9 22:21:17 CEST 2023"}}, `{"foo":"2023-06-09 22:21:17"}`, []string{}},
{[]ExpValue{{"foo___time__DateTime__UnixDate__UTC__Europe_Ljubljana", "2023-06-09 22:21:17"}}, `{"foo":"Fri Jun 9 20:21:17 UTC 2023"}`, []string{}},
{[]ExpValue{{"obj___json", `{"x":1,"y":"v"}`}}, `{"obj":{"x":1,"y":"v"}}`, []string{}},
{[]ExpValue{{"___json", `{"x":1,"y":"v"}`}}, `{"x":1,"y":"v"}`, []string{}},
{[]ExpValue{{"obj___json___optional", ``}}, ``, []string{}},
{[]ExpValue{{"___json___optional", ``}}, ``, []string{}},
}
func TestExpression(t *testing.T) {
t.Parallel()
for i, tt := range Tests {
tt := tt
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
t.Parallel()
output := map[string]any{}
errI := 0
for _, ev := range tt.Exps {
e, err := regex2json.NewExpression(ev.Expression)
require.NoError(t, err, "% -+#.1v", err)
err = e.Apply(output, ev.Value)
if err != nil && errI < len(tt.Errors) {
assert.EqualError(t, err, tt.Errors[errI])
errI++
continue
}
}
if len(output) == 0 {
assert.Equal(t, tt.Expected, "")
} else {
j, err := json.Marshal(output)
require.NoError(t, err)
assert.Equal(t, tt.Expected, string(j))
}
})
}
}