Skip to content

Commit fc9b7d7

Browse files
authored
go: Prefer the longest keywords (#404)
Some langauges have two different step keywords starting with the same characters. In cases like these, we need to ensure that if a user types out the longer of these keywords, that is what gets matched. This is done by sorting the keywords for each dialect. As well as this, I've added handling to make sure that the sorting is only done once per dialect to make it inline with other fixes. This required a couple of fields to be added to the `Dialect` struct, so I've updated the `builtin_dialects.go.jq` file to built the `Dialect` struct in a way that allows zero base instantiation.
1 parent 5ed2972 commit fc9b7d7

File tree

4 files changed

+591
-251
lines changed

4 files changed

+591
-251
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
1313
- [Java] Prefer the longest step keyword ([#401](https://github.com/cucumber/gherkin/pull/401))
1414
- [.NET] Prefer the longest step keyword ([#405](https://github.com/cucumber/gherkin/pull/405))
1515
- [PHP] Prefer the longest step keyword ([#403](https://github.com/cucumber/gherkin/pull/403))
16+
- [Go] Prefer the longest step keyword ([#403](https://github.com/cucumber/gherkin/pull/404))
1617

1718
## [32.1.1] - 2025-04-11
1819
### Fixed

go/dialect.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,34 @@
11
package gherkin
22

3-
import messages "github.com/cucumber/messages/go/v24"
3+
import (
4+
"sort"
5+
6+
messages "github.com/cucumber/messages/go/v24"
7+
)
8+
9+
func init() {
10+
// only needs to be calcuated one, on startup
11+
for _, d := range builtinDialects {
12+
d.stepKeywords = []string{}
13+
d.stepKeywords = append(d.stepKeywords, d.Keywords[given]...)
14+
d.stepKeywords = append(d.stepKeywords, d.Keywords[when]...)
15+
d.stepKeywords = append(d.stepKeywords, d.Keywords[then]...)
16+
d.stepKeywords = append(d.stepKeywords, d.Keywords[and]...)
17+
d.stepKeywords = append(d.stepKeywords, d.Keywords[but]...)
18+
sort.Slice(d.stepKeywords, func(i, j int) bool {
19+
return d.stepKeywords[i] > d.stepKeywords[j]
20+
})
21+
}
22+
}
423

524
type Dialect struct {
625
Language string
726
Name string
827
Native string
928
Keywords map[string][]string
1029
KeywordTypes map[string]messages.StepKeywordType
30+
31+
stepKeywords []string
1132
}
1233

1334
func (g *Dialect) FeatureKeywords() []string {
@@ -23,12 +44,7 @@ func (g *Dialect) ScenarioKeywords() []string {
2344
}
2445

2546
func (g *Dialect) StepKeywords() []string {
26-
result := g.Keywords["given"]
27-
result = append(result, g.Keywords["when"]...)
28-
result = append(result, g.Keywords["then"]...)
29-
result = append(result, g.Keywords["and"]...)
30-
result = append(result, g.Keywords["but"]...)
31-
return result
47+
return g.stepKeywords
3248
}
3349

3450
func (g *Dialect) BackgroundKeywords() []string {

0 commit comments

Comments
 (0)