Skip to content

Commit

Permalink
Fix using the same previous data table column multiple times in the s…
Browse files Browse the repository at this point in the history
…ame cell (#2083)
  • Loading branch information
Vampire committed Jan 15, 2025
1 parent 6c584d3 commit d5d147a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ private void turnIntoSimpleParameterization(List<Expression> column) throws Inva

// otherwise generate the extractors and closure
List<Statement> statements = new ArrayList<>();
List<String> referencedPreviousVariables = previousVariableAccesses.stream().map(VariableExpression::getName).collect(toList());
Set<String> referencedPreviousVariables = previousVariableAccesses.stream().map(VariableExpression::getName).collect(toSet());
generatePreviousColumnExtractorStatements(referencedPreviousVariables, row, statements);
ReturnStatement providerStatement = new ReturnStatement(providerExpression);
providerStatement.setSourcePosition(providerExpression);
Expand All @@ -702,7 +702,7 @@ private void turnIntoSimpleParameterization(List<Expression> column) throws Inva
rewriteSimpleParameterization(binExpr, varExpr, true);
}

private void generatePreviousColumnExtractorStatements(List<String> referencedPreviousVariables, int row,
private void generatePreviousColumnExtractorStatements(Set<String> referencedPreviousVariables, int row,
List<Statement> statements) {
for (String referencedPreviousVariable : referencedPreviousVariables) {
statements.add(new ExpressionStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package org.spockframework.smoke.ast

import org.spockframework.EmbeddedSpecification
import org.spockframework.specs.extension.SpockSnapshotter
import spock.lang.Issue
import spock.lang.Snapshot
import spock.util.Show

Expand Down Expand Up @@ -83,4 +84,24 @@ class DataTablesAstSpec extends EmbeddedSpecification {
then:
snapshotter.assertThat(result.source).matchesSnapshot()
}

@Issue('https://github.com/spockframework/spock/issues/2083')
def 'using a variable in a cell multiple times compiles'() {
given:
snapshotter.featureBody()

when:
def result = compiler.transpileFeatureBody '''
expect:
a + b == result
where:
a | b | result
1 | 2 | a + b
3 | 4 | a + a // causes the compile error
''', EnumSet.of(Show.METHODS)

then:
snapshotter.assertThat(result.source).matchesSnapshot()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package aPackage
import spock.lang.*

class ASpec extends Specification {
def "aFeature"() {
/*--------- tag::snapshot[] ---------*/
public void $spock_feature_0_0(java.lang.Object a, java.lang.Object b, java.lang.Object result) {
org.spockframework.runtime.ErrorCollector $spock_errorCollector = org.spockframework.runtime.ErrorRethrower.INSTANCE
org.spockframework.runtime.ValueRecorder $spock_valueRecorder = new org.spockframework.runtime.ValueRecorder()
org.spockframework.runtime.SpockRuntime.callBlockEntered(this, 0)
try {
org.spockframework.runtime.SpockRuntime.verifyCondition($spock_errorCollector, $spock_valueRecorder.reset(), 'a + b == result', 2, 9, null, $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(4), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(2), $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(0), a) + $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(1), b)) == $spock_valueRecorder.record($spock_valueRecorder.startRecordingValue(3), result)))
}
catch (java.lang.Throwable $spock_condition_throwable) {
org.spockframework.runtime.SpockRuntime.conditionFailedWithException($spock_errorCollector, $spock_valueRecorder, 'a + b == result', 2, 9, null, $spock_condition_throwable)}
finally {
}
org.spockframework.runtime.SpockRuntime.callBlockExited(this, 0)
this.getSpecificationContext().getMockController().leaveScope()
}

public java.lang.Object $spock_feature_0_0prov0() {
return [1, 3]
}

public java.lang.Object $spock_feature_0_0prov1(java.util.List $spock_p_a) {
return [2, 4]
}

public java.lang.Object $spock_feature_0_0prov2(java.util.List $spock_p_a, java.util.List $spock_p_b) {
return [{ ->
java.lang.Object a = $spock_p_a.get(0)
java.lang.Object b = $spock_p_b.get(0)
return a + b
}.call(), { ->
java.lang.Object a = $spock_p_a.get(1)
return a + a
}.call()]
}

public java.lang.Object $spock_feature_0_0proc(java.lang.Object $spock_p0, java.lang.Object $spock_p1, java.lang.Object $spock_p2) {
java.lang.Object a = (( $spock_p0 ) as java.lang.Object)
java.lang.Object b = (( $spock_p1 ) as java.lang.Object)
java.lang.Object result = (( $spock_p2 ) as java.lang.Object)
return new java.lang.Object[]{ a , b , result }
}
/*--------- end::snapshot[] ---------*/
}
}

0 comments on commit d5d147a

Please sign in to comment.