From b5782f817bf95894c9e2f47b52165fca68b8f167 Mon Sep 17 00:00:00 2001 From: bayashi <42190+bayashi@users.noreply.github.com> Date: Fri, 8 Mar 2024 17:01:24 +0000 Subject: [PATCH] Add `Debug` method to show debug info on fail --- report/report.go | 13 +++++++++++++ witness.go | 27 ++++++++++++++++++++------- witness_test.go | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) diff --git a/report/report.go b/report/report.go index 11678ac..b5d8b8e 100644 --- a/report/report.go +++ b/report/report.go @@ -22,6 +22,7 @@ type Failure struct { name string `label:"Test name"` trace string `label:"Trace"` messages []map[string]string + debugInfo []map[string]*obj.Object `label:"Debug"` } func NewFailure() *Failure { @@ -73,6 +74,11 @@ func (f *Failure) Messages(msgs []map[string]string) *Failure { return f } +func (f *Failure) DebugInfo(info []map[string]*obj.Object) *Failure { + f.debugInfo = info + return f +} + func (f *Failure) Put() string { r := &Report{ Contents: f.buildContents(), @@ -152,6 +158,13 @@ func (f *Failure) buildContents() []*Content { } } + for _, i := range f.debugInfo { + for label, obj := range i { + label = strings.Join([]string{f.fieldLabel("debugInfo"), label}, " ") + contents = append(contents, &Content{Label: label, Body: obj.AsString()}) + } + } + return contents } diff --git a/witness.go b/witness.go index 99f3023..7539cae 100644 --- a/witness.go +++ b/witness.go @@ -13,12 +13,13 @@ import ( // Witness is a context of the fail report type Witness struct { - got *obj.Object - expect *obj.Object - name string - messages []map[string]string // additional info as {"label": "message"} - showDiff bool // If true, show a diff string for "got" and "expect" - showRaw bool // If true, show raw values as string(raw string or dumped string) for "got" and "expect" + got *obj.Object + expect *obj.Object + name string + messages []map[string]string // additional info as {"label": "message"} + debugInfo []map[string]*obj.Object // Debug info as {"label": *obj.Object} + showDiff bool // If true, show a diff string for "got" and "expect" + showRaw bool // If true, show raw values as string(raw string or dumped string) for "got" and "expect" } // You can write "witness.New(witness.ShowDiff, witness.NotShowRaw)" instead of raw boolean @@ -144,6 +145,18 @@ func (w *Witness) Message(label string, msg string) *Witness { return w } +// Set debug information to show on fail +func Debug(label string, info any) *Witness { + return New().Debug(label, info) +} + +// Set debug information to show on fail +func (w *Witness) Debug(label string, info any) *Witness { + w.debugInfo = append(w.debugInfo, map[string]*obj.Object{label: obj.NewObject(info)}) + + return w +} + func baseReprot(reason string) *report.Failure { return report.NewFailure(). Trace(strings.Join(trace.Info(), "\n\t")). @@ -174,7 +187,7 @@ func (w *Witness) FailNow(t *testing.T, reason string) { } func (w *Witness) buildReport(t *testing.T, reason string) *report.Failure { - r := baseReprot(reason).Messages(w.messages) + r := baseReprot(reason).Messages(w.messages).DebugInfo(w.debugInfo) if w.name != "" { r.Name(strings.Join([]string{t.Name(), w.name}, "/")) diff --git a/witness_test.go b/witness_test.go index 0e011ac..69f7e64 100644 --- a/witness_test.go +++ b/witness_test.go @@ -230,3 +230,19 @@ func TestFailWithAdditionalMessage(t *testing.T) { t.Error(msg) } } + +func TestFailWithDebugInfo(t *testing.T) { + stub() + + Got(1).Expect(2).Debug("label", "debug info").Fail(t, "Not same") + + // Fail reason: Not same + // Type: Expect:int, Got:int + // Expected: 2 + // Actually got: 1 + // Debug label: "debug info" + + if ok, msg := tu.Match(`Debug label:\s*\t"debug info"\n`, res); !ok { + t.Error(msg) + } +}