Skip to content

Commit 045e97e

Browse files
committed
add t.Output
1 parent 011e717 commit 045e97e

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

output_disabled.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//go:build !go1.25
2+
3+
package rapid
4+
5+
// Output is only available on Go 1.25+.
6+
func (t *T) Output() io.Writer {
7+
t.Helper()
8+
t.Fatalf("[rapid] Output requires Go 1.25 or newer")
9+
}

output_enabled.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build go1.25
2+
3+
package rapid
4+
5+
import "io"
6+
7+
// Output returns a Writer that writes to the same test output stream as T.Log.
8+
// The output is indented like T.Log lines, but Output does not
9+
// add source locations or newlines. The output is internally line
10+
// buffered, and a call to T.Log or the end of the test will implicitly
11+
// flush the buffer, followed by a newline. After a test function and all its
12+
// parents return, neither Output nor the Write method may be called.
13+
//
14+
// Only available on Go >= 1.25
15+
func (t *T) Output() io.Writer {
16+
if t.rawLog != nil {
17+
return t.rawLog.Writer()
18+
} else if t.tbLog {
19+
tout := t.tb.(interface{ Output() io.Writer })
20+
return tout.Output()
21+
} else {
22+
return io.Discard
23+
}
24+
}

output_enabled_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build go1.25
2+
3+
package rapid
4+
5+
import (
6+
"bytes"
7+
"testing"
8+
)
9+
10+
func TestOutputRawLog(t *testing.T) {
11+
t.Parallel()
12+
13+
msg := []byte("Hello World")
14+
15+
out := captureTestOutput(t, func(t *T) {
16+
t.Output().Write(msg)
17+
}, nil)
18+
19+
if !bytes.Contains(out, msg) {
20+
t.Errorf("expected output to contain %q, got: %q", msg, out)
21+
}
22+
}

0 commit comments

Comments
 (0)