2022-06-09 07:47:05 -07:00
|
|
|
package aghalg_test
|
2021-02-11 10:49:03 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
2022-06-09 07:47:05 -07:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghalg"
|
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"
|
|
|
|
)
|
|
|
|
|
2022-07-11 07:40:00 -07:00
|
|
|
func TestNullBool_MarshalJSON(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
wantErrMsg string
|
|
|
|
want []byte
|
|
|
|
in aghalg.NullBool
|
|
|
|
}{{
|
|
|
|
name: "null",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: []byte("null"),
|
|
|
|
in: aghalg.NBNull,
|
|
|
|
}, {
|
|
|
|
name: "true",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: []byte("true"),
|
|
|
|
in: aghalg.NBTrue,
|
|
|
|
}, {
|
|
|
|
name: "false",
|
|
|
|
wantErrMsg: "",
|
|
|
|
want: []byte("false"),
|
|
|
|
in: aghalg.NBFalse,
|
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
got, err := tc.in.MarshalJSON()
|
|
|
|
testutil.AssertErrorMsg(t, tc.wantErrMsg, err)
|
|
|
|
|
|
|
|
assert.Equal(t, tc.want, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("json", func(t *testing.T) {
|
|
|
|
in := &struct {
|
|
|
|
A aghalg.NullBool
|
|
|
|
}{
|
|
|
|
A: aghalg.NBTrue,
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := json.Marshal(in)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, []byte(`{"A":true}`), got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
2022-06-09 07:47:05 -07:00
|
|
|
want aghalg.NullBool
|
2021-02-11 10:49:03 -07:00
|
|
|
}{{
|
|
|
|
name: "empty",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte{},
|
2022-06-09 07:47:05 -07:00
|
|
|
want: aghalg.NBNull,
|
2021-02-11 10:49:03 -07:00
|
|
|
}, {
|
|
|
|
name: "null",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("null"),
|
2022-06-09 07:47:05 -07:00
|
|
|
want: aghalg.NBNull,
|
2021-02-11 10:49:03 -07:00
|
|
|
}, {
|
|
|
|
name: "true",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("true"),
|
2022-06-09 07:47:05 -07:00
|
|
|
want: aghalg.NBTrue,
|
2021-02-11 10:49:03 -07:00
|
|
|
}, {
|
|
|
|
name: "false",
|
|
|
|
wantErrMsg: "",
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("false"),
|
2022-06-09 07:47:05 -07:00
|
|
|
want: aghalg.NBFalse,
|
2021-02-11 10:49:03 -07:00
|
|
|
}, {
|
|
|
|
name: "invalid",
|
2022-06-09 07:47:05 -07:00
|
|
|
wantErrMsg: `unmarshalling json data into aghalg.NullBool: bad value "invalid"`,
|
2022-01-25 09:47:02 -07:00
|
|
|
data: []byte("invalid"),
|
2022-06-09 07:47:05 -07:00
|
|
|
want: aghalg.NBNull,
|
2021-02-11 10:49:03 -07:00
|
|
|
}}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2022-06-09 07:47:05 -07:00
|
|
|
var got aghalg.NullBool
|
2021-02-11 10:49:03 -07:00
|
|
|
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) {
|
2022-06-09 07:47:05 -07:00
|
|
|
want := aghalg.NBTrue
|
2021-02-11 10:49:03 -07:00
|
|
|
var got struct {
|
2022-06-09 07:47:05 -07:00
|
|
|
A aghalg.NullBool
|
2021-02-11 10:49:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|