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

bugfix: ensure no panics when calling t.Name() inside a generator #58

Closed
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
8 changes: 8 additions & 0 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,14 @@ func (t *T) SkipNow() {
t.skip("(*T).SkipNow() called")
}

func (t *T) Name() string {
if t.tb != nil {
return t.tb.Name()
}

return ""
}

// Errorf is equivalent to [T.Logf] followed by [T.Fail].
func (t *T) Errorf(format string, args ...any) {
if t.tbLog && t.tb != nil {
Expand Down
14 changes: 14 additions & 0 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package rapid

import (
"fmt"
"strings"
"testing"
)
Expand Down Expand Up @@ -93,3 +94,16 @@ func BenchmarkCheckOverhead(b *testing.B) {
checkTB(b, deadline, f)
}
}

func TestNameInGeneratorStillWorksIfUsingExample(t *testing.T) {
g := Custom(func(t *T) string {
str := Just("rapid test").Draw(t, "")
return fmt.Sprintf("%s-%s", str, t.Name())
})
t.Run("does not panic when calling t.Name() inside generator called via .Example()", func(t *testing.T) {
exmpl := g.Example(0)
if exmpl != "rapid test-" {
t.Fatalf("expected example %s to contain the rapid test identifier and not panic", exmpl)
}
})
}