Skip to content

Commit 7f2159f

Browse files
authored
Fix combined labels with comments being ignored (#2121)
1 parent a3a77a2 commit 7f2159f

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,33 @@ 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+
// Usually, you have a label on a statement like
204+
//
205+
// combined:
206+
// x << [1]
207+
//
208+
// and the label stays on that statement and could be used in later stages of the AST processing.
209+
// Especially for "combined" this is essential for proper operation.
210+
//
211+
// But if the label has a description like
212+
//
213+
// combined: 'combined with x'
214+
// x << [1]
215+
//
216+
// then the description is added as "text" to the current block and the whole statement is swallowed.
217+
// If the label is needed for further processing like for "combined", this is then missing,
218+
// so transplant the label to the following statement which it actually affects.
219+
statementLabelToTransplant = (getDescription(stat) == null) ? null : stat.getStatementLabel();
198220
}
199221

200222
checkIsValidSuccessor(method, BlockParseInfo.METHOD_END,
@@ -215,7 +237,7 @@ private Block addBlock(Method method, Statement stat) throws InvalidSpecCompileE
215237
String label = stat.getStatementLabel();
216238

217239
for (BlockParseInfo blockInfo: BlockParseInfo.values()) {
218-
if (!label.equals(blockInfo.toString())) continue;
240+
if (!label.equals(blockInfo.toString())) continue;
219241

220242
checkIsValidSuccessor(method, blockInfo, stat.getLineNumber(), stat.getColumnNumber());
221243
Block block = blockInfo.addNewBlock(method);
@@ -226,9 +248,9 @@ private Block addBlock(Method method, Statement stat) throws InvalidSpecCompileE
226248
block.getDescriptions().add(description);
227249

228250
return block;
229-
}
251+
}
230252

231-
throw new InvalidSpecCompileException(stat, "Unrecognized block label: " + label);
253+
throw new InvalidSpecCompileException(stat, "Unrecognized block label: " + label);
232254
}
233255

234256
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)