aboutsummaryrefslogtreecommitdiff
path: root/lib/test
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2021-03-14 22:37:59 +0700
committerShulhan <ms@kilabit.info>2021-03-14 22:40:05 +0700
commite7552ad0189f761875bc1c2ca3dd716d43a01e0d (patch)
treefd68ec29d23fb9a807a7382088020e3b17d446fd /lib/test
parent882727d89c8e7f9b9761009ccdca1af8c18d0a30 (diff)
downloadpakakeh.go-e7552ad0189f761875bc1c2ca3dd716d43a01e0d.tar.xz
all: refactoring the test.Assert and test.AssertBench signature
Previously, the test.Assert and test.AssertBench functions has the boolean parameter to print the stack trace of test in case its not equal. Since this parameter is not mandatory and its usually always set to "true", we remove them from function signature to simplify the call to Assert and AssertBench.
Diffstat (limited to 'lib/test')
-rw-r--r--lib/test/test.go47
-rw-r--r--lib/test/test_test.go16
2 files changed, 20 insertions, 43 deletions
diff --git a/lib/test/test.go b/lib/test/test.go
index a8343bd2..b28b676b 100644
--- a/lib/test/test.go
+++ b/lib/test/test.go
@@ -37,56 +37,35 @@ func printStackTrace(t testing.TB, trace []byte) {
}
//
-// Assert will compare two interfaces: `exp` and `got` for equality.
-// If both are not equal, the test will throw panic parameter describe the
-// position (type and value) where both are not matched.
+// Assert compare two interfaces: `exp` and `got` for equality.
+// If both parameters are not equal, the function will call Fatalf that
+// describe the position (type and value) where value are not matched.
//
// If `exp` implement the extended `reflect.Equaler`, then it will use the
// method `IsEqual()` with `got` as parameter.
//
-// If debug parameter is true it will print the stack trace of testing.T
-// instance.
+// WARNING: this method does not support recursive pointer, for example a node
+// that point to parent and parent that point back to node again.
//
-// WARNING: this method does not support recursive pointer, for example node
-// that point to parent and parent that point back to node.
-//
-func Assert(t *testing.T, name string, exp, got interface{}, debug bool) {
+func Assert(t *testing.T, name string, exp, got interface{}) {
err := reflect.DoEqual(exp, got)
if err == nil {
return
}
- if debug {
- trace := make([]byte, 1024)
- runtime.Stack(trace, false)
- printStackTrace(t, trace)
- }
+ trace := make([]byte, 1024)
+ runtime.Stack(trace, false)
+ printStackTrace(t, trace)
t.Fatalf("!!! %s: %s", name, err)
}
//
-// AssertBench will compare two interfaces: `exp` and `got` whether its same
-// with `equal` value.
+// AssertBench will compare two interfaces: `exp` and `got` for equality.
+// If both parameters are not equal, the function will call Fatalf that
+// describe the position (type and value) where value are not matched.
//
-// If comparison result is not same with `equal`, it will print the result and
-// expectation and then terminate the test routine.
-//
-func AssertBench(b *testing.B, name string, exp, got interface{}, equal bool) {
- if reflect.IsEqual(exp, got) == equal {
- return
- }
-
- trace := make([]byte, 1024)
- runtime.Stack(trace, false)
-
- printStackTrace(b, trace)
-
- b.Fatalf(">>> Got %s:\n\t'%+v';\n"+
- " want:\n\t'%+v'\n", name, got, exp)
-}
-
-func AssertBench2(b *testing.B, name string, exp, got interface{}) {
+func AssertBench(b *testing.B, name string, exp, got interface{}) {
err := reflect.DoEqual(exp, got)
if err == nil {
return
diff --git a/lib/test/test_test.go b/lib/test/test_test.go
index c4f2f2a9..992fe888 100644
--- a/lib/test/test_test.go
+++ b/lib/test/test_test.go
@@ -10,22 +10,20 @@ import (
func TestAssert(t *testing.T) {
cases := []struct {
- desc string
- in interface{}
- exp interface{}
- expEqual bool
+ desc string
+ in interface{}
+ exp interface{}
}{
{
- desc: "With nil",
- in: nil,
- exp: nil,
- expEqual: true,
+ desc: "With nil",
+ in: nil,
+ exp: nil,
},
}
for _, c := range cases {
t.Log(c.desc)
- Assert(t, "interface{}", c.exp, c.in, c.expEqual)
+ Assert(t, "interface{}", c.exp, c.in)
}
}