2024-01-05 12:06:12 +00:00
|
|
|
package dyn_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/databricks/cli/libs/dyn"
|
2024-03-25 11:01:09 +00:00
|
|
|
assert "github.com/databricks/cli/libs/dyn/dynassert"
|
2024-01-05 12:06:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestValueUnderlyingMap(t *testing.T) {
|
|
|
|
v := dyn.V(
|
|
|
|
map[string]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
"key": dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
2024-01-05 12:06:12 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
vv1, ok := v.AsMap()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsMap()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustMap()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind map, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustMap()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingSequence(t *testing.T) {
|
|
|
|
v := dyn.V(
|
|
|
|
[]dyn.Value{
|
2024-07-16 11:27:27 +00:00
|
|
|
dyn.NewValue("value", []dyn.Location{{File: "file", Line: 1, Column: 2}}),
|
2024-01-05 12:06:12 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
vv1, ok := v.AsSequence()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsSequence()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustSequence()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind sequence, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustSequence()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingString(t *testing.T) {
|
|
|
|
v := dyn.V("value")
|
|
|
|
|
|
|
|
vv1, ok := v.AsString()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsString()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustString()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind string, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustString()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingBool(t *testing.T) {
|
|
|
|
v := dyn.V(true)
|
|
|
|
|
|
|
|
vv1, ok := v.AsBool()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsBool()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustBool()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind bool, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustBool()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingInt(t *testing.T) {
|
|
|
|
v := dyn.V(int(1))
|
|
|
|
|
|
|
|
vv1, ok := v.AsInt()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsInt()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustInt()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind int, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustInt()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Test int32 type specifically.
|
|
|
|
v = dyn.V(int32(1))
|
|
|
|
vv1, ok = v.AsInt()
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int64(1), vv1)
|
|
|
|
|
|
|
|
// Test int64 type specifically.
|
|
|
|
v = dyn.V(int64(1))
|
|
|
|
vv1, ok = v.AsInt()
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, int64(1), vv1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingFloat(t *testing.T) {
|
|
|
|
v := dyn.V(float32(1.0))
|
|
|
|
|
|
|
|
vv1, ok := v.AsFloat()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsFloat()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustFloat()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind float, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustFloat()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Test float64 type specifically.
|
|
|
|
v = dyn.V(float64(1.0))
|
|
|
|
vv1, ok = v.AsFloat()
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, float64(1.0), vv1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValueUnderlyingTime(t *testing.T) {
|
2024-08-29 13:02:34 +00:00
|
|
|
v := dyn.V(dyn.FromTime(time.Now()))
|
2024-01-05 12:06:12 +00:00
|
|
|
|
|
|
|
vv1, ok := v.AsTime()
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
2024-06-19 15:24:57 +00:00
|
|
|
_, ok = dyn.InvalidValue.AsTime()
|
2024-01-05 12:06:12 +00:00
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
vv2 := v.MustTime()
|
|
|
|
assert.Equal(t, vv1, vv2)
|
|
|
|
|
|
|
|
// Test panic.
|
2024-06-19 15:24:57 +00:00
|
|
|
assert.PanicsWithValue(t, "expected kind time, got invalid", func() {
|
|
|
|
dyn.InvalidValue.MustTime()
|
2024-01-05 12:06:12 +00:00
|
|
|
})
|
|
|
|
}
|