MCPcopy Index your code
hub / github.com/gopherdata/gophernotes / TestPrintStdout

Function TestPrintStdout

kernel_test.go:222–275  ·  view source on GitHub ↗

TestPrintStdout tests that data written to stdout publishes the same data in a "stdout" "stream" message.

(t *testing.T)

Source from the content-addressed store, hash-verified

220
221// TestPrintStdout tests that data written to stdout publishes the same data in a "stdout" "stream" message.
222func 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
254cases:
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.
278func TestPrintStderr(t *testing.T) {

Callers

nothing calls this directly

Calls 1

testOutputStreamFunction · 0.85

Tested by

no test coverage detected