rapydscript-ns 0.8.3 → 0.8.4

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 (116) 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 +8 -0
  5. package/HACKING.md +103 -103
  6. package/LICENSE +24 -24
  7. package/PYTHON_DIFFERENCES_REPORT.md +2 -2
  8. package/PYTHON_FEATURE_COVERAGE.md +13 -13
  9. package/README.md +670 -6
  10. package/TODO.md +5 -6
  11. package/add-toc-to-readme +2 -2
  12. package/bin/export +75 -75
  13. package/bin/rapydscript +70 -70
  14. package/bin/web-repl-export +102 -102
  15. package/build +2 -2
  16. package/language-service/index.js +155 -6
  17. package/package.json +1 -1
  18. package/publish.py +37 -37
  19. package/release/baselib-plain-pretty.js +2006 -229
  20. package/release/baselib-plain-ugly.js +70 -3
  21. package/release/compiler.js +11554 -3870
  22. package/release/signatures.json +31 -29
  23. package/session.vim +4 -4
  24. package/setup.cfg +2 -2
  25. package/src/ast.pyj +93 -1
  26. package/src/baselib-builtins.pyj +22 -1
  27. package/src/baselib-containers.pyj +99 -0
  28. package/src/baselib-errors.pyj +44 -0
  29. package/src/baselib-internal.pyj +94 -4
  30. package/src/baselib-itertools.pyj +97 -97
  31. package/src/baselib-str.pyj +24 -0
  32. package/src/compiler.pyj +36 -36
  33. package/src/errors.pyj +30 -30
  34. package/src/lib/aes.pyj +646 -646
  35. package/src/lib/copy.pyj +120 -0
  36. package/src/lib/elementmaker.pyj +83 -83
  37. package/src/lib/encodings.pyj +126 -126
  38. package/src/lib/gettext.pyj +569 -569
  39. package/src/lib/itertools.pyj +580 -580
  40. package/src/lib/math.pyj +193 -193
  41. package/src/lib/operator.pyj +11 -11
  42. package/src/lib/pythonize.pyj +20 -20
  43. package/src/lib/random.pyj +118 -118
  44. package/src/lib/re.pyj +470 -470
  45. package/src/lib/react.pyj +74 -0
  46. package/src/lib/traceback.pyj +63 -63
  47. package/src/lib/uuid.pyj +77 -77
  48. package/src/monaco-language-service/builtins.js +5 -0
  49. package/src/monaco-language-service/diagnostics.js +25 -3
  50. package/src/monaco-language-service/dts.js +550 -550
  51. package/src/output/classes.pyj +108 -8
  52. package/src/output/codegen.pyj +16 -2
  53. package/src/output/comments.pyj +45 -45
  54. package/src/output/exceptions.pyj +201 -105
  55. package/src/output/functions.pyj +9 -0
  56. package/src/output/jsx.pyj +164 -0
  57. package/src/output/literals.pyj +28 -2
  58. package/src/output/modules.pyj +1 -1
  59. package/src/output/operators.pyj +8 -2
  60. package/src/output/statements.pyj +2 -2
  61. package/src/output/stream.pyj +1 -0
  62. package/src/output/treeshake.pyj +182 -182
  63. package/src/output/utils.pyj +72 -72
  64. package/src/parse.pyj +417 -113
  65. package/src/string_interpolation.pyj +72 -72
  66. package/src/tokenizer.pyj +29 -0
  67. package/src/unicode_aliases.pyj +576 -576
  68. package/src/utils.pyj +192 -192
  69. package/test/_import_one.pyj +37 -37
  70. package/test/_import_two/__init__.pyj +11 -11
  71. package/test/_import_two/level2/deep.pyj +4 -4
  72. package/test/_import_two/other.pyj +6 -6
  73. package/test/_import_two/sub.pyj +13 -13
  74. package/test/aes_vectors.pyj +421 -421
  75. package/test/annotations.pyj +80 -80
  76. package/test/decorators.pyj +77 -77
  77. package/test/docstrings.pyj +39 -39
  78. package/test/elementmaker_test.pyj +45 -45
  79. package/test/functions.pyj +151 -151
  80. package/test/generators.pyj +41 -41
  81. package/test/generic.pyj +370 -370
  82. package/test/imports.pyj +72 -72
  83. package/test/internationalization.pyj +73 -73
  84. package/test/lint.pyj +164 -164
  85. package/test/loops.pyj +85 -85
  86. package/test/numpy.pyj +734 -734
  87. package/test/omit_function_metadata.pyj +20 -20
  88. package/test/python_features.pyj +19 -6
  89. package/test/regexp.pyj +55 -55
  90. package/test/repl.pyj +121 -121
  91. package/test/scoped_flags.pyj +76 -76
  92. package/test/unit/index.js +2177 -64
  93. package/test/unit/language-service-dts.js +543 -543
  94. package/test/unit/language-service-hover.js +455 -455
  95. package/test/unit/language-service.js +590 -4
  96. package/test/unit/web-repl.js +303 -0
  97. package/tools/cli.js +547 -547
  98. package/tools/compile.js +219 -219
  99. package/tools/completer.js +131 -131
  100. package/tools/embedded_compiler.js +251 -251
  101. package/tools/gettext.js +185 -185
  102. package/tools/ini.js +65 -65
  103. package/tools/msgfmt.js +187 -187
  104. package/tools/repl.js +223 -223
  105. package/tools/test.js +118 -118
  106. package/tools/utils.js +128 -128
  107. package/tools/web_repl.js +95 -95
  108. package/try +41 -41
  109. package/web-repl/env.js +196 -74
  110. package/web-repl/index.html +163 -163
  111. package/web-repl/main.js +252 -254
  112. package/web-repl/prism.css +139 -139
  113. package/web-repl/prism.js +113 -113
  114. package/web-repl/rapydscript.js +224 -102
  115. package/web-repl/sha1.js +25 -25
  116. package/hack_demo.pyj +0 -112
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)