-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshell_test.go
102 lines (94 loc) · 2.17 KB
/
shell_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"io"
"io/ioutil"
"reflect"
"strings"
"testing"
)
func StringShellReader(lines string) *SimpleShellReader {
r := strings.NewReader(lines)
sh := NewShellReader(r, "")
return sh
}
func TestShellReaderReadCommand_eof(t *testing.T) {
sh := StringShellReader("")
sh.SetOutput(ioutil.Discard)
_, eof, err := sh.ReadCommand()
if err != io.EOF {
t.Fatal("non-eof error returned")
}
if !eof {
t.Fatal("eof not returned")
}
sh = StringShellReader(":hello shell")
sh.SetOutput(ioutil.Discard)
_, eof, err = sh.ReadCommand()
if err != nil {
t.Fatal("error returned")
}
if !eof {
t.Fatal("eof not returned")
}
}
func TestShellReaderReadCommand_multi(t *testing.T) {
sh := StringShellReader(":hello\n:shell\n")
sh.SetOutput(ioutil.Discard)
cmd, eof, err := sh.ReadCommand()
if err != nil {
t.Fatalf("error returned")
}
if eof {
t.Fatalf("eof returned")
}
if !reflect.DeepEqual(cmd, []string{"hello"}) {
t.Fatalf("unexpected command: %v", cmd)
}
cmd, eof, err = sh.ReadCommand()
if err != nil {
t.Fatalf("error returned")
}
if eof {
t.Fatalf("eof returned")
}
if !reflect.DeepEqual(cmd, []string{"shell"}) {
t.Fatalf("unexpected command: %v", cmd)
}
cmd, eof, err = sh.ReadCommand()
if err != io.EOF {
t.Fatalf("non-eof error returned")
}
if !eof {
t.Fatalf("eof returned")
}
if len(cmd) != 0 {
t.Fatalf("unexpected command: %v", cmd)
}
}
func TestShellReaderReadCommand_single(t *testing.T) {
cmd := func(strs ...string) []string { return strs }
for i, test := range []struct {
str string
cmd []string
}{
{":pop", cmd("pop")},
{":push .items .[]", cmd("push", ".items", ".[]")},
{":push +.items | .[]", cmd("push", ".items | .[]")},
{".items | .[]", cmd("push", ".items | .[]")},
{"?.items | .[]", cmd("peek", ".items | .[]")},
{".", cmd("write")},
{"..", cmd("pop")},
{"\n..", cmd("pop")},
} {
sh := StringShellReader(test.str)
sh.SetOutput(ioutil.Discard)
cmd, _, err := sh.ReadCommand()
if err != nil {
t.Errorf("command %d (%q) %v", i, test.str, err)
continue
}
if !reflect.DeepEqual(cmd, test.cmd) {
t.Errorf("command %d (%q) got %q (expect %q)", i, test.str, cmd, test.cmd)
}
}
}