-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from cucumber/internal-parser-pkg
Added an internal package for tags filtering
- Loading branch information
Showing
7 changed files
with
124 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package tags | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/cucumber/messages-go/v10" | ||
) | ||
|
||
// ApplyTagFilter will apply a filter string on the | ||
// array of pickles and returned the filtered list. | ||
func ApplyTagFilter(filter string, pickles []*messages.Pickle) []*messages.Pickle { | ||
if filter == "" { | ||
return pickles | ||
} | ||
|
||
var result = []*messages.Pickle{} | ||
|
||
for _, pickle := range pickles { | ||
if match(filter, pickle.Tags) { | ||
result = append(result, pickle) | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
// Based on http://behat.readthedocs.org/en/v2.5/guides/6.cli.html#gherkin-filters | ||
func match(filter string, tags []*messages.Pickle_PickleTag) (ok bool) { | ||
ok = true | ||
|
||
for _, andTags := range strings.Split(filter, "&&") { | ||
var okComma bool | ||
|
||
for _, tag := range strings.Split(andTags, ",") { | ||
tag = strings.TrimSpace(tag) | ||
tag = strings.Replace(tag, "@", "", -1) | ||
|
||
okComma = contains(tags, tag) || okComma | ||
|
||
if tag[0] == '~' { | ||
tag = tag[1:] | ||
okComma = !contains(tags, tag) || okComma | ||
} | ||
} | ||
|
||
ok = ok && okComma | ||
} | ||
|
||
return | ||
} | ||
|
||
func contains(tags []*messages.Pickle_PickleTag, tag string) bool { | ||
for _, t := range tags { | ||
tagName := strings.Replace(t.Name, "@", "", -1) | ||
|
||
if tagName == tag { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
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,51 @@ | ||
package tags_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cucumber/messages-go/v10" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/cucumber/godog/internal/tags" | ||
) | ||
|
||
type tag = messages.Pickle_PickleTag | ||
type pickle = messages.Pickle | ||
|
||
type testcase struct { | ||
filter string | ||
expected []*pickle | ||
} | ||
|
||
var testdata = []*pickle{p1, p2, p3} | ||
var p1 = &pickle{Id: "one", Tags: []*tag{{Name: "@one"}, {Name: "@wip"}}} | ||
var p2 = &pickle{Id: "two", Tags: []*tag{{Name: "@two"}, {Name: "@wip"}}} | ||
var p3 = &pickle{Id: "three", Tags: []*tag{{Name: "@hree"}, {Name: "@wip"}}} | ||
|
||
var testcases = []testcase{ | ||
{filter: "", expected: testdata}, | ||
|
||
{filter: "@one", expected: []*pickle{p1}}, | ||
{filter: "~@one", expected: []*pickle{p2, p3}}, | ||
{filter: "one", expected: []*pickle{p1}}, | ||
{filter: " one ", expected: []*pickle{p1}}, | ||
|
||
{filter: "@one,@two", expected: []*pickle{p1, p2}}, | ||
{filter: "@one,~@two", expected: []*pickle{p1, p3}}, | ||
{filter: " @one , @two ", expected: []*pickle{p1, p2}}, | ||
|
||
{filter: "@one&&@two", expected: []*pickle{}}, | ||
{filter: "@one&&~@two", expected: []*pickle{p1}}, | ||
{filter: "@one&&@wip", expected: []*pickle{p1}}, | ||
|
||
{filter: "@one&&@two,@wip", expected: []*pickle{p1}}, | ||
} | ||
|
||
func Test_ApplyTagFilter(t *testing.T) { | ||
for _, tc := range testcases { | ||
t.Run("", func(t *testing.T) { | ||
actual := tags.ApplyTagFilter(tc.filter, testdata) | ||
assert.Equal(t, tc.expected, actual) | ||
}) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.