Skip to content

Commit 3a29d44

Browse files
committed
Fix combined labels with comments being ignored
1 parent 15ff926 commit 3a29d44

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

spock-core/src/main/java/org/spockframework/compiler/SpecParser.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,18 @@ private void buildBlocks(Method method) throws InvalidSpecCompileException {
190190
List<Statement> stats = AstUtil.getStatements(method.getAst());
191191
Block currBlock = method.addBlock(new AnonymousBlock(method));
192192

193+
String statementLabelToTransplant = null;
193194
for (Statement stat : stats) {
194-
if (stat.getStatementLabel() == null)
195+
if (stat.getStatementLabel() == null) {
196+
if (statementLabelToTransplant != null) {
197+
stat.setStatementLabel(statementLabelToTransplant);
198+
}
195199
currBlock.getAst().add(stat);
196-
else
200+
} else {
197201
currBlock = addBlock(method, stat);
202+
}
203+
// if the statement is a label with description, transplant the label to the actual statement
204+
statementLabelToTransplant = (getDescription(stat) == null) ? null : stat.getStatementLabel();
198205
}
199206

200207
checkIsValidSuccessor(method, BlockParseInfo.METHOD_END,
@@ -215,7 +222,7 @@ private Block addBlock(Method method, Statement stat) throws InvalidSpecCompileE
215222
String label = stat.getStatementLabel();
216223

217224
for (BlockParseInfo blockInfo: BlockParseInfo.values()) {
218-
if (!label.equals(blockInfo.toString())) continue;
225+
if (!label.equals(blockInfo.toString())) continue;
219226

220227
checkIsValidSuccessor(method, blockInfo, stat.getLineNumber(), stat.getColumnNumber());
221228
Block block = blockInfo.addNewBlock(method);
@@ -226,9 +233,9 @@ private Block addBlock(Method method, Statement stat) throws InvalidSpecCompileE
226233
block.getDescriptions().add(description);
227234

228235
return block;
229-
}
236+
}
230237

231-
throw new InvalidSpecCompileException(stat, "Unrecognized block label: " + label);
238+
throw new InvalidSpecCompileException(stat, "Unrecognized block label: " + label);
232239
}
233240

234241
private String getDescription(Statement stat) {

spock-specs/src/test/groovy/org/spockframework/smoke/Blocks.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def m3() {
5151
and: "and"
5252
where: ""
5353
and: z << 1
54+
combined: "foo"
55+
zz << 2
5456
}
5557
""")
5658

@@ -69,7 +71,7 @@ def m3() {
6971
and:
7072
def m3 = specInfo.features[2]
7173
m3.blocks*.kind == [SETUP,EXPECT,WHERE]
72-
m3.blocks*.texts.flatten() == ["given","and",""]
74+
m3.blocks*.texts.flatten() == ["given","and","","foo"]
7375
}
7476

7577
def "unknown label"() {

spock-specs/src/test/groovy/org/spockframework/smoke/parameterization/DataProviders.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,29 @@ where: a << b
297297
]
298298
}
299299

300+
def 'combined label can have a comment'() {
301+
when:
302+
def results = runner.runSpecBody '''
303+
def 'a feature (#a #b)'() {
304+
expect:
305+
true
306+
307+
where:
308+
a << [3]
309+
combined: 'combined'
310+
b << [1, 2]
311+
}
312+
'''
313+
314+
then:
315+
results.testsStartedCount == 1 + 2
316+
results.testEvents().started().list().testDescriptor.displayName == [
317+
'a feature (#a #b)',
318+
'a feature (3 1)',
319+
'a feature (3 2)'
320+
]
321+
}
322+
300323
@Issue('https://github.com/spockframework/spock/issues/2074')
301324
def 'multi-assignments can be combined'() {
302325
when:

0 commit comments

Comments
 (0)