Skip to content

Commit

Permalink
Validate argument amount in old(...) calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed Oct 12, 2023
1 parent 03b1d6c commit 227308e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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
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 227308e

Please sign in to comment.