Skip to content

Regular expression acceptance tests #35

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

Merged
merged 10 commits into from
Oct 15, 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
15 changes: 1 addition & 14 deletions go/cucumber_expression_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,7 @@ type Currency struct {
ISO4217 string
}

func TestCucumberExpressionGeneratory(t *testing.T) {
t.Run("documents expression generation", func(t *testing.T) {
parameterTypeRegistry := NewParameterTypeRegistry()

/// [generate-expression]
generator := NewCucumberExpressionGenerator(parameterTypeRegistry)
undefinedStepText := "I have 2 cucumbers and 1.5 tomato"
generatedExpression := generator.GenerateExpressions(undefinedStepText)[0]
require.Equal(t, "I have {int} cucumbers and {float} tomato", generatedExpression.Source())
require.Equal(t, "int", generatedExpression.ParameterNames()[0])
require.Equal(t, "float", generatedExpression.ParameterTypes()[1].Name())
/// [generate-expression]
})

func TestCucumberExpressionGenerator(t *testing.T) {
t.Run("generates expression for no args", func(t *testing.T) {
assertExpression(t, "hello", []string{}, "hello")
})
Expand Down
6 changes: 3 additions & 3 deletions go/cucumber_expression_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"
)

type AstExpectation struct {
type ParserExpectation struct {
Expression string `yaml:"expression"`
ExpectedAst node `yaml:"expected_ast"`
Exception string `yaml:"exception"`
Expand All @@ -26,15 +26,15 @@ func TestCucumberExpressionParser(t *testing.T) {
require.Equal(t, expected, err.Error())
}

directory := "../testdata/ast/"
directory := "../testdata/cucumber-expression/parser/"
files, err := ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation AstExpectation
var expectation ParserExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)

Expand Down
33 changes: 3 additions & 30 deletions go/cucumber_expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ import (
"testing"
)

type ExpressionExpectation struct {
type CucumberExpressionMatchingExpectation struct {
Expression string `yaml:"expression"`
Text string `yaml:"text"`
ExpectedArgs []interface{} `yaml:"expected_args"`
Exception string `yaml:"exception"`
}

type RegexExpectation struct {
Expression string `yaml:"expression"`
ExpectedRegex string `yaml:"expected_regex"`
}

func TestCucumberExpression(t *testing.T) {

t.Run("acceptance tests pass", func(t *testing.T) {
Expand Down Expand Up @@ -49,15 +44,15 @@ func TestCucumberExpression(t *testing.T) {
}
}

directory := "../testdata/expression/"
directory := "../testdata/cucumber-expression/matching/"
files, err := ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation ExpressionExpectation
var expectation CucumberExpressionMatchingExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)
if expectation.Exception == "" {
Expand All @@ -67,28 +62,6 @@ func TestCucumberExpression(t *testing.T) {
}
})
}

assertRegex := func(t *testing.T, expected string, expr string) {
parameterTypeRegistry := NewParameterTypeRegistry()
expression, err := NewCucumberExpression(expr, parameterTypeRegistry)
require.NoError(t, err)
require.Equal(t, expected, expression.Regexp().String())
}

directory = "../testdata/regex/"
files, err = ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation RegexExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)
assertRegex(t, expectation.ExpectedRegex, expectation.Expression)
})
}
})

t.Run("documents expression generation", func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions go/cucumber_expression_tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (
"testing"
)

type TokenExpectation struct {
type TokenizerExpectation struct {
Expression string `yaml:"expression"`
ExpectedTokens []token `yaml:"expected_tokens"`
Exception string `yaml:"exception"`
}

func TestCucumberExpressionTokenizer(t *testing.T) {

directory := "../testdata/tokens/"
directory := "../testdata/cucumber-expression/tokenizer/"
files, err := ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation TokenExpectation
var expectation TokenizerExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)

Expand Down
41 changes: 41 additions & 0 deletions go/cucumber_expression_transformation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cucumberexpressions

import (
"fmt"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"io/ioutil"
"testing"
)

type TransformationExpectation struct {
Expression string `yaml:"expression"`
ExpectedRegex string `yaml:"expected_regex"`
}

func TestCucumberExpressionTransformation(t *testing.T) {

t.Run("acceptance tests pass", func(t *testing.T) {
assertRegex := func(t *testing.T, expected string, expr string) {
parameterTypeRegistry := NewParameterTypeRegistry()
expression, err := NewCucumberExpression(expr, parameterTypeRegistry)
require.NoError(t, err)
require.Equal(t, expected, expression.Regexp().String())
}

directory := "../testdata/cucumber-expression/transformation/"
files, err := ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation TransformationExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)
assertRegex(t, expectation.ExpectedRegex, expectation.Expression)
})
}
})
}
37 changes: 35 additions & 2 deletions go/regular_expression_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
package cucumberexpressions

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

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

