Commit 0ab728f6 authored by earncef's avatar earncef Committed by Hanzei
Browse files

New tests for type_specific.go and fixed code (#75)

* Added new tests for type_specific.go and fixed the code to work for both MSI and Map
parent f031c854
......@@ -36,8 +36,8 @@ func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[stri
return s
}
s, ok := v.data.([]Map)
if !ok {
s := v.ObjxMapSlice()
if s == nil {
if len(optionalDefault) == 1 {
return optionalDefault[0]
}
......@@ -55,16 +55,11 @@ func (v *Value) MSISlice(optionalDefault ...[]map[string]interface{}) []map[stri
//
// Panics if the object is not a []map[string]interface{}.
func (v *Value) MustMSISlice() []map[string]interface{} {
s, ok := v.data.([]Map)
if !ok {
return v.data.([]map[string]interface{})
if s := v.MSISlice(); s != nil {
return s
}
result := make([]map[string]interface{}, len(s))
for i := range s {
result[i] = s[i].Value().MustMSI()
}
return result
return v.data.([]map[string]interface{})
}
// IsMSI gets whether the object contained is a map[string]interface{} or not.
......@@ -213,6 +208,8 @@ func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
switch s[i].(type) {
case Map:
result[i] = s[i].(Map)
case map[string]interface{}:
result[i] = New(s[i])
default:
return nil
}
......@@ -224,12 +221,8 @@ func (v *Value) ObjxMapSlice(optionalDefault ...[](Map)) [](Map) {
//
// Panics if the object is not a [](Map).
func (v *Value) MustObjxMapSlice() [](Map) {
if s, ok := v.data.([]map[string]interface{}); ok {
result := make([]Map, len(s))
for i := range s {
result[i] = s[i]
}
return result
if s := v.ObjxMapSlice(); s != nil {
return s
}
return v.data.([](Map))
}
......
......@@ -29,12 +29,19 @@ func TestMSI(t *testing.T) {
func TestMSISlice(t *testing.T) {
val := map[string]interface{}(map[string]interface{}{"name": "Tyler"})
m := objx.Map{"value": []map[string]interface{}{val}, "value2": []objx.Map{val}, "nothing": nil}
m := objx.Map{
"value": []map[string]interface{}{val},
"value2": []objx.Map{val},
"value3": []interface{}{val},
"nothing": nil,
}
assert.Equal(t, val, m.Get("value").MSISlice()[0])
assert.Equal(t, val, m.Get("value2").MSISlice()[0])
assert.Equal(t, val, m.Get("value3").MSISlice()[0])
assert.Equal(t, val, m.Get("value").MustMSISlice()[0])
assert.Equal(t, val, m.Get("value2").MustMSISlice()[0])
assert.Equal(t, val, m.Get("value3").MustMSISlice()[0])
assert.Equal(t, []map[string]interface{}(nil), m.Get("nothing").MSISlice())
assert.Equal(t, val, m.Get("nothing").MSISlice([]map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Tyler"})})[0])
assert.Panics(t, func() {
......@@ -230,13 +237,23 @@ func TestObjxMap(t *testing.T) {
func TestObjxMapSlice(t *testing.T) {
val := (objx.Map)(objx.New(1))
m := objx.Map{"value": [](objx.Map){val}, "value2": []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})}, "nothing": nil}
m := objx.Map{
"value": [](objx.Map){val},
"value2": []map[string]interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})},
"value3": []interface{}{val},
"value4": []interface{}{map[string]interface{}(map[string]interface{}{"name": "Taylor"})},
"nothing": nil,
}
valMSI := objx.Map{"name": "Taylor"}
assert.Equal(t, val, m.Get("value").ObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value2").ObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value3").ObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value4").ObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value").MustObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value2").MustObjxMapSlice()[0])
assert.Equal(t, val, m.Get("value3").MustObjxMapSlice()[0])
assert.Equal(t, valMSI, m.Get("value4").MustObjxMapSlice()[0])
assert.Equal(t, [](objx.Map)(nil), m.Get("nothing").ObjxMapSlice())
assert.Equal(t, val, m.Get("nothing").ObjxMapSlice([](objx.Map){(objx.Map)(objx.New(1))})[0])
assert.Panics(t, func() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment