-
Notifications
You must be signed in to change notification settings - Fork 1
/
cat_test.go
222 lines (204 loc) · 5.27 KB
/
cat_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"testing"
)
type ClosingBuffer struct {
*bytes.Buffer
}
func (cb *ClosingBuffer) Close() error {
return nil
}
func TestGetNonPrintingStr(t *testing.T) {
if GetNonPrintingStr(65) != "A" {
t.Errorf("Wrong string for byte between 32 and 127")
}
if GetNonPrintingStr(127) != "^?" {
t.Errorf("Wrong string for byte 127")
}
if GetNonPrintingStr(130) != "M-^B" {
t.Errorf("Wrong string for byte between 127 and 160")
}
if GetNonPrintingStr(170) != "M-*" {
t.Errorf("Wrong string for byte between 160 and 255")
}
if GetNonPrintingStr(255) != "M-^?" {
t.Errorf("Wrong string for byte greater than 255")
}
if GetNonPrintingStr('\t') != "\t" {
t.Errorf("Wrong string for byte \t")
}
if GetNonPrintingStr(30) != "^^" {
t.Errorf("Wrong string for byte less than 32")
}
*displayTabChar = true
if GetNonPrintingStr('\t') != "^I" {
t.Errorf("Wrong string for byte \t and ")
}
*displayTabChar = false
}
func TestChoose(t *testing.T) {
rc, err := Choose("-b")
if rc != nil || err != nil {
t.Errorf("Error and input handler is nil for flag argument")
}
rc, err = Choose("-")
if rc != os.Stdin || err != nil {
t.Errorf("Error is nil and input handler is std input for flag argument")
}
rc, err = Choose("Makefile")
if err != nil {
t.Errorf("Error is nil and input handler is file for flag argument")
}
}
func TestCat1(t *testing.T) {
// when no input handler and flags are sent
args := []string{"./cat"}
var rc io.ReadCloser
cb := &ClosingBuffer{bytes.NewBufferString("abcd\n")}
rc = cb
out := mockStdout(args, rc)
if out != "abcd\n" {
t.Errorf("Error when no input handler and flags are sent")
}
}
func TestCat2(t *testing.T) {
// when no flags are given
args := []string{"./cat", "testdata/dummy_data"}
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when no flags are sent")
}
}
func TestCat3(t *testing.T) {
// error when invalid filename given
if os.Getenv("EXIT") == "true" {
args := []string{"./cat", "abcd"}
Cat(args, os.Stdin)
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestCat3")
cmd.Env = append(os.Environ(), "EXIT=true")
output, _ := cmd.StderrPipe()
if err := cmd.Start(); err != nil {
t.Error(err)
}
outBytes, _ := ioutil.ReadAll(output)
if !equal("open abcd: no such file or directory\n", outBytes) {
t.Error("error string when invalid filename doesn't match")
}
err := cmd.Wait()
if e, ok := err.(*exec.ExitError); !ok || e.Success() {
t.Error("error when invalid filename given doesn't exit")
}
}
func TestCatFlagb(t *testing.T) {
args := []string{"./cat", "-b", "testdata/dummy_data"}
*numberNonBlankOutputLines = true
defer func() {
*numberNonBlankOutputLines = false
}()
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data_flagb", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when flag b is sent")
}
}
func TestCatFlagev(t *testing.T) {
args := []string{"./cat", "-e", "testdata/dummy_data"}
*displayDollarAtEnd = true
defer func() {
*displayDollarAtEnd = false
}()
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data_flage", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when flag e is sent")
}
}
func TestCatFlagn(t *testing.T) {
args := []string{"./cat", "-n", "testdata/dummy_data"}
*numberOutputLines = true
defer func() {
*numberOutputLines = false
}()
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data_flagn", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when flag n is sent")
}
}
func TestCatFlags(t *testing.T) {
args := []string{"./cat", "-s", "testdata/dummy_data"}
*singleSpacedOutput = true
defer func() {
*singleSpacedOutput = false
}()
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data_flags", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when flag s is sent")
}
}
func TestCatMultipleFlags(t *testing.T) {
args := []string{"./cat", "-b", "-e", "-s", "-u", "-t", "testdata/dummy_data"}
*numberNonBlankOutputLines = true
*displayDollarAtEnd = true
*singleSpacedOutput = true
*displayTabChar = true
*disableOutputBuffer = true
defer func() {
*numberNonBlankOutputLines = false
*displayDollarAtEnd = false
*singleSpacedOutput = false
*displayTabChar = false
*disableOutputBuffer = false
}()
out := mockStdout(args, os.Stdin)
f, _ := os.OpenFile("testdata/dummy_data_multiple_flags", os.O_RDONLY, 0755)
b := make([]byte, 1024)
f.Read(b)
if !equal(out, b) {
t.Errorf("Error when multiple flags are sent")
}
}
func equal(str string, b []byte) bool {
for i, value := range b {
if i < len(str) && value != str[i] {
fmt.Println("error: ", string(value), " :", i)
return false
}
}
return true
}
func mockStdout(args []string, reader io.ReadCloser) string {
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
Cat(args, reader)
output := make(chan string)
go func() {
var buf bytes.Buffer
io.Copy(&buf, r)
output <- buf.String()
}()
w.Close()
os.Stdout = oldStdout
return <-output
}