-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
114 lines (84 loc) · 2.41 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
package main
import (
"bytes"
"fmt"
"strings"
"testing"
"github.com/bbland1/goDo/cmd"
// "github.com/bbland1/goDo/task"
)
func TestNoCommandArgs(t *testing.T) {
var bufferOut bytes.Buffer
var bufferErr bytes.Buffer
args := []string{"godo"}
exitCode := runAppLogic(&bufferOut, &bufferErr, args)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
expectedOutput := cmd.Greeting
output := strings.TrimSpace(bufferOut.String())
if output != expectedOutput {
t.Errorf("Expected output: %q, got: %q", expectedOutput, output)
}
}
func TestUnknownCommand(t *testing.T) {
var bufferOut bytes.Buffer
var bufferErr bytes.Buffer
args := []string{"godo", "unknown"}
exitCode := runAppLogic(&bufferOut, &bufferErr, args)
if exitCode != 1 {
t.Errorf("Exit code of 1 was expected but got %d", exitCode)
}
expectedErrMsg := fmt.Sprintf("Unknown command: %s", "unknown")
output := strings.TrimSpace(bufferErr.String())
if output != expectedErrMsg {
t.Errorf("Expected output: %q, got: %q", expectedErrMsg, output)
}
}
func TestValidCommandPassed(t *testing.T) {
var bufferOut bytes.Buffer
var bufferErr bytes.Buffer
var exitCode int
cmd.RegisterCommand(cmd.NewHelpCommand(&bufferOut, &bufferErr, &exitCode))
args := []string{"godo", "help"}
exitCode = runAppLogic(&bufferOut, &bufferErr, args)
if exitCode != 0 {
t.Errorf("Exit code of 0 was expected but got %d", exitCode)
}
}
func TestCommandInitFail(t *testing.T) {
var bufferOut bytes.Buffer
var bufferErr bytes.Buffer
failure := &failingCommand{
name: "fail",
description: "a fail",
}
cmd.RegisterCommand(failure)
args := []string{"goDo", "fail"}
exitCode := runAppLogic(&bufferOut, &bufferErr, args)
if exitCode != 1 {
t.Errorf("Expected exit code 1, but got %d", exitCode)
}
expectedErrorMsg := "Error initializing command: fail"
output := strings.TrimSpace(bufferErr.String())
if output != expectedErrorMsg {
t.Errorf("Expected stderr to contain '%s', but got '%s'", expectedErrorMsg, bufferErr.String())
}
}
type failingCommand struct {
name string
description string
}
func (cmd *failingCommand) Init(args []string) error {
return fmt.Errorf("%s", cmd.name)
}
func (cmd *failingCommand) Run() {}
func (cmd *failingCommand) Called() bool {
return false
}
func (cmd *failingCommand) GetName() string {
return cmd.name
}
func (cmd *failingCommand) GetDescription() string {
return cmd.description
}