2021-02-11 10:49:03 -07:00
|
|
|
package dhcpd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-22 01:58:18 -07:00
|
|
|
"github.com/AdguardTeam/golibs/testutil"
|
2021-02-11 10:49:03 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-06-10 08:57:53 -07:00
|
|
|
func TestNullBool_UnmarshalJSON(t *testing.T) {
|
2021-02-11 10:49:03 -07:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
2022-01-25 09:47:02 -07:00
|
|
|
data []byte
|
2021-02-11 10:49:03 -07:00
|
|
|
want nullBool
|
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte{},
|
2021-02-11 10:49:03 -07:00
|
|
|
want: nbNull,
|
|
|
|
}, {
|
|
|
|
name: "null",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("null"),
|
2021-02-11 10:49:03 -07:00
|
|
|
want: nbNull,
|
|
|
|
}, {
|
|
|
|
name: "true",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("true"),
|
2021-02-11 10:49:03 -07:00
|
|
|
want: nbTrue,
|
|
|
|
}, {
|
|
|
|
name: "false",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("false"),
|
2021-02-11 10:49:03 -07:00
|
|
|
want: nbFalse,
|
|
|
|
}, {
|
|
|
|
name: "invalid",
|
2022-01-25 09:47:02 -07:00
|
|
|
wantErrMsg: `invalid nullBool value "invalid"`,
|
|
|
|
data: []byte("invalid"),
|
2021-02-11 10:49:03 -07:00
|
|
|
want: nbNull,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
var got nullBool
|
|
|
|
err := got.UnmarshalJSON(tc.data)
|
2021-10-22 01:58:18 -07:00
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
2021-02-11 10:49:03 -07:00
|
|
|
|
|
|
|
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 06:00:09 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-11 10:49:03 -07:00
|
|
|
assert.Equal(t, want, got.A)
|
|
|
|
})
|
|
|
|
}
|