Add test for omitted field

Introduce a new test, `TestVariable_Omitted`, to verify the behavior of the `NilBoolean` type fields, ensuring the correct handling of omitted and non-omitted fields during JSON unmarshalling.
This commit is contained in:
Winni Neessen 2024-09-11 11:34:44 +02:00
parent 7f2cf28053
commit 754b2b90aa
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -602,6 +602,28 @@ func TestVariable_MarshalJSON_UInt64(t *testing.T) {
}
}
func TestVariable_Omitted(t *testing.T) {
type JSONType struct {
NilValue NilBoolean `json:"nilvalue"`
Omitted NilBoolean `json:"omitted,omitempty"`
}
var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil {
t.Errorf(ErrUnmarshalFailed, err)
}
if jt.NilValue.NotNil() {
t.Error(ErrExpectedNil)
}
if jt.NilValue.Omitted() {
t.Error("expected nil value to be not omitted")
}
if !jt.Omitted.Omitted() {
t.Error("expected omitted value to be omitted")
}
}
func ExampleVariable_UnmarshalJSON() {
type JSONType struct {
Bool NilBoolean `json:"bool"`