-
Notifications
You must be signed in to change notification settings - Fork 14
/
command_test.go
54 lines (47 loc) · 1.13 KB
/
command_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
package main
import (
"bytes"
"testing"
)
type commandArguments struct {
name string
args []string
}
type mockCommander struct {
commands []commandArguments
}
func newMockCommander() *mockCommander {
return &mockCommander{make([]commandArguments, 0)}
}
func (c *mockCommander) Run(name string, args ...string) error {
c.commands = append(c.commands, commandArguments{name, args})
return nil
}
func TestNewCommand(t *testing.T) {
w := new(bytes.Buffer)
c := NewCommand(w, nil)
if c.out != w {
t.Errorf("unexpected command stdout")
}
if c.err != w {
t.Errorf("unexpected command stderr")
}
}
func TestCommand_Run(t *testing.T) {
cases := []struct {
inputs []string
expect string
}{
{[]string{"hello, world!"}, "hello, world!\n"},
{[]string{"publish", "docker"}, "publish docker\n"},
{[]string{"there", "are", "four", "arguments"}, "there are four arguments\n"},
}
for _, c := range cases {
w := new(bytes.Buffer)
cmd := NewCommand(w, w)
_ = cmd.Run("echo", c.inputs...)
if actual := w.String(); c.expect != actual {
t.Errorf("expect command echo value is `%s`, actual is `%s`", c.expect, actual)
}
}
}