Skip to content

Commit

Permalink
Validate argument amount in old(...) calls (#1810)
Browse files Browse the repository at this point in the history
Previously if you had multiple arguments to an `old(...)` call 2nd and
on were simply ignored,
and if you had no argument the compiler throws a `NullPointerException`.

Now the compiler validates that there is exactly one argument to an `old(...)` call.

Due to the multiple arguments now being forbidden, this is theoretically
a breaking change,
but until recently the whole feature was not even documented and
probably very few people use it at all, let alone with multiple arguments.
  • Loading branch information
Vampire authored Oct 24, 2023
1 parent 4b325b3 commit 1137415
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ include::include.adoc[]

== 2.4 (tbd)

=== Breaking Changes

- Calling `old(...)` with multiple arguments is now a compilation error. Previously the additional arguments were simply ignored.

== 2.4-M1 (2022-11-30)

* Fix issues with Spring 6/Spring Boot 3 spockPull:1541[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ private boolean handleOldCall(MethodCallExpression expr) {

expr.setMethod(new ConstantExpression(expr.getMethodAsString() + "Impl"));
List<Expression> args = AstUtil.getArgumentList(expr);
if (args.size() != 1) {
resources.getErrorReporter().error(expr, "old() must have exactly one argument");
return true;
}

VariableExpression oldValue = resources.captureOldValue(args.get(0));
args.set(0, oldValue);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ class AccessingOldValues extends EmbeddedSpecification {
e.line == 2
}

def "old() must have exactly one argument"() {
when:
compiler.compileFeatureBody """
when: true
then: old($oldArguments)
"""

then:
InvalidSpecCompileException e = thrown()
e.message == 'old() must have exactly one argument @ line 2, column 11.'

where:
oldArguments << [
'',
'null, null'
]
}

def "may occur outside of a condition"() {
def list = [1,2,3]

Expand Down

0 comments on commit 1137415

Please sign in to comment.