-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created internal packages for formatters, storage and models
- Loading branch information
Showing
31 changed files
with
991 additions
and
769 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package formatters | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/cucumber/messages-go/v10" | ||
|
||
"github.com/cucumber/godog/internal/models" | ||
) | ||
|
||
type registeredFormatter struct { | ||
name string | ||
description string | ||
fmt FormatterFunc | ||
} | ||
|
||
var registeredFormatters []*registeredFormatter | ||
|
||
// FindFmt searches available formatters registered | ||
// and returns FormaterFunc matched by given | ||
// format name or nil otherwise | ||
func FindFmt(name string) FormatterFunc { | ||
for _, el := range registeredFormatters { | ||
if el.name == name { | ||
return el.fmt | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Format registers a feature suite output | ||
// formatter by given name, description and | ||
// FormatterFunc constructor function, to initialize | ||
// formatter with the output recorder. | ||
func Format(name, description string, f FormatterFunc) { | ||
registeredFormatters = append(registeredFormatters, ®isteredFormatter{ | ||
name: name, | ||
fmt: f, | ||
description: description, | ||
}) | ||
} | ||
|
||
// AvailableFormatters gives a map of all | ||
// formatters registered with their name as key | ||
// and description as value | ||
func AvailableFormatters() map[string]string { | ||
fmts := make(map[string]string, len(registeredFormatters)) | ||
|
||
for _, f := range registeredFormatters { | ||
fmts[f.name] = f.description | ||
} | ||
|
||
return fmts | ||
} | ||
|
||
// Formatter is an interface for feature runner | ||
// output summary presentation. | ||
// | ||
// New formatters may be created to represent | ||
// suite results in different ways. These new | ||
// formatters needs to be registered with a | ||
// godog.Format function call | ||
type Formatter interface { | ||
TestRunStarted() | ||
Feature(*messages.GherkinDocument, string, []byte) | ||
Pickle(*messages.Pickle) | ||
Defined(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition) | ||
Failed(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition, error) | ||
Passed(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition) | ||
Skipped(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition) | ||
Undefined(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition) | ||
Pending(*messages.Pickle, *messages.Pickle_PickleStep, *models.StepDefinition) | ||
Summary() | ||
} | ||
|
||
// FormatterFunc builds a formatter with given | ||
// suite name and io.Writer to record output | ||
type FormatterFunc func(string, io.Writer) Formatter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package formatters | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"runtime" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/cucumber/godog/colors" | ||
"github.com/cucumber/godog/internal/models" | ||
"github.com/cucumber/godog/internal/utils" | ||
"github.com/cucumber/messages-go/v10" | ||
) | ||
|
||
var ( | ||
red = colors.Red | ||
redb = colors.Bold(colors.Red) | ||
green = colors.Green | ||
blackb = colors.Bold(colors.Black) | ||
yellow = colors.Yellow | ||
cyan = colors.Cyan | ||
cyanb = colors.Bold(colors.Cyan) | ||
whiteb = colors.Bold(colors.White) | ||
) | ||
|
||
// repeats a space n times | ||
var s = utils.S | ||
|
||
var ( | ||
passed = models.Passed | ||
failed = models.Failed | ||
skipped = models.Skipped | ||
undefined = models.Undefined | ||
pending = models.Pending | ||
) | ||
|
||
type sortFeaturesByName []*models.Feature | ||
|
||
func (s sortFeaturesByName) Len() int { return len(s) } | ||
func (s sortFeaturesByName) Less(i, j int) bool { return s[i].Feature.Name < s[j].Feature.Name } | ||
func (s sortFeaturesByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
|
||
type sortPicklesByID []*messages.Pickle | ||
|
||
func (s sortPicklesByID) Len() int { return len(s) } | ||
func (s sortPicklesByID) Less(i, j int) bool { | ||
iID := mustConvertStringToInt(s[i].Id) | ||
jID := mustConvertStringToInt(s[j].Id) | ||
return iID < jID | ||
} | ||
func (s sortPicklesByID) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
|
||
type sortPickleStepResultsByPickleStepID []models.PickleStepResult | ||
|
||
func (s sortPickleStepResultsByPickleStepID) Len() int { return len(s) } | ||
func (s sortPickleStepResultsByPickleStepID) Less(i, j int) bool { | ||
iID := mustConvertStringToInt(s[i].PickleStepID) | ||
jID := mustConvertStringToInt(s[j].PickleStepID) | ||
return iID < jID | ||
} | ||
func (s sortPickleStepResultsByPickleStepID) Swap(i, j int) { s[i], s[j] = s[j], s[i] } | ||
|
||
func mustConvertStringToInt(s string) int { | ||
i, err := strconv.Atoi(s) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return i | ||
} | ||
|
||
// DefinitionID ... | ||
func DefinitionID(sd *models.StepDefinition) string { | ||
ptr := sd.HandlerValue.Pointer() | ||
f := runtime.FuncForPC(ptr) | ||
file, line := f.FileLine(ptr) | ||
dir := filepath.Dir(file) | ||
|
||
fn := strings.Replace(f.Name(), dir, "", -1) | ||
var parts []string | ||
for _, gr := range matchFuncDefRef.FindAllStringSubmatch(fn, -1) { | ||
parts = append(parts, strings.Trim(gr[1], "_.")) | ||
} | ||
if len(parts) > 0 { | ||
// case when suite is a structure with methods | ||
fn = strings.Join(parts, ".") | ||
} else { | ||
// case when steps are just plain funcs | ||
fn = strings.Trim(fn, "_.") | ||
} | ||
|
||
if pkg := os.Getenv("GODOG_TESTED_PACKAGE"); len(pkg) > 0 { | ||
fn = strings.Replace(fn, pkg, "", 1) | ||
fn = strings.TrimLeft(fn, ".") | ||
fn = strings.Replace(fn, "..", ".", -1) | ||
} | ||
|
||
return fmt.Sprintf("%s:%d -> %s", filepath.Base(file), line, fn) | ||
} | ||
|
||
var matchFuncDefRef = regexp.MustCompile(`\(([^\)]+)\)`) |
Oops, something went wrong.