diff options
| author | Shulhan <ms@kilabit.info> | 2022-09-08 01:18:07 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-09-13 18:15:26 +0700 |
| commit | fce12484ff19e316d4a58a0f24dc312c5b99c810 (patch) | |
| tree | 7d474cbbaf93144f5c2e1379f8bfdcad33864fa4 /lib/test | |
| parent | c899459bd251cdefac8f97db32a1253891f5bf97 (diff) | |
| download | pakakeh.go-fce12484ff19e316d4a58a0f24dc312c5b99c810.tar.xz | |
lib/test: change the Assert parameter to Writer
Since we only need to call Log and Fatalf during Assert, no need to pass
the whole instance of testing.T to Assert.
By changing it to Writer, we also can test the Assert.
This remove the AssertBench, because it have the same function
parameters and body as Assert.
Diffstat (limited to 'lib/test')
| -rw-r--r-- | lib/test/test.go | 54 | ||||
| -rw-r--r-- | lib/test/test_test.go | 43 | ||||
| -rw-r--r-- | lib/test/test_writer.go | 22 | ||||
| -rw-r--r-- | lib/test/writer.go | 16 |
4 files changed, 95 insertions, 40 deletions
diff --git a/lib/test/test.go b/lib/test/test.go index a7331f6c..bf4413d6 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -7,18 +7,21 @@ package test import ( "runtime" - "testing" "github.com/shuLhan/share/lib/reflect" ) -func printStackTrace(t testing.TB, trace []byte) { +func printStackTrace(w Writer, trace []byte) { var ( - lines = 0 - start = 0 - end = 0 + lines int + start int + end int + x int + b byte + ok bool ) - for x, b := range trace { + + for x, b = range trace { if b == '\n' { lines++ if lines == 3 { @@ -31,7 +34,10 @@ func printStackTrace(t testing.TB, trace []byte) { } } - t.Log("\n!!! ERR " + string(trace[start:end])) + _, ok = w.(*testWriter) + if !ok { + w.Log("\n!!! ERR " + string(trace[start:end])) + } } // Assert compare two interfaces: `exp` and `got` for equality. @@ -43,32 +49,24 @@ func printStackTrace(t testing.TB, trace []byte) { // // WARNING: this method does not support recursive pointer, for example a node // that point to parent and parent that point back to node again. -func Assert(t *testing.T, name string, exp, got interface{}) { - err := reflect.DoEqual(exp, got) - if err == nil { - return - } - - trace := make([]byte, 1024) - runtime.Stack(trace, false) - printStackTrace(t, trace) - - t.Fatalf("!!! %s: %s", name, err) -} +func Assert(w Writer, name string, exp, got interface{}) { + var ( + err error + trace []byte + ) -// 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. -func AssertBench(b *testing.B, name string, exp, got interface{}) { - err := reflect.DoEqual(exp, got) + err = reflect.DoEqual(exp, got) if err == nil { return } - trace := make([]byte, 1024) + trace = make([]byte, 1024) runtime.Stack(trace, false) + printStackTrace(w, trace) - printStackTrace(b, trace) - - b.Fatalf("!!! %s: %s", name, err) + if len(name) == 0 { + w.Fatalf(`!!! %s`, err) + } else { + w.Fatalf(`!!! %s: %s`, name, err) + } } diff --git a/lib/test/test_test.go b/lib/test/test_test.go index ce0e62f9..9f1e1c14 100644 --- a/lib/test/test_test.go +++ b/lib/test/test_test.go @@ -9,21 +9,40 @@ import ( ) func TestAssert(t *testing.T) { - cases := []struct { - in interface{} - exp interface{} + type testCase struct { + a interface{} + b interface{} + exp string desc string - }{ - { - desc: "With nil", - in: nil, - exp: nil, - }, } - for _, c := range cases { - t.Log(c.desc) + var str = `a string` - Assert(t, "interface{}", c.exp, c.in) + var cases = []testCase{{ + desc: `nil interface{}`, + a: nil, + b: &str, + exp: `!!! DoEqual: IsValid: expecting <invalid Value>(false), got <*string Value>(true)`, + }, { + desc: `short string`, + a: `a string`, + b: `b string`, + exp: `!!! DoEqual: expecting string(a string), got string(b string)`, + }} + + var ( + c testCase + tw testWriter + got string + ) + + for _, c = range cases { + Assert(&tw, ``, c.a, c.b) + got = tw.String() + if c.exp != got { + t.Fatalf(`want: %s, got: %s`, c.exp, got) + } + + tw.Reset() } } diff --git a/lib/test/test_writer.go b/lib/test/test_writer.go new file mode 100644 index 00000000..045df891 --- /dev/null +++ b/lib/test/test_writer.go @@ -0,0 +1,22 @@ +// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package test + +import ( + "bytes" + "fmt" +) + +// testWriter implement some of testing.TB. +type testWriter struct { + bytes.Buffer +} + +func (tw *testWriter) Error(args ...any) {} +func (tw *testWriter) Errorf(format string, args ...any) {} +func (tw *testWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) } +func (tw *testWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) } +func (tw *testWriter) Log(args ...any) { fmt.Fprint(tw, args...) } +func (tw *testWriter) Logf(format string, args ...any) { fmt.Fprintf(tw, format, args...) } diff --git a/lib/test/writer.go b/lib/test/writer.go new file mode 100644 index 00000000..4ab2a4e2 --- /dev/null +++ b/lib/test/writer.go @@ -0,0 +1,16 @@ +// Copyright 2022, Shulhan <ms@kilabit.info>. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package test + +// Writer contains common methods between testing.T and testing.B, a subset of +// testing.TB that cannot be used do private method. +type Writer interface { + Error(args ...any) + Errorf(format string, args ...any) + Fatal(args ...any) + Fatalf(format string, args ...any) + Log(args ...any) + Logf(format string, args ...any) +} |
