-
Notifications
You must be signed in to change notification settings - Fork 3
/
mspec.go
110 lines (92 loc) · 2.81 KB
/
mspec.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
package mspec
import (
"strings"
"github.com/eduncan911/go-mspec/colors"
)
var config *MSpecConfig
// MSpecConfig defines the configuration used by the package.
type MSpecConfig struct {
output outputType
AnsiOfFeature string
AnsiOfGiven string
AnsiOfWhen string
AnsiOfThen string
AnsiOfThenNotImplemented string
AnsiOfThenWithError string
AnsiOfCode string
AnsiOfCodeError string
AnsiOfExpectedError string
assertFn func(*Specification) Assert
lastFeature string
lastGiven string
lastWhen string
lastSpec string
}
func init() {
ResetConfig()
// set to verbose output by default
SetVerbose()
// register the default Assertions package
AssertionsFn(func(s *Specification) Assert {
return newAssertions(s)
})
}
// AssertionsFn will assign the assertions used for all tests.
// The specified struct must implement the mspec.Assert interface.
//
// mspec.AssertionsFn(func(s *Specification) Assert {
// return &MyCustomAssertions{}
// })
func AssertionsFn(fn func(s *Specification) Assert) {
config.assertFn = fn
}
// SetConfig takes a Config instance and will be used for all tests
// until ResetConfig() is called.
//
// mspec.SetConfig(Config{
// AnsiOfFeature: "", // remove color coding for Feature
// })
//
func SetConfig(c MSpecConfig) {
config = &c
}
// ResetConfig will reset all options back to their default configuration.
// Useful for custom colors in the middle of a specification.
func ResetConfig() {
// setup a default configuration
config = &MSpecConfig{
AnsiOfFeature: strings.Join([]string{colors.White}, ""),
AnsiOfGiven: strings.Join([]string{colors.Grey}, ""),
AnsiOfWhen: strings.Join([]string{colors.LightGreen}, ""),
AnsiOfThen: strings.Join([]string{colors.Green}, ""),
AnsiOfThenNotImplemented: strings.Join([]string{colors.LightYellow}, ""),
AnsiOfThenWithError: strings.Join([]string{colors.RegBg, colors.White, colors.Bold}, ""),
AnsiOfCode: strings.Join([]string{colors.Grey}, ""),
AnsiOfCodeError: strings.Join([]string{colors.White, colors.Bold}, ""),
AnsiOfExpectedError: strings.Join([]string{colors.Red}, ""),
}
}
// SetVerbose is used to set the output to Stdout (default).
// Do not use this at this time. The package API
// will most likely change.
func SetVerbose() {
config.output = outputStdout
}
// SetSilent is used to make all output silent.
// Do not use this at this time. The package API
// will most likely change.
func SetSilent() {
config.output = outputNone
}
type outputType int
const (
outputNone outputType = 1 << iota
outputStdout
outputStderr
outputHTML
)
func (c *MSpecConfig) resetLasts() {
c.lastGiven = ""
c.lastWhen = ""
c.lastSpec = ""
}