Fix String concatentation bug.

This commit is contained in:
Jack Conradson 2016-09-21 15:49:56 -07:00
parent 78ceb9301b
commit 5755dd256d
2 changed files with 17 additions and 4 deletions

View File

@ -262,8 +262,8 @@ public final class EAssignment extends AExpression {
rhs.write(writer, globals); // write the bytecode for the rhs rhs.write(writer, globals); // write the bytecode for the rhs
if (!(rhs instanceof EBinary) || ((EBinary)rhs).cat) { if (!(rhs instanceof EBinary) || !((EBinary)rhs).cat) { // check to see if the rhs has already done a concatenation
writer.writeAppendStrings(rhs.actual); // append the rhs's value unless it's also a concatenation writer.writeAppendStrings(rhs.actual); // append the rhs's value since it's hasn't already
} }
writer.writeToStrings(); // put the value for string concat onto the stack writer.writeToStrings(); // put the value for string concat onto the stack

View File

@ -21,7 +21,9 @@ package org.elasticsearch.painless;
import static org.elasticsearch.painless.WriterConstants.MAX_INDY_STRING_CONCAT_ARGS; import static org.elasticsearch.painless.WriterConstants.MAX_INDY_STRING_CONCAT_ARGS;
import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
public class StringTests extends ScriptTestCase { public class StringTests extends ScriptTestCase {
@ -222,6 +224,17 @@ public class StringTests extends ScriptTestCase {
}); });
} }
public void testComplexCompoundAssignment() {
Map<String, Object> params = new HashMap<>();
Map<String, Object> ctx = new HashMap<>();
ctx.put("_id", "somerandomid");
params.put("ctx", ctx);
assertEquals("somerandomid.somerandomid", exec("ctx._id += '.' + ctx._id", params, false));
assertEquals("somerandomid.somerandomid", exec("String x = 'somerandomid'; x += '.' + x"));
assertEquals("somerandomid.somerandomid", exec("def x = 'somerandomid'; x += '.' + x"));
}
public void testAppendStringIntoMap() { public void testAppendStringIntoMap() {
assertEquals("nullcat", exec("def a = new HashMap(); a.cat += 'cat'")); assertEquals("nullcat", exec("def a = new HashMap(); a.cat += 'cat'"));
} }