Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use shared test data #9

Merged
merged 17 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

* Only allow escape character (\) in front of (, ), or \. Throw error otherwise. ([#17](https://github.com/cucumber/tag-expressions/pull/17))

### Deprecated

### Removed

### Fixed

* Document escaping. ([#16](https://github.com/cucumber/tag-expressions/issues/16), [#17](https://github.com/cucumber/tag-expressions/pull/17))
* [Ruby] Empty expression evaluates to true

## [4.1.0] - 2021-10-08

### Added
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ For more complex Tag Expressions you can use parenthesis for clarity, or to chan

(@smoke or @ui) and (not @slow)

## Escaping

If you need to use one of the reserved characters `(`, `)`, `\` or ` ` (whitespace) in a tag,
you can escape it with a `\`. Examples:

| Gherkin Tag | Escaped Tag Expression |
| ------------- | ---------------------- |
| @x(y) | @x\(y\) |
| @x\y | @x\\y |

## Migrating from old style tags

Older versions of Cucumber used a different syntax for tags. The list below
Expand Down
1 change: 1 addition & 0 deletions go/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ dist_compressed/
*.iml
# upx dist/cucumber-gherkin-openbsd-386 fails with a core dump
core.*.!usr!bin!upx-ucl
.idea
3 changes: 2 additions & 1 deletion go/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
GO_SOURCE_FILES := $(wildcard *.go)
TEST_FILES := $(wildcard ../testdata/*.yml)

default: .linted .tested
.PHONY: default
Expand All @@ -7,7 +8,7 @@ default: .linted .tested
gofmt -w $^
touch $@

.tested: $(GO_SOURCE_FILES)
.tested: $(GO_SOURCE_FILES) $(TEST_FILES)
go test ./...
touch $@

Expand Down
32 changes: 32 additions & 0 deletions go/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tagexpressions

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"
)

type ErrorTest struct {
Expression string `yaml:"expression"`
Error string `yaml:"error"`
}

func TestErrors(t *testing.T) {
contents, err := ioutil.ReadFile("../testdata/errors.yml")
require.NoError(t, err)
var tests []ErrorTest
err = yaml.Unmarshal(contents, &tests)
require.NoError(t, err)

for _, test := range tests {
name := fmt.Sprintf("fails to parse \"%s\" with \"%s\"", test.Expression, test.Error)
t.Run(name, func(t *testing.T) {
_, err := Parse(test.Expression)
require.Error(t, err)
require.Equal(t, test.Error, err.Error())
})
}
}
43 changes: 43 additions & 0 deletions go/evaluations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tagexpressions

import (
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"strings"
"testing"

"github.com/stretchr/testify/require"
)

type Evaluation struct {
Expression string `yaml:"expression"`
Tests []Test `yaml:"tests"`
}

type Test struct {
Variables []string `yaml:"variables"`
Result bool `yaml:"result"`
}

func TestEvaluations(t *testing.T) {
contents, err := ioutil.ReadFile("../testdata/evaluations.yml")
require.NoError(t, err)
var evaluations []Evaluation
err = yaml.Unmarshal(contents, &evaluations)
require.NoError(t, err)

for _, evaluation := range evaluations {
for _, test := range evaluation.Tests {
variables := strings.Join(test.Variables, ", ")
name := fmt.Sprintf("evaluates [%s] to %t", variables, test.Result)
t.Run(name, func(t *testing.T) {
expression, err := Parse(evaluation.Expression)
require.NoError(t, err)

result := expression.Evaluate(test.Variables)
require.Equal(t, test.Result, result)
})
}
}
}
93 changes: 38 additions & 55 deletions go/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tagexpressions

import (
"bytes"
"errors"
"fmt"
"strings"
"unicode"
Expand All @@ -17,7 +16,10 @@ type Evaluatable interface {
}

func Parse(infix string) (Evaluatable, error) {
tokens := tokenize(infix)
tokens, err := tokenize(infix)
if err != nil {
return nil, err
}
if len(tokens) == 0 {
return &trueExpr{}, nil
}
Expand All @@ -27,13 +29,13 @@ func Parse(infix string) (Evaluatable, error) {

for _, token := range tokens {
if isUnary(token) {
if err := check(expectedTokenType, OPERAND); err != nil {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
operators.Push(token)
expectedTokenType = OPERAND
} else if isBinary(token) {
if err := check(expectedTokenType, OPERATOR); err != nil {
if err := check(infix, expectedTokenType, OPERATOR); err != nil {
return nil, err
}
for operators.Len() > 0 &&
Expand All @@ -45,27 +47,27 @@ func Parse(infix string) (Evaluatable, error) {
operators.Push(token)
expectedTokenType = OPERAND
} else if "(" == token {
if err := check(expectedTokenType, OPERAND); err != nil {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
operators.Push(token)
expectedTokenType = OPERAND
} else if ")" == token {
if err := check(expectedTokenType, OPERATOR); err != nil {
if err := check(infix, expectedTokenType, OPERATOR); err != nil {
return nil, err
}
for operators.Len() > 0 && operators.Peek() != "(" {
pushExpr(operators.Pop(), expressions)
}
if operators.Len() == 0 {
return nil, errors.New("Syntax error. Unmatched )")
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Unmatched ).", infix)
}
if operators.Peek() == "(" {
operators.Pop()
}
expectedTokenType = OPERATOR
} else {
if err := check(expectedTokenType, OPERAND); err != nil {
if err := check(infix, expectedTokenType, OPERAND); err != nil {
return nil, err
}
pushExpr(token, expressions)
Expand All @@ -75,7 +77,7 @@ func Parse(infix string) (Evaluatable, error) {

for operators.Len() > 0 {
if operators.Peek() == "(" {
return nil, errors.New("Syntax error. Unmatched (")
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Unmatched (.", infix)
}
pushExpr(operators.Pop(), expressions)
}
Expand All @@ -97,51 +99,38 @@ var PREC = map[string]int{
"not": 2,
}

func tokenize(expr string) []string {
func tokenize(expr string) ([]string, error) {
var tokens []string
var token bytes.Buffer

collectToken := func() {
if token.Len() > 0 {
tokens = append(tokens, token.String())
token.Reset()
}
}

escaped := false
for _, c := range expr {
if unicode.IsSpace(c) {
collectToken()
escaped = false
continue
}

ch := string(c)

switch ch {
case "\\":
if escaped {
token.WriteString(ch)
if escaped {
if c == '(' || c == ')' || c == '\\' || unicode.IsSpace(c) {
token.WriteRune(c)
escaped = false
} else {
escaped = true
return nil, fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Illegal escape before \"%s\".", expr, string(c))
}
case "(", ")":
if escaped {
token.WriteString(ch)
escaped = false
} else {
collectToken()
tokens = append(tokens, ch)
} else if c == '\\' {
escaped = true
} else if c == '(' || c == ')' || unicode.IsSpace(c) {
if token.Len() > 0 {
tokens = append(tokens, token.String())
token.Reset()
}
default:
token.WriteString(ch)
escaped = false
if !unicode.IsSpace(c) {
tokens = append(tokens, string(c))
}
} else {
token.WriteRune(c)
}
}
if token.Len() > 0 {
tokens = append(tokens, token.String())
}

collectToken()
return tokens
return tokens, nil
}

func isUnary(token string) bool {
Expand All @@ -157,9 +146,9 @@ func isOp(token string) bool {
return ok
}

func check(expectedTokenType, tokenType string) error {
func check(infix, expectedTokenType, tokenType string) error {
if expectedTokenType != tokenType {
return fmt.Errorf("Syntax error. Expected %s", expectedTokenType)
return fmt.Errorf("Tag expression \"%s\" could not be parsed because of syntax error: Expected %s.", infix, expectedTokenType)
}
return nil
}
Expand Down Expand Up @@ -198,17 +187,11 @@ func (l *literalExpr) Evaluate(variables []string) bool {
}

func (l *literalExpr) ToString() string {
return strings.Replace(
strings.Replace(
strings.Replace(l.value, "\\", "\\\\", -1),
"(",
"\\(",
-1,
),
")",
"\\)",
-1,
)
s1 := l.value
s2 := strings.Replace(s1, "\\", "\\\\", -1)
s3 := strings.Replace(s2, "(", "\\(", -1)
s4 := strings.Replace(s3, ")", "\\)", -1)
return strings.Replace(s4, " ", "\\ ", -1)
}

type orExpr struct {
Expand Down
Loading