forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uses ensureNoPanic test helper function so there is no need to write the test case name prior to executing code that might cause a panic.
- Loading branch information
Karrick S. McDermott
committed
Apr 26, 2019
1 parent
ce0a001
commit 0eb2682
Showing
3 changed files
with
115 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package goavro | ||
|
||
// NOTE: This file was copied from https://github.com/karrick/gorill | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func ensureBuffer(tb testing.TB, buf []byte, n int, want string) { | ||
tb.Helper() | ||
if got, want := n, len(want); got != want { | ||
tb.Fatalf("GOT: %v; WANT: %v", got, want) | ||
} | ||
if got, want := string(buf[:n]), want; got != want { | ||
tb.Errorf("GOT: %v; WANT: %v", got, want) | ||
} | ||
} | ||
|
||
func ensureError(tb testing.TB, err error, contains ...string) { | ||
tb.Helper() | ||
if len(contains) == 0 { | ||
if err != nil { | ||
tb.Errorf("GOT: %v; WANT: %v", err, contains) | ||
} | ||
} else if err == nil { | ||
tb.Errorf("GOT: %v; WANT: %v", err, contains) | ||
} else { | ||
for _, stub := range contains { | ||
if !strings.Contains(err.Error(), stub) { | ||
tb.Errorf("GOT: %v; WANT: %v", err, stub) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func ensurePanic(tb testing.TB, want string, callback func()) { | ||
tb.Helper() | ||
defer func() { | ||
r := recover() | ||
if r == nil { | ||
tb.Fatalf("GOT: %v; WANT: %v", r, want) | ||
return | ||
} | ||
if got := fmt.Sprintf("%v", r); got != want { | ||
tb.Fatalf("GOT: %v; WANT: %v", got, want) | ||
} | ||
}() | ||
callback() | ||
} | ||
|
||
// ensureNoPanic prettifies the output so one knows which test case caused a | ||
// panic. | ||
func ensureNoPanic(tb testing.TB, test string, callback func()) { | ||
tb.Helper() | ||
defer func() { | ||
if r := recover(); r != nil { | ||
tb.Fatalf("TEST: %s: GOT: %v", test, r) | ||
} | ||
}() | ||
callback() | ||
} | ||
|
||
func ensureStringSlicesMatch(tb testing.TB, actual, expected []string) { | ||
tb.Helper() | ||
if got, want := len(actual), len(expected); got != want { | ||
tb.Errorf("GOT: %v; WANT: %v", got, want) | ||
} | ||
la := len(actual) | ||
le := len(expected) | ||
for i := 0; i < la || i < le; i++ { | ||
if i < la { | ||
if i < le { | ||
if got, want := actual[i], expected[i]; got != want { | ||
tb.Errorf("GOT: %q; WANT: %q", got, want) | ||
} | ||
} else { | ||
tb.Errorf("GOT: %q (extra)", actual[i]) | ||
} | ||
} else if i < le { | ||
tb.Errorf("WANT: %q (missing)", expected[i]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters