diff options
author | Matthieu <matthieu.cneude@gmail.com> | 2020-06-01 19:53:47 +0200 |
---|---|---|
committer | Matthieu <matthieu.cneude@gmail.com> | 2020-06-01 19:53:47 +0200 |
commit | 3d13ff7c33caabd597062b963b8733d07c140b39 (patch) | |
tree | 3953e17fd2bd5cfc273e70b827cb87800d7ee34b | |
parent | 533014054ecdc560f8a5a53044bfe9bd09891342 (diff) |
Add test helpers in gokit
-rw-r--r-- | gokit/test/test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/gokit/test/test.go b/gokit/test/test.go new file mode 100644 index 0000000..4e73ed3 --- /dev/null +++ b/gokit/test/test.go @@ -0,0 +1,36 @@ +package test + +import ( + "fmt" + "path/filepath" + "reflect" + "runtime" + "testing" +) + +// assert fails the test if the condition is false. +func assert(tb testing.TB, condition bool, msg string, v ...interface{}) { + if !condition { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...) + tb.FailNow() + } +} + +// ok fails the test if an err is not nil. +func ok(tb testing.TB, err error) { + if err != nil { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error()) + tb.FailNow() + } +} + +// equals fails the test if exp is not equal to act. +func equals(tb testing.TB, exp, act interface{}) { + if !reflect.DeepEqual(exp, act) { + _, file, line, _ := runtime.Caller(1) + fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act) + tb.FailNow() + } +} |