rapydscript-ns 0.9.3 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/.agignore +1 -1
  2. package/.github/workflows/ci.yml +38 -38
  3. package/=template.pyj +5 -5
  4. package/CHANGELOG.md +18 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_GAPS.md +52 -142
  8. package/README.md +51 -21
  9. package/TODO.md +1 -26
  10. package/add-toc-to-readme +2 -2
  11. package/bin/export +75 -75
  12. package/bin/rapydscript +0 -0
  13. package/bin/web-repl-export +102 -102
  14. package/build +2 -2
  15. package/language-service/index.js +88 -36
  16. package/package.json +1 -1
  17. package/publish.py +37 -37
  18. package/release/baselib-plain-pretty.js +157 -31
  19. package/release/baselib-plain-ugly.js +5 -5
  20. package/release/compiler.js +724 -426
  21. package/release/signatures.json +29 -29
  22. package/session.vim +4 -4
  23. package/setup.cfg +2 -2
  24. package/src/ast.pyj +7 -0
  25. package/src/baselib-containers.pyj +41 -4
  26. package/src/baselib-errors.pyj +4 -3
  27. package/src/baselib-internal.pyj +47 -18
  28. package/src/baselib-str.pyj +16 -3
  29. package/src/compiler.pyj +36 -36
  30. package/src/errors.pyj +30 -30
  31. package/src/lib/aes.pyj +646 -646
  32. package/src/lib/collections.pyj +227 -3
  33. package/src/lib/copy.pyj +120 -120
  34. package/src/lib/elementmaker.pyj +83 -83
  35. package/src/lib/encodings.pyj +126 -126
  36. package/src/lib/gettext.pyj +569 -569
  37. package/src/lib/itertools.pyj +580 -580
  38. package/src/lib/math.pyj +193 -193
  39. package/src/lib/operator.pyj +11 -11
  40. package/src/lib/pprint.pyj +455 -0
  41. package/src/lib/random.pyj +118 -118
  42. package/src/lib/react.pyj +74 -74
  43. package/src/lib/statistics.pyj +0 -0
  44. package/src/lib/traceback.pyj +63 -63
  45. package/src/lib/uuid.pyj +77 -77
  46. package/src/monaco-language-service/completions.js +21 -14
  47. package/src/monaco-language-service/diagnostics.js +2 -2
  48. package/src/monaco-language-service/dts.js +58 -15
  49. package/src/monaco-language-service/package.json +3 -0
  50. package/src/output/classes.pyj +25 -2
  51. package/src/output/codegen.pyj +4 -1
  52. package/src/output/comments.pyj +45 -45
  53. package/src/output/exceptions.pyj +201 -201
  54. package/src/output/jsx.pyj +164 -164
  55. package/src/output/treeshake.pyj +182 -182
  56. package/src/output/utils.pyj +72 -72
  57. package/src/parse.pyj +42 -7
  58. package/src/string_interpolation.pyj +72 -72
  59. package/src/tokenizer.pyj +18 -2
  60. package/src/unicode_aliases.pyj +576 -576
  61. package/src/utils.pyj +192 -192
  62. package/test/_import_one.pyj +37 -37
  63. package/test/_import_two/__init__.pyj +11 -11
  64. package/test/_import_two/level2/deep.pyj +4 -4
  65. package/test/_import_two/other.pyj +6 -6
  66. package/test/_import_two/sub.pyj +13 -13
  67. package/test/aes_vectors.pyj +421 -421
  68. package/test/annotations.pyj +80 -80
  69. package/test/baselib.pyj +23 -0
  70. package/test/chainmap.pyj +185 -0
  71. package/test/dataclasses.pyj +3 -4
  72. package/test/decorators.pyj +77 -77
  73. package/test/docstrings.pyj +39 -39
  74. package/test/elementmaker_test.pyj +45 -45
  75. package/test/enum.pyj +1 -1
  76. package/test/functions.pyj +151 -151
  77. package/test/generators.pyj +41 -41
  78. package/test/generic.pyj +370 -370
  79. package/test/internationalization.pyj +73 -73
  80. package/test/lint.pyj +164 -164
  81. package/test/loops.pyj +85 -85
  82. package/test/numpy.pyj +734 -734
  83. package/test/pprint.pyj +232 -0
  84. package/test/python_features.pyj +1 -1
  85. package/test/repl.pyj +121 -121
  86. package/test/scoped_flags.pyj +76 -76
  87. package/test/statistics.pyj +224 -0
  88. package/test/str.pyj +4 -4
  89. package/test/unit/index.js +455 -0
  90. package/test/unit/language-service-completions.js +2 -0
  91. package/test/unit/language-service-dts.js +113 -0
  92. package/test/unit/language-service-hover.js +455 -455
  93. package/test/unit/language-service.js +135 -2
  94. package/test/unit/web-repl.js +349 -1
  95. package/tools/compiler.d.ts +367 -367
  96. package/tools/completer.js +131 -131
  97. package/tools/export.js +4 -2
  98. package/tools/gettext.js +185 -185
  99. package/tools/ini.js +65 -65
  100. package/tools/msgfmt.js +187 -187
  101. package/tools/repl.js +223 -223
  102. package/tools/test.js +118 -118
  103. package/tools/utils.js +141 -128
  104. package/tools/web_repl.js +95 -95
  105. package/try +41 -41
  106. package/web-repl/env.js +196 -196
  107. package/web-repl/index.html +163 -163
  108. package/web-repl/prism.css +139 -139
  109. package/web-repl/prism.js +113 -113
  110. package/web-repl/rapydscript.js +228 -226
  111. package/web-repl/sha1.js +25 -25
