Skip to content

Commit

Permalink
add unit tests for TestSetNestedStringSlice, TestSetNestedSlice etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
kidlj committed Nov 24, 2021
1 parent e53cf07 commit b06d491
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,63 @@ func TestNestedFieldCopy(t *testing.T) {
func TestCacheableObject(t *testing.T) {
runtimetesting.CacheableObjectTest(t, UnstructuredJSONScheme)
}

func TestSetNestedStringSlice(t *testing.T) {
obj := map[string]interface{}{
"x": map[string]interface{}{
"y": 1,
"a": "foo",
},
}

err := SetNestedStringSlice(obj, []string{"bar"}, "x", "z")
assert.NoError(t, err)
assert.Len(t, obj["x"], 3)
assert.Len(t, obj["x"].(map[string]interface{})["z"], 1)
assert.Equal(t, obj["x"].(map[string]interface{})["z"].([]interface{})[0], "bar")
}

func TestSetNestedSlice(t *testing.T) {
obj := map[string]interface{}{
"x": map[string]interface{}{
"y": 1,
"a": "foo",
},
}

err := SetNestedSlice(obj, []interface{}{"bar"}, "x", "z")
assert.NoError(t, err)
assert.Len(t, obj["x"], 3)
assert.Len(t, obj["x"].(map[string]interface{})["z"], 1)
assert.Equal(t, obj["x"].(map[string]interface{})["z"].([]interface{})[0], "bar")
}

func TestSetNestedStringMap(t *testing.T) {
obj := map[string]interface{}{
"x": map[string]interface{}{
"y": 1,
"a": "foo",
},
}

err := SetNestedStringMap(obj, map[string]string{"b": "bar"}, "x", "z")
assert.NoError(t, err)
assert.Len(t, obj["x"], 3)
assert.Len(t, obj["x"].(map[string]interface{})["z"], 1)
assert.Equal(t, obj["x"].(map[string]interface{})["z"].(map[string]interface{})["b"], "bar")
}

func TestSetNestedMap(t *testing.T) {
obj := map[string]interface{}{
"x": map[string]interface{}{
"y": 1,
"a": "foo",
},
}

err := SetNestedMap(obj, map[string]interface{}{"b": "bar"}, "x", "z")
assert.NoError(t, err)
assert.Len(t, obj["x"], 3)
assert.Len(t, obj["x"].(map[string]interface{})["z"], 1)
assert.Equal(t, obj["x"].(map[string]interface{})["z"].(map[string]interface{})["b"], "bar")
}

0 comments on commit b06d491

Please sign in to comment.