Skip to content

Commit ea1239c

Browse files
committed
[Painless] Add String Casting Tests (#36945)
This adds additional standard casting tests for String as the original type. This also cleans up the error messages in the String to char cast method.
1 parent 36f03f3 commit ea1239c

File tree

3 files changed

+107
-5
lines changed

3 files changed

+107
-5
lines changed

modules/lang-painless/src/main/java/org/elasticsearch/painless/Utility.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ public static String charToString(final char value) {
3030
}
3131

3232
public static char StringTochar(final String value) {
33+
if (value == null) {
34+
throw new ClassCastException("cannot cast " +
35+
"null " + String.class.getCanonicalName() + " to " + char.class.getCanonicalName());
36+
}
37+
3338
if (value.length() != 1) {
34-
throw new ClassCastException("Cannot cast [String] with length greater than one to [char].");
39+
throw new ClassCastException("cannot cast " +
40+
String.class.getCanonicalName() + " with length not equal to one to " + char.class.getCanonicalName());
3541
}
3642

3743
return value.charAt(0);

modules/lang-painless/src/test/java/org/elasticsearch/painless/StandardCastTests.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,4 +545,100 @@ public void testNumberCasts() {
545545
expectScriptThrows(ClassCastException.class, () -> exec("Number o = new ArrayList(); ArrayList b = o;"));
546546
expectScriptThrows(ClassCastException.class, () -> exec("Number o = new ArrayList(); ArrayList b = (ArrayList)o;"));
547547
}
548+
549+
public void testStringCasts() {
550+
exec("String o = 'string'; Object n = o;");
551+
exec("String o = null; Object n = o;");
552+
exec("String o = 'string'; Object n = (Object)o;");
553+
exec("String o = null; Object n = (Object)o;");
554+
555+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Number n = o;"));
556+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Number n = o;"));
557+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Number n = (String)o;"));
558+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Number n = (String)o;"));
559+
560+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; boolean b = o;"));
561+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; boolean b = o;"));
562+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; boolean b = (boolean)o;"));
563+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; boolean b = (boolean)o;"));
564+
565+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; byte b = o;"));
566+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; byte b = o;"));
567+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; byte b = (byte)o;"));
568+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; byte b = (byte)o;"));
569+
570+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; short b = o;"));
571+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; short b = o;"));
572+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; short b = (short)o;"));
573+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; short b = (short)o;"));
574+
575+
assertEquals('s', exec("String s = 's'; (char)s"));
576+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; char b = o;"));
577+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; char b = o;"));
578+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; char b = (char)o;"));
579+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; char b = (char)o;"));
580+
581+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; int b = o;"));
582+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; int b = o;"));
583+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; int b = (int)o;"));
584+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; int b = (int)o;"));
585+
586+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; long b = o;"));
587+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; long b = o;"));
588+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; long b = (long)o;"));
589+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; long b = (long)o;"));
590+
591+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; float b = o;"));
592+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; float b = o;"));
593+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; float b = (float)o;"));
594+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; float b = (float)o;"));
595+
596+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; double b = o;"));
597+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; double b = o;"));
598+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; double b = (double)o;"));
599+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; double b = (double)o;"));
600+
601+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Boolean b = o;"));
602+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Boolean b = o;"));
603+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Boolean b = (Boolean)o;"));
604+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Boolean b = (Boolean)o;"));
605+
606+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Byte b = o;"));
607+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Byte b = o;"));
608+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Byte b = (Byte)o;"));
609+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Byte b = (Byte)o;"));
610+
611+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Short b = o;"));
612+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Short b = o;"));
613+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Short b = (Short)o;"));
614+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Short b = (Short)o;"));
615+
616+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Character b = o;"));
617+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Character b = o;"));
618+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Character b = (Character)o;"));
619+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Character b = (Character)o;"));
620+
621+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Integer b = o;"));
622+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Integer b = o;"));
623+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Integer b = (Integer)o;"));
624+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Integer b = (Integer)o;"));
625+
626+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Long b = o;"));
627+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Long b = o;"));
628+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Long b = (Long)o;"));
629+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Long b = (Long)o;"));
630+
631+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Float b = o;"));
632+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Float b = o;"));
633+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Float b = (Float)o;"));
634+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Float b = (Float)o;"));
635+
636+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Double b = o;"));
637+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Double b = o;"));
638+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; Double b = (Double)o;"));
639+
expectScriptThrows(ClassCastException.class, () -> exec("String o = null; Double b = (Double)o;"));
640+
641+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; ArrayList b = o;"));
642+
expectScriptThrows(ClassCastException.class, () -> exec("String o = 'string'; ArrayList b = (ArrayList)o;"));
643+
}
548644
}

modules/lang-painless/src/test/java/org/elasticsearch/painless/StringTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,22 +170,22 @@ public void testStringAndCharacter() {
170170
ClassCastException expected = expectScriptThrows(ClassCastException.class, false, () -> {
171171
assertEquals("cc", exec("return (String)(char)\"cc\""));
172172
});
173-
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
173+
assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char"));
174174

175175
expected = expectScriptThrows(ClassCastException.class, false, () -> {
176176
assertEquals("cc", exec("return (String)(char)'cc'"));
177177
});
178-
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
178+
assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char"));
179179

180180
expected = expectScriptThrows(ClassCastException.class, () -> {
181181
assertEquals('c', exec("String s = \"cc\"; (char)s"));
182182
});
183-
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
183+
assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char"));
184184

185185
expected = expectScriptThrows(ClassCastException.class, () -> {
186186
assertEquals('c', exec("String s = 'cc'; (char)s"));
187187
});
188-
assertTrue(expected.getMessage().contains("Cannot cast [String] with length greater than one to [char]."));
188+
assertTrue(expected.getMessage().contains("cannot cast java.lang.String with length not equal to one to char"));
189189
}
190190

191191
public void testDefConcat() {

0 commit comments

Comments
 (0)