2020-06-27 15:48:36 +00:00
|
|
|
package v1
|
2020-01-24 17:21:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2020-07-22 19:42:43 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-01-24 17:21:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecodeOpts(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
res interface{}
|
|
|
|
err error
|
|
|
|
result string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "EmptyInput",
|
|
|
|
input: "",
|
|
|
|
res: &struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}{},
|
|
|
|
result: `{}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "EmptyJSON",
|
|
|
|
input: "{}",
|
|
|
|
res: &struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}{},
|
|
|
|
result: `{}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "BadInput",
|
|
|
|
input: "bad",
|
|
|
|
res: &struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}{},
|
|
|
|
err: errors.New("open bad: no such file or directory"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "GoodDirect",
|
|
|
|
input: `{"name":"test"}`,
|
|
|
|
res: &struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
}{},
|
|
|
|
result: `{"name":"test"}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
err := decodeOpts(test.input, test.res)
|
|
|
|
if test.err == nil {
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err)
|
2020-01-24 17:21:31 +00:00
|
|
|
recoded, err := json.Marshal(test.res)
|
2020-07-22 19:42:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.DeepEqual(t, []byte(test.result), recoded, "Unexpected recoded value")
|
2020-01-24 17:21:31 +00:00
|
|
|
} else {
|
2020-07-22 19:42:43 +00:00
|
|
|
assert.ErrorContains(t, test.err.Error(), err)
|
2020-01-24 17:21:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|