-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadme_test.go
288 lines (235 loc) · 5.11 KB
/
readme_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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package sabi_test
import (
"fmt"
"os"
//"strconv"
"strings"
"testing"
"time"
"github.com/sttk/sabi"
"github.com/sttk/sabi/errs"
)
var origOsArgs []string = os.Args
func reset() {
os.Args = origOsArgs
}
type ( // possible error reasons
NoName struct{}
FailToGetHour struct{}
FailToOutput struct{ Text string }
)
type GreetDax interface {
UserName() (string, errs.Err)
Hour() (int, errs.Err)
Output(text string) errs.Err
}
func GreetLogic(dax GreetDax) errs.Err {
hour, err := dax.Hour()
if err.IsNotOk() {
return err
}
var s string
switch {
case 5 <= hour && hour < 12:
s = "Good morning, "
case 12 <= hour && hour < 16:
s = "Good afternoon, "
case 16 <= hour && hour < 21:
s = "Good evening, "
default:
s = "Hi, "
}
err = dax.Output(s)
if err.IsNotOk() {
return err
}
name, err := dax.UserName()
if err.IsNotOk() {
return err
}
return dax.Output(name + ".\n")
}
type MapGreetDax struct {
sabi.Dax
m map[string]any
}
func (dax MapGreetDax) UserName() (string, errs.Err) {
name, exists := dax.m["username"]
if !exists {
return "", errs.New(NoName{})
}
return name.(string), errs.Ok()
}
func (dax MapGreetDax) Hour() (int, errs.Err) {
hour, exists := dax.m["hour"]
if !exists {
return 0, errs.New(FailToGetHour{})
}
return hour.(int), errs.Ok()
}
func (dax MapGreetDax) Output(text string) errs.Err {
var s string
v, exists := dax.m["greeting"]
if exists {
s = v.(string)
}
if s == "error" { // for testings the error case
return errs.New(FailToOutput{Text: text})
}
dax.m["greeting"] = s + text
return errs.Ok()
}
func NewMapGreetDaxBase(m map[string]any) sabi.DaxBase {
base := sabi.NewDaxBase()
return struct {
sabi.DaxBase
MapGreetDax
}{
DaxBase: base,
MapGreetDax: MapGreetDax{m: m},
}
}
func TestGreetLogic_morning(t *testing.T) {
m := make(map[string]any)
base := NewMapGreetDaxBase(m)
m["username"] = "everyone"
m["hour"] = 10
err := sabi.Txn(base, GreetLogic)
if err.IsNotOk() {
t.Errorf(err.Error())
}
if m["greeting"] != "Good morning, everyone.\n" {
t.Errorf("Bad greeting: %v\n", m["greeting"])
}
}
type CliArgsDax struct {
sabi.Dax
}
func (dax CliArgsDax) UserName() (string, errs.Err) {
if len(os.Args) <= 1 {
return "", errs.New(NoName{})
}
return os.Args[1], errs.Ok()
}
//func (dax CliArgsDax) Hour() (int, errs.Err) {
// if len(os.Args) <= 2 {
// return 0, errs.New(FailToGetHour{})
// }
// n, err := strconv.Atoi(os.Args[2])
// if err != nil {
// return 0, errs.New(FailToGetHour{}, err)
// }
// return n, errs.Ok()
//}
type ConsoleDax struct {
sabi.Dax
}
// func (dax ConsoleDax) Output(text string) errs.Err {
func (dax ConsoleDax) Print(text string) errs.Err {
fmt.Print(text)
return errs.Ok()
}
func NewGreetDaxBase() sabi.DaxBase {
base := sabi.NewDaxBase()
return struct {
sabi.DaxBase
CliArgsDax
SystemClockDax
MemoryDax
ConsoleDax
}{
DaxBase: base,
CliArgsDax: CliArgsDax{Dax: base},
SystemClockDax: SystemClockDax{Dax: base},
MemoryDax: MemoryDax{Dax: base},
ConsoleDax: ConsoleDax{Dax: base},
}
}
func app() errs.Err {
base := NewGreetDaxBase()
defer base.Close()
return base.Uses("memory", &MemoryDaxSrc{}).
IfOk(sabi.Txn_(base, GreetLogic)).
IfOk(sabi.Txn_(base, PrintLogic))
//return base.Uses("memory", MemoryDaxSrc{}).
// IfOk(sabi.Txn_(base, GreetLogic))
//return sabi.Txn(base, GreetLogic)
}
func TestGreetApp_main(t *testing.T) {
defer reset()
os.Args = []string{"cmd", "foo", "10"}
if err := sabi.StartApp(app); err.IsNotOk() {
//fmt.Println(err.Error())
//os.Exit(1);
t.Errorf(err.Error())
}
}
type SystemClockDax struct {
sabi.Dax
}
func (dax SystemClockDax) Hour() (int, errs.Err) {
return time.Now().Hour(), errs.Ok()
}
type MemoryDaxSrc struct {
buf strings.Builder
}
func (ds *MemoryDaxSrc) Setup(ag sabi.AsyncGroup) errs.Err {
return errs.Ok()
}
func (ds *MemoryDaxSrc) Close() {
ds.buf.Reset()
}
func (ds *MemoryDaxSrc) CreateDaxConn() (sabi.DaxConn, errs.Err) {
return MemoryDaxConn{buf: &(ds.buf)}, errs.Ok()
}
type MemoryDaxConn struct {
buf *strings.Builder
}
func (conn MemoryDaxConn) Append(text string) {
conn.buf.WriteString(text)
}
func (conn MemoryDaxConn) Get() string {
return conn.buf.String()
}
func (conn MemoryDaxConn) Commit(ag sabi.AsyncGroup) errs.Err {
return errs.Ok()
}
func (conn MemoryDaxConn) IsCommitted() bool {
return true
}
func (conn MemoryDaxConn) Rollback(ag sabi.AsyncGroup) {
}
func (conn MemoryDaxConn) ForceBack(ag sabi.AsyncGroup) {
conn.buf.Reset()
}
func (conn MemoryDaxConn) Close() {
}
type MemoryDax struct {
sabi.Dax
}
func (dax MemoryDax) Output(text string) errs.Err {
conn, err := sabi.GetDaxConn[MemoryDaxConn](dax, "memory")
if err.IsNotOk() {
return err
}
conn.Append(text)
return err
}
func (dax MemoryDax) GetText() (string, errs.Err) {
conn, err := sabi.GetDaxConn[MemoryDaxConn](dax, "memory")
if err.IsNotOk() {
return "", err
}
return conn.Get(), err
}
type PrintDax interface {
GetText() (string, errs.Err)
Print(text string) errs.Err
}
func PrintLogic(dax PrintDax) errs.Err {
text, err := dax.GetText()
if err.IsNotOk() {
return err
}
return dax.Print(text)
}