Skip to content

Commit a13cd83

Browse files
committed
test(runtime): cover non-ASCII Java char conversions and boolean field reads
Specs pass U+00E9, U+20AC, U+4E2D, a lone high surrogate, an ASCII control and ASCII through every char path: char() and plain string arguments, char return values, JS array to char[], char[] element get and set, instance and static char fields, and a char() value returned from a JS callback. Java-side values are read back as ints and the element read uses a Java-created char[], so a broken direction cannot be masked by the opposite one. They also pin the empty-string (U+0000) and multi-unit (first code unit) behaviour, and read instance and static boolean fields.
1 parent 4058cb4 commit a13cd83

2 files changed

Lines changed: 160 additions & 1 deletion

File tree

‎test-app/app/src/main/assets/app/tests/testPrimitiveTypeConversion.js‎

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,122 @@ describe("Tests primitive type conversion", function () {
2424
});
2525

2626

27-
});
27+
});
28+
describe("Tests char conversion of non-ASCII values", function () {
29+
30+
var T = com.tns.tests.CharConversionTest;
31+
32+
// U+00E9, U+20AC, U+4E2D, a lone high surrogate, an ASCII control, ASCII.
33+
var samples = ["é", "€", "中", "\ud83d", "\u0007", "x"];
34+
35+
function codeUnits(javaIntArray) {
36+
var result = [];
37+
for (var i = 0; i < javaIntArray.length; i++) {
38+
result.push(javaIntArray[i]);
39+
}
40+
return result;
41+
}
42+
43+
function expectedUnits(values) {
44+
return values.map(function (s) { return s.charCodeAt(0); });
45+
}
46+
47+
it("char_conversion_char_cast_argument_keeps_the_utf16_code_unit", function () {
48+
samples.forEach(function (s) {
49+
expect(T.codeUnitOf(char(s))).toBe(s.charCodeAt(0));
50+
});
51+
});
52+
53+
it("char_conversion_char_return_value_round_trips", function () {
54+
samples.forEach(function (s) {
55+
expect(T.echo(char(s))).toBe(s);
56+
});
57+
});
58+
59+
it("char_conversion_js_array_to_char_array_argument", function () {
60+
expect(codeUnits(T.codeUnitsOf(samples))).toEqual(expectedUnits(samples));
61+
});
62+
63+
it("char_conversion_char_array_element_get", function () {
64+
var arr = T.charsOf(samples.join(""));
65+
for (var i = 0; i < samples.length; i++) {
66+
expect(arr[i]).toBe(samples[i]);
67+
}
68+
});
69+
70+
it("char_conversion_char_array_element_set", function () {
71+
var arr = T.charsOf("aaaaaa");
72+
for (var i = 0; i < samples.length; i++) {
73+
arr[i] = samples[i];
74+
}
75+
expect(codeUnits(T.codeUnitsOf(arr))).toEqual(expectedUnits(samples));
76+
for (var j = 0; j < samples.length; j++) {
77+
expect(arr[j]).toBe(samples[j]);
78+
}
79+
});
80+
81+
it("char_conversion_element_set_takes_first_code_unit_or_nul", function () {
82+
var arr = T.charsOf("aaa");
83+
arr[0] = "€b";
84+
arr[1] = "";
85+
arr[2] = "😀";
86+
expect(codeUnits(T.codeUnitsOf(arr))).toEqual([0x20ac, 0, 0xd83d]);
87+
});
88+
89+
it("char_conversion_char_field_set_and_get", function () {
90+
var t = new T();
91+
samples.forEach(function (s) {
92+
t.charField = s;
93+
expect(t.charFieldCodeUnit()).toBe(s.charCodeAt(0));
94+
expect(t.charField).toBe(s);
95+
96+
T.staticCharField = s;
97+
expect(T.staticCharFieldCodeUnit()).toBe(s.charCodeAt(0));
98+
expect(T.staticCharField).toBe(s);
99+
});
100+
});
101+
102+
it("char_conversion_char_field_set_takes_first_code_unit_or_nul", function () {
103+
var t = new T();
104+
t.charField = "€b";
105+
expect(t.charFieldCodeUnit()).toBe(0x20ac);
106+
t.charField = "";
107+
expect(t.charFieldCodeUnit()).toBe(0);
108+
});
109+
110+
it("char_conversion_char_cast_returned_from_js_callback", function () {
111+
var seen = [];
112+
var callback = new com.tns.tests.CharConversionTest.CharEcho({
113+
echo: function (c) {
114+
seen.push(c);
115+
return char(c);
116+
}
117+
});
118+
samples.forEach(function (s) {
119+
expect(T.codeUnitFromCallback(callback, char(s))).toBe(s.charCodeAt(0));
120+
});
121+
expect(seen).toEqual(samples);
122+
});
123+
124+
it("char_conversion_plain_string_argument_takes_first_code_unit_or_nul", function () {
125+
samples.forEach(function (s) {
126+
expect(T.codeUnitOf(s)).toBe(s.charCodeAt(0));
127+
expect(T.echo(s)).toBe(s);
128+
expect(T.codeUnitOf(new String(s))).toBe(s.charCodeAt(0));
129+
});
130+
expect(T.codeUnitOf("\u20acb")).toBe(0x20ac);
131+
expect(T.codeUnitOf("")).toBe(0);
132+
});
133+
134+
it("boolean_field_get_returns_the_java_value", function () {
135+
var t = new T();
136+
expect(t.booleanField).toBe(false);
137+
t.booleanField = true;
138+
expect(t.booleanField).toBe(true);
139+
140+
T.staticBooleanField = true;
141+
expect(T.staticBooleanField).toBe(true);
142+
T.staticBooleanField = false;
143+
expect(T.staticBooleanField).toBe(false);
144+
});
145+
});

‎test-app/app/src/main/java/com/tns/tests/CharConversionTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,49 @@ public static interface Logger {
55
public void print(Object value);
66
}
77

8+
public static interface CharEcho {
9+
char echo(char value);
10+
}
11+
812
public static void printWithChar(Logger logger, char value) {
913
assert logger != null;
1014
logger.print(value);
1115
}
16+
17+
public char charField;
18+
public static char staticCharField;
19+
public boolean booleanField;
20+
public static boolean staticBooleanField;
21+
22+
public static int codeUnitOf(char value) {
23+
return value;
24+
}
25+
26+
public static char echo(char value) {
27+
return value;
28+
}
29+
30+
public static int[] codeUnitsOf(char[] values) {
31+
int[] units = new int[values.length];
32+
for (int i = 0; i < values.length; i++) {
33+
units[i] = values[i];
34+
}
35+
return units;
36+
}
37+
38+
public static char[] charsOf(String value) {
39+
return value.toCharArray();
40+
}
41+
42+
public int charFieldCodeUnit() {
43+
return charField;
44+
}
45+
46+
public static int staticCharFieldCodeUnit() {
47+
return staticCharField;
48+
}
49+
50+
public static int codeUnitFromCallback(CharEcho callback, char value) {
51+
return callback.echo(value);
52+
}
1253
}

0 commit comments

Comments
 (0)