Skip to content

Commit

Permalink
Introduce gobottest package with test helpers
Browse files Browse the repository at this point in the history
- this package is for testing purposes only
  • Loading branch information
gmarik committed Feb 22, 2016
1 parent 93b452f commit 96134bf
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
35 changes: 35 additions & 0 deletions gobottest/gobottest.go
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)))
}
}
37 changes: 37 additions & 0 deletions gobottest/gobottest_test.go
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")
}
}

0 comments on commit 96134bf

Please sign in to comment.