github.com/jchengjr77/canaveral@v1.0.1-0.20200715160102-ea9245d1a2fb/lib/iotesting_test.go (about) 1 package lib 2 3 import ( 4 "fmt" 5 "os" 6 "testing" 7 ) 8 9 func sample() { 10 fmt.Println("hello1") 11 fmt.Println("hello2") 12 fmt.Println("hello3") 13 } 14 func TestCaptureOutput(t *testing.T) { 15 out := CaptureOutput(sample) 16 want := "hello1\nhello2\nhello3\n" 17 if out != want { 18 t.Errorf( 19 "func CaptureOutput() returned '%s' should be '%s'", out, want) 20 } 21 } 22 23 func TestRedirOut(t *testing.T) { 24 origOut := RedirOut() 25 defer func() { 26 os.Stdout = origOut 27 }() 28 if os.Stdout == origOut { 29 t.Error("func RedirOut() failed to generate new stdout file_d.") 30 } 31 } 32 33 func TestResetOut(t *testing.T) { 34 _, writer, _ := os.Pipe() 35 origOut := os.Stdout 36 os.Stdout = writer // redirect output away 37 if os.Stdout == origOut { 38 t.Error("stdout redirection messed up. There's something wrong in the test.") 39 } 40 ResetOut(origOut) 41 if os.Stdout != origOut { 42 t.Error("func ResetOut() failed to reset stdout to original.") 43 } 44 }