Skip to content

Commit

Permalink
Params: sanity check unmarshalling works with multiline json.
Browse files Browse the repository at this point in the history
This doesn't add any new behavior, but adds additional testing to make
sure that we can safely unmarshal data regardless of whether it is
formatted with indents / multilines.
  • Loading branch information
wlynch authored and tekton-robot committed May 10, 2022
1 parent 1d0bce5 commit 79e591b
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions pkg/apis/pipeline/v1beta1/param_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1_test

import (
"bytes"
"context"
"encoding/json"
"reflect"
Expand Down Expand Up @@ -168,23 +169,56 @@ type ArrayOrStringHolder struct {

func TestArrayOrString_UnmarshalJSON(t *testing.T) {
cases := []struct {
input string
input map[string]interface{}
result v1beta1.ArrayOrString
}{
{"{\"val\": \"123\"}", *v1beta1.NewArrayOrString("123")},
{"{\"val\": \"\"}", *v1beta1.NewArrayOrString("")},
{"{\"val\":[]}", v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{}}},
{"{\"val\":[\"oneelement\"]}", v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"oneelement"}}},
{"{\"val\":[\"multiple\", \"elements\"]}", *v1beta1.NewArrayOrString("multiple", "elements")},
{
input: map[string]interface{}{"val": "123"},
result: *v1beta1.NewArrayOrString("123"),
},
{
input: map[string]interface{}{"val": ""},
result: *v1beta1.NewArrayOrString(""),
},
{
input: map[string]interface{}{"val": nil},
result: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: nil},
},
{
input: map[string]interface{}{"val": []string{}},
result: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{}},
},
{
input: map[string]interface{}{"val": []string{"oneelement"}},
result: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"oneelement"}},
},
{
input: map[string]interface{}{"val": []string{"multiple", "elements"}},
result: v1beta1.ArrayOrString{Type: v1beta1.ParamTypeArray, ArrayVal: []string{"multiple", "elements"}},
},
}

for _, c := range cases {
var result ArrayOrStringHolder
if err := json.Unmarshal([]byte(c.input), &result); err != nil {
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
}
if !reflect.DeepEqual(result.AOrS, c.result) {
t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
for _, opts := range []func(enc *json.Encoder){
// Default encoding
func(enc *json.Encoder) {},
// Multiline encoding
func(enc *json.Encoder) { enc.SetIndent("", " ") },
} {
b := new(bytes.Buffer)
enc := json.NewEncoder(b)
opts(enc)
if err := enc.Encode(c.input); err != nil {
t.Fatalf("error encoding json: %v", err)
}

var result ArrayOrStringHolder
if err := json.Unmarshal(b.Bytes(), &result); err != nil {
t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
}
if !reflect.DeepEqual(result.AOrS, c.result) {
t.Errorf("expected %+v, got %+v", c.result, result)
}
}
}
}
Expand Down

0 comments on commit 79e591b

Please sign in to comment.