Skip to content

Commit

Permalink
chore: refactor test code into a utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Sep 30, 2023
1 parent 25469af commit 3457924
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
25 changes: 25 additions & 0 deletions tests/utils.go
Original file line number Diff line number Diff line change
@@ -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
}
}
17 changes: 3 additions & 14 deletions tests/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"testing"

"github.com/atombender/go-jsonschema/tests"
test "github.com/atombender/go-jsonschema/tests/data/validation"
)

Expand Down Expand Up @@ -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)
})
}
}
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 3457924

Please sign in to comment.