@@ -993,3 +993,61 @@ func TestNestedCleanup(t *T) {
993
993
t .Errorf ("unexpected cleanup count: got %d want 3" , ranCleanup )
994
994
}
995
995
}
996
+
997
+ func TestOutputWriter (t * T ) {
998
+ tstate := newTestState (1 , allMatcher ())
999
+ buf := & strings.Builder {}
1000
+ root := & T {
1001
+ common : common {
1002
+ signal : make (chan bool ),
1003
+ barrier : make (chan bool ),
1004
+ name : "" ,
1005
+ w : buf ,
1006
+ },
1007
+ tstate : tstate ,
1008
+ }
1009
+
1010
+ f := func (t * T ) {
1011
+ t .Run ("" , func (t * T ) {
1012
+ w := outputWriter {& t .common , nil }
1013
+ w .Write ([]byte ("a\n " ))
1014
+ w .Write ([]byte ("b\n " ))
1015
+ w .Write ([]byte ("a\n b\n " ))
1016
+ t .Fail ()
1017
+ })
1018
+ }
1019
+ root .Run ("check output of outputWriter" , f )
1020
+ tstate .release ()
1021
+
1022
+ if tstate .running != 0 || tstate .numWaiting != 0 {
1023
+ t .Errorf ("running and waiting non-zero: got %d and %d" , tstate .running , tstate .numWaiting )
1024
+ }
1025
+ got := strings .TrimSpace (buf .String ())
1026
+ output := `
1027
+ --- FAIL: check output of outputWriter (0.00s)
1028
+ --- FAIL: check output of outputWriter/#00 (0.00s)
1029
+ a
1030
+ b
1031
+ a
1032
+ b
1033
+ `
1034
+ want := strings .TrimSpace (output )
1035
+ re := makeRegexp (want )
1036
+ if ok , err := regexp .MatchString (re , got ); ! ok || err != nil {
1037
+ t .Errorf ("output:\n got:\n %s\n want:\n %s" , got , want )
1038
+ }
1039
+ }
1040
+
1041
+ func TestOutputWriterBuffering (t * T ) {
1042
+ w := outputWriter {& t .common , nil }
1043
+
1044
+ w .Write ([]byte ("Hel" ))
1045
+ w .Write ([]byte ("lo\n World\n Input to log\n \n \n More logging\n Shouldn't be logged" ))
1046
+ w .Write ([]byte ("Also shouldn't be logged" ))
1047
+
1048
+ bufContents := string (w .b )
1049
+ expected := "Shouldn't be loggedAlso shouldn't be logged"
1050
+ if bufContents != expected {
1051
+ t .Errorf ("unexpected buffer contents: got %q want %q" , bufContents , expected )
1052
+ }
1053
+ }
0 commit comments