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