(t *testing.T)
| 442 | } |
| 443 | |
| 444 | func TestPipelineWithLinewiseFunction(t *testing.T) { |
| 445 | t.Parallel() |
| 446 | ctx := context.Background() |
| 447 | |
| 448 | p := pipe.New() |
| 449 | // Print the numbers from 1 to 20 (generated from scratch): |
| 450 | p.Add( |
| 451 | seqFunction(20), |
| 452 | // Discard all but the multiples of 5, and emit the results |
| 453 | // separated by spaces on one line: |
| 454 | pipe.LinewiseFunction( |
| 455 | "multiples-of-5", |
| 456 | func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error { |
| 457 | n, err := strconv.Atoi(string(line)) |
| 458 | if err != nil { |
| 459 | return err |
| 460 | } |
| 461 | if n%5 != 0 { |
| 462 | return nil |
| 463 | } |
| 464 | _, err = fmt.Fprintf(w, " %d", n) |
| 465 | return err |
| 466 | }, |
| 467 | ), |
| 468 | // Read the words and square them, emitting the results one per |
| 469 | // line: |
| 470 | pipe.ScannerFunction( |
| 471 | "square-multiples-of-5", |
| 472 | func(r io.Reader) (pipe.Scanner, error) { |
| 473 | scanner := bufio.NewScanner(r) |
| 474 | scanner.Split(bufio.ScanWords) |
| 475 | return scanner, nil |
| 476 | }, |
| 477 | func(_ context.Context, _ pipe.Env, line []byte, w *bufio.Writer) error { |
| 478 | n, err := strconv.Atoi(string(line)) |
| 479 | if err != nil { |
| 480 | return err |
| 481 | } |
| 482 | _, err = fmt.Fprintf(w, "%d\n", n*n) |
| 483 | return err |
| 484 | }, |
| 485 | ), |
| 486 | ) |
| 487 | |
| 488 | out, err := p.Output(ctx) |
| 489 | assert.NoError(t, err) |
| 490 | assert.EqualValues(t, "25\n100\n225\n400\n", out) |
| 491 | } |
| 492 | |
| 493 | func TestScannerAlwaysFlushes(t *testing.T) { |
| 494 | t.Parallel() |
nothing calls this directly
no test coverage detected