/
bddl.go
90 lines (75 loc) · 2.15 KB
/
bddl.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
package bddl
import (
"bytes"
"fmt"
"os"
"testing"
"github.com/wsxiaoys/terminal/color"
)
// PrintColorsEnabled can be changed to disable the use of terminal coloring.
// One usecase is to add a command line flag to your test that controls its value.
//
// func init() {
// flag.BoolVar(&bddl.PrintColorsEnabled, "color", true, "want colors?")
// }
//
// go test -color=false
//
// or use the environment variable
// TERMCOLORS=false go test
var PrintColorsEnabled = true
// Check for presence of the TERMCOLORS environment variable to set the TerminalColorsEnabled setting.
func init() {
PrintColorsEnabled = os.Getenv("TERMCOLORS") != "false"
}
func bddl(t *testing.T, lead string, args ...interface{}) {
var msg bytes.Buffer
if PrintColorsEnabled {
fmt.Fprint(&msg, color.Sprintf("@{g}%s ", lead))
for i, each := range args {
if i%2 == 0 {
fmt.Fprint(&msg, color.Sprintf("@{g}%v ", each))
} else {
fmt.Fprint(&msg, color.Sprintf("@{m}%v ", each))
}
}
} else {
fmt.Fprint(&msg, " ", lead)
for _, each := range args {
fmt.Fprint(&msg, " ", each)
}
}
t.Log(msg.String())
}
func Feature(t *testing.T, args ...interface{}) {
bddl(t, "Feature:", args...)
}
func Scenario(t *testing.T, args ...interface{}) {
bddl(t, "Scenario:", args...)
}
func Given(t *testing.T, args ...interface{}) {
bddl(t, "Given", args...)
}
func When(t *testing.T, args ...interface{}) {
bddl(t, " When", args...)
}
func Then(t *testing.T, args ...interface{}) {
bddl(t, " Then", args...)
}
func And(t *testing.T, args ...interface{}) {
bddl(t, " and", args...)
}
func Comment(t *testing.T, comment string, args ...interface{}) {
if PrintColorsEnabled {
t.Log(append(append([]interface{}{}, fmt.Sprintf(" ")+color.Sprintf("@{b}%s", "Comment:"+" "+comment)), args...)...)
} else {
t.Log(append(append([]interface{}{}, "Comment:"+" "+comment), args...)...)
}
}
func TODO(t *testing.T, comment string, args ...interface{}) {
if PrintColorsEnabled {
t.Log(append(append([]interface{}{}, fmt.Sprintf(" ")+color.Sprintf("@{R}%s", "TODO:"+" "+comment)), args...)...)
} else {
t.Log(append(append([]interface{}{}, "Comment:"+" "+comment), args...)...)
}
}