Skip to content

Commit c133263

Browse files
Change YAML test data to encode expectations directly (#34)
1 parent 4b6ae11 commit c133263

File tree

104 files changed

+1370
-655
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+1370
-655
lines changed

go/ast.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ const (
2424
)
2525

2626
type node struct {
27-
NodeType nodeType `json:"type"`
28-
Start int `json:"start"`
29-
End int `json:"end"`
30-
Token string `json:"token"`
31-
Nodes []node `json:"nodes"`
27+
NodeType nodeType `yaml:"type"`
28+
Start int `yaml:"start"`
29+
End int `yaml:"end"`
30+
Token string `yaml:"token"`
31+
Nodes []node `yaml:"nodes"`
3232
}
3333

3434
func (node node) text() string {
@@ -60,10 +60,10 @@ const (
6060
)
6161

6262
type token struct {
63-
Text string `json:"text"`
64-
TokenType tokenType `json:"type"`
65-
Start int `json:"start"`
66-
End int `json:"end"`
63+
Text string `yaml:"text"`
64+
TokenType tokenType `yaml:"type"`
65+
Start int `yaml:"start"`
66+
End int `yaml:"end"`
6767
}
6868

6969
var nullNode = node{textNode, -1, -1, "", nil}

go/cucumber_expression_parser_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package cucumberexpressions
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"github.com/stretchr/testify/require"
76
"gopkg.in/yaml.v3"
87
"io/ioutil"
98
"testing"
109
)
1110

11+
type AstExpectation struct {
12+
Expression string `yaml:"expression"`
13+
ExpectedAst node `yaml:"expected_ast"`
14+
Exception string `yaml:"exception"`
15+
}
16+
1217
func TestCucumberExpressionParser(t *testing.T) {
1318
var assertAst = func(t *testing.T, expected node, expression string) {
1419
ast, err := parse(expression)
1520
require.NoError(t, err)
1621
require.Equal(t, expected, ast)
17-
require.Equal(t, expected, ast)
1822
}
1923
var assertThrows = func(t *testing.T, expected string, expression string) {
2024
_, err := parse(expression)
@@ -30,15 +34,12 @@ func TestCucumberExpressionParser(t *testing.T) {
3034
contents, err := ioutil.ReadFile(directory + file.Name())
3135
require.NoError(t, err)
3236
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
33-
var expectation expectation
37+
var expectation AstExpectation
3438
err = yaml.Unmarshal(contents, &expectation)
3539
require.NoError(t, err)
3640

3741
if expectation.Exception == "" {
38-
var node node
39-
err = json.Unmarshal([]byte(expectation.Expected), &node)
40-
require.NoError(t, err)
41-
assertAst(t, node, expectation.Expression)
42+
assertAst(t, expectation.ExpectedAst, expectation.Expression)
4243
} else {
4344
assertThrows(t, expectation.Exception, expectation.Expression)
4445
}

go/cucumber_expression_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
package cucumberexpressions
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"github.com/stretchr/testify/require"
76
"gopkg.in/yaml.v3"
87
"io/ioutil"
98
"reflect"
109
"regexp"
11-
"strings"
1210
"testing"
1311
)
1412

13+
type ExpressionExpectation struct {
14+
Expression string `yaml:"expression"`
15+
Text string `yaml:"text"`
16+
ExpectedArgs []interface{} `yaml:"expected_args"`
17+
Exception string `yaml:"exception"`
18+
}
19+
20+
type RegexExpectation struct {
21+
Expression string `yaml:"expression"`
22+
ExpectedRegex string `yaml:"expected_regex"`
23+
}
24+
1525
func TestCucumberExpression(t *testing.T) {
1626

1727
t.Run("acceptance tests pass", func(t *testing.T) {
1828

19-
assertMatches := func(t *testing.T, expected string, expr string, text string) {
29+
assertMatches := func(t *testing.T, expectedArgs []interface{}, expr string, text string) {
2030
parameterTypeRegistry := NewParameterTypeRegistry()
2131
expression, err := NewCucumberExpression(expr, parameterTypeRegistry)
2232
require.NoError(t, err)
2333
args, err := expression.Match(text)
2434
require.NoError(t, err)
25-
26-
values := strings.Builder{}
27-
encoder := json.NewEncoder(&values)
28-
encoder.SetEscapeHTML(false)
29-
err = encoder.Encode(argumentValues(args))
30-
require.NoError(t, err)
31-
require.Equal(t, expected, strings.TrimSuffix(values.String(), "\n"))
35+
require.Equal(t, expectedArgs, argumentValues(args))
3236
}
3337

3438
assertThrows := func(t *testing.T, expected string, expr string, text string) {
@@ -53,11 +57,11 @@ func TestCucumberExpression(t *testing.T) {
5357
contents, err := ioutil.ReadFile(directory + file.Name())
5458
require.NoError(t, err)
5559
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
56-
var expectation expectation
60+
var expectation ExpressionExpectation
5761
err = yaml.Unmarshal(contents, &expectation)
5862
require.NoError(t, err)
5963
if expectation.Exception == "" {
60-
assertMatches(t, expectation.Expected, expectation.Expression, expectation.Text)
64+
assertMatches(t, expectation.ExpectedArgs, expectation.Expression, expectation.Text)
6165
} else {
6266
assertThrows(t, expectation.Exception, expectation.Expression, expectation.Text)
6367
}
@@ -79,25 +83,23 @@ func TestCucumberExpression(t *testing.T) {
7983
contents, err := ioutil.ReadFile(directory + file.Name())
8084
require.NoError(t, err)
8185
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
82-
var expectation expectation
86+
var expectation RegexExpectation
8387
err = yaml.Unmarshal(contents, &expectation)
8488
require.NoError(t, err)
85-
assertRegex(t, expectation.Expected, expectation.Expression)
89+
assertRegex(t, expectation.ExpectedRegex, expectation.Expression)
8690
})
8791
}
8892
})
8993

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

93-
/// [capture-match-arguments]
9497
expr := "I have {int} cuke(s)"
9598
expression, err := NewCucumberExpression(expr, parameterTypeRegistry)
9699
require.NoError(t, err)
97100
args, err := expression.Match("I have 7 cukes")
98101
require.NoError(t, err)
99102
require.Equal(t, args[0].GetValue(), 7)
100-
/// [capture-match-arguments]
101103
})
102104

103105
t.Run("matches float", func(t *testing.T) {
@@ -185,7 +187,6 @@ func TestCucumberExpression(t *testing.T) {
185187
require.NoError(t, err)
186188
err = parameterTypeRegistry.DefineParameterType(colorParameterType)
187189

188-
/// [capture-match-arguments]
189190
expr := "{textAndOrNumber}"
190191
expression, err := NewCucumberExpression(expr, parameterTypeRegistry)
191192
require.NoError(t, err)
@@ -197,7 +198,6 @@ func TestCucumberExpression(t *testing.T) {
197198
numArgs, err := expression.Match(num)
198199
require.NoError(t, err)
199200
require.Equal(t, numArgs[0].GetValue(), []*string{nil, &num})
200-
/// [capture-match-arguments]
201201
})
202202
}
203203

go/cucumber_expression_tokenizer_test.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
package cucumberexpressions
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"github.com/stretchr/testify/require"
76
"gopkg.in/yaml.v3"
87
"io/ioutil"
98
"testing"
109
)
1110

12-
type expectation struct {
13-
Expression string `yaml:"expression"`
14-
Text string `yaml:"text"`
15-
Expected string `yaml:"expected"`
16-
Exception string `yaml:"exception"`
11+
type TokenExpectation struct {
12+
Expression string `yaml:"expression"`
13+
ExpectedTokens []token `yaml:"expected_tokens"`
14+
Exception string `yaml:"exception"`
1715
}
1816

1917
func TestCucumberExpressionTokenizer(t *testing.T) {
@@ -26,15 +24,12 @@ func TestCucumberExpressionTokenizer(t *testing.T) {
2624
contents, err := ioutil.ReadFile(directory + file.Name())
2725
require.NoError(t, err)
2826
t.Run(fmt.Sprintf("%s", file.Name()), func(t *testing.T) {
29-
var expectation expectation
27+
var expectation TokenExpectation
3028
err = yaml.Unmarshal(contents, &expectation)
3129
require.NoError(t, err)
3230

3331
if expectation.Exception == "" {
34-
var token []token
35-
err = json.Unmarshal([]byte(expectation.Expected), &token)
36-
require.NoError(t, err)
37-
assertTokenizes(t, token, expectation.Expression)
32+
assertTokenizes(t, expectation.ExpectedTokens, expectation.Expression)
3833
} else {
3934
assertThrows(t, expectation.Exception, expectation.Expression)
4035
}

java/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@
4545
<artifactId>apiguardian-api</artifactId>
4646
<version>1.1.2</version>
4747
</dependency>
48-
<dependency>
49-
<groupId>com.google.code.gson</groupId>
50-
<artifactId>gson</artifactId>
51-
<version>2.8.8</version>
52-
<scope>test</scope>
53-
</dependency>
5448
<dependency>
5549
<groupId>org.hamcrest</groupId>
5650
<artifactId>hamcrest</artifactId>

java/src/main/java/io/cucumber/cucumberexpressions/Ast.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ private Node(Type type, int start, int end, List<Node> nodes, String token) {
4646
this.token = token;
4747
this.start = start;
4848
this.end = end;
49-
5049
}
5150

5251
enum Type {
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.cucumber.cucumberexpressions;
2+
3+
import io.cucumber.cucumberexpressions.Ast.Node;
4+
import org.junit.jupiter.api.extension.ParameterContext;
5+
import org.junit.jupiter.params.converter.ArgumentConversionException;
6+
import org.junit.jupiter.params.converter.ArgumentConverter;
7+
import org.yaml.snakeyaml.Yaml;
8+
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.nio.file.Path;
12+
import java.util.List;
13+
import java.util.stream.Collectors;
14+
15+
import static java.nio.file.Files.newInputStream;
16+
17+
class AstExpectation {
18+
public String expression;
19+
public YamlableNode expected_ast;
20+
public String exception;
21+
22+
static class Converter implements ArgumentConverter {
23+
Yaml yaml = new Yaml();
24+
25+
@Override
26+
public AstExpectation convert(Object source, ParameterContext context) throws ArgumentConversionException {
27+
try {
28+
Path path = (Path) source;
29+
InputStream inputStream = newInputStream(path);
30+
return yaml.loadAs(inputStream, AstExpectation.class);
31+
} catch (IOException e) {
32+
throw new ArgumentConversionException("Could not load " + source, e);
33+
}
34+
}
35+
}
36+
37+
static class YamlableNode {
38+
public Ast.Node.Type type;
39+
public List<YamlableNode> nodes;
40+
public String token;
41+
public int start;
42+
public int end;
43+
44+
public Node toNode() {
45+
if (token != null) {
46+
return new Node(type, start, end, token);
47+
} else {
48+
return new Node(type, start, end, nodes.stream().map(YamlableNode::toNode).collect(Collectors.toList()));
49+
}
50+
}
51+
}
52+
}

java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionParserTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ private static List<Path> acceptance_tests_pass() throws IOException {
3030

3131
@ParameterizedTest
3232
@MethodSource
33-
void acceptance_tests_pass(@ConvertWith(FileToExpectationConverter.class) Expectation expectation) {
34-
if (expectation.getException() == null) {
35-
Node node = parser.parse(expectation.getExpression());
36-
assertThat(node.toString(), is(expectation.getExpected()));
33+
void acceptance_tests_pass(@ConvertWith(AstExpectation.Converter.class) AstExpectation expectation) {
34+
if (expectation.exception == null) {
35+
Node node = parser.parse(expectation.expression);
36+
assertThat(node, is(expectation.expected_ast.toNode()));
3737
} else {
3838
CucumberExpressionException exception = assertThrows(
3939
CucumberExpressionException.class,
40-
() -> parser.parse(expectation.getExpression()));
41-
assertThat(exception.getMessage(), is(expectation.getException()));
40+
() -> parser.parse(expectation.expression));
41+
assertThat(exception.getMessage(), is(expectation.exception));
4242
}
4343
}
4444

0 commit comments

Comments
 (0)