forked from snychka/golang-temperature-converter-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
270 lines (239 loc) · 5.77 KB
/
main_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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"reflect"
"strings"
"testing"
)
var binaryName = "tempconverterTestBinary"
func TestMain(m *testing.M) {
build := exec.Command("go", "build", "-o", binaryName)
err := build.Run()
if err != nil {
fmt.Printf("could not make binary %v", err)
os.Exit(1)
}
exitCode := m.Run()
cleanUp := exec.Command("rm", "-f", binaryName)
cleanUperr := cleanUp.Run()
if cleanUperr != nil {
fmt.Println("could not clean up", err)
}
os.Exit(exitCode)
}
func TestCheckForArgumentsM1(t *testing.T) {
t.Run("invalid args", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
// Runs the program with not enough arguments.
cmd := exec.Command(path.Join(dir, binaryName), []string{}...)
output, err := cmd.CombinedOutput()
if err == nil || !strings.Contains(string(output), errInvalidArguments.Error()) {
t.Fatal("Did not validate command line arguments properly")
}
// Runs the program with more than enough
cmd2 := exec.Command(path.Join(dir, binaryName), []string{"one", "two"}...)
output2, err2 := cmd2.CombinedOutput()
if err2 == nil || !strings.Contains(string(output2), errInvalidArguments.Error()) {
t.Fatal("Did not validate command line arguments properly")
}
})
}
func TestAssignsToOriginUnitM1(t *testing.T) {
isAssigningToOriginUnit := false
isAssigningToOriginUnitFromCorrectFunction := false
src, err := ioutil.ReadFile("main.go")
if err != nil {
log.Fatal(err)
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", src, 0)
if err != nil {
panic(err)
}
for _, decl := range f.Decls {
if reflect.TypeOf(decl).String() == "*ast.FuncDecl" {
funcDecl := decl.(*ast.FuncDecl)
// Only interested in main() function
if funcDecl.Name.Name != "main" {
continue
}
for _, decl2 := range funcDecl.Body.List {
if reflect.TypeOf(decl2).String() == "*ast.AssignStmt" {
assignToOriginUnit := decl2.(*ast.AssignStmt)
for _, ouVariable := range assignToOriginUnit.Lhs {
ident := ouVariable.(*ast.Ident)
if ident.Obj.Name == "originUnit" {
isAssigningToOriginUnit = true
}
}
for _, ouValue := range assignToOriginUnit.Rhs {
if reflect.TypeOf(ouValue).String() == "*ast.CallExpr" {
v1 := ouValue.(*ast.CallExpr)
v2 := v1.Fun.(*ast.SelectorExpr)
v3 := v2.X.(*ast.Ident)
if v3.Name == "strings" && v2.Sel.Name == "ToUpper" {
isAssigningToOriginUnitFromCorrectFunction = true
}
}
}
}
}
if !isAssigningToOriginUnit || !isAssigningToOriginUnitFromCorrectFunction {
t.Error("Did not assign proper value to originUnit")
}
}
}
}
func TestReadsCurrentTemperatureM2(t *testing.T) {
t.Run("reads current temperature", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(path.Join(dir, binaryName), []string{"C"}...)
stdin, e := cmd.StdinPipe()
if e != nil {
panic(e)
}
stderr, e := cmd.StderrPipe()
if e != nil {
panic(e)
}
if e := cmd.Start(); e != nil {
panic(e)
}
_, e = stdin.Write([]byte("FFFF\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte("n\n"))
if e != nil {
panic(e)
}
stdin.Close()
errPrint, _ := ioutil.ReadAll(stderr)
if !strings.Contains(string(errPrint), errReadingInput.Error()) {
t.Fatal("Did not print errReadingInput error")
}
})
}
func TestCheckConversionM2(t *testing.T) {
t.Run("converts temperature", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(path.Join(dir, binaryName), []string{"F"}...)
stdin, e := cmd.StdinPipe()
if e != nil {
panic(e)
}
outPipe, e := cmd.StdoutPipe()
if e != nil {
panic(e)
}
if e := cmd.Start(); e != nil {
panic(e)
}
_, e = stdin.Write([]byte("32\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte("n\n"))
if e != nil {
panic(e)
}
stdin.Close()
outPrint, _ := ioutil.ReadAll(outPipe)
if !strings.Contains(string(outPrint), "32 F = 0 C") {
t.Fatal("Did not properly convert temperature")
}
})
}
func TestPromptAgainM2(t *testing.T) {
t.Run("convert again", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(path.Join(dir, binaryName), []string{"F"}...)
stdin, e := cmd.StdinPipe()
if e != nil {
panic(e)
}
outPipe, e := cmd.StdoutPipe()
if e != nil {
panic(e)
}
if e := cmd.Start(); e != nil {
panic(e)
}
_, e = stdin.Write([]byte("32\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte(" Y\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte("40\n"))
if e != nil {
panic(e)
}
stdin.Close()
outPrint, _ := ioutil.ReadAll(outPipe)
if !strings.Contains(string(outPrint), "32 F = 0 C") ||
!strings.Contains(string(outPrint), "40 F = 4 C") {
t.Fatal("Did not prompt user for another run")
}
})
}
func TestParsePromptToUpperM2(t *testing.T) {
t.Run("prompt to upper", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cmd := exec.Command(path.Join(dir, binaryName), []string{"F"}...)
stdin, e := cmd.StdinPipe()
if e != nil {
panic(e)
}
outPipe, e := cmd.StdoutPipe()
if e != nil {
panic(e)
}
if e := cmd.Start(); e != nil {
panic(e)
}
_, e = stdin.Write([]byte("32\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte("y\n"))
if e != nil {
panic(e)
}
_, e = stdin.Write([]byte("212\n"))
if e != nil {
panic(e)
}
stdin.Close()
outPrint, _ := ioutil.ReadAll(outPipe)
if !strings.Contains(string(outPrint), "32 F = 0 C") ||
!strings.Contains(string(outPrint), "212 F = 100 C") {
t.Fatal("Did not properly parse user input")
}
})
}