Skip to content

Commit 332100b

Browse files
Merge pull request #754 from cucumber/js-empty-string-fix
cucumber-expressions: fix captured empty strings being undefined
2 parents 8bc371b + 2a3c094 commit 332100b

6 files changed

Lines changed: 94 additions & 2 deletions

File tree

cucumber-expressions/go/cucumber_expression_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,38 @@ func TestCucumberExpression(t *testing.T) {
100100
)
101101
})
102102

103+
t.Run("matches single quoted empty string as empty string", func(t *testing.T) {
104+
require.Equal(
105+
t,
106+
MatchCucumberExpression(t, "three {string} mice", `three '' mice`),
107+
[]interface{}{""},
108+
)
109+
})
110+
111+
t.Run("matches double quoted empty string as empty string", func(t *testing.T) {
112+
require.Equal(
113+
t,
114+
MatchCucumberExpression(t, "three {string} mice", `three "" mice`),
115+
[]interface{}{""},
116+
)
117+
})
118+
119+
t.Run("matches single quoted empty string as empty string along with other strings", func(t *testing.T) {
120+
require.Equal(
121+
t,
122+
MatchCucumberExpression(t, "three {string} and {string} mice", `three '' and 'handsome' mice`),
123+
[]interface{}{"", "handsome"},
124+
)
125+
})
126+
127+
t.Run("matches double quoted empty string as empty string along with other strings", func(t *testing.T) {
128+
require.Equal(
129+
t,
130+
MatchCucumberExpression(t, "three {string} and {string} mice", `three "" and "handsome" mice`),
131+
[]interface{}{"", "handsome"},
132+
)
133+
})
134+
103135
t.Run("matches escaped parenthesis", func(t *testing.T) {
104136
require.Equal(
105137
t,

cucumber-expressions/java/src/test/java/io/cucumber/cucumberexpressions/CucumberExpressionTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,26 @@ public void matches_single_quoted_string_with_escaped_single_quote() {
8282
assertEquals(singletonList("bl'nd"), match("three {string} mice", "three 'bl\\'nd' mice"));
8383
}
8484

85+
@Test
86+
public void matches_single_quoted_empty_string_as_empty_string() {
87+
assertEquals(singletonList(""), match("three {string} mice", "three '' mice"));
88+
}
89+
90+
@Test
91+
public void matches_double_quoted_empty_string_as_empty_string() {
92+
assertEquals(singletonList(""), match("three {string} mice", "three \"\" mice"));
93+
}
94+
95+
@Test
96+
public void matches_single_quoted_empty_string_as_empty_string_along_with_other_strings() {
97+
assertEquals(asList("", "handsome"), match("three {string} and {string} mice", "three '' and 'handsome' mice"));
98+
}
99+
100+
@Test
101+
public void matches_double_quoted_empty_string_as_empty_string_along_with_other_strings() {
102+
assertEquals(asList("", "handsome"), match("three {string} and {string} mice", "three \"\" and \"handsome\" mice"));
103+
}
104+
85105
@Test
86106
public void matches_escaped_parenthesis() {
87107
assertEquals(singletonList("blind"), match("three \\(exceptionally) {string} mice", "three (exceptionally) \"blind\" mice"));

cucumber-expressions/javascript/src/Group.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class Group {
88

99
get values(): string[] {
1010
return (this.children.length === 0 ? [this] : this.children)
11+
.filter(g => typeof g.start !== 'undefined')
1112
.map(g => g.value)
12-
.filter(v => v !== null)
1313
}
1414
}

cucumber-expressions/javascript/src/ParameterTypeRegistry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default class ParameterTypeRegistry {
4949
'string',
5050
ParameterTypeRegistry.STRING_REGEXP,
5151
String,
52-
s => s.replace(/\\"/g, '"').replace(/\\'/g, "'"),
52+
s => (s || '').replace(/\\"/g, '"').replace(/\\'/g, "'"),
5353
true,
5454
false
5555
)

cucumber-expressions/javascript/test/CucumberExpressionTest.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ describe('CucumberExpression', () => {
9595
)
9696
})
9797

98+
it('matches single quoted empty string as empty string', () => {
99+
assert.deepStrictEqual(match('three {string} mice', "three '' mice"),
100+
['']
101+
)
102+
})
103+
104+
it('matches double quoted empty string as empty string ', () => {
105+
assert.deepStrictEqual(match('three {string} mice', 'three "" mice'),
106+
['']
107+
)
108+
})
109+
110+
it('matches single quoted empty string as empty string, along with other strings', () => {
111+
assert.deepStrictEqual(match('three {string} and {string} mice', "three '' and 'handsome' mice"),
112+
['', 'handsome']
113+
)
114+
})
115+
116+
it('matches double quoted empty string as empty string, along with other strings', () => {
117+
assert.deepStrictEqual(match('three {string} and {string} mice', 'three "" and "handsome" mice'),
118+
['', 'handsome']
119+
)
120+
})
121+
98122
it('matches escaped parenthesis', () => {
99123
assert.deepStrictEqual(
100124
match(

cucumber-expressions/ruby/spec/cucumber/cucumber_expressions/cucumber_expression_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ module CucumberExpressions
5555
expect(match('three {string} mice', "three 'bl\\'nd' mice")).to eq(["bl'nd"])
5656
end
5757

58+
it('matches single quoted empty string as empty string') do
59+
expect(match('three {string} mice', "three '' mice")).to eq([''])
60+
end
61+
62+
it('matches double quoted empty string as empty string') do
63+
expect(match('three {string} mice', 'three "" mice')).to eq([''])
64+
end
65+
66+
it('matches single quoted empty string as empty string, along with other strings') do
67+
expect(match('three {string} and {string} mice', "three '' and 'handsome' mice")).to eq(['', 'handsome'])
68+
end
69+
70+
it('matches double quoted empty string as empty string, along with other strings') do
71+
expect(match('three {string} and {string} mice', 'three "" and "handsome" mice')).to eq(['', 'handsome'])
72+
end
73+
5874
it 'matches escaped parentheses' do
5975
expect(match('three \\(exceptionally) {string} mice', 'three (exceptionally) "blind" mice')).to eq(['blind'])
6076
end

0 commit comments

Comments
 (0)