From 754b2b90aaa6760bec84beaef83bcd971df77c42 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 11 Sep 2024 11:34:44 +0200 Subject: [PATCH] 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. --- niljson_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/niljson_test.go b/niljson_test.go index 63535f0..f068f57 100644 --- a/niljson_test.go +++ b/niljson_test.go @@ -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"`