150 lines
2.8 KiB
Go
150 lines
2.8 KiB
Go
package expr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/stretchr/testify/require"
|
|
"testing"
|
|
)
|
|
|
|
// extract typed-value
|
|
|
|
func TestVars_Decode(t *testing.T) {
|
|
|
|
t.Run("mix", func(t *testing.T) {
|
|
var (
|
|
req = require.New(t)
|
|
|
|
dst = &struct {
|
|
Int int64
|
|
Uint64 uint64
|
|
String string `var:"STRING"`
|
|
RawString string `var:"rawString"`
|
|
Bool bool
|
|
Unexisting byte
|
|
}{}
|
|
|
|
vars = RVars{
|
|
"int": Must(NewInteger(42)),
|
|
"STRING": Must(NewString("foo")),
|
|
"bool": Must(NewBoolean(true)),
|
|
"missing": Must(NewBoolean(true)),
|
|
}.Vars()
|
|
)
|
|
|
|
req.NoError(vars.Decode(dst))
|
|
req.Equal(int64(42), dst.Int)
|
|
req.Equal("foo", dst.String)
|
|
req.Equal(true, dst.Bool)
|
|
req.Empty(dst.Unexisting)
|
|
})
|
|
|
|
t.Run("interfaces", func(t *testing.T) {
|
|
var (
|
|
req = require.New(t)
|
|
|
|
dst = &struct {
|
|
IString interface{} `var:"iString"`
|
|
IInteger interface{} `var:"iInteger"`
|
|
IBool interface{} `var:"iBool"`
|
|
}{}
|
|
|
|
vars = RVars{
|
|
"iString": Must(NewString("foo")),
|
|
"iInteger": Must(NewInteger(42)),
|
|
"iBool": Must(NewBoolean(true)),
|
|
}.Vars()
|
|
)
|
|
|
|
req.NoError(vars.Decode(dst))
|
|
})
|
|
|
|
t.Run("vars-in-vars", func(t *testing.T) {
|
|
var (
|
|
req = require.New(t)
|
|
|
|
dst = &struct {
|
|
Vars RVars `var:"vars"`
|
|
}{}
|
|
|
|
vars = RVars{
|
|
"vars": RVars{"foo": Must(NewString("bar"))}.Vars(),
|
|
}.Vars()
|
|
)
|
|
|
|
req.NoError(vars.Decode(dst))
|
|
})
|
|
|
|
t.Run("int-uint", func(t *testing.T) {
|
|
var (
|
|
req = require.New(t)
|
|
|
|
dst = &struct {
|
|
Int int64
|
|
Uint64 uint64
|
|
}{}
|
|
|
|
vars = RVars{
|
|
"uint64": Must(NewAny("42")),
|
|
"int": Must(NewAny("42")),
|
|
}.Vars()
|
|
)
|
|
|
|
dst.Uint64 = 0
|
|
dst.Int = 0
|
|
|
|
req.NoError(vars.Decode(dst))
|
|
req.Equal(uint64(42), dst.Uint64)
|
|
req.Equal(int64(42), dst.Int)
|
|
})
|
|
}
|
|
|
|
func TestVars_UnmarshalJSON(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
json string
|
|
vars *Vars
|
|
}{
|
|
{"empty", "", &Vars{}},
|
|
{"empty", "{}", &Vars{}},
|
|
{"string", `{"a":{"@value":"b"}}`, RVars{"a": &Unresolved{value: "b"}}.Vars()},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var (
|
|
r = require.New(t)
|
|
v = &Vars{}
|
|
)
|
|
|
|
r.NoError(v.UnmarshalJSON([]byte(c.json)))
|
|
r.Equal(c.vars, v)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestVars_UMarshalJSON(t *testing.T) {
|
|
cases := []struct {
|
|
name string
|
|
json string
|
|
vars *Vars
|
|
}{
|
|
{"empty", "{}", &Vars{}},
|
|
{"string", `{"a":{"@value":"b","@type":"String"}}`, RVars{"a": &String{value: "b"}}.Vars()},
|
|
{"array",
|
|
`{"arr":{"@value":[{"@value":"foo","@type":"String"},{"@value":"bar","@type":"String"}],"@type":"Array"}}`,
|
|
RVars{"arr": &Array{value: []TypedValue{&String{value: "foo"}, &String{value: "bar"}}}}.Vars()},
|
|
}
|
|
|
|
for _, c := range cases {
|
|
t.Run(c.name, func(t *testing.T) {
|
|
var (
|
|
r = require.New(t)
|
|
)
|
|
|
|
j, err := json.Marshal(c.vars)
|
|
r.NoError(err)
|
|
r.Equal(c.json, string(j))
|
|
})
|
|
}
|
|
}
|