diff --git a/openapi/utils_test.go b/openapi/utils_test.go index bb3a8c60..b9d0ff4b 100644 --- a/openapi/utils_test.go +++ b/openapi/utils_test.go @@ -3,6 +3,7 @@ package openapi import ( "encoding/json" "testing" + "time" ) func TestPtrBool(t *testing.T) { @@ -253,3 +254,490 @@ func TestNullableFloat64(t *testing.T) { t.Errorf("NullableFloat64.UnmarshalJSON().Get() = %v, want %v", *nf64.Get(), 42.0) } } + +func TestNullableBool_MarshalJSON(t *testing.T) { + trueValue := true + nullableBool := NewNullableBool(&trueValue) + + data, err := nullableBool.MarshalJSON() + if err != nil { + t.Fatalf("MarshalJSON failed: %v", err) + } + + expected := `true` + if string(data) != expected { + t.Errorf("Expected JSON %s, got %s", expected, string(data)) + } + + // Test null value + nullableBool.Unset() + data, err = nullableBool.MarshalJSON() + if err != nil { + t.Fatalf("MarshalJSON failed: %v", err) + } + + expected = `null` + if string(data) != expected { + t.Errorf("Expected JSON %s, got %s", expected, string(data)) + } +} + +func TestNullableInt_Get(t *testing.T) { + value := 42 + nullableInt := NewNullableInt(&value) + + got := nullableInt.Get() + if got == nil || *got != value { + t.Errorf("Expected Get() to return %d, got %v", value, got) + } + + nullableInt.Unset() + got = nullableInt.Get() + if got != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", *got) + } +} + +func TestNullableInt_Set(t *testing.T) { + value := 42 + nullableInt := NullableInt{} + + nullableInt.Set(&value) + got := nullableInt.Get() + if got == nil || *got != value { + t.Errorf("Expected Set() to set value %d, got %v", value, got) + } + + if !nullableInt.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt_IsSet(t *testing.T) { + nullableInt := NullableInt{} + + if nullableInt.IsSet() { + t.Errorf("Expected IsSet() to return false for unset NullableInt, got true") + } + + value := 42 + nullableInt.Set(&value) + + if !nullableInt.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt_Unset(t *testing.T) { + value := 42 + nullableInt := NewNullableInt(&value) + + nullableInt.Unset() + if nullableInt.IsSet() { + t.Errorf("Expected IsSet() to return false after Unset(), got true") + } + + if nullableInt.Get() != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", nullableInt.Get()) + } +} + +func TestNullableInt32_Get(t *testing.T) { + value := int32(42) + nullableInt32 := NewNullableInt32(&value) + + got := nullableInt32.Get() + if got == nil || *got != value { + t.Errorf("Expected Get() to return %d, got %v", value, got) + } + + nullableInt32.Unset() + got = nullableInt32.Get() + if got != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", got) + } +} + +func TestNullableInt32_Set(t *testing.T) { + value := int32(42) + nullableInt32 := NullableInt32{} + + nullableInt32.Set(&value) + got := nullableInt32.Get() + if got == nil || *got != value { + t.Errorf("Expected Set() to set value %d, got %v", value, got) + } + + if !nullableInt32.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt32_IsSet(t *testing.T) { + nullableInt32 := NullableInt32{} + + if nullableInt32.IsSet() { + t.Errorf("Expected IsSet() to return false for unset NullableInt32, got true") + } + + value := int32(42) + nullableInt32.Set(&value) + + if !nullableInt32.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt32_Unset(t *testing.T) { + value := int32(42) + nullableInt32 := NewNullableInt32(&value) + + nullableInt32.Unset() + if nullableInt32.IsSet() { + t.Errorf("Expected IsSet() to return false after Unset(), got true") + } + + if nullableInt32.Get() != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", nullableInt32.Get()) + } +} + +func TestNullableInt32_MarshalJSON(t *testing.T) { + value := int32(42) + nullableInt32 := NewNullableInt32(&value) + + data, err := nullableInt32.MarshalJSON() + if err != nil { + t.Fatalf("Unexpected error during MarshalJSON: %v", err) + } + + expected := "42" + if string(data) != expected { + t.Errorf("Expected MarshalJSON output to be %s, got %s", expected, string(data)) + } +} + +func TestNullableInt32_UnmarshalJSON(t *testing.T) { + data := []byte("42") + nullableInt32 := NullableInt32{} + + err := nullableInt32.UnmarshalJSON(data) + if err != nil { + t.Fatalf("Unexpected error during UnmarshalJSON: %v", err) + } + + got := nullableInt32.Get() + if got == nil || *got != 42 { + t.Errorf("Expected UnmarshalJSON to set value to 42, got %v", got) + } + + if !nullableInt32.IsSet() { + t.Errorf("Expected IsSet() to return true after UnmarshalJSON, got false") + } +} + +func TestNullableInt64_Get(t *testing.T) { + value := int64(64) + nullableInt64 := NewNullableInt64(&value) + + got := nullableInt64.Get() + if got == nil || *got != value { + t.Errorf("Expected Get() to return %d, got %v", value, got) + } + + nullableInt64.Unset() + got = nullableInt64.Get() + if got != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", got) + } +} + +func TestNullableInt64_Set(t *testing.T) { + value := int64(64) + nullableInt64 := NullableInt64{} + + nullableInt64.Set(&value) + got := nullableInt64.Get() + if got == nil || *got != value { + t.Errorf("Expected Set() to set value %d, got %v", value, got) + } + + if !nullableInt64.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt64_IsSet(t *testing.T) { + nullableInt64 := NullableInt64{} + + if nullableInt64.IsSet() { + t.Errorf("Expected IsSet() to return false for unset NullableInt64, got true") + } + + value := int64(64) + nullableInt64.Set(&value) + + if !nullableInt64.IsSet() { + t.Errorf("Expected IsSet() to return true after Set(), got false") + } +} + +func TestNullableInt64_Unset(t *testing.T) { + value := int64(64) + nullableInt64 := NewNullableInt64(&value) + + nullableInt64.Unset() + if nullableInt64.IsSet() { + t.Errorf("Expected IsSet() to return false after Unset(), got true") + } + + if nullableInt64.Get() != nil { + t.Errorf("Expected Get() to return nil after Unset(), got %v", nullableInt64.Get()) + } +} + +func TestNewNullableInt64(t *testing.T) { + val := int64(42) + nullableInt := NewNullableInt64(&val) + + if nullableInt == nil { + t.Fatal("Expected NewNullableInt64 to return a non-nil value") + } + if *nullableInt.value != val { + t.Fatalf("Expected %d, got %d", val, *nullableInt.value) + } + if !nullableInt.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test MarshalJSON for NullableInt64 +func TestNullableInt64_MarshalJSON(t *testing.T) { + val := int64(42) + nullableInt := NewNullableInt64(&val) + data, err := nullableInt.MarshalJSON() + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := "42" + if string(data) != expected { + t.Fatalf("Expected %s, got %s", expected, string(data)) + } +} + +// Test UnmarshalJSON for NullableInt64 +func TestNullableInt64_UnmarshalJSON(t *testing.T) { + nullableInt := &NullableInt64{} + data := []byte("42") + err := nullableInt.UnmarshalJSON(data) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := int64(42) + if *nullableInt.value != expected { + t.Fatalf("Expected %d, got %d", expected, *nullableInt.value) + } + if !nullableInt.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test MarshalJSON for NullableFloat32 +func TestNullableFloat32_MarshalJSON(t *testing.T) { + val := float32(42.42) + nullableFloat := NewNullableFloat32(&val) + data, err := nullableFloat.MarshalJSON() + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := "42.42" + if string(data) != expected { + t.Fatalf("Expected %s, got %s", expected, string(data)) + } +} + +// Test UnmarshalJSON for NullableFloat32 +func TestNullableFloat32_UnmarshalJSON(t *testing.T) { + nullableFloat := &NullableFloat32{} + data := []byte("42.42") + err := nullableFloat.UnmarshalJSON(data) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := float32(42.42) + if *nullableFloat.Get() != expected { + t.Fatalf("Expected %f, got %f", expected, *nullableFloat.Get()) + } + if !nullableFloat.IsSet() { + t.Fatal("Expected IsSet to be true") + } +} + +// Test MarshalJSON for NullableFloat64 +func TestNullableFloat64_MarshalJSON(t *testing.T) { + val := float64(42.42) + nullableFloat := NewNullableFloat64(&val) + data, err := nullableFloat.MarshalJSON() + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := "42.42" + if string(data) != expected { + t.Fatalf("Expected %s, got %s", expected, string(data)) + } +} + +// Test UnmarshalJSON for NullableFloat64 +func TestNullableFloat64_UnmarshalJSON(t *testing.T) { + nullableFloat := &NullableFloat64{} + data := []byte("42.42") + err := nullableFloat.UnmarshalJSON(data) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := float64(42.42) + if *nullableFloat.Get() != expected { + t.Fatalf("Expected %f, got %f", expected, *nullableFloat.Get()) + } + if !nullableFloat.IsSet() { + t.Fatal("Expected IsSet to be true") + } +} + +func TestNewNullableString(t *testing.T) { + val := "Hello, World!" + nullableString := NewNullableString(&val) + + if nullableString == nil { + t.Fatal("Expected NewNullableString to return a non-nil value") + } + if *nullableString.value != val { + t.Fatalf("Expected %s, got %s", val, *nullableString.value) + } + if !nullableString.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test MarshalJSON for NullableString +func TestNullableString_MarshalJSON(t *testing.T) { + val := "Hello, World!" + nullableString := NewNullableString(&val) + data, err := nullableString.MarshalJSON() + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := `"Hello, World!"` + if string(data) != expected { + t.Fatalf("Expected %s, got %s", expected, string(data)) + } +} + +// Test UnmarshalJSON for NullableString +func TestNullableString_UnmarshalJSON(t *testing.T) { + nullableString := &NullableString{} + data := []byte(`"Hello, World!"`) + err := nullableString.UnmarshalJSON(data) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := "Hello, World!" + if *nullableString.value != expected { + t.Fatalf("Expected %s, got %s", expected, *nullableString.value) + } + if !nullableString.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test NewNullableTime constructor +func TestNewNullableTime(t *testing.T) { + val := time.Now() + nullableTime := NewNullableTime(&val) + + if nullableTime == nil { + t.Fatal("Expected NewNullableTime to return a non-nil value") + } + if nullableTime.value == nil { + t.Fatal("Expected value to be set") + } + if !nullableTime.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test MarshalJSON for NullableTime +func TestNullableTime_MarshalJSON(t *testing.T) { + // Get current time and truncate nanoseconds + val := time.Now().Truncate(time.Second) // Truncate to second precision + nullableTime := NewNullableTime(&val) + data, err := nullableTime.MarshalJSON() + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + // Format the time to second precision + expected := `"` + val.Format("2006-01-02T15:04:05-07:00") + `"` + if string(data) != expected { + t.Fatalf("Expected %s, got %s", expected, string(data)) + } +} + +// Test UnmarshalJSON for NullableTime +func TestNullableTime_UnmarshalJSON(t *testing.T) { + nullableTime := &NullableTime{} + data := []byte(`"2025-01-01T00:00:00Z"`) + err := nullableTime.UnmarshalJSON(data) + + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + + expected := "2025-01-01T00:00:00Z" + if nullableTime.value.Format(time.RFC3339) != expected { + t.Fatalf("Expected %s, got %s", expected, nullableTime.value.Format(time.RFC3339)) + } + if !nullableTime.isSet { + t.Fatal("Expected isSet to be true") + } +} + +// Test IsNil function +func TestIsNil(t *testing.T) { + var nilPtr *int + if !IsNil(nilPtr) { + t.Fatal("Expected IsNil(nilPtr) to return true") + } + + var nonNilPtr = new(int) + if IsNil(nonNilPtr) { + t.Fatal("Expected IsNil(nonNilPtr) to return false") + } + + var emptySlice []int + if !IsNil(emptySlice) { + t.Fatal("Expected IsNil(emptySlice) to return true") + } + + var nonEmptySlice = []int{1} + if IsNil(nonEmptySlice) { + t.Fatal("Expected IsNil(nonEmptySlice) to return false") + } +}