/
when.go
47 lines (39 loc) · 990 Bytes
/
when.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
package examples
import (
"flag"
"testing"
)
var filter string
func init() {
flag.StringVar(&filter, "examples.run", "", "define a filter for example cases")
flag.Parse()
}
type Harness interface {
Log(...interface{})
FailNow()
}
func Describe(description string, harness Harness, results ...result) {
exampleBlock(description, harness, results)
}
func When(description string, harness Harness, results ...result) {
exampleBlock(description, harness, results)
}
func exampleBlock(description string, harness Harness, results []result) {
failed := false
for _, result := range results {
if !result.Skip && result.Failed {
harness.Log("When " + description + " " + result.Description)
failed = true
} else if testing.Verbose() && !result.Failed && !result.Skip {
harness.Log("PASS: When " + description + " " + result.Description)
}
}
if failed {
harness.FailNow()
}
}
type result struct {
Description string
Failed bool
Skip bool
}