forked from hybridgroup/gobot
-
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.
Introduce
gobottest
package with test helpers
- this package is for testing purposes only
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 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,35 @@ | ||
package gobottest | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var errFunc = func(t *testing.T, message string) { | ||
t.Errorf(message) | ||
} | ||
|
||
func logFailure(t *testing.T, message string) { | ||
_, file, line, _ := runtime.Caller(2) | ||
s := strings.Split(file, "/") | ||
errFunc(t, fmt.Sprintf("%v:%v: %v", s[len(s)-1], line, message)) | ||
} | ||
|
||
// Assert checks if a and b are equal, emis a t.Errorf if they are not equal. | ||
func Assert(t *testing.T, a interface{}, b interface{}) { | ||
if !reflect.DeepEqual(a, b) { | ||
logFailure(t, fmt.Sprintf("%v - \"%v\", should equal, %v - \"%v\"", | ||
a, reflect.TypeOf(a), b, reflect.TypeOf(b))) | ||
} | ||
} | ||
|
||
// Refute checks if a and b are equal, emis a t.Errorf if they are equal. | ||
func Refute(t *testing.T, a interface{}, b interface{}) { | ||
if reflect.DeepEqual(a, b) { | ||
logFailure(t, fmt.Sprintf("%v - \"%v\", should not equal, %v - \"%v\"", | ||
a, reflect.TypeOf(a), b, reflect.TypeOf(b))) | ||
} | ||
} |
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,37 @@ | ||
package gobottest | ||
|
||
import "testing" | ||
|
||
func TestAssert(t *testing.T) { | ||
err := "" | ||
errFunc = func(t *testing.T, message string) { | ||
err = message | ||
} | ||
|
||
Assert(t, 1, 1) | ||
if err != "" { | ||
t.Errorf("Assert failed: 1 should equal 1") | ||
} | ||
|
||
Assert(t, 1, 2) | ||
if err == "" { | ||
t.Errorf("Assert failed: 1 should not equal 2") | ||
} | ||
} | ||
|
||
func TestRefute(t *testing.T) { | ||
err := "" | ||
errFunc = func(t *testing.T, message string) { | ||
err = message | ||
} | ||
|
||
Refute(t, 1, 2) | ||
if err != "" { | ||
t.Errorf("Refute failed: 1 should not be 2") | ||
} | ||
|
||
Refute(t, 1, 1) | ||
if err == "" { | ||
t.Errorf("Refute failed: 1 should not be 1") | ||
} | ||
} |