From 96134bfe821ae9d8772e7f38a51c1afda91e8272 Mon Sep 17 00:00:00 2001 From: gmarik Date: Mon, 22 Feb 2016 00:27:08 -0500 Subject: [PATCH] Introduce `gobottest` package with test helpers - this package is for testing purposes only --- gobottest/gobottest.go | 35 +++++++++++++++++++++++++++++++++++ gobottest/gobottest_test.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 gobottest/gobottest.go create mode 100644 gobottest/gobottest_test.go diff --git a/gobottest/gobottest.go b/gobottest/gobottest.go new file mode 100644 index 000000000..83c8930cb --- /dev/null +++ b/gobottest/gobottest.go @@ -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))) + } +} diff --git a/gobottest/gobottest_test.go b/gobottest/gobottest_test.go new file mode 100644 index 000000000..d1095515a --- /dev/null +++ b/gobottest/gobottest_test.go @@ -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") + } +}