Skip to content

Commit

Permalink
test logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Apr 19, 2023
1 parent 7fd8073 commit 2152af8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
20 changes: 16 additions & 4 deletions Spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,13 @@ func (spec *Spec) lookupRetryEventually() (assert.Eventually, bool) {
return assert.Eventually{}, false
}

func (spec *Spec) printDescription(t *T) {
func (spec *Spec) printDescription(tb testing.TB) {
spec.testingTB.Helper()
tb.Helper()
var lines []interface{}

var spaceIndentLevel int
for _, c := range t.contexts() {
for _, c := range spec.specsFromParent() {
if c.description == `` {
continue
}
Expand All @@ -303,7 +304,7 @@ func (spec *Spec) printDescription(t *T) {
spaceIndentLevel++
}

internal.Log(t, lines...)
internal.Log(tb, lines...)
}

// TODO: add group name representation here
Expand Down Expand Up @@ -389,7 +390,18 @@ func (spec *Spec) runTB(tb testing.TB, blk func(*T)) {
tb.Parallel()
}

spec.printDescription(newT(tb, spec))
tb.Cleanup(func() {
var shouldPrint bool
if tb.Failed() {
shouldPrint = true
}
if testing.Verbose() {
shouldPrint = true
}
if shouldPrint {
spec.printDescription(newT(tb, spec))
}
})

test := func(tb testing.TB) {
tb.Helper()
Expand Down
20 changes: 18 additions & 2 deletions assert/AnyOf.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
// OneOf function checks a list of values and matches an expectation against each element of the list.
// If any of the elements pass the assertion, then the assertion helper function does not fail the test.
func OneOf[V any](tb testing.TB, vs []V, blk func(it It, got V), msg ...any) {
tb.Helper()
Must(tb).AnyOf(func(a *AnyOf) {
a.name = "OneOf"
a.cause = "None of the element matched the expectations"
for _, v := range vs {
a.Test(func(it It) { blk(it, v) })
if a.OK() {
Expand All @@ -36,6 +39,9 @@ type AnyOf struct {

mutex sync.Mutex
passed bool

name string
cause string
}

// Test will test a block of assertion that must succeed in order to make AnyOf pass.
Expand Down Expand Up @@ -71,8 +77,18 @@ func (ao *AnyOf) Finish(msg ...interface{}) {
return
}
ao.TB.Log(fmterror.Message{
Method: "AnyOf",
Cause: "None of the .Test succeeded",
Method: func() string {
if ao.name != "" {
return ao.name
}
return "AnyOf"
}(),
Cause: func() string {
if ao.cause != "" {
return ao.cause
}
return "None of the .Test succeeded"
}(),
Message: msg,
Values: nil,
})
Expand Down
7 changes: 7 additions & 0 deletions assert/AnyOf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func TestOneOf(t *testing.T) {

t.Must.Contain(stub.Get(t).Logs.String(), msg)
})

s.Then("assertion failure message includes the assertion helper name", func(t *testcase.T) {
act(t)

t.Must.Contain(stub.Get(t).Logs.String(), "OneOf")
t.Must.Contain(stub.Get(t).Logs.String(), "None of the element matched the expectations")
})
})

s.When("assertion pass only for one of the slice element", func(s *testcase.Spec) {
Expand Down

0 comments on commit 2152af8

Please sign in to comment.