-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexecutor_test.go
82 lines (69 loc) · 1.47 KB
/
executor_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmds
import (
"bytes"
"context"
"errors"
"io"
"testing"
)
var errGeneric = errors.New("an error occurred")
var root = &Command{
Subcommands: map[string]*Command{
"test": &Command{
Run: func(req *Request, re ResponseEmitter, env Environment) error {
re.Emit(env)
return nil
},
},
"testError": &Command{
Run: func(req *Request, re ResponseEmitter, env Environment) error {
err := errGeneric
if err != nil {
return err
}
re.Emit(env)
return nil
},
},
},
}
type wc struct {
io.Writer
io.Closer
}
type env int
func TestExecutor(t *testing.T) {
env := env(42)
req, err := NewRequest(context.Background(), []string{"test"}, nil, nil, nil, root)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
re, err := NewWriterResponseEmitter(wc{&buf, nopCloser{}}, req)
if err != nil {
t.Fatal(err)
}
x := NewExecutor(root)
x.Execute(req, re, &env)
if out := buf.String(); out != "42\n" {
t.Errorf("expected output \"42\" but got %q", out)
}
}
func TestExecutorError(t *testing.T) {
env := env(42)
req, err := NewRequest(context.Background(), []string{"testError"}, nil, nil, nil, root)
if err != nil {
t.Fatal(err)
}
re, res := NewChanResponsePair(req)
x := NewExecutor(root)
x.Execute(req, re, &env)
_, err = res.Next()
if err == nil {
t.Fatal("expected error but got nil")
}
expErr := "an error occurred"
if err.Error() != expErr {
t.Fatalf("expected error message %q but got: %s", expErr, err)
}
}