Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug method accepts multiple info to dump #39

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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"`
debugInfo []map[string][]*obj.Object `label:"Debug"`
}

func NewFailure() *Failure {
Expand Down Expand Up @@ -74,7 +74,7 @@ func (f *Failure) Messages(msgs []map[string]string) *Failure {
return f
}

func (f *Failure) DebugInfo(info []map[string]*obj.Object) *Failure {
func (f *Failure) DebugInfo(info []map[string][]*obj.Object) *Failure {
f.debugInfo = info
return f
}
Expand Down Expand Up @@ -159,9 +159,13 @@ func (f *Failure) buildContents() []*Content {
}

for _, i := range f.debugInfo {
for label, obj := range i {
for label, objs := range i {
label = strings.Join([]string{f.fieldLabel("debugInfo"), label}, " ")
contents = append(contents, &Content{Label: label, Body: obj.AsString()})
body := []string{}
for _, o := range objs {
body = append(body, o.AsString())
}
contents = append(contents, &Content{Label: label, Body: strings.Join(body, "\n--\n")})
}
}

Expand Down
20 changes: 12 additions & 8 deletions witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ type Witness struct {
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"
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 @@ -146,13 +146,17 @@ func (w *Witness) Message(label string, msg string) *Witness {
}

// Set debug information to show on fail
func Debug(label string, info any) *Witness {
return New().Debug(label, info)
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)})
func (w *Witness) Debug(label string, info ...any) *Witness {
infoList := make([]*obj.Object, 0, len(info))
for _, i := range info {
infoList = append(infoList, obj.NewObject(i))
}
w.debugInfo = append(w.debugInfo, map[string][]*obj.Object{label: infoList})

return w
}
Expand Down
18 changes: 18 additions & 0 deletions witness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,21 @@ func TestFailWithDebugInfo(t *testing.T) {
t.Error(msg)
}
}

func TestFailWithMultipleDebugInfo(t *testing.T) {
stub()

Got(1).Expect(2).Debug("label", "debug info", "one more debug info").Fail(t, "Not same")

// Fail reason: Not same
// Type: Expect:int, Got:int
// Expected: 2
// Actually got: 1
// Debug label: "debug info"
// --
// "one more debug info"

if ok, msg := tu.Match(`Debug label:[\t\s]+"debug info"\n[\t\s]+--\n[\t\s]+"one more debug info"`, res); !ok {
t.Error(msg)
}
}
Loading