Skip to content

Commit

Permalink
matched step func name and reference in pretty printer
Browse files Browse the repository at this point in the history
  • Loading branch information
l3pp4rd committed May 28, 2016
1 parent f9fab51 commit f9eab9d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 8 deletions.
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.PHONY: test gherkin
.PHONY: test gherkin bump

VERS := $(shell grep 'const Version' -m 1 godog.go | awk -F\" '{print $$2}')

test:
@echo "running all tests"
Expand All @@ -16,3 +18,10 @@ gherkin:
@mkdir gherkin
@curl -s -L https://github.com/cucumber/gherkin-go/tarball/$(VERS) | tar -C gherkin -zx --strip-components 1
@rm -rf gherkin/{.travis.yml,.gitignore,*_test.go,gherkin-generate*,*.razor,*.jq,Makefile,CONTRIBUTING.md}

bump:
@if [ -z "$(VERSION)" ]; then echo "Provide version like: 'VERSION=$(VERS) make bump'"; exit 1; fi
@echo "bumping version from: $(VERS) to $(VERSION)"
@sed -i.bak 's/$(VERS)/$(VERSION)/g' godog.go
@sed -i.bak 's/$(VERS)/$(VERSION)/g' examples/api/version.feature
@find . -name '*.bak' | xargs rm
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ See implementation examples:

### Changes

**2016-05-28**
- show nicely formatted called step func name and file path

**2016-05-26**
- pack gherkin dependency in a subpackage to prevent compatibility
conflicts.
conflicts in the future. If recently upgraded, probably you will need to
reference gherkin as `github.com/DATA-DOG/godog/gherkin` instead.

**2016-05-25**
- refactored test suite build tooling in order to use standard **go test**
Expand Down
2 changes: 1 addition & 1 deletion examples/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ package main

import (
"github.com/DATA-DOG/godog"
"gopkg.in/cucumber/gherkin-go.v3"
"github.com/DATA-DOG/godog/gherkin"
)

type apiFeature struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"net/http/httptest"

"github.com/DATA-DOG/godog"
"gopkg.in/cucumber/gherkin-go.v3"
"github.com/DATA-DOG/godog/gherkin"
)

type apiFeature struct {
Expand Down
2 changes: 1 addition & 1 deletion examples/api/version.feature
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Feature: get version
And the response should match json:
"""
{
"version": "v0.2.0"
"version": "v0.4.2"
}
"""
3 changes: 2 additions & 1 deletion examples/db/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"net/http/httptest"
"strings"

"github.com/DATA-DOG/go-txdb"
"github.com/DATA-DOG/godog"
"gopkg.in/cucumber/gherkin-go.v3"
"github.com/DATA-DOG/godog/gherkin"
)

func init() {
Expand Down
2 changes: 1 addition & 1 deletion godog.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ Godog was inspired by Behat and the above description is taken from it's documen
package godog

// Version of package - based on Semantic Versioning 2.0.0 http://semver.org/
const Version = "v0.4.0"
const Version = "v0.4.2"
25 changes: 24 additions & 1 deletion stepdef.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ package godog

import (
"fmt"
"path/filepath"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/DATA-DOG/godog/gherkin"
)

var matchFuncDefRef = regexp.MustCompile(`\(([^\)]+)\)`)

// StepDef is a registered step definition
// contains a StepHandler and regexp which
// is used to match a step. Args which
Expand All @@ -26,7 +30,25 @@ type StepDef struct {
}

func (sd *StepDef) funcName() string {
return runtime.FuncForPC(sd.hv.Pointer()).Name()
ptr := sd.hv.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, "_.")
}

return fmt.Sprintf("%s:%d -> %s", filepath.Base(file), line, fn)
}

// run a step with the matched arguments using
Expand Down Expand Up @@ -153,6 +175,7 @@ func (sd *StepDef) run() error {
if nil == ret {
return nil
}

return ret.(error)
}

Expand Down

0 comments on commit f9eab9d

Please sign in to comment.