2023-10-25 11:56:42 +00:00
|
|
|
package convert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/diag"
|
2023-12-22 13:20:45 +00:00
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2023-10-25 11:56:42 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNormalizeStruct(t *testing.T) {
|
|
|
|
type Tmp struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
Bar string `json:"bar"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V("baz"),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStructElementDiagnostic(t *testing.T) {
|
|
|
|
type Tmp struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
Bar string `json:"bar"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{"an": dyn.V("error")}),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected string, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
|
|
|
|
// Elements that encounter an error during normalization are dropped.
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
}, vout.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStructUnknownField(t *testing.T) {
|
|
|
|
type Tmp struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V("baz"),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Warning,
|
|
|
|
Summary: `unknown field: bar`,
|
|
|
|
Location: vin.Get("foo").Location(),
|
|
|
|
}, err[0])
|
|
|
|
|
|
|
|
// The field that can be mapped to the struct field is retained.
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
}, vout.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStructNil(t *testing.T) {
|
|
|
|
type Tmp struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NilValue
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStructError(t *testing.T) {
|
|
|
|
type Tmp struct {
|
|
|
|
Foo string `json:"foo"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("string")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected map, found string`,
|
|
|
|
Location: vin.Get("foo").Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
func TestNormalizeStructNestedError(t *testing.T) {
|
|
|
|
type Nested struct {
|
|
|
|
F1 int `json:"f1"`
|
|
|
|
F2 int `json:"f2"`
|
|
|
|
}
|
|
|
|
type Tmp struct {
|
|
|
|
Foo Nested `json:"foo"`
|
|
|
|
Bar Nested `json:"bar"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ Tmp
|
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V("error"),
|
|
|
|
"f2": dyn.V(1),
|
|
|
|
}),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(1),
|
|
|
|
"f2": dyn.V("error"),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 2)
|
|
|
|
|
|
|
|
// Verify that valid fields are retained.
|
|
|
|
assert.Equal(t,
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V(map[string]dyn.Value{
|
|
|
|
"f2": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
vout,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:56:42 +00:00
|
|
|
func TestNormalizeMap(t *testing.T) {
|
|
|
|
var typ map[string]string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V("baz"),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeMapElementDiagnostic(t *testing.T) {
|
|
|
|
var typ map[string]string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V("bar"),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{"an": dyn.V("error")}),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected string, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
|
|
|
|
// Elements that encounter an error during normalization are dropped.
|
|
|
|
assert.Equal(t, map[string]any{
|
|
|
|
"foo": "bar",
|
|
|
|
}, vout.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeMapNil(t *testing.T) {
|
|
|
|
var typ map[string]string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NilValue
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeMapError(t *testing.T) {
|
|
|
|
var typ map[string]string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("string")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected map, found string`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
func TestNormalizeMapNestedError(t *testing.T) {
|
|
|
|
type Nested struct {
|
|
|
|
F1 int `json:"f1"`
|
|
|
|
F2 int `json:"f2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ map[string]Nested
|
|
|
|
vin := dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V("error"),
|
|
|
|
"f2": dyn.V(1),
|
|
|
|
}),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(1),
|
|
|
|
"f2": dyn.V("error"),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 2)
|
|
|
|
|
|
|
|
// Verify that valid fields are retained.
|
|
|
|
assert.Equal(t,
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"foo": dyn.V(map[string]dyn.Value{
|
|
|
|
"f2": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
"bar": dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
vout,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:56:42 +00:00
|
|
|
func TestNormalizeSlice(t *testing.T) {
|
|
|
|
var typ []string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("foo"),
|
|
|
|
dyn.V("bar"),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeSliceElementDiagnostic(t *testing.T) {
|
|
|
|
var typ []string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V([]dyn.Value{
|
|
|
|
dyn.V("foo"),
|
|
|
|
dyn.V("bar"),
|
|
|
|
dyn.V(map[string]dyn.Value{"an": dyn.V("error")}),
|
2023-10-25 11:56:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected string, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
|
|
|
|
// Elements that encounter an error during normalization are dropped.
|
|
|
|
assert.Equal(t, []any{"foo", "bar"}, vout.AsAny())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeSliceNil(t *testing.T) {
|
|
|
|
var typ []string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NilValue
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeSliceError(t *testing.T) {
|
|
|
|
var typ []string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("string")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected sequence, found string`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
2024-02-13 14:12:19 +00:00
|
|
|
func TestNormalizeSliceNestedError(t *testing.T) {
|
|
|
|
type Nested struct {
|
|
|
|
F1 int `json:"f1"`
|
|
|
|
F2 int `json:"f2"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var typ []Nested
|
|
|
|
vin := dyn.V([]dyn.Value{
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V("error"),
|
|
|
|
"f2": dyn.V(1),
|
|
|
|
}),
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(1),
|
|
|
|
"f2": dyn.V("error"),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
vout, err := Normalize(typ, vin)
|
|
|
|
assert.Len(t, err, 2)
|
|
|
|
|
|
|
|
// Verify that valid fields are retained.
|
|
|
|
assert.Equal(t,
|
|
|
|
dyn.V([]dyn.Value{
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"f2": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
dyn.V(map[string]dyn.Value{
|
|
|
|
"f1": dyn.V(int64(1)),
|
|
|
|
}),
|
|
|
|
}),
|
|
|
|
vout,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-10-25 11:56:42 +00:00
|
|
|
func TestNormalizeString(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("string")
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
|
|
|
assert.Equal(t, vin, vout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStringNil(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(nil, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected string, found nil`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStringFromBool(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(true, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NewValue("true", vin.Location()), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStringFromInt(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(123, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NewValue("123", vin.Location()), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStringFromFloat(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(1.20, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.NewValue("1.2", vin.Location()), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeStringError(t *testing.T) {
|
|
|
|
var typ string
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{"an": dyn.V("error")})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected string, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeBool(t *testing.T) {
|
|
|
|
var typ bool
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(true)
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(true), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeBoolNil(t *testing.T) {
|
|
|
|
var typ bool
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(nil, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected bool, found nil`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeBoolFromString(t *testing.T) {
|
|
|
|
var typ bool
|
|
|
|
|
|
|
|
for _, c := range []struct {
|
|
|
|
Input string
|
|
|
|
Output bool
|
|
|
|
}{
|
|
|
|
{"true", true},
|
|
|
|
{"false", false},
|
|
|
|
{"Y", true},
|
|
|
|
{"N", false},
|
|
|
|
{"on", true},
|
|
|
|
{"off", false},
|
|
|
|
} {
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(c.Input)
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(c.Output), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeBoolFromStringError(t *testing.T) {
|
|
|
|
var typ bool
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("abc")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected bool, found string`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeBoolError(t *testing.T) {
|
|
|
|
var typ bool
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{"an": dyn.V("error")})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected bool, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeInt(t *testing.T) {
|
|
|
|
var typ int
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(123)
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(int64(123)), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeIntNil(t *testing.T) {
|
|
|
|
var typ int
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(nil, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected int, found nil`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeIntFromString(t *testing.T) {
|
|
|
|
var typ int
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("123")
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(int64(123)), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeIntFromStringError(t *testing.T) {
|
|
|
|
var typ int
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("abc")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `cannot parse "abc" as an integer`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeIntError(t *testing.T) {
|
|
|
|
var typ int
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{"an": dyn.V("error")})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected int, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeFloat(t *testing.T) {
|
|
|
|
var typ float64
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(1.2)
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(1.2), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeFloatNil(t *testing.T) {
|
|
|
|
var typ float64
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.NewValue(nil, dyn.Location{File: "file", Line: 1, Column: 1})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected float, found nil`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeFloatFromString(t *testing.T) {
|
|
|
|
var typ float64
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("1.2")
|
2023-10-25 11:56:42 +00:00
|
|
|
vout, err := Normalize(&typ, vin)
|
|
|
|
assert.Empty(t, err)
|
2023-12-22 13:20:45 +00:00
|
|
|
assert.Equal(t, dyn.V(1.2), vout)
|
2023-10-25 11:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeFloatFromStringError(t *testing.T) {
|
|
|
|
var typ float64
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V("abc")
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `cannot parse "abc" as a floating point number`,
|
|
|
|
Location: vin.Location(),
|
|
|
|
}, err[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNormalizeFloatError(t *testing.T) {
|
|
|
|
var typ float64
|
2023-12-22 13:20:45 +00:00
|
|
|
vin := dyn.V(map[string]dyn.Value{"an": dyn.V("error")})
|
2023-10-25 11:56:42 +00:00
|
|
|
_, err := Normalize(&typ, vin)
|
|
|
|
assert.Len(t, err, 1)
|
|
|
|
assert.Equal(t, diag.Diagnostic{
|
|
|
|
Severity: diag.Error,
|
|
|
|
Summary: `expected float, found map`,
|
2023-12-22 13:20:45 +00:00
|
|
|
Location: dyn.Location{},
|
2023-10-25 11:56:42 +00:00
|
|
|
}, err[0])
|
|
|
|
}
|