Skip to content

Commit

Permalink
refactor: use official tag-expressions library (#8)
Browse files Browse the repository at this point in the history
There is now an official tag parsing library which we can use instead of
our custom one:
https://pkg.go.dev/github.com/cucumber/tag-expressions/go/v5. This
refactors gocuke to use that instead.
  • Loading branch information
aaronc authored Jan 12, 2023
1 parent 500df10 commit 0c2109f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 129 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/regen-network/gocuke
go 1.17

require (
github.com/alecthomas/participle/v2 v2.0.0-alpha7
github.com/cockroachdb/apd/v3 v3.1.0
github.com/cucumber/common/messages/go/v19 v19.1.2
github.com/cucumber/gherkin/go/v26 v26.0.3
github.com/cucumber/messages/go/v21 v21.0.1
github.com/cucumber/tag-expressions/go/v5 v5.0.1
github.com/google/go-cmp v0.5.5
github.com/stretchr/testify v1.8.1
gotest.tools/v3 v3.1.0
Expand All @@ -17,6 +17,7 @@ require (
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/uuid v4.2.0+incompatible // indirect
github.com/kr/text v0.2.0 // indirect
github.com/lib/pq v1.10.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
106 changes: 0 additions & 106 deletions internal/tag/expr.go

This file was deleted.

26 changes: 20 additions & 6 deletions internal/tag/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,31 @@ import messages "github.com/cucumber/messages/go/v21"
type Tags map[string]bool

func NewTags(tags ...string) Tags {
res := map[string]bool{}
for _, tag := range tags {
res[tag] = true
have := map[string]bool{}
var res []string
for _, t := range tags {
if !have[t] {
have[t] = true
res = append(res, t)
}
}
return res
}

func NewTagsFromPickleTags(pickleTags []*messages.PickleTag) Tags {
res := map[string]bool{}
for _, tag := range pickleTags {
res[tag.Name] = true
have := map[string]bool{}
var res []string
for _, t := range pickleTags {
if !have[t.Name] {
have[t.Name] = true
res = append(res, t.Name)
}
}
return res
}

type Tags []string

func (t Tags) Match(expr tag.Evaluatable) bool {
return expr.Evaluate(t)
}
6 changes: 3 additions & 3 deletions run_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ func (r *docRunner) runScenario(t *testing.T, pickle *messages.Pickle) {
t.Helper()

tags := tag.NewTagsFromPickleTags(pickle.Tags)
if r.tagExpr != nil && !r.tagExpr.Match(tags) {
if r.tagExpr != nil && !tags.Match(r.tagExpr) {
t.SkipNow()
}

if testing.Short() {
if r.shortTagExpr != nil && !r.shortTagExpr.Match(tags) {
if r.shortTagExpr != nil && !tags.Match(r.shortTagExpr) {
t.SkipNow()
}
}

if globalTagExpr != nil {
if !globalTagExpr.Match(tags) {
if !tags.Match(globalTagExpr) {
t.SkipNow()
}
}
Expand Down
4 changes: 2 additions & 2 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Runner struct {
beforeStepHooks []*stepDef
afterStepHooks []*stepDef
suiteUsesRapid bool
tagExpr *tag.Expr
shortTagExpr *tag.Expr
tagExpr tag.Evaluatable
shortTagExpr tag.Evaluatable
}

type suiteInjector struct {
Expand Down
13 changes: 7 additions & 6 deletions tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@ package gocuke
import (
"flag"
"fmt"
"github.com/regen-network/gocuke/internal/tag"

tag "github.com/cucumber/tag-expressions/go/v5"
)

var flagTags = flag.String("gocuke.tags", "",
"specify a cucumber tags expression to select tests to run (ex. 'not @long')")

var globalTagExpr *tag.Expr
var globalTagExpr tag.Evaluatable

func initGlobalTagExpr() *tag.Expr {
func initGlobalTagExpr() tag.Evaluatable {
if globalTagExpr == nil {
if flagTags != nil && *flagTags != "" {
var err error
globalTagExpr, err = tag.ParseExpr(*flagTags)
globalTagExpr, err = tag.Parse(*flagTags)
if err != nil {
if err != nil {
panic(fmt.Errorf("error parsing tag expression %q: %v\n", *flagTags, err))
Expand All @@ -32,7 +33,7 @@ func initGlobalTagExpr() *tag.Expr {
// in parentheses to allow expressions like "(@smoke or @ui) and (not @slow)".
func (r *Runner) Tags(tagExpr string) *Runner {
var err error
r.tagExpr, err = tag.ParseExpr(tagExpr)
r.tagExpr, err = tag.Parse(tagExpr)
if err != nil {
r.topLevelT.Fatalf("error parsing tag expression %s: %v", tagExpr, err)
}
Expand All @@ -44,7 +45,7 @@ func (r *Runner) Tags(tagExpr string) *Runner {
// any other tag expression that is applied with Tags() when running short tests.
func (r *Runner) ShortTags(tagExpr string) *Runner {
var err error
r.shortTagExpr, err = tag.ParseExpr(tagExpr)
r.shortTagExpr, err = tag.Parse(tagExpr)
if err != nil {
r.topLevelT.Fatalf("error parsing tag expression %s: %v", tagExpr, err)
}
Expand Down
13 changes: 8 additions & 5 deletions tags_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package gocuke

import (
"github.com/regen-network/gocuke/internal/tag"
"gotest.tools/v3/assert"
"strings"
"testing"

tagexpressions "github.com/cucumber/tag-expressions/go/v5"
"gotest.tools/v3/assert"

"github.com/regen-network/gocuke/internal/tag"
)

func TestTags(t *testing.T) {
Expand All @@ -13,7 +16,7 @@ func TestTags(t *testing.T) {

type tagsSuite struct {
TestingT
expr *tag.Expr
expr tagexpressions.Evaluatable
tags []string
}

Expand All @@ -31,7 +34,7 @@ func (s *tagsSuite) IEatOtherCukes() {

func (s *tagsSuite) TheTagExpression(a DocString) {
var err error
s.expr, err = tag.ParseExpr(a.Content)
s.expr, err = tagexpressions.Parse(a.Content)
assert.NilError(s, err, a.Content)
}

Expand All @@ -40,7 +43,7 @@ func (s *tagsSuite) IMatch(a string) {
}

func (s *tagsSuite) TheResultIs(a string) {
res := s.expr.Match(tag.NewTags(s.tags...))
res := tag.NewTags(s.tags...).Match(s.expr)
switch a {
case "true":
assert.Assert(s, res)
Expand Down

0 comments on commit 0c2109f

Please sign in to comment.