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 12 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

* [Ruby] Empty expression evaluates to true

## [4.1.0] - 2021-10-08

### Added
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
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)
})
}
}
}
19 changes: 9 additions & 10 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 @@ -27,13 +26,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 +44,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 +74,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 Down Expand Up @@ -157,9 +156,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
227 changes: 0 additions & 227 deletions go/parser_test.go

This file was deleted.

Loading