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 {
@ -182,7 +184,7 @@ public class StringTests extends ScriptTestCase {
}); });
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char].")); assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
} }
public void testDefConcat() { public void testDefConcat() {
assertEquals("a" + (byte)2, exec("def x = 'a'; def y = (byte)2; return x + y")); assertEquals("a" + (byte)2, exec("def x = 'a'; def y = (byte)2; return x + y"));
assertEquals("a" + (short)2, exec("def x = 'a'; def y = (short)2; return x + y")); assertEquals("a" + (short)2, exec("def x = 'a'; def y = (short)2; return x + y"));
@ -205,7 +207,7 @@ public class StringTests extends ScriptTestCase {
exec("def x = null; def y = null; return x + y"); exec("def x = null; def y = null; return x + y");
}); });
} }
public void testDefCompoundAssignment() { public void testDefCompoundAssignment() {
assertEquals("a" + (byte)2, exec("def x = 'a'; x += (byte)2; return x")); assertEquals("a" + (byte)2, exec("def x = 'a'; x += (byte)2; return x"));
assertEquals("a" + (short)2, exec("def x = 'a'; x += (short)2; return x")); assertEquals("a" + (short)2, exec("def x = 'a'; x += (short)2; return x"));
@ -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'"));
} }