diff options
| author | Shulhan <ms@kilabit.info> | 2025-01-21 21:39:42 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2025-01-22 20:04:15 +0700 |
| commit | b9cff7d8f66d679218d1eaf4b015febbc48abc9e (patch) | |
| tree | f2ba14734cfe90e56444c6066434b8d173e0902d | |
| parent | 73a9b318ea8afe535050c1a007e427b6cd8be453 (diff) | |
| download | pakakeh.go-b9cff7d8f66d679218d1eaf4b015febbc48abc9e.tar.xz | |
lib/test: call Helper method inside Assert
The Helper method mark the Assert function as test helper, which when
printing file and line information, the stack trace from Assert function
will be skipped.
This remove manual lines skipping that previously we have.
| -rw-r--r-- | lib/test/buffer_writer.go | 4 | ||||
| -rw-r--r-- | lib/test/test.go | 42 | ||||
| -rw-r--r-- | lib/test/writer.go | 1 |
3 files changed, 8 insertions, 39 deletions
diff --git a/lib/test/buffer_writer.go b/lib/test/buffer_writer.go index 676582f8..c9e3790b 100644 --- a/lib/test/buffer_writer.go +++ b/lib/test/buffer_writer.go @@ -36,6 +36,10 @@ func (bw *BufferWriter) Fatalf(format string, args ...any) { fmt.Fprintf(bw, format, args...) } +func (bw *BufferWriter) Helper() { + // NOOP +} + // Log write the arguments into buffer. func (bw *BufferWriter) Log(args ...any) { fmt.Fprint(bw, args...) diff --git a/lib/test/test.go b/lib/test/test.go index 07f9bc20..5a85f58a 100644 --- a/lib/test/test.go +++ b/lib/test/test.go @@ -8,42 +8,12 @@ package test import ( "bytes" "fmt" - "runtime" "git.sr.ht/~shulhan/pakakeh.go/lib/reflect" "git.sr.ht/~shulhan/pakakeh.go/lib/text" "git.sr.ht/~shulhan/pakakeh.go/lib/text/diff" ) -func printStackTrace(w Writer, trace []byte) { - var ( - lines int - start int - end int - x int - b byte - ok bool - ) - - for x, b = range trace { - if b == '\n' { - lines++ - if lines == 3 { - start = x + 1 - } - if lines == 5 { - end = x + 1 - break - } - } - } - - _, ok = w.(*BufferWriter) - if !ok { - w.Log("\n!!! ERR " + string(trace[start:end])) - } -} - // Assert compare two interfaces: exp and got for equality. // If both parameters are not equal, the function will call print and try to // describe the position (type and value) where value are not matched and call @@ -87,12 +57,10 @@ func printStackTrace(w Writer, trace []byte) { // LIMITATION: 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(w Writer, name string, exp, got interface{}) { - var ( - logp = `Assert` + w.Helper() - err error - trace []byte - ) + var logp = `Assert` + var err error err = reflect.DoEqual(exp, got) if err == nil { @@ -103,10 +71,6 @@ func Assert(w Writer, name string, exp, got interface{}) { return } - trace = make([]byte, 1024) - runtime.Stack(trace, false) - printStackTrace(w, trace) - if len(name) == 0 { w.Fatalf(`!!! %s: %s`, logp, err) } else { diff --git a/lib/test/writer.go b/lib/test/writer.go index 19dddb97..3d6471d5 100644 --- a/lib/test/writer.go +++ b/lib/test/writer.go @@ -11,6 +11,7 @@ type Writer interface { Errorf(format string, args ...any) Fatal(args ...any) Fatalf(format string, args ...any) + Helper() Log(args ...any) Logf(format string, args ...any) } |