package/test/generic.pyj CHANGED
@@ -1,370 +1,370 @@
1
- # globals: assrt, ρσ_last_exception
2
-
3
- import traceback
4
-
5
- def throw_test(code):
6
- assrt.throws(def():
7
- RapydScript.parse(code, {'filename':code}).body[0]
8
- , RapydScript.SyntaxError)
9
-
10
- # unary operators
11
- assrt.equal(-(1), -1)
12
- assrt.equal(-(-1), 1)
13
- assrt.equal(+(+1), 1)
14
-
15
- # arithmetic
16
- assrt.equal(3**4, Math.pow(3, 4))
17
- assrt.equal(100**-2, Math.pow(100, -2))
18
- assrt.equal(2*5**2*3, 150)
19
- assrt.equal(-2**2, -4)
20
- assrt.equal((-2)**2, 4)
21
- assrt.equal(pow(2, 2, 2), 0)
22
- assrt.equal(100 // 3, 33)
23
- assrt.equal(-100 // 3, -34)
24
- a = 100
25
- a //= 3
26
- assrt.equal(a, 33)
27
- assrt.equal(0b11, 3)
28
- assrt.ok(42 / "x" is NaN)
29
- assrt.ok(42 is not NaN)
30
- assrt.ok(NaN is parseInt('asd', 10))
31
- assrt.ok('NaN' is not 42 / 'x')
32
-
33
- # comparisons
34
- assrt.ok(3<5<7)
35
- assrt.equal(-1 < 0 == 1 < 0, False)
36
-
37
- # Empty tuple
38
- assrt.deepEqual((), [])
39
-
40
- # Conditional operators
41
- assrt.equal(1 if True else 2, 1)
42
- assrt.equal(1
43
- if True else 2, 1)
44
- assrt.deepEqual([x for x in ("asd" if True else "xyz") if x], 'asd'.split(''))
45
- assrt.deepEqual((1, [x for x in [2] if x]), [1, [2]])
46
-
47
- # Comprehensions
48
- assrt.deepEqual([a+1 for a in [1,2,3] if a > 1], [3, 4])
49
- assrt.deepEqual({a+1:a+2 for a in [1,2,3] if a > 1}, {3:4, 4:5})
50
- assrt.deepEqual({a+1 for a in [1,2,3] if a > 1}, set([3, 4]))
51
- assrt.deepEqual([(a+1, 1) for a in [1,2,3] if a > 1], [[3, 1], [4, 1]])
52
-
53
- # Destructuring assignment
54
- a, (b, (c, d)), e = [1, [2, [3, 4]], 5]
55
- assrt.deepEqual([a, b, c, d, e], [1, 2, 3, 4, 5])
56
- for x, (y, z) in [ [1, [2, 3]] ]:
57
- assrt.deepEqual([x, y, z], [1, 2, 3])
58
- assrt.deepEqual([ [x, y, z, w] for ((x, y), (z, w)) in [ [[1, 2], [3, 4]] ] ],
59
- [[1, 2, 3, 4]])
60
- assrt.deepEqual([ [x, y, z] for x, (y, z) in [ [1, [2, 3] ] ]],
61
- [[1, 2, 3]])
62
- (a, b) = [1, 2]
63
- assrt.deepEqual([a, b], [1, 2])
64
- (a, (b, c)) = [1, [2, 3]]
65
- assrt.deepEqual([a, b, c], [1, 2, 3])
66
-
67
- # Chained assignment
68
- a = b = 11
69
- assrt.deepEqual([a, b], [11, 11])
70
- def inc():
71
- nonlocal a
72
- a += 1
73
- return a
74
- a = b = inc()
75
- assrt.deepEqual([a, b], [12, 12])
76
- a, b = c, d = 1, 2
77
- assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
78
- (a, b) = [c, d] = 1, 2
79
- assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
80
- a, (x, b) = c, d = [1, [2, 3]]
81
- assrt.deepEqual([a, x, b, c, d], [1, 2, 3, 1, [2, 3]])
82
- a, b = [c, d] = [e, f] = [1, 2]
83
- assrt.deepEqual([a, b, c, d, e, f], [1, 2, 1, 2, 1, 2])
84
- a = b, c = 1, 2
85
- assrt.deepEqual([a, b, c], [[1,2], 1, 2])
86
-
87
- throw_test('a += b += 1')
88
- throw_test('a += 1, 2 += b')
89
- throw_test('a += [1, 2] += b')
90
- throw_test('function = 1')
91
- throw_test('def function():\n pass')
92
- throw_test('class function:\n pass')
93
- throw_test('while 1:\npass')
94
- throw_test('def f():\n while 1:\n pass')
95
- throw_test('1 1')
96
- throw_test('z = x y')
97
- throw_test('while a = 1: pass')
98
- throw_test('while 1\n pass')
99
- throw_test('for a in [1]\n pass')
100
-
101
- # object literals
102
- {1:
103
- 1
104
- } # Check that arbitrary indentation is allowed after : for an object literal key
105
-
106
- # strict equality
107
- assrt(1 == 1) # number vs number: ok
108
- assrt(True == True) # boolean vs boolean: ok
109
- assrt(not (1 == True)) # number vs boolean: NEVER equal
110
- assrt(1 != True) # number vs boolean: NEVER equal
111
- assrt(not ("" == False)) # string vs boolean: NEVER equal
112
- assrt(not ("0" == 0)) # string vs integer: NEVER equal
113
- assrt(not ("" == 0)) # string vs integer: NEVER equal
114
- assrt(bool(1) == True) # boolean conversion
115
- assrt(bool("") == False) # boolean conversion
116
- assrt(v'1 == true') # javascript override
117
- assrt(not v'(1 != true)') # javascript override
118
- assrt(v'String("test")' == "test") # this should do string conversion rather than creating a string object
119
- assrt(String("test") != "test") # this should create a string object
120
-
121
-
122
- # raw JavaScript
123
- v'var def = {}' # ability to bypass reserved keywords in declaration
124
- v'def'.item = 3 # ability to use reserved keywords
125
- assrt(v'def.item' == 3)
126
-
127
- n = 5
128
- assrt.equal(v"""
129
- (function() {
130
- var s = 0
131
- for (var i=0; i<=n; i++) {
132
- s += i
133
- }
134
- return s
135
- })()
136
- """, (def():
137
- s = 0
138
- for i in range(n+1):
139
- s += i
140
- return s
141
- )()) # shared scoping and equivalent capability
142
- a = []
143
- for v'var i = 0; i < 1; i++':
144
- a[i] = 1 # noqa: undef
145
- assrt.deepEqual(a, [1])
146
-
147
- # String literals
148
- a = '\u00ad'
149
- assrt.equal(a.charCodeAt(0), 0xad)
150
- # String literal concatenation
151
-
152
- assrt.equal('1' '2', '12')
153
- assrt.equal('1'
154
- '2' '3', '123')
155
- assrt.equal(u'a', 'a')
156
- assrt.equal('a' * 5, 'aaaaa')
157
- assrt.ok(isinstance('a', str))
158
-
159
- # Various bits of miscellaneous code that caused parser infinite loops and other breakage
160
- throw_test('for a in b:\\n 1+1')
161
-
162
- def localvars_in_comprehension():
163
- return {k:i for i, k in enumerate([1,2])}
164
- assrt.deepEqual(localvars_in_comprehension(), {'1':0, '2':1})
165
-
166
- # Numbers
167
- assrt.equal(1e2, 100)
168
- assrt.equal(-1e2, -100)
169
- assrt.equal(1e+2, 100)
170
- assrt.equal(1E-1, 0.1)
171
-
172
- # Equality operators
173
-
174
- class D:
175
-
176
- def __init__(self, data):
177
- self.data = data
178
-
179
- def __eq__(self, other):
180
- return self.data == other.data
181
-
182
- assrt.ok(D(1) == D(1))
183
- assrt.ok(D(2) != D(1))
184
- assrt.ok(D(1) == {'data':1})
185
- assrt.ok({'data':1} == D(1))
186
-
187
- arr = []
188
- {} == arr.append(2)
189
- assrt.ok(arr.length is 1)
190
-
191
- call_count = 0
192
- def cc():
193
- nonlocal call_count
194
- call_count += 1
195
- return [1]
196
- cc()[-1]
197
- assrt.equal(call_count, 1)
198
-
199
- # Test the del operator
200
-
201
- # JS object
202
- deleteme = {1:1}
203
- assrt.ok(Object.keys(deleteme).indexOf('1') is not -1)
204
- del deleteme[1]
205
- assrt.ok(Object.keys(deleteme).indexOf('1') is -1)
206
-
207
- # list
208
- deleteme = [1, 2]
209
- del deleteme[0]
210
- assrt.equal(deleteme.length, 1)
211
- assrt.deepEqual(deleteme, [2])
212
- deleteme = [1, 2, 3]
213
- del deleteme[::2]
214
- assrt.deepEqual(deleteme, [2])
215
- deleteme = [1, 2, 3]
216
- del deleteme[::1]
217
- assrt.equal(deleteme.length, 0)
218
- deleteme = [1, 2, 3]
219
- del deleteme[1:2]
220
- assrt.deepEqual(deleteme, [1, 3])
221
-
222
- # global
223
- del deleteme
224
-
225
- # python dict
226
- from __python__ import dict_literals, overload_getitem
227
- deleteme = {1:1, 2:2}
228
- assrt.ok(1 in deleteme)
229
- del deleteme[1]
230
- assrt.ok(1 not in deleteme)
231
- assrt.equal(len(deleteme), 1)
232
-
233
- # Asserts
234
-
235
- assrt.throws(def():
236
- assert 0
237
- , AssertionError
238
- )
239
- assrt.throws(def():
240
- assert 0, 'xdx'
241
- , /xdx/
242
- )
243
-
244
- # Exceptions
245
-
246
- class MyException(Exception):
247
-
248
- def __init__(self, message, val):
249
- Exception.__init__(self, message)
250
- self.val = val
251
-
252
- try:
253
- raise MyException('xxx', 3)
254
- except MyException as e:
255
- assrt.equal(e.name, 'MyException')
256
- assrt.equal(e.message, 'xxx')
257
- assrt.equal(e.val, 3)
258
- assrt.equal(e.toString(), 'MyException: xxx')
259
- assrt.ok(v'e instanceof MyException')
260
- assrt.ok(v'e instanceof Error')
261
-
262
- try:
263
- raise Error('eee')
264
- except Exception:
265
- # Test that we can catch JS errors and that local variables in the except block are declared
266
- xxlocalvar = 1
267
- assrt.equal(xxlocalvar, 1)
268
-
269
- def errf():
270
- raise MyException()
271
-
272
- try:
273
- errf()
274
- except MyException as e:
275
- fe = traceback.format_exception()
276
- assrt.ok(str.strip(fe[-2]).startsWith('at errf'))
277
-
278
-
279
- def stackf():
280
- return traceback.format_stack()
281
- assrt.ok(str.strip(stackf()[-1]).startsWith('at stackf'))
282
-
283
-
284
- def else_without_finally(fail):
285
- ans = []
286
- try:
287
- if fail:
288
- raise Exception('a')
289
- ans.push('ok')
290
- except:
291
- ans.append('ex')
292
- else:
293
- ans.append('el')
294
- return ans
295
-
296
- def else_with_finally(fail):
297
- ans = []
298
- try:
299
- if fail:
300
- raise Exception('a')
301
- ans.push('ok')
302
- except:
303
- ans.push('ex')
304
- else:
305
- ans.push('el')
306
- finally:
307
- ans.push('fi')
308
- return ans
309
-
310
- def exc_in_else():
311
- ans = []
312
- try:
313
- try:
314
- ans.push('ok')
315
- except:
316
- pass
317
- else:
318
- raise Exception('1')
319
- finally:
320
- ans.push('fi')
321
- except:
322
- ans.push('ex')
323
- return ans
324
-
325
- assrt.deepEqual(else_without_finally(), ['ok', 'el'])
326
- assrt.deepEqual(else_without_finally(True), ['ex'])
327
- assrt.deepEqual(else_with_finally(), ['ok', 'el', 'fi'])
328
- assrt.deepEqual(else_with_finally(True), ['ex', 'fi'])
329
- assrt.deepEqual(exc_in_else(), ['ok', 'fi', 'ex'])
330
-
331
- # Existential operator
332
-
333
- assrt.ok(not gfsgs?)
334
- assrt.equal(gfsgs ? 1, 1)
335
- assrt.equal(gfsgs?.b, undefined)
336
- assrt.equal(gfsgs?(), undefined) # noqa: undef
337
- undef = undefined
338
- assrt.ok(not undef?)
339
- undef = None
340
- assrt.ok(not undef?)
341
- undef = 0
342
- assrt.ok(undef?)
343
- assrt.equal(undef?(), undefined)
344
- assrt.equal(undef?.xxx?(), undefined)
345
- assrt.equal(undef.a, undefined)
346
- assrt.equal(undef[1], undefined)
347
- assrt.equal(undef ? 1, 0)
348
- ml = 1, '''
349
- '''
350
- assrt.deepEqual(ml, [1, '\n'])
351
-
352
-
353
- # localvars in conditions
354
-
355
- gv = 1
356
- (def ():
357
- if (gv = 2):
358
- pass
359
- assrt.equal(gv, 2)
360
- )()
361
- assrt.equal(gv, 1)
362
-
363
- assrt.ok(1 not in [2, 3])
364
- assrt.ok(1 in [1, 2])
365
- assrt.ok("1" + "2" in ["12", "x"])
366
- assrt.ok("1" + "2" not in ["1", "2"])
367
- if "1" + "2" not in ["1", "2"]:
368
- assrt.ok(1)
369
- else:
370
- assrt.ok(0)
1
+ # globals: assrt, ρσ_last_exception
2
+
3
+ import traceback
4
+
5
+ def throw_test(code):
6
+ assrt.throws(def():
7
+ RapydScript.parse(code, {'filename':code}).body[0]
8
+ , RapydScript.SyntaxError)
9
+
10
+ # unary operators
11
+ assrt.equal(-(1), -1)
12
+ assrt.equal(-(-1), 1)
13
+ assrt.equal(+(+1), 1)
14
+
15
+ # arithmetic
16
+ assrt.equal(3**4, Math.pow(3, 4))
17
+ assrt.equal(100**-2, Math.pow(100, -2))
18
+ assrt.equal(2*5**2*3, 150)
19
+ assrt.equal(-2**2, -4)
20
+ assrt.equal((-2)**2, 4)
21
+ assrt.equal(pow(2, 2, 2), 0)
22
+ assrt.equal(100 // 3, 33)
23
+ assrt.equal(-100 // 3, -34)
24
+ a = 100
25
+ a //= 3
26
+ assrt.equal(a, 33)
27
+ assrt.equal(0b11, 3)
28
+ assrt.ok(42 / "x" is NaN)
29
+ assrt.ok(42 is not NaN)
30
+ assrt.ok(NaN is parseInt('asd', 10))
31
+ assrt.ok('NaN' is not 42 / 'x')
32
+
33
+ # comparisons
34
+ assrt.ok(3<5<7)
35
+ assrt.equal(-1 < 0 == 1 < 0, False)
36
+
37
+ # Empty tuple
38
+ assrt.deepEqual((), [])
39
+
40
+ # Conditional operators
41
+ assrt.equal(1 if True else 2, 1)
42
+ assrt.equal(1
43
+ if True else 2, 1)
44
+ assrt.deepEqual([x for x in ("asd" if True else "xyz") if x], 'asd'.split(''))
45
+ assrt.deepEqual((1, [x for x in [2] if x]), [1, [2]])
46
+
47
+ # Comprehensions
48
+ assrt.deepEqual([a+1 for a in [1,2,3] if a > 1], [3, 4])
49
+ assrt.deepEqual({a+1:a+2 for a in [1,2,3] if a > 1}, {3:4, 4:5})
50
+ assrt.deepEqual({a+1 for a in [1,2,3] if a > 1}, set([3, 4]))
51
+ assrt.deepEqual([(a+1, 1) for a in [1,2,3] if a > 1], [[3, 1], [4, 1]])
52
+
53
+ # Destructuring assignment
54
+ a, (b, (c, d)), e = [1, [2, [3, 4]], 5]
55
+ assrt.deepEqual([a, b, c, d, e], [1, 2, 3, 4, 5])
56
+ for x, (y, z) in [ [1, [2, 3]] ]:
57
+ assrt.deepEqual([x, y, z], [1, 2, 3])
58
+ assrt.deepEqual([ [x, y, z, w] for ((x, y), (z, w)) in [ [[1, 2], [3, 4]] ] ],
59
+ [[1, 2, 3, 4]])
60
+ assrt.deepEqual([ [x, y, z] for x, (y, z) in [ [1, [2, 3] ] ]],
61
+ [[1, 2, 3]])
62
+ (a, b) = [1, 2]
63
+ assrt.deepEqual([a, b], [1, 2])
64
+ (a, (b, c)) = [1, [2, 3]]
65
+ assrt.deepEqual([a, b, c], [1, 2, 3])
66
+
67
+ # Chained assignment
68
+ a = b = 11
69
+ assrt.deepEqual([a, b], [11, 11])
70
+ def inc():
71
+ nonlocal a
72
+ a += 1
73
+ return a
74
+ a = b = inc()
75
+ assrt.deepEqual([a, b], [12, 12])
76
+ a, b = c, d = 1, 2
77
+ assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
78
+ (a, b) = [c, d] = 1, 2
79
+ assrt.deepEqual([a, b, c, d], [1, 2, 1, 2])
80
+ a, (x, b) = c, d = [1, [2, 3]]
81
+ assrt.deepEqual([a, x, b, c, d], [1, 2, 3, 1, [2, 3]])
82
+ a, b = [c, d] = [e, f] = [1, 2]
83
+ assrt.deepEqual([a, b, c, d, e, f], [1, 2, 1, 2, 1, 2])
84
+ a = b, c = 1, 2
85
+ assrt.deepEqual([a, b, c], [[1,2], 1, 2])
86
+
87
+ throw_test('a += b += 1')
88
+ throw_test('a += 1, 2 += b')
89
+ throw_test('a += [1, 2] += b')
90
+ throw_test('function = 1')
91
+ throw_test('def function():\n pass')
92
+ throw_test('class function:\n pass')
93
+ throw_test('while 1:\npass')
94
+ throw_test('def f():\n while 1:\n pass')
95
+ throw_test('1 1')
96
+ throw_test('z = x y')
97
+ throw_test('while a = 1: pass')
98
+ throw_test('while 1\n pass')
99
+ throw_test('for a in [1]\n pass')
100
+
101
+ # object literals
102
+ {1:
103
+ 1
104
+ } # Check that arbitrary indentation is allowed after : for an object literal key
105
+
106
+ # strict equality
107
+ assrt(1 == 1) # number vs number: ok
108
+ assrt(True == True) # boolean vs boolean: ok
109
+ assrt(not (1 == True)) # number vs boolean: NEVER equal
110
+ assrt(1 != True) # number vs boolean: NEVER equal
111
+ assrt(not ("" == False)) # string vs boolean: NEVER equal
112
+ assrt(not ("0" == 0)) # string vs integer: NEVER equal
113
+ assrt(not ("" == 0)) # string vs integer: NEVER equal
114
+ assrt(bool(1) == True) # boolean conversion
115
+ assrt(bool("") == False) # boolean conversion
116
+ assrt(v'1 == true') # javascript override
117
+ assrt(not v'(1 != true)') # javascript override
118
+ assrt(v'String("test")' == "test") # this should do string conversion rather than creating a string object
119
+ assrt(String("test") != "test") # this should create a string object
120
+
121
+
122
+ # raw JavaScript
123
+ v'var def = {}' # ability to bypass reserved keywords in declaration
124
+ v'def'.item = 3 # ability to use reserved keywords
125
+ assrt(v'def.item' == 3)
126
+
127
+ n = 5
128
+ assrt.equal(v"""
129
+ (function() {
130
+ var s = 0
131
+ for (var i=0; i<=n; i++) {
132
+ s += i
133
+ }
134
+ return s
135
+ })()
136
+ """, (def():
137
+ s = 0
138
+ for i in range(n+1):
139
+ s += i
140
+ return s
141
+ )()) # shared scoping and equivalent capability
142
+ a = []
143
+ for v'var i = 0; i < 1; i++':
144
+ a[i] = 1 # noqa: undef
145
+ assrt.deepEqual(a, [1])
146
+
147
+ # String literals
148
+ a = '\u00ad'
149
+ assrt.equal(a.charCodeAt(0), 0xad)
150
+ # String literal concatenation
151
+
152
+ assrt.equal('1' '2', '12')
153
+ assrt.equal('1'
154
+ '2' '3', '123')
155
+ assrt.equal(u'a', 'a')
156
+ assrt.equal('a' * 5, 'aaaaa')
157
+ assrt.ok(isinstance('a', str))
158
+
159
+ # Various bits of miscellaneous code that caused parser infinite loops and other breakage
160
+ throw_test('for a in b:\\n 1+1')
161
+
162
+ def localvars_in_comprehension():
163
+ return {k:i for i, k in enumerate([1,2])}
164
+ assrt.deepEqual(localvars_in_comprehension(), {'1':0, '2':1})
165
+
166
+ # Numbers
167
+ assrt.equal(1e2, 100)
168
+ assrt.equal(-1e2, -100)
169
+ assrt.equal(1e+2, 100)
170
+ assrt.equal(1E-1, 0.1)
171
+
172
+ # Equality operators
173
+
174
+ class D:
175
+
176
+ def __init__(self, data):
177
+ self.data = data
178
+
179
+ def __eq__(self, other):
180
+ return self.data == other.data
181
+
182
+ assrt.ok(D(1) == D(1))
183
+ assrt.ok(D(2) != D(1))
184
+ assrt.ok(D(1) == {'data':1})
185
+ assrt.ok({'data':1} == D(1))
186
+
187
+ arr = []
188
+ {} == arr.append(2)
189
+ assrt.ok(arr.length is 1)
190
+
191
+ call_count = 0
192
+ def cc():
193
+ nonlocal call_count
194
+ call_count += 1
195
+ return [1]
196
+ cc()[-1]
197
+ assrt.equal(call_count, 1)
198
+
199
+ # Test the del operator
200
+
201
+ # JS object
202
+ deleteme = {1:1}
203
+ assrt.ok(Object.keys(deleteme).indexOf('1') is not -1)
204
+ del deleteme[1]
205
+ assrt.ok(Object.keys(deleteme).indexOf('1') is -1)
206
+
207
+ # list
208
+ deleteme = [1, 2]
209
+ del deleteme[0]
210
+ assrt.equal(deleteme.length, 1)
211
+ assrt.deepEqual(deleteme, [2])
212
+ deleteme = [1, 2, 3]
213
+ del deleteme[::2]
214
+ assrt.deepEqual(deleteme, [2])
215
+ deleteme = [1, 2, 3]
216
+ del deleteme[::1]
217
+ assrt.equal(deleteme.length, 0)
218
+ deleteme = [1, 2, 3]
219
+ del deleteme[1:2]
220
+ assrt.deepEqual(deleteme, [1, 3])
221
+
222
+ # global
223
+ del deleteme
224
+
225
+ # python dict
226
+ from __python__ import dict_literals, overload_getitem
227
+ deleteme = {1:1, 2:2}
228
+ assrt.ok(1 in deleteme)
229
+ del deleteme[1]
230
+ assrt.ok(1 not in deleteme)
231
+ assrt.equal(len(deleteme), 1)
232
+
233
+ # Asserts
234
+
235
+ assrt.throws(def():
236
+ assert 0
237
+ , AssertionError
238
+ )
239
+ assrt.throws(def():
240
+ assert 0, 'xdx'
241
+ , /xdx/
242
+ )
243
+
244
+ # Exceptions
245
+
246
+ class MyException(Exception):
247
+
248
+ def __init__(self, message, val):
249
+ Exception.__init__(self, message)
250
+ self.val = val
251
+
252
+ try:
253
+ raise MyException('xxx', 3)
254
+ except MyException as e:
255
+ assrt.equal(e.name, 'MyException')
256
+ assrt.equal(e.message, 'xxx')
257
+ assrt.equal(e.val, 3)
258
+ assrt.equal(e.toString(), 'MyException: xxx')
259
+ assrt.ok(v'e instanceof MyException')
260
+ assrt.ok(v'e instanceof Error')
261
+
262
+ try:
263
+ raise Error('eee')
264
+ except Exception:
265
+ # Test that we can catch JS errors and that local variables in the except block are declared
266
+ xxlocalvar = 1
267
+ assrt.equal(xxlocalvar, 1)
268
+
269
+ def errf():
270
+ raise MyException()
271
+
272
+ try:
273
+ errf()
274
+ except MyException as e:
275
+ fe = traceback.format_exception()
276
+ assrt.ok(str.strip(fe[-2]).startsWith('at errf'))
277
+
278
+
279
+ def stackf():
280
+ return traceback.format_stack()
281
+ assrt.ok(str.strip(stackf()[-1]).startsWith('at stackf'))
282
+
283
+
284
+ def else_without_finally(fail):
285
+ ans = []
286
+ try:
287
+ if fail:
288
+ raise Exception('a')
289
+ ans.push('ok')
290
+ except:
291
+ ans.append('ex')
292
+ else:
293
+ ans.append('el')
294
+ return ans
295
+
296
+ def else_with_finally(fail):
297
+ ans = []
298
+ try:
299
+ if fail:
300
+ raise Exception('a')
301
+ ans.push('ok')
302
+ except:
303
+ ans.push('ex')
304
+ else:
305
+ ans.push('el')
306
+ finally:
307
+ ans.push('fi')
308
+ return ans
309
+
310
+ def exc_in_else():
311
+ ans = []
312
+ try:
313
+ try:
314
+ ans.push('ok')
315
+ except:
316
+ pass
317
+ else:
318
+ raise Exception('1')
319
+ finally:
320
+ ans.push('fi')
321
+ except:
322
+ ans.push('ex')
323
+ return ans
324
+
325
+ assrt.deepEqual(else_without_finally(), ['ok', 'el'])
326
+ assrt.deepEqual(else_without_finally(True), ['ex'])
327
+ assrt.deepEqual(else_with_finally(), ['ok', 'el', 'fi'])
328
+ assrt.deepEqual(else_with_finally(True), ['ex', 'fi'])
329
+ assrt.deepEqual(exc_in_else(), ['ok', 'fi', 'ex'])
330
+
331
+ # Existential operator
332
+
333
+ assrt.ok(not gfsgs?)
334
+ assrt.equal(gfsgs ? 1, 1)
335
+ assrt.equal(gfsgs?.b, undefined)
336
+ assrt.equal(gfsgs?(), undefined) # noqa: undef
337
+ undef = undefined
338
+ assrt.ok(not undef?)
339
+ undef = None
340
+ assrt.ok(not undef?)
341
+ undef = 0
342
+ assrt.ok(undef?)
343
+ assrt.equal(undef?(), undefined)
344
+ assrt.equal(undef?.xxx?(), undefined)
345
+ assrt.equal(undef.a, undefined)
346
+ assrt.equal(undef[1], undefined)
347
+ assrt.equal(undef ? 1, 0)
348
+ ml = 1, '''
349
+ '''
350
+ assrt.deepEqual(ml, [1, '\n'])
351
+
352
+
353
+ # localvars in conditions
354
+
355
+ gv = 1
356
+ (def ():
357
+ if (gv = 2):
358
+ pass
359
+ assrt.equal(gv, 2)
360
+ )()
361
+ assrt.equal(gv, 1)
362
+
363
+ assrt.ok(1 not in [2, 3])
364
+ assrt.ok(1 in [1, 2])
365
+ assrt.ok("1" + "2" in ["12", "x"])
366
+ assrt.ok("1" + "2" not in ["1", "2"])
367
+ if "1" + "2" not in ["1", "2"]:
368
+ assrt.ok(1)
369
+ else:
370
+ assrt.ok(0)