init: basic tools
This commit is contained in:
66
runner_test.go
Normal file
66
runner_test.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package script_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.uint32.ru/dmitry/script"
|
||||
)
|
||||
|
||||
var echoProcessor = func(_ context.Context, in []string) ([]string, error) {
|
||||
return in, nil
|
||||
}
|
||||
|
||||
func TestBasicRun(t *testing.T) {
|
||||
t.Parallel()
|
||||
input := [][]string{
|
||||
{"hello", "world"},
|
||||
}
|
||||
|
||||
r := script.NewMemReader(input)
|
||||
w := script.NewMemWriter()
|
||||
|
||||
conf := script.RunConfig{
|
||||
Input: r,
|
||||
Output: w,
|
||||
Processor: echoProcessor,
|
||||
}
|
||||
|
||||
if err := script.Run(t.Context(), conf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
output := w.Output()
|
||||
|
||||
if !reflect.DeepEqual(input, output) {
|
||||
t.Errorf("incorrect output, want: %v, got: %v", input, output)
|
||||
}
|
||||
}
|
||||
|
||||
type infiniteReader struct{}
|
||||
|
||||
func (ir *infiniteReader) Read(_ context.Context) ([]string, error) {
|
||||
return []string{"infinity", "looks", "like", "this"}, nil
|
||||
}
|
||||
|
||||
func TestRunnerObeysContext(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r := &infiniteReader{}
|
||||
w := script.NewNopWriter()
|
||||
|
||||
conf := script.RunConfig{
|
||||
Input: r,
|
||||
Output: w,
|
||||
Processor: echoProcessor,
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(t.Context(), time.Millisecond)
|
||||
defer cancel()
|
||||
|
||||
if err := script.Run(ctx, conf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user