2021-02-11 20:49:03 +03:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNullBool_UnmarshalText(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
data []byte
|
|
|
|
wantErrMsg string
|
|
|
|
want nullBool
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
data: []byte{},
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: nbNull,
|
|
|
|
}, {
|
|
|
|
name: "null",
|
|
|
|
data: []byte("null"),
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: nbNull,
|
|
|
|
}, {
|
|
|
|
name: "true",
|
|
|
|
data: []byte("true"),
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: nbTrue,
|
|
|
|
}, {
|
|
|
|
name: "false",
|
|
|
|
data: []byte("false"),
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: nbFalse,
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
|
|
|
data: []byte("flase"),
|
|
|
|
wantErrMsg: `invalid nullBool value "flase"`,
|
|
|
|
want: nbNull,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var got nullBool
|
|
|
|
err := got.UnmarshalJSON(tc.data)
|
|
|
|
if tc.wantErrMsg == "" {
|
2021-04-13 16:00:09 +03:00
|
|
|
assert.NoError(t, err)
|
2021-02-11 20:49:03 +03:00
|
|
|
} else {
|
2021-04-13 16:00:09 +03:00
|
|
|
require.Error(t, err)
|
|
|
|
|
2021-02-11 20:49:03 +03:00
|
|
|
assert.Equal(t, tc.wantErrMsg, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
want := nbTrue
|
|
|
|
var got struct {
|
|
|
|
A nullBool
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal([]byte(`{"A":true}`), &got)
|
2021-04-13 16:00:09 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 20:49:03 +03:00
|
|
|
assert.Equal(t, want, got.A)
|
|
|
|
})
|
|
|
|
}
|