Test types.StateType#UnmarshalJSON()

This commit is contained in:
Alexander A. Klimov 2024-09-05 15:38:00 +02:00
parent 277a61d30e
commit e0e357bcc9

View file

@ -0,0 +1,36 @@
package types
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestStateType_UnmarshalJSON(t *testing.T) {
subtests := []struct {
name string
input string
output StateType
error bool
}{
{name: "bad-json", input: "bad JSON", error: true},
{name: "bool", input: "false", error: true},
{name: "string", input: `"0"`, error: true},
{name: "negative", input: "-1", error: true},
{name: "fraction", input: "0.5", error: true},
{name: "out-of-range", input: "2", error: true},
{name: "soft", input: "0", output: StateSoft},
{name: "hard", input: "1", output: StateHard},
}
for _, st := range subtests {
t.Run(st.name, func(t *testing.T) {
var s StateType
if err := s.UnmarshalJSON([]byte(st.input)); st.error {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, st.output, s)
}
})
}
}