Skip to content

Commit

Permalink
Add Debug(label string, info any) method to show debug info on fail
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Mar 8, 2024
1 parent 43a4c74 commit a5cd7cb
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 34 deletions.
1 change: 1 addition & 0 deletions actually.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type testingA struct {
showRawData bool
name string
failed bool
debugInfo []map[string]any
}

// Got sets the value you actually got. Got() creates *testingA and returns it.
Expand Down
14 changes: 14 additions & 0 deletions actually_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,17 @@ func invalidCall(a *testingA) {
panic(panicReason_NotCalledGot)
}
}

func (a *testingA) wi() *w.Witness {
wi := w.New()

if len(a.debugInfo) > 0 {
for _, di := range a.debugInfo {
for lable, info := range di {
wi.Debug(lable, info)
}
}
}

return wi
}
4 changes: 1 addition & 3 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package actually

import (
"testing"

w "github.com/bayashi/witness"
)

// NoError(t) method asserts that an error you got is NOt kind of error.
Expand All @@ -27,7 +25,7 @@ func (a *testingA) NoError(t *testing.T, testNames ...string) *testingA {

if a.got != nil {
var reason string
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
if !a.isTypeOfError() {
reason = reason_WrongType
wi.Message(notice_Label, "It should be type of error")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.21

require github.com/pmezard/go-difflib v1.0.0 // indirect

require github.com/bayashi/witness v0.0.17
require github.com/bayashi/witness v0.0.18

require github.com/davecgh/go-spew v1.1.1 // indirect
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/bayashi/witness v0.0.17 h1:f8bZL+MB+06DAF14YXgHqf7IF2oepcM2ILZ2H9khXOc=
github.com/bayashi/witness v0.0.17/go.mod h1:xxXU08y35qzkp0kDRGVYuvHB5JVxFKe6vF16puu7gEo=
github.com/bayashi/witness v0.0.18 h1:NntBW+sFL7GAgrav/4I++L9saaVK7UH5MV0v+y5bs48=
github.com/bayashi/witness v0.0.18/go.mod h1:xxXU08y35qzkp0kDRGVYuvHB5JVxFKe6vF16puu7gEo=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
6 changes: 6 additions & 0 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ func Dump(a any) string {
return witness.Dump(a)
}

// Debug is a helper function to show debug info on fail
func (a *testingA) Debug(label string, info any) *testingA {
a.debugInfo = append(a.debugInfo, map[string]any{label: info})
return a
}

// Fail is to show decorated fail report. (Actual shortcut to witness.Fail)
/*
if g != e {
Expand Down
14 changes: 14 additions & 0 deletions helpers_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actually

import (
"fmt"
"strings"
"testing"
)

Expand Down Expand Up @@ -56,3 +58,15 @@ func TestFi(t *testing.T) {
}
}, message_ExpectTrue)
}

func TestDebug(t *testing.T) {
a := Got(1).Debug("label", 123)
a.t = t

stub()
a.fail(a.wi(), "reason")

if !strings.Contains(fmt.Sprintf("%#v", stubWitness), `{"label":(`) {
t.Error("not include debug info")
}
}
4 changes: 1 addition & 3 deletions len.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package actually
import (
"reflect"
"testing"

w "github.com/bayashi/witness"
)

// Len asserts that the specified object has specific length.
Expand All @@ -15,7 +13,7 @@ func (a *testingA) Len(t *testing.T, testNames ...string) *testingA {
a.t = t
a.t.Helper()

wi := w.Got(a.got).Expect(a.expect)
wi := a.wi().Got(a.got).Expect(a.expect)

if k, ok := isValidExpect(a.expect); !ok {
return a.failf(wi, reason_ExpectvalueNotInt, k)
Expand Down
6 changes: 2 additions & 4 deletions match.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"regexp"
"testing"

w "github.com/bayashi/witness"
)

// Match method asserts that test data you got match expected value as regexp.
Expand All @@ -27,7 +25,7 @@ func (a *testingA) Match(t *testing.T, testNames ...string) *testingA {

target := fmt.Sprint(a.got)
if !r.MatchString(target) {
wi := w.Message(message_label_Regexp, r.String()).Message(message_label_Target, target)
wi := a.wi().Message(message_label_Regexp, r.String()).Message(message_label_Target, target)
return a.fail(wi, reason_NotMatch)
}

Expand All @@ -53,7 +51,7 @@ func (a *testingA) NotMatch(t *testing.T, testNames ...string) *testingA {

target := fmt.Sprint(a.got)
if r.MatchString(target) {
wi := w.Message(message_label_Regexp, r.String()).Message(message_label_Target, target)
wi := a.wi().Message(message_label_Regexp, r.String()).Message(message_label_Target, target)
return a.fail(wi, reason_UnexpectedlyMatch)
}

Expand Down
6 changes: 2 additions & 4 deletions nil.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package actually
import (
"reflect"
"testing"

w "github.com/bayashi/witness"
)

// Nil asserts that a test data you got is <nil>
Expand All @@ -18,7 +16,7 @@ func (a *testingA) Nil(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !a.isNil() {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectNilButNotNil)
}

Expand All @@ -36,7 +34,7 @@ func (a *testingA) NotNil(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if a.isNil() {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectIsNotNil)
}

Expand Down
18 changes: 8 additions & 10 deletions panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package actually

import (
"testing"

w "github.com/bayashi/witness"
)

// Panic asserts that a test function you got panics
Expand All @@ -17,12 +15,12 @@ func (a *testingA) Panic(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !isFuncType(a.got) {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}

if didPanic, _ := didPanic(a.got.(func())); !didPanic {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectPanic)
}

Expand All @@ -41,24 +39,24 @@ func (a *testingA) PanicMessage(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !isFuncType(a.got) {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}

didPanic, panicMessage := didPanic(a.got.(func()))

if !didPanic {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_ExpectPanic)
}

if !objectsAreSameType(a.expect, panicMessage) {
wi := w.Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
return a.fail(wi, reason_PanicButMsgwrongType)
}

if !objectsAreSame(a.expect, panicMessage) {
wi := w.Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got)).Expect(a.expect)
return a.fail(wi, reason_PanicButMsgDifferent)
}

Expand All @@ -76,12 +74,12 @@ func (a *testingA) NoPanic(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !isFuncType(a.got) {
wi := w.Got(a.got)
wi := a.wi().Got(a.got)
return a.fail(wi, reason_GotShouldFuncType)
}

if didPanic, panicMessage := didPanic(a.got.(func())); didPanic {
wi := w.Got(panicMessage).Message(gotFunc_Label, Dump(a.got))
wi := a.wi().Got(panicMessage).Message(gotFunc_Label, Dump(a.got))
return a.fail(wi, reason_ExpectNoPanic)
}

Expand Down
2 changes: 1 addition & 1 deletion same_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func reportForSame(a *testingA) *w.Witness {
r := w.Expect(a.expect).Got(a.got).Name(a.name)
r := a.wi().Expect(a.expect).Got(a.got).Name(a.name)
if a.showRawData {
r = r.ShowRaw()
}
Expand Down
10 changes: 4 additions & 6 deletions true.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package actually
import (
"reflect"
"testing"

w "github.com/bayashi/witness"
)

// True method asserts that a test data you got is true value of boolean type.
Expand All @@ -18,12 +16,12 @@ func (a *testingA) True(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !a.isBool() {
wi := w.Got(a.got).Message(notice_Label, "It should be boolean")
wi := a.wi().Got(a.got).Message(notice_Label, "It should be boolean")
return a.fail(wi, reason_WrongType)
}

if a.got != true {
return a.fail(w.Got(a.got), message_ExpectTrue)
return a.fail(a.wi().Got(a.got), message_ExpectTrue)
}

return a
Expand All @@ -40,12 +38,12 @@ func (a *testingA) False(t *testing.T, testNames ...string) *testingA {
a.t.Helper()

if !a.isBool() {
wi := w.Got(a.got).Message(notice_Label, "It should be boolean")
wi := a.wi().Got(a.got).Message(notice_Label, "It should be boolean")
return a.fail(wi, reason_WrongType)
}

if a.got != false {
return a.fail(w.Got(a.got), message_ExpectFalse)
return a.fail(a.wi().Got(a.got), message_ExpectFalse)
}

return a
Expand Down

0 comments on commit a5cd7cb

Please sign in to comment.