-
Notifications
You must be signed in to change notification settings - Fork 867
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable assigning a Number to java.lang.Character
Use coerceToNumber when JS Type is numeric and target type is java.lang.Character. This lets you assign a JavaScript number to a java.lang.Character without triggering an exception.
- Loading branch information
Showing
2 changed files
with
52 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
tests/src/test/java/org/mozilla/javascript/tests/CharacterAssignmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.mozilla.javascript.tests; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.mozilla.javascript.tools.shell.Global; | ||
|
||
/** | ||
* @author Jakob Silbereisen, Foconis Analytics GmbH | ||
*/ | ||
public class CharacterAssignmentTest { | ||
|
||
public static class X { | ||
public Character bigCharacter; | ||
public char primitiveCharacter; | ||
|
||
public X() {} | ||
|
||
public X(char c) { | ||
bigCharacter = c; | ||
primitiveCharacter = c; | ||
} | ||
} | ||
|
||
@Test | ||
public void assignCharacterTypesFromNumbers() { | ||
Utils.runWithAllModes( | ||
cx -> { | ||
final Global scope = new Global(); | ||
scope.init(cx); | ||
X x1 = new X(), x2 = new X(); | ||
scope.put("x1", scope, x1); | ||
scope.put("x2", scope, x2); | ||
scope.put("src", scope, new X('X')); | ||
Object ret = | ||
cx.evaluateString( | ||
scope, | ||
"x1.bigCharacter = 65;" | ||
+ "x1.primitiveCharacter = 65;" | ||
+ "x2.bigCharacter = src.primitiveCharacter;", | ||
"myScript.js", | ||
1, | ||
null); | ||
assertEquals('A', x1.bigCharacter); | ||
assertEquals('A', x1.primitiveCharacter); | ||
assertEquals('X', x2.bigCharacter); | ||
return null; | ||
}); | ||
} | ||
} |