2025-08-30 22:00:31 +03:00
|
|
|
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{}
|
|
|
|
|
2025-08-31 13:31:00 +03:00
|
|
|
func (ir *infiniteReader) Read() ([]string, error) {
|
2025-08-30 22:00:31 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|