-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add p/demo/uassert & p/demo/urequire (#928)
Addresses #923 Addresses #777 --------- Signed-off-by: Manfred Touron <[email protected]> Signed-off-by: moul <[email protected]>
- Loading branch information
Showing
14 changed files
with
714 additions
and
53 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 |
---|---|---|
@@ -1 +1,6 @@ | ||
module gno.land/p/demo/ownable | ||
|
||
require ( | ||
gno.land/p/demo/testutils v0.0.0-latest | ||
gno.land/p/demo/uassert v0.0.0-latest | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package uassert // import "gno.land/p/demo/uassert" |
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 @@ | ||
module gno.land/p/demo/uassert |
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,51 @@ | ||
package uassert | ||
|
||
import "strings" | ||
|
||
func fail(t TestingT, customMsgs []string, failureMessage string, args ...interface{}) bool { | ||
customMsg := "" | ||
if len(customMsgs) > 0 { | ||
customMsg = strings.Join(customMsgs, " ") | ||
} | ||
if customMsg != "" { | ||
failureMessage += " - " + customMsg | ||
} | ||
t.Errorf(failureMessage, args...) | ||
return false | ||
} | ||
|
||
func autofail(t TestingT, success bool, customMsgs []string, failureMessage string, args ...interface{}) bool { | ||
if success { | ||
return true | ||
} | ||
return fail(t, customMsgs, failureMessage, args...) | ||
} | ||
|
||
func checkDidPanic(f func()) (didPanic bool, message string) { | ||
didPanic = true | ||
defer func() { | ||
r := recover() | ||
|
||
if r == nil { | ||
message = "nil" | ||
return | ||
} | ||
|
||
err, ok := r.(error) | ||
if ok { | ||
message = err.Error() | ||
return | ||
} | ||
|
||
errStr, ok := r.(string) | ||
if ok { | ||
message = errStr | ||
return | ||
} | ||
|
||
message = "recover: unsupported type" | ||
}() | ||
f() | ||
didPanic = false | ||
return | ||
} |
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,59 @@ | ||
package uassert | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type mockTestingT struct { | ||
fmt string | ||
args []interface{} | ||
} | ||
|
||
// --- interface mock | ||
|
||
var _ TestingT = (*mockTestingT)(nil) | ||
|
||
func (mockT *mockTestingT) Helper() { /* noop */ } | ||
func (mockT *mockTestingT) Skip(args ...interface{}) { /* not implmented */ } | ||
func (mockT *mockTestingT) Fail() { /* not implmented */ } | ||
func (mockT *mockTestingT) FailNow() { /* not implmented */ } | ||
func (mockT *mockTestingT) Logf(fmt string, args ...interface{}) { /* noop */ } | ||
|
||
func (mockT *mockTestingT) Fatalf(fmt string, args ...interface{}) { | ||
mockT.fmt = "fatal: " + fmt | ||
mockT.args = args | ||
} | ||
|
||
func (mockT *mockTestingT) Errorf(fmt string, args ...interface{}) { | ||
mockT.fmt = "error: " + fmt | ||
mockT.args = args | ||
} | ||
|
||
// --- helpers | ||
|
||
func (mockT *mockTestingT) actualString() string { | ||
res := fmt.Sprintf(mockT.fmt, mockT.args...) | ||
mockT.reset() | ||
return res | ||
} | ||
|
||
func (mockT *mockTestingT) reset() { | ||
mockT.fmt = "" | ||
mockT.args = nil | ||
} | ||
|
||
func (mockT *mockTestingT) equals(t *testing.T, expected string) { | ||
actual := mockT.actualString() | ||
|
||
if expected != actual { | ||
t.Errorf("mockT differs:\n- expected: %s\n- actual: %s\n", expected, actual) | ||
} | ||
} | ||
|
||
func (mockT *mockTestingT) empty(t *testing.T) { | ||
if mockT.fmt != "" || mockT.args != nil { | ||
actual := mockT.actualString() | ||
t.Errorf("mockT should be empty, got %s", actual) | ||
} | ||
} |
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,11 @@ | ||
package uassert | ||
|
||
type TestingT interface { | ||
Helper() | ||
Skip(args ...interface{}) | ||
Fatalf(fmt string, args ...interface{}) | ||
Errorf(fmt string, args ...interface{}) | ||
Logf(fmt string, args ...interface{}) | ||
Fail() | ||
FailNow() | ||
} |
Oops, something went wrong.