Merge pull request #6 from wneessen/code-cleanup

Code cleanup
This commit is contained in:
Winni Neessen 2024-09-11 10:32:10 +02:00 committed by GitHub
commit c602cb5a96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,13 @@ import (
"testing" "testing"
) )
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( var jsonBytes = []byte(
`{ `{
"bool": true, "bool": true,
@ -37,7 +44,7 @@ func TestVariable_UnmarshalJSON_Boolean(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -68,10 +75,10 @@ func TestVariable_MarshalJSON_Boolean(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -83,7 +90,7 @@ func TestVariable_UnmarshalJSON_ByteSlice(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -114,10 +121,10 @@ func TestVariable_MarshalJSON_ByteSlice(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -130,7 +137,7 @@ func TestVariable_UnmarshalJSON_Float32(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -161,10 +168,10 @@ func TestVariable_MarshalJSON_Float32(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -177,7 +184,7 @@ func TestVariable_UnmarshalJSON_Float64(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -208,10 +215,10 @@ func TestVariable_MarshalJSON_Float64(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -224,7 +231,7 @@ func TestVariable_UnmarshalJSON_Int(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -234,7 +241,7 @@ func TestVariable_UnmarshalJSON_Int(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -255,10 +262,10 @@ func TestVariable_MarshalJSON_Int(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -271,7 +278,7 @@ func TestVariable_UnmarshalJSON_Int64(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -281,7 +288,7 @@ func TestVariable_UnmarshalJSON_Int64(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -302,10 +309,10 @@ func TestVariable_MarshalJSON_Int64(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -318,7 +325,7 @@ func TestVariable_UnmarshalJSON_String(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -349,10 +356,10 @@ func TestVariable_MarshalJSON_String(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -365,7 +372,7 @@ func TestVariable_UnmarshalJSON_UInt(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -375,7 +382,7 @@ func TestVariable_UnmarshalJSON_UInt(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -396,10 +403,10 @@ func TestVariable_MarshalJSON_UInt(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -412,7 +419,7 @@ func TestVariable_UnmarshalJSON_UInt8(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -422,7 +429,7 @@ func TestVariable_UnmarshalJSON_UInt8(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -443,10 +450,10 @@ func TestVariable_MarshalJSON_UInt8(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -459,7 +466,7 @@ func TestVariable_UnmarshalJSON_UInt16(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -469,7 +476,7 @@ func TestVariable_UnmarshalJSON_UInt16(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -490,10 +497,10 @@ func TestVariable_MarshalJSON_UInt16(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -506,7 +513,7 @@ func TestVariable_UnmarshalJSON_UInt32(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -516,7 +523,7 @@ func TestVariable_UnmarshalJSON_UInt32(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -537,10 +544,10 @@ func TestVariable_MarshalJSON_UInt32(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }
@ -553,7 +560,7 @@ func TestVariable_UnmarshalJSON_UInt64(t *testing.T) {
var jt JSONType var jt JSONType
if err := json.Unmarshal(jsonBytes, &jt); err != nil { 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() { if jt.Value.IsNil() {
@ -563,7 +570,7 @@ func TestVariable_UnmarshalJSON_UInt64(t *testing.T) {
t.Errorf("expected nil, but got value") t.Errorf("expected nil, but got value")
} }
if jt.Value.Value() != expected { 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() jt.Value.Reset()
@ -584,10 +591,10 @@ func TestVariable_MarshalJSON_UInt64(t *testing.T) {
} }
data, err := json.Marshal(&jt) data, err := json.Marshal(&jt)
if err != nil { if err != nil {
t.Errorf("failed to marshal json with nil types: %s", err) t.Errorf(ErrMarshalFailed, err)
} }
if !bytes.Equal(data, []byte(expected)) { if !bytes.Equal(data, []byte(expected)) {
t.Errorf("expected json to be %q, got %q", expected, string(data)) t.Errorf(ErrExpectedJSONString, expected, string(data))
} }
} }