Add outputWriter tests

This commit is contained in:
suntala 2025-03-05 22:59:58 +01:00
parent 521b634e0f
commit cecfa650af
1 changed files with 58 additions and 0 deletions

View File

@ -993,3 +993,61 @@ func TestNestedCleanup(t *T) {
t.Errorf("unexpected cleanup count: got %d want 3", ranCleanup)
}
}
func TestOutputWriter(t *T) {
tstate := newTestState(1, allMatcher())
buf := &strings.Builder{}
root := &T{
common: common{
signal: make(chan bool),
barrier: make(chan bool),
name: "",
w: buf,
},
tstate: tstate,
}
f := func(t *T) {
t.Run("", func(t *T) {
w := outputWriter{&t.common, nil}
w.Write([]byte("a\n"))
w.Write([]byte("b\n"))
w.Write([]byte("a\nb\n"))
t.Fail()
})
}
root.Run("check output of outputWriter", f)
tstate.release()
if tstate.running != 0 || tstate.numWaiting != 0 {
t.Errorf("running and waiting non-zero: got %d and %d", tstate.running, tstate.numWaiting)
}
got := strings.TrimSpace(buf.String())
output := `
--- FAIL: check output of outputWriter (0.00s)
--- FAIL: check output of outputWriter/#00 (0.00s)
a
b
a
b
`
want := strings.TrimSpace(output)
re := makeRegexp(want)
if ok, err := regexp.MatchString(re, got); !ok || err != nil {
t.Errorf("output:\ngot:\n%s\nwant:\n%s", got, want)
}
}
func TestOutputWriterBuffering(t *T) {
w := outputWriter{&t.common, nil}
w.Write([]byte("Hel"))
w.Write([]byte("lo\nWorld\nInput to log\n\n\nMore logging\nShouldn't be logged"))
w.Write([]byte("Also shouldn't be logged"))
bufContents := string(w.b)
expected := "Shouldn't be loggedAlso shouldn't be logged"
if bufContents != expected {
t.Errorf("unexpected buffer contents: got %q want %q", bufContents, expected)
}
}