add tests for reserved words. ban stores against _score

This commit is contained in:
Robert Muir 2016-05-10 22:55:06 -04:00
parent 5644f49095
commit abb015bcb9
2 changed files with 45 additions and 0 deletions

View File

@ -448,7 +448,12 @@ class AnalyzerExternal {
throw new IllegalArgumentException(AnalyzerUtility.error(ctx) + "Unknown variable [" + id + "].");
}
// special cases: reserved words
if ("_score".equals(id)) {
// read-only: don't allow stores
if (parentemd.storeExpr != null) {
throw new IllegalArgumentException(AnalyzerUtility.error(ctx) + "Variable [" + id + "] is read-only.");
}
metadata.scoreValueUsed = true;
}

View File

@ -0,0 +1,40 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.painless;
/** Tests for special reserved words such as _score */
public class ReservedWordTests extends ScriptTestCase {
/** check that we can't declare a variable of _score, its really reserved! */
public void testScoreVar() {
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
exec("int _score = 5; return _score;");
});
assertTrue(expected.getMessage().contains("Variable name [_score] already defined"));
}
/** check that we can't write to _score, its read-only! */
public void testScoreStore() {
IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
exec("_score = 5; return _score;");
});
assertTrue(expected.getMessage().contains("Variable [_score] is read-only"));
}
}