aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-09-10 20:00:39 +0700
committerShulhan <ms@kilabit.info>2023-09-11 02:16:52 +0700
commitf80218a8023ad2c171901bd422d2a8f6bd155a48 (patch)
tree4e8e283664f08f1aa2f35a66732996d5c45fe045
parent9801a607c1685f220350723848bcc4392d893850 (diff)
downloadpakakeh.go-f80218a8023ad2c171901bd422d2a8f6bd155a48.tar.xz
lib/test: refactoring, rename TestWriter to BufferWriter
The name TestWriter is considered stutter if its called from external package, test.TestWriter. While at it, implement the Error and Errorf in BufferWriter and add comment to each exported methods.
-rw-r--r--lib/test/example_test.go16
-rw-r--r--lib/test/test.go2
-rw-r--r--lib/test/test_test.go8
-rw-r--r--lib/test/test_writer.go41
-rw-r--r--lib/test/writer.go4
5 files changed, 49 insertions, 22 deletions
diff --git a/lib/test/example_test.go b/lib/test/example_test.go
index efd36eea..0835509a 100644
--- a/lib/test/example_test.go
+++ b/lib/test/example_test.go
@@ -60,7 +60,7 @@ func ExampleAssert_struct() {
}}
var (
- tw = test.TestWriter{}
+ tw = test.BufferWriter{}
)
for _, c := range cases {
@@ -77,7 +77,8 @@ func ExampleAssert_struct() {
func ExampleAssert_string() {
var (
- tw = test.TestWriter{}
+ tw = test.BufferWriter{}
+
exp string
got string
)
@@ -87,8 +88,8 @@ func ExampleAssert_string() {
test.Assert(&tw, ``, exp, got)
fmt.Println(tw.String())
- exp = `func (tw *TestWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) }`
- got = `func (tw *TestWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }`
+ exp = `func (tw *BufferWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) }`
+ got = `func (tw *BufferWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }`
tw.Reset()
test.Assert(&tw, ``, exp, got)
fmt.Println(tw.String())
@@ -96,13 +97,14 @@ func ExampleAssert_string() {
// !!! Assert: expecting string(a string), got string(b string)
// !!! :
// --++
- // 0 - func (tw *TestWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) }
- // 0 + func (tw *TestWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }
+ // 0 - func (tw *BufferWriter) Fatal(args ...any) { fmt.Fprint(tw, args...) }
+ // 0 + func (tw *BufferWriter) Fatalf(format string, args ...any) { fmt.Fprintf(tw, format, args...) }
}
func ExampleAssert_string2() {
var (
- tw = test.TestWriter{}
+ tw = test.BufferWriter{}
+
exp string
got string
)
diff --git a/lib/test/test.go b/lib/test/test.go
index 734c7e20..587242fe 100644
--- a/lib/test/test.go
+++ b/lib/test/test.go
@@ -38,7 +38,7 @@ func printStackTrace(w Writer, trace []byte) {
}
}
- _, ok = w.(*TestWriter)
+ _, ok = w.(*BufferWriter)
if !ok {
w.Log("\n!!! ERR " + string(trace[start:end]))
}
diff --git a/lib/test/test_test.go b/lib/test/test_test.go
index ff2b4a6f..3fb4bdd0 100644
--- a/lib/test/test_test.go
+++ b/lib/test/test_test.go
@@ -27,17 +27,17 @@ func TestAssert(t *testing.T) {
var (
c testCase
- tw TestWriter
+ bw BufferWriter
got string
)
for _, c = range cases {
- Assert(&tw, ``, c.a, c.b)
- got = tw.String()
+ Assert(&bw, ``, c.a, c.b)
+ got = bw.String()
if c.exp != got {
t.Fatalf(`want: %s, got: %s`, c.exp, got)
}
- tw.Reset()
+ bw.Reset()
}
}
diff --git a/lib/test/test_writer.go b/lib/test/test_writer.go
index a778e297..676582f8 100644
--- a/lib/test/test_writer.go
+++ b/lib/test/test_writer.go
@@ -9,14 +9,39 @@ import (
"fmt"
)
-// TestWriter write Errorx, Fatalx, and Logx to bytes.Buffer.
-type TestWriter struct {
+// BufferWriter implement the Writer interface.
+// Any call to ErrorXxx, FatalXxx, and LogXxx will write to embedded
+// bytes.Buffer.
+type BufferWriter 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...) }
+// Error write the arguments into buffer.
+func (bw *BufferWriter) Error(args ...any) {
+ fmt.Fprintln(bw, args...)
+}
+
+// Errorf write formatted string with arguments into buffer.
+func (bw *BufferWriter) Errorf(format string, args ...any) {
+ fmt.Fprintf(bw, format, args...)
+}
+
+// Fatal write the arguments to buffer.
+func (bw *BufferWriter) Fatal(args ...any) {
+ fmt.Fprint(bw, args...)
+}
+
+// Fatalf write formatted string with arguments into buffer.
+func (bw *BufferWriter) Fatalf(format string, args ...any) {
+ fmt.Fprintf(bw, format, args...)
+}
+
+// Log write the arguments into buffer.
+func (bw *BufferWriter) Log(args ...any) {
+ fmt.Fprint(bw, args...)
+}
+
+// Logf write formatted string with arguments into buffer.
+func (bw *BufferWriter) Logf(format string, args ...any) {
+ fmt.Fprintf(bw, format, args...)
+}
diff --git a/lib/test/writer.go b/lib/test/writer.go
index 4ab2a4e2..19dddb97 100644
--- a/lib/test/writer.go
+++ b/lib/test/writer.go
@@ -4,8 +4,8 @@
package test
-// Writer contains common methods between testing.T and testing.B, a subset of
-// testing.TB that cannot be used do private method.
+// Writer contains common methods between testing.T and testing.B, a subset
+// of testing.TB that cannot be used due to private methods.
type Writer interface {
Error(args ...any)
Errorf(format string, args ...any)