(t *testing.T)
| 539 | } |
| 540 | |
| 541 | func TestScannerFinishEarly(t *testing.T) { |
| 542 | t.Parallel() |
| 543 | ctx := context.Background() |
| 544 | |
| 545 | var length int64 |
| 546 | |
| 547 | p := pipe.New() |
| 548 | p.Add( |
| 549 | // Print the numbers from 1 to 20 (generated from scratch): |
| 550 | seqFunction(20), |
| 551 | |
| 552 | // Pass the numbers through up to 7, then exit with an ignored |
| 553 | // error: |
| 554 | pipe.LinewiseFunction( |
| 555 | "finish-after-7", |
| 556 | func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error { |
| 557 | fmt.Fprintf(w, "%s\n", line) |
| 558 | if string(line) == "7" { |
| 559 | return pipe.FinishEarly |
| 560 | } |
| 561 | return nil |
| 562 | }, |
| 563 | ), |
| 564 | |
| 565 | // Read the numbers and add them into the sum: |
| 566 | pipe.Function( |
| 567 | "compute-length", |
| 568 | func(_ context.Context, _ pipe.Env, stdin io.Reader, _ io.Writer) error { |
| 569 | var err error |
| 570 | length, err = io.Copy(io.Discard, stdin) |
| 571 | return err |
| 572 | }, |
| 573 | ), |
| 574 | ) |
| 575 | |
| 576 | err := p.Run(ctx) |
| 577 | assert.NoError(t, err) |
| 578 | // Make sure that all of the bytes emitted before the second |
| 579 | // stage's error were received by the third stage: |
| 580 | assert.EqualValues(t, 14, length) |
| 581 | } |
| 582 | |
| 583 | func TestPrintln(t *testing.T) { |
| 584 | t.Parallel() |
nothing calls this directly
no test coverage detected