From 296050ca707702a3d1668652f055422aea57d405 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 11 Sep 2024 10:27:55 +0200 Subject: [PATCH 1/3] Define error constants for JSON marshaling/unmarshaling tests Introduced `ErrUnmarshalFailed` and `ErrMarshalFailed` constants to standardize error messages in JSON tests. This enhances code readability and maintainability by avoiding repeated strings directly in the test cases. --- niljson_test.go | 53 +++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/niljson_test.go b/niljson_test.go index 315029c..8b53090 100644 --- a/niljson_test.go +++ b/niljson_test.go @@ -12,6 +12,11 @@ import ( "testing" ) +const ( + ErrUnmarshalFailed = "failed to unmarshal json with nil types: %s" + ErrMarshalFailed = "failed to marshal json with nil types: %s" +) + var jsonBytes = []byte( `{ "bool": true, @@ -37,7 +42,7 @@ func TestVariable_UnmarshalJSON_Boolean(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -68,7 +73,7 @@ func TestVariable_MarshalJSON_Boolean(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -83,7 +88,7 @@ func TestVariable_UnmarshalJSON_ByteSlice(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -114,7 +119,7 @@ func TestVariable_MarshalJSON_ByteSlice(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -130,7 +135,7 @@ func TestVariable_UnmarshalJSON_Float32(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -161,7 +166,7 @@ func TestVariable_MarshalJSON_Float32(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -177,7 +182,7 @@ func TestVariable_UnmarshalJSON_Float64(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -208,7 +213,7 @@ func TestVariable_MarshalJSON_Float64(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -224,7 +229,7 @@ func TestVariable_UnmarshalJSON_Int(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -255,7 +260,7 @@ func TestVariable_MarshalJSON_Int(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -271,7 +276,7 @@ func TestVariable_UnmarshalJSON_Int64(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -302,7 +307,7 @@ func TestVariable_MarshalJSON_Int64(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -318,7 +323,7 @@ func TestVariable_UnmarshalJSON_String(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -349,7 +354,7 @@ func TestVariable_MarshalJSON_String(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -365,7 +370,7 @@ func TestVariable_UnmarshalJSON_UInt(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -396,7 +401,7 @@ func TestVariable_MarshalJSON_UInt(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -412,7 +417,7 @@ func TestVariable_UnmarshalJSON_UInt8(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -443,7 +448,7 @@ func TestVariable_MarshalJSON_UInt8(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -459,7 +464,7 @@ func TestVariable_UnmarshalJSON_UInt16(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -490,7 +495,7 @@ func TestVariable_MarshalJSON_UInt16(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -506,7 +511,7 @@ func TestVariable_UnmarshalJSON_UInt32(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -537,7 +542,7 @@ func TestVariable_MarshalJSON_UInt32(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) @@ -553,7 +558,7 @@ func TestVariable_UnmarshalJSON_UInt64(t *testing.T) { var jt JSONType if err := json.Unmarshal(jsonBytes, &jt); err != nil { - t.Errorf("failed to unmarshal json with nil types: %s", err) + t.Errorf(ErrUnmarshalFailed, err) } if jt.Value.IsNil() { @@ -584,7 +589,7 @@ func TestVariable_MarshalJSON_UInt64(t *testing.T) { } data, err := json.Marshal(&jt) if err != nil { - t.Errorf("failed to marshal json with nil types: %s", err) + t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { t.Errorf("expected json to be %q, got %q", expected, string(data)) From 98bcb37c50ede38af2fc96b0926a5713347f5544 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 11 Sep 2024 10:30:02 +0200 Subject: [PATCH 2/3] Refactor error messages to use constants in tests Added a new constant ErrExpectedJSONString for uniformity in error messages. Updated all relevant test cases to utilize this new constant instead of hardcoding the error message, enhancing code maintainability and readability. --- niljson_test.go | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/niljson_test.go b/niljson_test.go index 8b53090..abea807 100644 --- a/niljson_test.go +++ b/niljson_test.go @@ -13,8 +13,9 @@ import ( ) const ( - ErrUnmarshalFailed = "failed to unmarshal json with nil types: %s" - ErrMarshalFailed = "failed to marshal json with nil types: %s" + ErrUnmarshalFailed = "failed to unmarshal json with nil types: %s" + ErrMarshalFailed = "failed to marshal json with nil types: %s" + ErrExpectedJSONString = "expected json to be %q, got %q" ) var jsonBytes = []byte( @@ -76,7 +77,7 @@ func TestVariable_MarshalJSON_Boolean(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -122,7 +123,7 @@ func TestVariable_MarshalJSON_ByteSlice(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -169,7 +170,7 @@ func TestVariable_MarshalJSON_Float32(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -216,7 +217,7 @@ func TestVariable_MarshalJSON_Float64(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -263,7 +264,7 @@ func TestVariable_MarshalJSON_Int(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -310,7 +311,7 @@ func TestVariable_MarshalJSON_Int64(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -357,7 +358,7 @@ func TestVariable_MarshalJSON_String(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -404,7 +405,7 @@ func TestVariable_MarshalJSON_UInt(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -451,7 +452,7 @@ func TestVariable_MarshalJSON_UInt8(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -498,7 +499,7 @@ func TestVariable_MarshalJSON_UInt16(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -545,7 +546,7 @@ func TestVariable_MarshalJSON_UInt32(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } @@ -592,7 +593,7 @@ func TestVariable_MarshalJSON_UInt64(t *testing.T) { t.Errorf(ErrMarshalFailed, err) } if !bytes.Equal(data, []byte(expected)) { - t.Errorf("expected json to be %q, got %q", expected, string(data)) + t.Errorf(ErrExpectedJSONString, expected, string(data)) } } From 9584d3ec3c3feb311efbd0411a1104691bf0e70a Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 11 Sep 2024 10:31:42 +0200 Subject: [PATCH 3/3] Refactor error messages in `niljson_test.go` Replaced hardcoded error strings with `ErrExpectedJSONInt` constant for consistency. This improves maintainability and readability of the test code by centralizing error message definitions. --- niljson_test.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/niljson_test.go b/niljson_test.go index abea807..c3f3f3d 100644 --- a/niljson_test.go +++ b/niljson_test.go @@ -16,6 +16,7 @@ const ( ErrUnmarshalFailed = "failed to unmarshal json with nil types: %s" ErrMarshalFailed = "failed to marshal json with nil types: %s" ErrExpectedJSONString = "expected json to be %q, got %q" + ErrExpectedJSONInt = "expected json to be %d, got %d" ) var jsonBytes = []byte( @@ -240,7 +241,7 @@ func TestVariable_UnmarshalJSON_Int(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -287,7 +288,7 @@ func TestVariable_UnmarshalJSON_Int64(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -381,7 +382,7 @@ func TestVariable_UnmarshalJSON_UInt(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -428,7 +429,7 @@ func TestVariable_UnmarshalJSON_UInt8(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -475,7 +476,7 @@ func TestVariable_UnmarshalJSON_UInt16(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -522,7 +523,7 @@ func TestVariable_UnmarshalJSON_UInt32(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset() @@ -569,7 +570,7 @@ func TestVariable_UnmarshalJSON_UInt64(t *testing.T) { t.Errorf("expected nil, but got value") } if jt.Value.Value() != expected { - t.Errorf("expected value to be %d, got %d", expected, jt.Value.Value()) + t.Errorf(ErrExpectedJSONInt, expected, jt.Value.Value()) } jt.Value.Reset()