Skip to content

Commit

Permalink
fix: cannot have multiple parameter type on the same line (#147) (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
vlalanne authored Nov 23, 2023
1 parent 2911ec3 commit 625de17
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 27 deletions.
10 changes: 5 additions & 5 deletions docs/parameter-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ GoBDD has support for [parameter types](https://cucumber.io/docs/cucumber/cucumb
* `{int}` - integer (-1 or 56)
* `{float}` - float (0.4 or 234.4)
* `{word}` - single word (`hello` or `pizza`)
* `{text}` - single-quoted or double-quoted strings (`'I like pizza'` or `"I like pizza"`)
* `{text}` - single-quoted or double-quoted strings (`'I like pizza!'` or `"I like pizza!"`)

You can add your own parameter types using `AddParameterTypes()` function. Here are a few examples

```go
s := gobdd.NewSuite(t)
s.AddParameterTypes(`{int}`, []string{`(\d)`})
s.AddParameterTypes(`{float}`, []string{`([-+]?\d*\.?\d*)`})
s.AddParameterTypes(`{word}`, []string{`([\d\w]+)`})
s.AddParameterTypes(`{text}`, []string{`"([\d\w\-\s]+)"`, `'([\d\w\-\s]+)'`})
s.AddParameterTypes(`{int}`, []string{`(-?\d+)`})
s.AddParameterTypes(`{float}`, []string{`([-+]?\d*\.?\d+)`})
s.AddParameterTypes(`{word}`, []string{`([^\s]+)`})
s.AddParameterTypes(`{text}`, []string{`"([^"\\]*(?:\\.[^"\\]*)*)"`, `'([^'\\]*(?:\\.[^'\\]*)*)'`})
```

The first argument accepts the parameter types. As the second parameter provides list of regular expressions that should replace the parameter.
Expand Down
19 changes: 15 additions & 4 deletions features/parameter-types.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,20 @@ Feature: parameter types
Scenario: simple word
When I use word pizza
Scenario: simple text with double quotes
When I use text "I like pizza"
When I use text "I like pizza!"
Then the result should equal text 'I like pizza!'
Scenario: simple text with single quotes
When I use text 'I like pizza'
When I use text 'I like pizza!'
Then the result should equal text 'I like pizza!'
Scenario: add two floats
When I add floats 1 and 2
Then the result should equal float 3
When I add floats -1.2 and 2.4
Then the result should equal float 1.2
Scenario: concat a word and a text with single quotes
When I concat word Hello and text ' World!'
Then the result should equal text 'Hello World!'
Scenario: concat a word and a text with double quotes
When I concat word Hello and text " World!"
Then the result should equal text "Hello World!"
Scenario: format text
When I format text "counter %d" with int -12
Then the result should equal text "counter -12"
29 changes: 15 additions & 14 deletions gobdd.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ func NewSuite(t TestingT, optionClosures ...func(*SuiteOptions)) *Suite {
parameterTypes: map[string][]string{},
}

s.AddParameterTypes(`{int}`, []string{`(\d)`})
s.AddParameterTypes(`{float}`, []string{`([-+]?\d*\.?\d*)`})
s.AddParameterTypes(`{word}`, []string{`([\d\w]+)`})
s.AddParameterTypes(`{text}`, []string{`"([\d\w\-\s]+)"`, `'([\d\w\-\s]+)'`})
// see https://github.com/cucumber/cucumber-expressions/blob/main/go/parameter_type_registry.go
s.AddParameterTypes(`{int}`, []string{`(-?\d+)`})
s.AddParameterTypes(`{float}`, []string{`([-+]?\d*\.?\d+)`})
s.AddParameterTypes(`{word}`, []string{`([^\s]+)`})
s.AddParameterTypes(`{text}`, []string{`"([^"\\]*(?:\\.[^"\\]*)*)"`, `'([^'\\]*(?:\\.[^'\\]*)*)'`})

return s
}
Expand All @@ -206,7 +207,7 @@ func NewSuite(t TestingT, optionClosures ...func(*SuiteOptions)) *Suite {
// The first argument is the parameter type and the second parameter is a list of regular expressions
// that should replace the parameter type.
//
// s.AddParameterTypes(`{int}`, []string{`(\d)`})
// s.AddParameterTypes(`{int}`, []string{`(\d)`})
//
// The regular expression should compile, otherwise will produce an error and stop executing.
func (s *Suite) AddParameterTypes(from string, to []string) {
Expand All @@ -228,8 +229,8 @@ func (s *Suite) AddParameterTypes(from string, to []string) {
// A step function can have any number of parameters (even zero),
// but it MUST accept a gobdd.StepTest and gobdd.Context as the first parameters (if there is any):
//
// func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
// }
// func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
// }
func (s *Suite) AddStep(expr string, step interface{}) {
err := validateStepFunc(step)
if err != nil {
Expand Down Expand Up @@ -263,7 +264,7 @@ func (s *Suite) applyParameterTypes(expr string) []string {
for from, to := range s.parameterTypes {
for _, t := range to {
if strings.Contains(expr, from) {
exprs = append(exprs, strings.ReplaceAll(expr, from, t))
exprs = append(exprs, s.applyParameterTypes(strings.ReplaceAll(expr, from, t))...)
}
}
}
Expand All @@ -279,8 +280,8 @@ func (s *Suite) applyParameterTypes(expr string) []string {
// A step function can have any number of parameters (even zero),
// but it MUST accept a gobdd.StepTest and gobdd.Context as the first parameters (if there is any):
//
// func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
// }
// func myStepFunction(t gobdd.StepTest, ctx gobdd.Context, first int, second int) {
// }
func (s *Suite) AddRegexStep(expr *regexp.Regexp, step interface{}) {
err := validateStepFunc(step)
if err != nil {
Expand Down Expand Up @@ -322,14 +323,14 @@ func (s *Suite) Run() {
}

func (s *Suite) executeFeature(feature feature) error {
f, err := feature.Open()
f, err := feature.Open()
if err != nil {
return err
}

if closer, ok := f.(io.Closer); ok {
defer closer.Close()
}
if closer, ok := f.(io.Closer); ok {
defer closer.Close()
}

featureIO := bufio.NewReader(f)

Expand Down
28 changes: 24 additions & 4 deletions gobdd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,18 @@ func TestParameterTypes(t *testing.T) {
suite.AddStep(`the result should equal {int}`, check)
suite.AddStep(`I add floats {float} and {float}`, addf)
suite.AddStep(`the result should equal float {float}`, checkf)
suite.AddStep(`the result should equal text {text}`, checkt)
suite.AddStep(`I use word {word}`, func(t StepTest, ctx Context, word string) {
if word != "pizza" {
t.Fatal("it should be pizza")
}
})
suite.AddStep(`I use text {text}`, func(t StepTest, ctx Context, text string) {
if text != "I like pizza" {
t.Fatal("it should say that I like pizza")
}
ctx.Set("stringRes", text)
})
suite.AddStep(`I concat word {word} and text {text}`, concat)
suite.AddStep(`I format text {text} with int {int}`, func(t StepTest, ctx Context, format string, value int) {
ctx.Set("stringRes", fmt.Sprintf(format, value))
})

suite.Run()
Expand Down Expand Up @@ -275,6 +278,23 @@ func add(_ StepTest, ctx Context, var1, var2 int) {
ctx.Set("sumRes", res)
}

func concat(_ StepTest, ctx Context, var1, var2 string) {
ctx.Set("stringRes", var1+var2)
}

func checkt(t StepTest, ctx Context, text string) {
received, err := ctx.GetString("stringRes")
if err != nil {
t.Error(err.Error())

return
}

if text != received {
t.Errorf("expected %s but %s received", text, received)
}
}

func checkf(t StepTest, ctx Context, sum float32) {
received, err := ctx.Get("sumRes")
if err != nil {
Expand All @@ -284,7 +304,7 @@ func checkf(t StepTest, ctx Context, sum float32) {
}

if sum != received {
t.Error("the sum doesn't match")
t.Errorf("expected %f but %f received", sum, received)
}
}

Expand Down

0 comments on commit 625de17

Please sign in to comment.