forked from cucumber/godog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatters_print_test.go
81 lines (65 loc) · 1.98 KB
/
formatters_print_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
package godog
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
)
func TestPrintingFormatters(t *testing.T) {
features, err := parseFeatures("", []string{"formatter-tests"})
if err != nil {
t.Fatalf("failed to parse formatter features: %v", err)
}
var buf bytes.Buffer
out := &tagColorWriter{w: &buf}
suite := &Suite{
features: features,
}
// inlining steps to have same source code line reference
suite.Step(`^(?:a )?failing step`, failingStepDef)
suite.Step(`^(?:a )?pending step$`, pendingStepDef)
suite.Step(`^(?:a )?passing step$`, passingStepDef)
suite.Step(`^odd (\d+) and even (\d+) number$`, oddEvenStepDef)
pkg := os.Getenv("GODOG_TESTED_PACKAGE")
os.Setenv("GODOG_TESTED_PACKAGE", "github.com/DATA-DOG/godog")
for _, feat := range features {
for name := range AvailableFormatters() {
expectOutputPath := strings.Replace(feat.Path, "features", name, 1)
expectOutputPath = strings.TrimSuffix(expectOutputPath, path.Ext(expectOutputPath))
if _, err := os.Stat(expectOutputPath); err != nil {
continue
}
buf.Reset() // flush the output
suite.fmt = FindFmt(name)(name, out) // prepare formatter
suite.features = []*feature{feat} // set the feature
expectedOutput, err := ioutil.ReadFile(expectOutputPath)
if err != nil {
t.Fatal(err)
}
suite.run()
suite.fmt.Summary()
expected := string(expectedOutput)
actual := buf.String()
if actual != expected {
t.Fatalf("%s does not match to:\n%s", expectOutputPath, actual)
}
}
}
os.Setenv("GODOG_TESTED_PACKAGE", pkg)
}
func passingStepDef() error { return nil }
func oddEvenStepDef(odd, even int) error { return oddOrEven(odd, even) }
func oddOrEven(odd, even int) error {
if odd%2 == 0 {
return fmt.Errorf("%d is not odd", odd)
}
if even%2 != 0 {
return fmt.Errorf("%d is not even", even)
}
return nil
}
func pendingStepDef() error { return ErrPending }
func failingStepDef() error { return fmt.Errorf("step failed") }