-
Notifications
You must be signed in to change notification settings - Fork 4
/
StringsTest.java
357 lines (314 loc) · 10.7 KB
/
StringsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package com.endoflineblog.truffle.part_11;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/**
* This is a set of unit tests for testing strings in EasyScript.
*/
public class StringsTest {
private Context context;
@BeforeEach
public void setUp() {
this.context = Context.newBuilder()
.option("cpusampler", "true")
.build();
}
@AfterEach
public void tearDown() {
this.context.close();
}
@Test
public void strings_can_be_created_with_single_quotes() {
Value result = this.context.eval("ezs",
" '' "
);
assertTrue(result.isString());
assertEquals("", result.asString());
}
@Test
public void single_quote_strings_can_contain_a_single_quote_by_escaping_it() {
Value result = this.context.eval("ezs",
" '\\'' "
);
assertTrue(result.isString());
assertEquals("'", result.asString());
}
@Test
public void strings_can_be_created_with_double_quotes() {
Value result = this.context.eval("ezs",
" \"\" "
);
assertTrue(result.isString());
assertEquals("", result.asString());
}
@Test
public void double_quote_strings_can_contain_a_double_quote_by_escaping_it() {
Value result = this.context.eval("ezs",
" \"\\\"\" "
);
assertTrue(result.isString());
assertEquals("\"", result.asString());
}
@Test
public void empty_string_is_falsy() {
Value result = this.context.eval("ezs", "" +
"let ret; " +
"if ('') { " +
" ret = 'empty string is truthy'; " +
"} else { " +
" ret = 'empty string is falsy'; " +
"} " +
"ret"
);
assertEquals("empty string is falsy", result.asString());
}
@Test
public void blank_string_is_truthy() {
Value result = this.context.eval("ezs", "" +
"let ret; " +
"if (' ') { " +
" ret = 'blank string is truthy'; " +
"} else { " +
" ret = 'blank string is falsy'; " +
"} " +
"ret"
);
assertEquals("blank string is truthy", result.asString());
}
@Test
public void strings_can_be_concatenated() {
Value result = this.context.eval("ezs",
" 'abc' + '_' + 'def' "
);
assertEquals("abc_def", result.asString());
}
@Test
public void properties_can_be_accessed_through_indexing() {
Value result = this.context.eval("ezs", "" +
"const arr = [0, 1, 2]; " +
"arr['length']"
);
assertEquals(3, result.asInt());
}
@Test
public void array_property_writes_through_indexing_are_ignored() {
Value result = this.context.eval("ezs", "" +
"const arr = [0, 1, 2]; " +
"const result = arr['length'] = 5; " +
"[result, arr.length]"
);
assertEquals(5, result.getArrayElement(0).asInt());
assertEquals(3, result.getArrayElement(1).asInt());
}
@Test
public void strings_have_a_length_property() {
Value result = this.context.eval("ezs",
" 'length'.length"
);
assertEquals(6, result.asInt());
}
@Test
public void string_properties_can_be_accessed_through_indexing() {
Value result = this.context.eval("ezs", "" +
"var l = 'length'; " +
"l[l]"
);
assertEquals(6, result.asInt());
}
@Test
public void indexed_property_writes_to_strings_have_no_effect() {
Value result = this.context.eval("ezs", "" +
"const str = 'abc'; " +
"const result = str['length'] = 5; " +
"[result, str.length]"
);
assertEquals(5, result.getArrayElement(0).asInt());
assertEquals(3, result.getArrayElement(1).asInt());
}
@Test
public void ezs_strings_in_polyglot_context_have_no_members() {
Value result = this.context.eval("ezs", " 'a' ");
assertTrue(result.isString());
assertFalse(result.hasMembers());
}
@Test
public void java_strings_can_be_used_for_indexing() {
this.context.eval("ezs", "" +
"function access(o, propertyName) { " +
" return o[propertyName]; " +
"} " +
"let str = 'ab'; "
);
Value ezsBindings = this.context.getBindings("ezs");
Value str = ezsBindings.getMember("str");
Value access = ezsBindings.getMember("access");
assertEquals(2, access.execute(str, "length").asInt());
}
@Test
public void strings_can_be_indexed() {
Value result = this.context.eval("ezs",
" 'abc'[1][0] "
);
assertEquals("b", result.asString());
}
@Test
public void strings_indexed_out_of_range_return_undefined() {
Value result = this.context.eval("ezs",
" 'abc'[-1] "
);
assertTrue(result.isNull());
assertEquals("undefined", result.toString());
}
@Test
public void strings_can_be_compared_for_equality() {
Value result = this.context.eval("ezs", "" +
"let ret = 'string equality is broken'; " +
"if ('abc' === 'a' + 'b' + 'c') " +
" ret = 'string equality works correctly'; " +
"ret"
);
assertEquals("string equality works correctly", result.asString());
}
@Test
public void strings_can_be_compared_with_less() {
Value result = this.context.eval("ezs",
" 'a' < 'b' "
);
assertTrue(result.asBoolean());
}
@Test
public void concatenated_strings_have_a_length_property() {
Value result = this.context.eval("ezs",
"('abc' + 'def').length"
);
assertEquals(6, result.asInt());
}
@Test
public void strings_have_a_charAt_method() {
Value result = this.context.eval("ezs",
" 'abc'.charAt(2)"
);
assertEquals("c", result.asString());
}
@Test
public void charAt_without_argument_defaults_to_0() {
Value result = this.context.eval("ezs",
" 'abc'.charAt()"
);
assertEquals("a", result.asString());
}
@Test
public void charAt_outside_range_returns_empty_string() {
Value result = this.context.eval("ezs",
" 'abc'.charAt(3) + 'abc'.charAt(-1)"
);
assertEquals("", result.asString());
}
@Test
public void charAt_called_on_an_empty_string_without_arguments_returns_empty_string() {
Value result = this.context.eval("ezs",
" ''.charAt()"
);
assertEquals("", result.asString());
}
@Test
public void methods_ignore_extra_arguments() {
Value result = this.context.eval("ezs",
" 'abc'.charAt(1, 2, 99)"
);
assertEquals("b", result.asString());
}
@Test
public void unknown_string_property_returns_undefined() {
Value result = this.context.eval("ezs", " 'a'.someProp");
assertTrue(result.isNull());
assertEquals("undefined", result.toString());
}
@Test
public void unknown_string_member_read_through_GraalVM_interop_throws() {
Value str = this.context.eval("ezs", "'my-string'");
assertFalse(str.hasMembers());
try {
str.getMember("doesNotExist");
fail("expected String.getMember() to throw");
} catch (UnsupportedOperationException e) {
// nothing to do here
}
}
@Test
public void methods_correctly_resolve_their_targets() {
Value result = this.context.eval("ezs", "" +
"function firstChar(str) { " +
" return str.charAt(0); " +
"} " +
"firstChar('A'); " +
"firstChar('B'); " +
"firstChar('C'); " +
"firstChar('D'); "
);
assertEquals("D", result.asString());
}
@Test
void string_properties_work_after_reading_non_existing_property() {
this.context.eval("ezs", "" +
"function readProp(str, prop) { " +
" return str[prop]; " +
"} " +
"let v1 = readProp('', 'does not exist'); " +
"let v2 = readProp('', 'length');");
Value ezsBindings = this.context.getBindings("ezs");
Value v1 = ezsBindings.getMember("v1");
Value v2 = ezsBindings.getMember("v2");
assertTrue(v1.isNull());
assertEquals(0, v2.asInt());
}
@Test
void chatAt_works_after_passing_it_undefined() {
Value charAtStr = this.context.eval("ezs", "" +
"function charAtStr(index) { " +
" return 'str'.charAt(index); " +
"} " +
"charAtStr; ");
assertEquals("s", charAtStr.execute().asString());
assertEquals("t", charAtStr.execute(1).asString());
assertEquals("r", charAtStr.execute(2).asString());
}
@Test
public void Math_props_can_be_accessed_through_indexing() {
Value result = this.context.eval("ezs",
"Math['abs'](-4)"
);
assertEquals(4, result.asInt());
}
@Test
public void string_index_writes_are_ignored() {
Value result = this.context.eval("ezs", "" +
"let s = 'a'; " +
"const tmp = s[0] = 'b'; " +
"s + tmp"
);
assertEquals("ab", result.asString());
}
@Test
public void count_algorithm_returns_its_input() {
int input = 10_000;
Value result = this.context.eval("ezs", "" +
"function countWhileCharAtIndexProp(n) { " +
" var ret = 0; " +
" while (n > 0) { " +
" n = n - ('a'['charAt'](0) + ''['charAt']())['length']; " +
" ret = ret + 1; " +
" } " +
" return ret; " +
"}" +
"countWhileCharAtIndexProp(" + input + ");"
);
assertEquals(input, result.asInt());
}
}