Skip to content

Commit 150c400

Browse files
committed
Created internal packages for formatters, storage and models
1 parent 28ad994 commit 150c400

File tree

105 files changed

+1493
-872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1493
-872
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ commands:
3636
go_test:
3737
description: "Run go test"
3838
steps:
39-
- run: sed -i 's#github.com/cucumber/godog_test#_test#g' formatter-tests/*/*
39+
- run: sed -i 's#github.com/cucumber/godog/internal/formatters_test#/internal/formatters_test#g' internal/formatters/formatter-tests/*/*
4040
- run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
4141
godog:
4242
description: "Run godog"

flags.go

+4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import (
1010
"time"
1111

1212
"github.com/cucumber/godog/colors"
13+
"github.com/cucumber/godog/internal/utils"
1314
)
1415

16+
// repeats a space n times
17+
var s = utils.S
18+
1519
var descFeaturesArgument = "Optional feature(s) to run. Can be:\n" +
1620
s(4) + "- dir " + colors.Yellow("(features/)") + "\n" +
1721
s(4) + "- feature " + colors.Yellow("(*.feature)") + "\n" +

flags_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/cucumber/godog/colors"
11+
"github.com/cucumber/godog/internal/formatters"
1112
)
1213

1314
func TestFlagsShouldRandomizeAndGenerateSeed(t *testing.T) {
@@ -61,7 +62,7 @@ func TestFlagsUsageShouldIncludeFormatDescriptons(t *testing.T) {
6162
output := colors.Uncolored(&buf)
6263

6364
// register some custom formatter
64-
Format("custom", "custom format description", junitFunc)
65+
Format("custom", "custom format description", formatters.JUnitFormatterFunc)
6566

6667
var opt Options
6768
flags := FlagSet(&opt)

fmt.go

+16-49
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,33 @@ import (
66
"strings"
77
"unicode/utf8"
88

9-
"github.com/cucumber/messages-go/v10"
9+
"github.com/cucumber/godog/colors"
10+
"github.com/cucumber/godog/formatters"
11+
internal_fmt "github.com/cucumber/godog/internal/formatters"
12+
"github.com/cucumber/godog/internal/models"
13+
"github.com/cucumber/godog/internal/storage"
1014
)
1115

12-
type registeredFormatter struct {
13-
name string
14-
description string
15-
fmt FormatterFunc
16-
}
17-
18-
var formatters []*registeredFormatter
19-
2016
// FindFmt searches available formatters registered
2117
// and returns FormaterFunc matched by given
2218
// format name or nil otherwise
2319
func FindFmt(name string) FormatterFunc {
24-
for _, el := range formatters {
25-
if el.name == name {
26-
return el.fmt
27-
}
28-
}
29-
30-
return nil
20+
return formatters.FindFmt(name)
3121
}
3222

3323
// Format registers a feature suite output
3424
// formatter by given name, description and
3525
// FormatterFunc constructor function, to initialize
3626
// formatter with the output recorder.
3727
func Format(name, description string, f FormatterFunc) {
38-
formatters = append(formatters, &registeredFormatter{
39-
name: name,
40-
fmt: f,
41-
description: description,
42-
})
28+
formatters.Format(name, description, f)
4329
}
4430

4531
// AvailableFormatters gives a map of all
4632
// formatters registered with their name as key
4733
// and description as value
4834
func AvailableFormatters() map[string]string {
49-
fmts := make(map[string]string, len(formatters))
50-
51-
for _, f := range formatters {
52-
fmts[f.name] = f.description
53-
}
54-
55-
return fmts
35+
return formatters.AvailableFormatters()
5636
}
5737

5838
// Formatter is an interface for feature runner
@@ -62,32 +42,17 @@ func AvailableFormatters() map[string]string {
6242
// suite results in different ways. These new
6343
// formatters needs to be registered with a
6444
// godog.Format function call
65-
type Formatter interface {
66-
TestRunStarted()
67-
Feature(*messages.GherkinDocument, string, []byte)
68-
Pickle(*messages.Pickle)
69-
Defined(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition)
70-
Failed(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition, error)
71-
Passed(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition)
72-
Skipped(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition)
73-
Undefined(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition)
74-
Pending(*messages.Pickle, *messages.Pickle_PickleStep, *StepDefinition)
75-
Summary()
76-
}
45+
type Formatter = formatters.Formatter
7746

7847
type storageFormatter interface {
79-
setStorage(*storage)
48+
SetStorage(*storage.Storage)
8049
}
8150

8251
// FormatterFunc builds a formatter with given
8352
// suite name and io.Writer to record output
84-
type FormatterFunc func(string, io.Writer) Formatter
85-
86-
func isLastStep(pickle *messages.Pickle, step *messages.Pickle_PickleStep) bool {
87-
return pickle.Steps[len(pickle.Steps)-1].Id == step.Id
88-
}
53+
type FormatterFunc = formatters.FormatterFunc
8954

90-
func printStepDefinitions(steps []*StepDefinition, w io.Writer) {
55+
func printStepDefinitions(steps []*models.StepDefinition, w io.Writer) {
9156
var longest int
9257
for _, def := range steps {
9358
n := utf8.RuneCountInString(def.Expr.String())
@@ -98,9 +63,11 @@ func printStepDefinitions(steps []*StepDefinition, w io.Writer) {
9863

9964
for _, def := range steps {
10065
n := utf8.RuneCountInString(def.Expr.String())
101-
location := def.definitionID()
66+
location := internal_fmt.DefinitionID(def)
10267
spaces := strings.Repeat(" ", longest-n)
103-
fmt.Fprintln(w, yellow(def.Expr.String())+spaces, blackb("# "+location))
68+
fmt.Fprintln(w,
69+
colors.Yellow(def.Expr.String())+spaces,
70+
colors.Bold(colors.Black)("# "+location))
10471
}
10572

10673
if len(steps) == 0 {

formatters/fmt.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package formatters
2+
3+
import (
4+
"io"
5+
6+
"github.com/cucumber/messages-go/v10"
7+
8+
"github.com/cucumber/godog/internal/models"
9+
)
10+
11+
type registeredFormatter struct {
12+
name string
13+
description string
14+
fmt FormatterFunc
15+
}
16+
17+
var registeredFormatters []*registeredFormatter
18+
19+
// FindFmt searches available formatters registered
20+
// and returns FormaterFunc matched by given
21+
// format name or nil otherwise
22+
func FindFmt(name string) FormatterFunc {
23+
for _, el := range registeredFormatters {
24+
if el.name == name {
25+
return el.fmt
26+
}
27+
}
28+
29+
return nil
30+
}
31+
32+
// Format registers a feature suite output
33+
// formatter by given name, description and
34+
// FormatterFunc constructor function, to initialize
35+
// formatter with the output recorder.
36+
func Format(name, description string, f FormatterFunc) {
37+
registeredFormatters = append(registeredFormatters, &registeredFormatter{
38+
name: name,
39+
fmt: f,
40+
description: description,
41+
})
42+
}
43+
44+
// AvailableFormatters gives a map of all
45+
// formatters registered with their name as key
46+
// and description as value
47+
func AvailableFormatters() map[string]string {
48+
fmts := make(map[string]string, len(registeredFormatters))
49+
50+
for _, f := range registeredFormatters {
51+
fmts[f.name] = f.description
52+
}
53+
54+
return fmts
55+
}
56+
57+
// Formatter is an interface for feature runner
58+
// output summary presentation.
59+
//
60+
// New formatters may be created to represent
61+
// suite results in different ways. These new
62+
// formatters needs to be registered with a
63+
// godog.Format function call
64+
type Formatter interface {
65+
TestRunStarted()
66+
Feature(*messages.GherkinDocument, string, []byte)
67+
Pickle(*messages.Pickle)
68+
Defined(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition)
69+
Failed(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition, error)
70+
Passed(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition)
71+
Skipped(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition)
72+
Undefined(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition)
73+
Pending(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition)
74+
Summary()
75+
}
76+
77+
// FormatterFunc builds a formatter with given
78+
// suite name and io.Writer to record output
79+
type FormatterFunc func(string, io.Writer) Formatter

formatters/fmt_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package formatters_test
2+
3+
import (
4+
"io"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/cucumber/godog"
11+
)
12+
13+
func Test_FindFmt(t *testing.T) {
14+
cases := map[string]bool{
15+
"cucumber": true,
16+
"events": true,
17+
"junit": true,
18+
"pretty": true,
19+
"progress": true,
20+
"unknown": false,
21+
"undef": false,
22+
}
23+
24+
for name, expected := range cases {
25+
t.Run(
26+
name,
27+
func(t *testing.T) {
28+
actual := godog.FindFmt(name)
29+
30+
if expected {
31+
assert.NotNilf(t, actual, "expected %s formatter should be available", name)
32+
} else {
33+
assert.Nilf(t, actual, "expected %s formatter should be available", name)
34+
}
35+
},
36+
)
37+
}
38+
}
39+
40+
func Test_AvailableFormatters(t *testing.T) {
41+
expected := map[string]string{
42+
"cucumber": "Produces cucumber JSON format output.",
43+
"events": "Produces JSON event stream, based on spec: 0.1.0.",
44+
"junit": "Prints junit compatible xml to stdout",
45+
"pretty": "Prints every feature with runtime statuses.",
46+
"progress": "Prints a character per step.",
47+
}
48+
49+
actual := godog.AvailableFormatters()
50+
assert.Equal(t, expected, actual)
51+
}
52+
53+
func Test_Format(t *testing.T) {
54+
actual := godog.FindFmt("Test_Format")
55+
require.Nil(t, actual)
56+
57+
godog.Format("Test_Format", "...", testFormatterFunc)
58+
actual = godog.FindFmt("Test_Format")
59+
60+
assert.NotNil(t, actual)
61+
}
62+
63+
func testFormatterFunc(suiteName string, out io.Writer) godog.Formatter {
64+
return nil
65+
}

0 commit comments

Comments
 (0)