-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
83 lines (70 loc) · 2.09 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
package main_test
import (
c "context"
"testing"
"github.com/go-bdd/gobdd"
"github.com/go-bdd/gobdd/context"
sapp "github.com/go-bdd/sample-app"
)
type appKey struct{}
type createAccountErr struct{}
type loginErr struct{}
func TestFeatures(t *testing.T) {
app := sapp.NewApp()
s := gobdd.NewSuite(t, gobdd.WithBeforeScenario(func(ctx context.Context) {
ctx.Set(appKey{}, app)
}))
s.AddStep(`I create a new account with username ([\da-zA-Z0-9]+)`, iCreateNewAccountWithUsername)
s.AddStep(`the creation of the account (succeeded|failed)`, theCreationOfAccount)
s.AddStep(`I log in to the system using username ([\da-zA-Z0-9]+)`, iLogInToTheSystemUsingUsername)
s.AddStep(`the logging in ([\da-zA-Z0-9]+)`, loggingIn)
go app.Run(c.Background())
s.Run()
}
func iCreateNewAccountWithUsername(t gobdd.TestingT, ctx context.Context, u string) context.Context {
app := getApp(ctx)
err := app.CreateNewAccount(u, "pass")
ctx.Set(createAccountErr{}, err)
return ctx
}
func iLogInToTheSystemUsingUsername(t gobdd.TestingT, ctx context.Context, username string) context.Context {
app := getApp(ctx)
err := app.Login(username, "pass")
ctx.Set(loginErr{}, err)
return ctx
}
func loggingIn(t gobdd.TestingT, ctx context.Context, result string) context.Context {
logErrRaw, err := ctx.Get(loginErr{})
if err != nil {
t.Error("missing login result")
return ctx
}
if result == "succeeded" && logErrRaw != nil {
t.Error("the login failed")
return ctx
}
if result == "failed" && logErrRaw == nil {
t.Error("the login succeeded")
}
return ctx
}
func theCreationOfAccount(t gobdd.TestingT, ctx context.Context, result string) context.Context {
givenErr, err := ctx.Get(createAccountErr{})
if err != nil {
t.Error("could not find result of creating the account")
}
if result == "succeeded" && givenErr != nil {
t.Error("expected to success but failed")
}
if result == "fails" && givenErr == nil {
t.Error("expected to fail but succeeded")
}
return ctx
}
func getApp(ctx context.Context) *sapp.App {
app, err := ctx.Get(appKey{})
if err != nil {
panic(err)
}
return app.(*sapp.App)
}