type RegularExpressionMatchingExpectation struct {
Expression string `yaml:"expression"`
Text string `yaml:"text"`
ExpectedArgs []interface{} `yaml:"expected_args"`
}

func TestRegularExpression(t *testing.T) {
directory := "../testdata/regular-expression/matching/"
files, err := ioutil.ReadDir(directory)
require.NoError(t, err)

for _, file := range files {
contents, err := ioutil.ReadFile(directory + file.Name())
require.NoError(t, err)
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
var expectation RegularExpressionMatchingExpectation
err = yaml.Unmarshal(contents, &expectation)
require.NoError(t, err)

parameterTypeRegistry := NewParameterTypeRegistry()
expr := regexp.MustCompile(expectation.Expression)
expression := NewRegularExpression(expr, parameterTypeRegistry)
args, err := expression.Match(expectation.Text)
require.NoError(t, err)
values := make([]interface{}, len(args))
for i, arg := range args {
values[i] = arg.GetValue()
}

require.Equal(t, expectation.ExpectedArgs, values)
})
}

t.Run("documents match arguments", func(t *testing.T) {
parameterTypeRegistry := NewParameterTypeRegistry()

/// [capture-match-arguments]
expr := regexp.MustCompile(`I have (\d+) cukes? in my (\w+) now`)
expression := NewRegularExpression(expr, parameterTypeRegistry)
args, err := expression.Match("I have 7 cukes in my belly now")
require.NoError(t, err)
require.Equal(t, args[0].GetValue(), 7)
require.Equal(t, args[1].GetValue(), "belly")
/// [capture-match-arguments]
})

t.Run("does no transform by default", func(t *testing.T) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package io.cucumber.cucumberexpressions;

import io.cucumber.cucumberexpressions.Ast.Node;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.ArgumentConverter;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.MethodSource;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static java.nio.file.Files.newDirectoryStream;
import static java.nio.file.Files.newInputStream;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -23,14 +30,14 @@ class CucumberExpressionParserTest {

private static List<Path> acceptance_tests_pass() throws IOException {
List<Path> paths = new ArrayList<>();
newDirectoryStream(Paths.get("..", "testdata", "ast")).forEach(paths::add);
newDirectoryStream(Paths.get("..", "testdata", "cucumber-expression", "parser")).forEach(paths::add);
paths.sort(Comparator.naturalOrder());
return paths;
}

@ParameterizedTest
@MethodSource
void acceptance_tests_pass(@ConvertWith(AstExpectation.Converter.class) AstExpectation expectation) {
void acceptance_tests_pass(@ConvertWith(Converter.class) Expectation expectation) {
if (expectation.exception == null) {
Node node = parser.parse(expectation.expression);
assertThat(node, is(expectation.expected_ast.toNode()));
Expand All @@ -42,4 +49,40 @@ void acceptance_tests_pass(@ConvertWith(AstExpectation.Converter.class) AstExpec
}
}

static class Expectation {
public String expression;
public YamlableNode expected_ast;
public String exception;
}

static class Converter implements ArgumentConverter {
Yaml yaml = new Yaml();

@Override
public Expectation convert(Object source, ParameterContext context) throws ArgumentConversionException {
try {
Path path = (Path) source;
InputStream inputStream = newInputStream(path);
return yaml.loadAs(inputStream, Expectation.class);
} catch (IOException e) {
throw new ArgumentConversionException("Could not load " + source, e);
}
}
}

static class YamlableNode {
public Ast.Node.Type type;
public List<YamlableNode> nodes;
public String token;
public int start;
public int end;

public Node toNode() {
if (token != null) {
return new Node(type, start, end, token);
} else {
return new Node(type, start, end, nodes.stream().map(YamlableNode::toNode).collect(Collectors.toList()));
}
}
}
}
Loading