-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp_test.go
executable file
·187 lines (161 loc) · 4 KB
/
app_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
package rubik
import (
"bytes"
"io/ioutil"
"path/filepath"
"reflect"
"testing"
"github.com/BurntSushi/toml"
"github.com/rubikorg/rubik/pkg"
)
type testConfig struct {
Port string
}
type testBlock struct {
}
func (tb testBlock) OnAttach(app *App) error {
return nil
}
func getDummyWsConfig() pkg.WorkspaceConfig {
commands := make(map[string]map[string]string)
commands["test"] = map[string]string{
"cwd": "go test -cover ./...",
"pwd": "./Go/ink",
}
commands["storage"] = map[string]string{
"cwd": "go test -cover storage_test.go",
"pwd": "./Go/ink",
}
// we need to write a rubik.toml file for testing our
// workspace config
return pkg.WorkspaceConfig{
ProjectName: "rubiktest",
Module: "rubiktest",
App: []pkg.Project{
{
Name: "appOne",
Path: "./cmd/appOne",
Watchable: false,
Logging: pkg.LoggingConfig{
Path: "./Go/ink/logs/$service.log",
ErrorPath: "./Go/ink/logs/$service.error.log",
Stream: "file",
Format: "$level: (DD/MM/YYYY) $message",
},
},
},
X: commands,
}
}
func init() {
workspaceConf := getDummyWsConfig()
var buf bytes.Buffer
enc := toml.NewEncoder(&buf)
err := enc.Encode(workspaceConf)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(filepath.Join("..", "..", "rubik.toml"), buf.Bytes(), 0755)
err = ioutil.WriteFile(filepath.Join(".", "config", "test.toml"), buf.Bytes(), 0755)
err = ioutil.WriteFile(filepath.Join(".", "config", "default.toml"), buf.Bytes(), 0755)
if err != nil {
panic(err)
}
}
func TestLoad(t *testing.T) {
var someMap map[string]interface{}
err := Load(&someMap)
if err != nil {
t.Error(err.Error())
return
}
err = Load(someMap)
if err == nil {
t.Error("Load() did not throw an error when you passed a value type")
}
}
func TestGetConfig(t *testing.T) {
err := Load(&testConfig{})
if err != nil {
t.Error(err.Error())
return
}
conf := GetConfig()
if conf == nil {
t.Error("GetConfig() returned nil")
return
}
t.Logf("%+v\n", conf)
_, ok := conf.(testConfig)
if !ok {
t.Error("GetConfig() not returning config of proper type as set while loading")
}
}
func TestCreate(t *testing.T) {
tRouter := Create("/")
if reflect.ValueOf(tRouter).Type() != reflect.ValueOf(Router{}).Type() {
t.Error("Create() did not return instance of Router struct")
return
}
if tRouter.basePath != "/" {
t.Error("Router has wrong base path in it. Value: " + tRouter.basePath)
return
}
tRouter = Create("/base")
if tRouter.basePath != "/base" {
t.Error("Router has wrong base path in it. Value: " + tRouter.basePath)
}
}
func TestUse(t *testing.T) {
r := Create("/")
Use(r)
if len(app.routers) <= 0 {
t.Error("Use() did not append a router to the routes slice of app")
}
}
func TestSetNotFoundHandler(t *testing.T) {
SetNotFoundHandler(notFoundHandler{})
// should not panic
intr := recover()
if intr != nil {
err := intr.(error)
t.Error(err.Error())
}
}
func TestEFunc(t *testing.T) {
err := E("some error")
if err.Error() != "some error" {
t.Error("E() did not save the error message properly")
}
}
func TestAttach(t *testing.T) {
Attach("TestBlock", testBlock{})
if len(app.blocks) == 0 {
t.Error("Attach() called but number of blocks inside rubik is still 0")
return
}
// check if blocks with same symbol cannot be attached
Attach("TestBlock", testBlock{})
if len(app.blocks) > 1 {
t.Error("Attach() called second time with same symbol and it got attached")
}
}
// func TestGetBlock(t *testing.T) {
// Attach("TestBlock", testBlock{})
// _, ok := GetBlock("TestBlock").(*testBlock)
// if !ok {
// t.Error("GetBlock() not returning proper block struct of type testBlock{}")
// }
// }
func TestBeforeRequest(t *testing.T) {
BeforeRequest(func(rc *HookContext) {})
if len(beforeHooks) == 0 {
t.Error("BeforeRequest() not attached to beforeHooks slice")
}
}
func TestAfterRequest(t *testing.T) {
AfterRequest(func(rc *HookContext) {})
if len(afterHooks) == 0 {
t.Error("AfterRequest() not attached to beforeHooks slice")
}
}