Skip to content

Commit

Permalink
Merge pull request #36 from bayashi/add-debug-method
Browse files Browse the repository at this point in the history
Add `Debug` method to show debug info on fail
  • Loading branch information
bayashi committed Mar 8, 2024
2 parents 3a08118 + b5782f8 commit a196065
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
13 changes: 13 additions & 0 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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
}

Expand Down
27 changes: 20 additions & 7 deletions witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")).
Expand Down Expand Up @@ -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}, "/"))
Expand Down
16 changes: 16 additions & 0 deletions witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit a196065

Please sign in to comment.