From 34579242aaebcc1f1a36d15e22481a6ae89e7c25 Mon Sep 17 00:00:00 2001 From: Claudio Beatrice Date: Sat, 30 Sep 2023 14:57:34 +0200 Subject: [PATCH] chore: refactor test code into a utility function --- tests/utils.go | 25 +++++++++++++++++++++++++ tests/validation_test.go | 17 +++-------------- 2 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 tests/utils.go diff --git a/tests/utils.go b/tests/utils.go new file mode 100644 index 00000000..a00edaaf --- /dev/null +++ b/tests/utils.go @@ -0,0 +1,25 @@ +package tests + +import "testing" + +func CheckError(t *testing.T, wantErr, gotErr error) { + t.Helper() + + if wantErr == nil && gotErr != nil { + t.Errorf("got error %v, want nil", gotErr) + + return + } + + if wantErr != nil && gotErr == nil { + t.Errorf("got nil, want error %v", wantErr) + + return + } + + if wantErr != nil && gotErr != nil && gotErr.Error() != wantErr.Error() { + t.Errorf("got error %v, want %v", gotErr, wantErr) + + return + } +} diff --git a/tests/validation_test.go b/tests/validation_test.go index 9f072516..b3f48c24 100644 --- a/tests/validation_test.go +++ b/tests/validation_test.go @@ -5,6 +5,7 @@ import ( "errors" "testing" + "github.com/atombender/go-jsonschema/tests" test "github.com/atombender/go-jsonschema/tests/data/validation" ) @@ -52,13 +53,7 @@ func TestMaxStringLength(t *testing.T) { err := json.Unmarshal([]byte(tC.data), &model) - if tC.wantErr == nil && err != nil { - t.Errorf("got error %v, want nil", err) - } else if tC.wantErr != nil && err == nil { - t.Errorf("got nil, want error %v", tC.wantErr) - } else if tC.wantErr != nil && err != nil && err.Error() != tC.wantErr.Error() { - t.Errorf("got error %v, want %v", err, tC.wantErr) - } + tests.CheckError(t, tC.wantErr, err) }) } } @@ -107,13 +102,7 @@ func TestMinStringLength(t *testing.T) { err := json.Unmarshal([]byte(tC.data), &model) - if tC.wantErr == nil && err != nil { - t.Errorf("got error %v, want nil", err) - } else if tC.wantErr != nil && err == nil { - t.Errorf("got nil, want error %v", tC.wantErr) - } else if tC.wantErr != nil && err != nil && err.Error() != tC.wantErr.Error() { - t.Errorf("got error %v, want %v", err, tC.wantErr) - } + tests.CheckError(t, tC.wantErr, err) }) } }