Skip to content

Commit

Permalink
Merge pull request #2998 from gophercloud/bp-v1-7b093f0
Browse files Browse the repository at this point in the history
[v1] testhelper: mark all helpers with t.Helper
  • Loading branch information
mandre committed Mar 18, 2024
2 parents 1e00fc2 + 7fc854b commit fab2a6e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions testhelper/convenience.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ func yellow(str interface{}) string {
}

func logFatal(t *testing.T, str string) {
t.Helper()
t.Fatalf(logBodyFmt, prefix(3), str)
}

func logError(t *testing.T, str string) {
t.Helper()
t.Errorf(logBodyFmt, prefix(3), str)
}

Expand Down Expand Up @@ -214,13 +216,17 @@ func deepDiff(expected, actual interface{}, logDifference diffLogger) {
// AssertEquals compares two arbitrary values and performs a comparison. If the
// comparison fails, a fatal error is raised that will fail the test
func AssertEquals(t *testing.T, expected, actual interface{}) {
t.Helper()

if expected != actual {
logFatal(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
}
}

// CheckEquals is similar to AssertEquals, except with a non-fatal error
func CheckEquals(t *testing.T, expected, actual interface{}) {
t.Helper()

if expected != actual {
logError(t, fmt.Sprintf("expected %s but got %s", green(expected), yellow(actual)))
}
Expand All @@ -229,6 +235,8 @@ func CheckEquals(t *testing.T, expected, actual interface{}) {
// AssertDeepEquals - like Equals - performs a comparison - but on more complex
// structures that requires deeper inspection
func AssertDeepEquals(t *testing.T, expected, actual interface{}) {
t.Helper()

pre := prefix(2)

differed := false
Expand All @@ -247,6 +255,8 @@ func AssertDeepEquals(t *testing.T, expected, actual interface{}) {

// CheckDeepEquals is similar to AssertDeepEquals, except with a non-fatal error
func CheckDeepEquals(t *testing.T, expected, actual interface{}) {
t.Helper()

pre := prefix(2)

deepDiff(expected, actual, func(path []string, expected, actual interface{}) {
Expand All @@ -264,13 +274,17 @@ func isByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) b

// AssertByteArrayEquals a convenience function for checking whether two byte arrays are equal
func AssertByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) {
t.Helper()

if !isByteArrayEquals(t, expectedBytes, actualBytes) {
logFatal(t, "The bytes differed.")
}
}

// CheckByteArrayEquals a convenience function for silent checking whether two byte arrays are equal
func CheckByteArrayEquals(t *testing.T, expectedBytes []byte, actualBytes []byte) {
t.Helper()

if !isByteArrayEquals(t, expectedBytes, actualBytes) {
logError(t, "The bytes differed.")
}
Expand Down Expand Up @@ -321,13 +335,17 @@ func isJSONEquals(t *testing.T, expectedJSON string, actual interface{}) bool {
// This is useful for comparing structures that are built as nested map[string]interface{} values,
// which are a pain to construct as literals.
func AssertJSONEquals(t *testing.T, expectedJSON string, actual interface{}) {
t.Helper()

if !isJSONEquals(t, expectedJSON, actual) {
logFatal(t, "The generated JSON structure differed.")
}
}

// CheckJSONEquals is similar to AssertJSONEquals, but nonfatal.
func CheckJSONEquals(t *testing.T, expectedJSON string, actual interface{}) {
t.Helper()

if !isJSONEquals(t, expectedJSON, actual) {
logError(t, "The generated JSON structure differed.")
}
Expand All @@ -336,6 +354,8 @@ func CheckJSONEquals(t *testing.T, expectedJSON string, actual interface{}) {
// AssertNoErr is a convenience function for checking whether an error value is
// an actual error
func AssertNoErr(t *testing.T, e error) {
t.Helper()

if e != nil {
logFatal(t, fmt.Sprintf("unexpected error %s", yellow(e.Error())))
}
Expand All @@ -344,13 +364,17 @@ func AssertNoErr(t *testing.T, e error) {
// AssertErr is a convenience function for checking whether an error value is
// nil
func AssertErr(t *testing.T, e error) {
t.Helper()

if e == nil {
logFatal(t, fmt.Sprintf("expected error, got nil"))
}
}

// CheckNoErr is similar to AssertNoErr, except with a non-fatal error
func CheckNoErr(t *testing.T, e error) {
t.Helper()

if e != nil {
logError(t, fmt.Sprintf("unexpected error %s", yellow(e.Error())))
}
Expand All @@ -364,6 +388,8 @@ func CheckNoErr(t *testing.T, e error) {
// CheckErr panics if expected contains anything other than non-nil pointers to
// either a type that implements error, or to any interface type.
func CheckErr(t *testing.T, e error, expected ...interface{}) {
t.Helper()

if e == nil {
logError(t, "expected error, got nil")
return
Expand All @@ -381,13 +407,17 @@ func CheckErr(t *testing.T, e error, expected ...interface{}) {

// AssertIntLesserOrEqual verifies that first value is lesser or equal than second values
func AssertIntLesserOrEqual(t *testing.T, v1 int, v2 int) {
t.Helper()

if !(v1 <= v2) {
logFatal(t, fmt.Sprintf("The first value \"%v\" is greater than the second value \"%v\"", v1, v2))
}
}

// AssertIntGreaterOrEqual verifies that first value is greater or equal than second values
func AssertIntGreaterOrEqual(t *testing.T, v1 int, v2 int) {
t.Helper()

if !(v1 >= v2) {
logFatal(t, fmt.Sprintf("The first value \"%v\" is lesser than the second value \"%v\"", v1, v2))
}
Expand Down

0 comments on commit fab2a6e

Please sign in to comment.