-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
167 lines (123 loc) · 3.87 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
package main
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/bbland1/goDo/cmd"
"github.com/bbland1/goDo/task"
)
func TestUsageAndExit(t *testing.T) {
var buffer bytes.Buffer
exitCode := usageAndExit(&buffer, "this is a test error message", 3)
if exitCode != 3 {
t.Errorf("Exit code of 3 was expected but got %d", exitCode)
}
expectedOutput := "this is a test error message"
output := strings.TrimSpace(buffer.String())
if !strings.Contains(output, expectedOutput) {
t.Errorf("Expected error message to contain %q, but got %q", expectedOutput, output)
}
}
func TestNoCommandArgs(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main"}, db)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
expectedOutput := cmd.Greeting
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestUnknownCommand(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main", "unknown"}, db)
if exitCode != 1 {
t.Errorf("Exit code of 1 was expected but got %d", exitCode)
}
expectedOutput := fmt.Sprintf("unknown command passed to goDo: %s", "unknown")
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestHelpCommand(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main", "help"}, db)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
expectedOutput := cmd.UserManual
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestVersionCommand(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main", "version"}, db)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
expectedOutput := "goDo v" + cmd.BuildInfo.Version
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestAddCommandNoArgs(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main", "add"}, db)
if exitCode != 1 {
t.Errorf("Exit code of 1 was expected but got %d", exitCode)
}
expectedOutput := "a description string needs to be passed to add a task"
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestAddCommandArgs(t *testing.T) {
var buffer bytes.Buffer
db, err := task.InitDatabase(":memory:")
if err != nil {
t.Fatalf("InitDatabase failed at creating the db, %v", err)
}
defer db.Close()
exitCode := runAppLogic(&buffer, []string{"main", "add", "tester"}, db)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
expectedOutput := ""
output := strings.TrimSpace(buffer.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}