TestPrintStdout tests that data written to stdout publishes the same data in a "stdout" "stream" message.
(t *testing.T)
| 220 | |
| 221 | // TestPrintStdout tests that data written to stdout publishes the same data in a "stdout" "stream" message. |
| 222 | func TestPrintStdout(t *testing.T) { |
| 223 | cases := []struct { |
| 224 | Input []string |
| 225 | Output []string |
| 226 | }{ |
| 227 | {[]string{ |
| 228 | `import "fmt"`, |
| 229 | "a := 1", |
| 230 | "fmt.Println(a)", |
| 231 | }, []string{"1\n"}}, |
| 232 | {[]string{ |
| 233 | "a = 2", |
| 234 | "fmt.Print(a)", |
| 235 | }, []string{"2"}}, |
| 236 | {[]string{ |
| 237 | `import "os"`, |
| 238 | `os.Stdout.WriteString("3")`, |
| 239 | }, []string{"3"}}, |
| 240 | {[]string{ |
| 241 | `fmt.Fprintf(os.Stdout, "%d\n", 4)`, |
| 242 | }, []string{"4\n"}}, |
| 243 | {[]string{ |
| 244 | `import "time"`, |
| 245 | "for i := 0; i < 3; i++ {", |
| 246 | " fmt.Println(i)", |
| 247 | " time.Sleep(500 * time.Millisecond)", // Stall to prevent prints from buffering into single message. |
| 248 | "}", |
| 249 | }, []string{"0\n", "1\n", "2\n"}}, |
| 250 | } |
| 251 | |
| 252 | t.Logf("Should produce stdout stream messages when writing to stdout") |
| 253 | |
| 254 | cases: |
| 255 | for k, tc := range cases { |
| 256 | // Give a progress report. |
| 257 | t.Logf(" Evaluating code snippet %d/%d.", k+1, len(cases)) |
| 258 | |
| 259 | // Get the result. |
| 260 | stdout, _ := testOutputStream(t, strings.Join(tc.Input, "\n")) |
| 261 | |
| 262 | // Compare the result. |
| 263 | if len(stdout) != len(tc.Output) { |
| 264 | t.Errorf("\t%s Test case expected %d message(s) on stdout but got %d.", failure, len(tc.Output), len(stdout)) |
| 265 | continue |
| 266 | } |
| 267 | for i, expected := range tc.Output { |
| 268 | if stdout[i] != expected { |
| 269 | t.Errorf("\t%s Test case returned unexpected messages on stdout.", failure) |
| 270 | continue cases |
| 271 | } |
| 272 | } |
| 273 | t.Logf("\t%s Returned the expected messages on stdout.", success) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | // TestPrintStderr tests that data written to stderr publishes the same data in a "stderr" "stream" message. |
| 278 | func TestPrintStderr(t *testing.T) { |
nothing calls this directly
no test coverage detected