From c5a0a581237cfbf84336acf874927f9bcf3b6664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20L=C3=B6nnblad?= Date: Sun, 7 Jun 2020 09:10:56 +0200 Subject: [PATCH] Broke out the tag filtering logic to its own file --- suite.go | 30 ------------------------------ tag_filter.go | 42 ++++++++++++++++++++++++++++++++++++++++++ tag_filter_test.go | 4 ++-- 3 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 tag_filter.go diff --git a/suite.go b/suite.go index aa874fc..3d3366e 100644 --- a/suite.go +++ b/suite.go @@ -812,33 +812,3 @@ func applyTagFilter(tags string, ft *feature) { ft.pickles = pickles } - -// based on http://behat.readthedocs.org/en/v2.5/guides/6.cli.html#gherkin-filters -func matchesTags(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.Replace(strings.TrimSpace(tag), "@", "", -1) - if tag[0] == '~' { - tag = tag[1:] - okComma = !hasTag(tags, tag) || okComma - } else { - okComma = hasTag(tags, tag) || okComma - } - } - ok = ok && okComma - } - return -} - -func hasTag(tags []*messages.Pickle_PickleTag, tag string) bool { - for _, t := range tags { - tName := strings.Replace(t.Name, "@", "", -1) - - if tName == tag { - return true - } - } - return false -} diff --git a/tag_filter.go b/tag_filter.go new file mode 100644 index 0000000..3b95abc --- /dev/null +++ b/tag_filter.go @@ -0,0 +1,42 @@ +package godog + +import ( + "strings" + + "github.com/cucumber/messages-go/v10" +) + +// based on http://behat.readthedocs.org/en/v2.5/guides/6.cli.html#gherkin-filters +func matchesTags(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.Replace(strings.TrimSpace(tag), "@", "", -1) + + okComma = hasTag(tags, tag) || okComma + if tag[0] == '~' { + tag = tag[1:] + okComma = !hasTag(tags, tag) || okComma + } + } + + ok = ok && okComma + } + + return +} + +func hasTag(tags []*messages.Pickle_PickleTag, tag string) bool { + for _, t := range tags { + tName := strings.Replace(t.Name, "@", "", -1) + + if tName == tag { + return true + } + } + + return false +} diff --git a/tag_filter_test.go b/tag_filter_test.go index c79bfdc..f29b6ef 100644 --- a/tag_filter_test.go +++ b/tag_filter_test.go @@ -6,13 +6,13 @@ import ( "github.com/cucumber/messages-go/v10" ) -func assertNotMatchesTagFilter(tags []*messages.Pickle_PickleTag, filter string, t *testing.T) { +func assertNotMatchesTagFilter(tags []*tag, filter string, t *testing.T) { if matchesTags(filter, tags) { t.Errorf(`expected tags: %v not to match tag filter "%s", but it did`, tags, filter) } } -func assertMatchesTagFilter(tags []*messages.Pickle_PickleTag, filter string, t *testing.T) { +func assertMatchesTagFilter(tags []*tag, filter string, t *testing.T) { if !matchesTags(filter, tags) { t.Errorf(`expected tags: %v to match tag filter "%s", but it did not`, tags, filter) }