rapydscript-ns 0.8.4 → 0.9.0
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.
- package/.agignore +1 -1
- package/.github/workflows/ci.yml +38 -38
- package/=template.pyj +5 -5
- package/CHANGELOG.md +18 -0
- package/HACKING.md +103 -103
- package/LICENSE +24 -24
- package/README.md +715 -169
- package/TODO.md +9 -2
- package/add-toc-to-readme +2 -2
- package/bin/export +75 -75
- package/bin/rapydscript +70 -70
- package/bin/web-repl-export +102 -102
- package/build +2 -2
- package/language-service/index.js +36 -27
- package/package.json +1 -1
- package/publish.py +37 -37
- package/release/baselib-plain-pretty.js +2358 -168
- package/release/baselib-plain-ugly.js +73 -3
- package/release/compiler.js +6282 -3092
- package/release/signatures.json +31 -30
- package/session.vim +4 -4
- package/setup.cfg +2 -2
- package/src/ast.pyj +1 -0
- package/src/baselib-builtins.pyj +340 -2
- package/src/baselib-bytes.pyj +664 -0
- package/src/baselib-errors.pyj +1 -1
- package/src/baselib-internal.pyj +267 -60
- package/src/baselib-itertools.pyj +110 -97
- package/src/baselib-str.pyj +22 -4
- package/src/compiler.pyj +36 -36
- package/src/errors.pyj +30 -30
- package/src/lib/abc.pyj +317 -0
- package/src/lib/aes.pyj +646 -646
- package/src/lib/copy.pyj +120 -120
- package/src/lib/dataclasses.pyj +532 -0
- package/src/lib/elementmaker.pyj +83 -83
- package/src/lib/encodings.pyj +126 -126
- package/src/lib/enum.pyj +125 -0
- package/src/lib/gettext.pyj +569 -569
- package/src/lib/itertools.pyj +580 -580
- package/src/lib/math.pyj +193 -193
- package/src/lib/operator.pyj +11 -11
- package/src/lib/pythonize.pyj +20 -20
- package/src/lib/random.pyj +118 -118
- package/src/lib/re.pyj +504 -470
- package/src/lib/react.pyj +74 -74
- package/src/lib/traceback.pyj +63 -63
- package/src/lib/typing.pyj +577 -0
- package/src/lib/uuid.pyj +77 -77
- package/src/monaco-language-service/builtins.js +14 -4
- package/src/monaco-language-service/diagnostics.js +19 -20
- package/src/monaco-language-service/dts.js +550 -550
- package/src/output/classes.pyj +62 -26
- package/src/output/comments.pyj +45 -45
- package/src/output/exceptions.pyj +201 -201
- package/src/output/functions.pyj +78 -5
- package/src/output/jsx.pyj +164 -164
- package/src/output/loops.pyj +5 -2
- package/src/output/operators.pyj +100 -34
- package/src/output/treeshake.pyj +182 -182
- package/src/output/utils.pyj +72 -72
- package/src/parse.pyj +80 -16
- package/src/string_interpolation.pyj +72 -72
- package/src/tokenizer.pyj +9 -4
- package/src/unicode_aliases.pyj +576 -576
- package/src/utils.pyj +192 -192
- package/test/_import_one.pyj +37 -37
- package/test/_import_two/__init__.pyj +11 -11
- package/test/_import_two/level2/deep.pyj +4 -4
- package/test/_import_two/other.pyj +6 -6
- package/test/_import_two/sub.pyj +13 -13
- package/test/abc.pyj +291 -0
- package/test/aes_vectors.pyj +421 -421
- package/test/annotations.pyj +80 -80
- package/test/arithmetic_nostrict.pyj +88 -0
- package/test/arithmetic_types.pyj +169 -0
- package/test/baselib.pyj +91 -0
- package/test/bytes.pyj +467 -0
- package/test/classes.pyj +1 -0
- package/test/comparison_ops.pyj +173 -0
- package/test/dataclasses.pyj +253 -0
- package/test/decorators.pyj +77 -77
- package/test/docstrings.pyj +39 -39
- package/test/elementmaker_test.pyj +45 -45
- package/test/enum.pyj +134 -0
- package/test/eval_exec.pyj +56 -0
- package/test/format.pyj +148 -0
- package/test/functions.pyj +151 -151
- package/test/generators.pyj +41 -41
- package/test/generic.pyj +370 -370
- package/test/imports.pyj +72 -72
- package/test/internationalization.pyj +73 -73
- package/test/lint.pyj +164 -164
- package/test/loops.pyj +85 -85
- package/test/numpy.pyj +734 -734
- package/test/object.pyj +64 -0
- package/test/omit_function_metadata.pyj +20 -20
- package/test/python_compat.pyj +17 -15
- package/test/python_features.pyj +70 -15
- package/test/regexp.pyj +83 -55
- package/test/repl.pyj +121 -121
- package/test/scoped_flags.pyj +76 -76
- package/test/tuples.pyj +96 -0
- package/test/typing.pyj +469 -0
- package/test/unit/index.js +116 -7
- package/test/unit/language-service-dts.js +543 -543
- package/test/unit/language-service-hover.js +455 -455
- package/test/unit/language-service.js +84 -0
- package/test/unit/web-repl.js +804 -1
- package/test/vars_locals_globals.pyj +94 -0
- package/tools/cli.js +558 -547
- package/tools/compile.js +224 -219
- package/tools/completer.js +131 -131
- package/tools/embedded_compiler.js +262 -251
- package/tools/gettext.js +185 -185
- package/tools/ini.js +65 -65
- package/tools/lint.js +16 -19
- package/tools/msgfmt.js +187 -187
- package/tools/repl.js +223 -223
- package/tools/test.js +118 -118
- package/tools/utils.js +128 -128
- package/tools/web_repl.js +95 -95
- package/try +41 -41
- package/web-repl/env.js +196 -196
- package/web-repl/index.html +163 -163
- package/web-repl/main.js +252 -252
- package/web-repl/prism.css +139 -139
- package/web-repl/prism.js +113 -113
- package/web-repl/rapydscript.js +224 -224
- package/web-repl/sha1.js +25 -25
- package/PYTHON_DIFFERENCES_REPORT.md +0 -291
- package/PYTHON_FEATURE_COVERAGE.md +0 -200
package/test/repl.pyj
CHANGED
|
@@ -1,121 +1,121 @@
|
|
|
1
|
-
# globals: compiler_dir
|
|
2
|
-
stdout = []
|
|
3
|
-
def clear():
|
|
4
|
-
stdout.length = 0
|
|
5
|
-
|
|
6
|
-
class FakeConsole:
|
|
7
|
-
|
|
8
|
-
def log(self):
|
|
9
|
-
Array.prototype.slice.call(arguments).forEach(def (arg):
|
|
10
|
-
stdout.push((arg or '').toString())
|
|
11
|
-
)
|
|
12
|
-
stdout.push('\n')
|
|
13
|
-
|
|
14
|
-
def error(self):
|
|
15
|
-
Array.prototype.slice.call(arguments).forEach(def (arg):
|
|
16
|
-
stdout.push((arg or '').toString())
|
|
17
|
-
)
|
|
18
|
-
stdout.push('\n')
|
|
19
|
-
|
|
20
|
-
class FakeReadline:
|
|
21
|
-
|
|
22
|
-
def __init__(self):
|
|
23
|
-
self.listeners = {}
|
|
24
|
-
self._prompt = ''
|
|
25
|
-
|
|
26
|
-
def setPrompt(self, prompt):
|
|
27
|
-
self._prompt = prompt
|
|
28
|
-
|
|
29
|
-
def write(self, data):
|
|
30
|
-
stdout.push(data)
|
|
31
|
-
|
|
32
|
-
def clearLine(self):
|
|
33
|
-
pass
|
|
34
|
-
|
|
35
|
-
def on(self, event, callback):
|
|
36
|
-
self.listeners[event] = callback
|
|
37
|
-
return self
|
|
38
|
-
|
|
39
|
-
def prompt(self):
|
|
40
|
-
stdout.push(self._prompt)
|
|
41
|
-
|
|
42
|
-
def send_line(self, text):
|
|
43
|
-
self.listeners['line'](text)
|
|
44
|
-
|
|
45
|
-
repl = require('./repl')
|
|
46
|
-
rl = FakeReadline()
|
|
47
|
-
|
|
48
|
-
send_line = rl.send_line.bind(rl)
|
|
49
|
-
|
|
50
|
-
def send_text(text):
|
|
51
|
-
for line in text.split('\n'):
|
|
52
|
-
send_line(line)
|
|
53
|
-
|
|
54
|
-
def check(text, output):
|
|
55
|
-
send_text(text)
|
|
56
|
-
eq(output, stdout[0])
|
|
57
|
-
clear()
|
|
58
|
-
|
|
59
|
-
def check_in(text, output):
|
|
60
|
-
send_text(text)
|
|
61
|
-
assrt.ok(output in stdout, output + ' not in ' + stdout)
|
|
62
|
-
clear()
|
|
63
|
-
|
|
64
|
-
def check_not_in(text, output):
|
|
65
|
-
send_text(text)
|
|
66
|
-
assrt.ok(output not in stdout)
|
|
67
|
-
clear()
|
|
68
|
-
|
|
69
|
-
readline = {
|
|
70
|
-
'createInterface': def(options):
|
|
71
|
-
rl.completer = options.completer
|
|
72
|
-
return rl
|
|
73
|
-
}
|
|
74
|
-
repl({'lib_path':compiler_dir, 'console':FakeConsole(), 'readline':readline, 'terminal':False, 'show_js':False, 'histfile':False})
|
|
75
|
-
eq = assrt.equal
|
|
76
|
-
eq('>>> ', stdout[-1])
|
|
77
|
-
clear()
|
|
78
|
-
check('1', '1')
|
|
79
|
-
check_in('if 1:\n 2\n \n ', '2')
|
|
80
|
-
check_not_in('if 1:\n 2\n ', '2')
|
|
81
|
-
check_in('1 +\n1\n\n', '2')
|
|
82
|
-
check('max(1, 2)', '2')
|
|
83
|
-
send_text(
|
|
84
|
-
'''
|
|
85
|
-
class A:
|
|
86
|
-
|
|
87
|
-
def __init__(self, a):
|
|
88
|
-
self. a = a
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
''')
|
|
92
|
-
clear()
|
|
93
|
-
check_in('b = A(1)\nb.a', '1')
|
|
94
|
-
check_in('c = A(2)\nc.a', '2')
|
|
95
|
-
send_text('from __python__ import dict_literals\nd={1:1}')
|
|
96
|
-
check_in('isinstance(d, dict)', 'true')
|
|
97
|
-
send_text('from __python__ import no_dict_literals\nd={1:1}')
|
|
98
|
-
check_in('isinstance(d, dict)', 'false')
|
|
99
|
-
# Test completions
|
|
100
|
-
def completions(line):
|
|
101
|
-
return rl.completer(line)[0]
|
|
102
|
-
|
|
103
|
-
def check_completions():
|
|
104
|
-
items = completions(arguments[0])
|
|
105
|
-
for x in Array.prototype.slice.call(arguments, 1):
|
|
106
|
-
assrt.ok(items and x in items, x + ' not in completions for: ' + arguments[0])
|
|
107
|
-
|
|
108
|
-
check_completions('', 'return', 'A')
|
|
109
|
-
check_completions('Array.', 'isArray', 'apply')
|
|
110
|
-
send_text('x = ""\ny = []'), clear()
|
|
111
|
-
check_completions('x.', 'substr', 'trim')
|
|
112
|
-
check_completions('y.', 'concat', 'push')
|
|
113
|
-
check_completions('x.sl', 'slice')
|
|
114
|
-
send_text('y = {"x":1}'), clear()
|
|
115
|
-
check_completions('y.', 'x')
|
|
116
|
-
|
|
117
|
-
# Test docstrings
|
|
118
|
-
clear()
|
|
119
|
-
send_text('def ds():\n "xxx"\n\n')
|
|
120
|
-
clear()
|
|
121
|
-
check('ds.__doc__', "'xxx'")
|
|
1
|
+
# globals: compiler_dir
|
|
2
|
+
stdout = []
|
|
3
|
+
def clear():
|
|
4
|
+
stdout.length = 0
|
|
5
|
+
|
|
6
|
+
class FakeConsole:
|
|
7
|
+
|
|
8
|
+
def log(self):
|
|
9
|
+
Array.prototype.slice.call(arguments).forEach(def (arg):
|
|
10
|
+
stdout.push((arg or '').toString())
|
|
11
|
+
)
|
|
12
|
+
stdout.push('\n')
|
|
13
|
+
|
|
14
|
+
def error(self):
|
|
15
|
+
Array.prototype.slice.call(arguments).forEach(def (arg):
|
|
16
|
+
stdout.push((arg or '').toString())
|
|
17
|
+
)
|
|
18
|
+
stdout.push('\n')
|
|
19
|
+
|
|
20
|
+
class FakeReadline:
|
|
21
|
+
|
|
22
|
+
def __init__(self):
|
|
23
|
+
self.listeners = {}
|
|
24
|
+
self._prompt = ''
|
|
25
|
+
|
|
26
|
+
def setPrompt(self, prompt):
|
|
27
|
+
self._prompt = prompt
|
|
28
|
+
|
|
29
|
+
def write(self, data):
|
|
30
|
+
stdout.push(data)
|
|
31
|
+
|
|
32
|
+
def clearLine(self):
|
|
33
|
+
pass
|
|
34
|
+
|
|
35
|
+
def on(self, event, callback):
|
|
36
|
+
self.listeners[event] = callback
|
|
37
|
+
return self
|
|
38
|
+
|
|
39
|
+
def prompt(self):
|
|
40
|
+
stdout.push(self._prompt)
|
|
41
|
+
|
|
42
|
+
def send_line(self, text):
|
|
43
|
+
self.listeners['line'](text)
|
|
44
|
+
|
|
45
|
+
repl = require('./repl')
|
|
46
|
+
rl = FakeReadline()
|
|
47
|
+
|
|
48
|
+
send_line = rl.send_line.bind(rl)
|
|
49
|
+
|
|
50
|
+
def send_text(text):
|
|
51
|
+
for line in text.split('\n'):
|
|
52
|
+
send_line(line)
|
|
53
|
+
|
|
54
|
+
def check(text, output):
|
|
55
|
+
send_text(text)
|
|
56
|
+
eq(output, stdout[0])
|
|
57
|
+
clear()
|
|
58
|
+
|
|
59
|
+
def check_in(text, output):
|
|
60
|
+
send_text(text)
|
|
61
|
+
assrt.ok(output in stdout, output + ' not in ' + stdout)
|
|
62
|
+
clear()
|
|
63
|
+
|
|
64
|
+
def check_not_in(text, output):
|
|
65
|
+
send_text(text)
|
|
66
|
+
assrt.ok(output not in stdout)
|
|
67
|
+
clear()
|
|
68
|
+
|
|
69
|
+
readline = {
|
|
70
|
+
'createInterface': def(options):
|
|
71
|
+
rl.completer = options.completer
|
|
72
|
+
return rl
|
|
73
|
+
}
|
|
74
|
+
repl({'lib_path':compiler_dir, 'console':FakeConsole(), 'readline':readline, 'terminal':False, 'show_js':False, 'histfile':False})
|
|
75
|
+
eq = assrt.equal
|
|
76
|
+
eq('>>> ', stdout[-1])
|
|
77
|
+
clear()
|
|
78
|
+
check('1', '1')
|
|
79
|
+
check_in('if 1:\n 2\n \n ', '2')
|
|
80
|
+
check_not_in('if 1:\n 2\n ', '2')
|
|
81
|
+
check_in('1 +\n1\n\n', '2')
|
|
82
|
+
check('max(1, 2)', '2')
|
|
83
|
+
send_text(
|
|
84
|
+
'''
|
|
85
|
+
class A:
|
|
86
|
+
|
|
87
|
+
def __init__(self, a):
|
|
88
|
+
self. a = a
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
''')
|
|
92
|
+
clear()
|
|
93
|
+
check_in('b = A(1)\nb.a', '1')
|
|
94
|
+
check_in('c = A(2)\nc.a', '2')
|
|
95
|
+
send_text('from __python__ import dict_literals\nd={1:1}')
|
|
96
|
+
check_in('isinstance(d, dict)', 'true')
|
|
97
|
+
send_text('from __python__ import no_dict_literals\nd={1:1}')
|
|
98
|
+
check_in('isinstance(d, dict)', 'false')
|
|
99
|
+
# Test completions
|
|
100
|
+
def completions(line):
|
|
101
|
+
return rl.completer(line)[0]
|
|
102
|
+
|
|
103
|
+
def check_completions():
|
|
104
|
+
items = completions(arguments[0])
|
|
105
|
+
for x in Array.prototype.slice.call(arguments, 1):
|
|
106
|
+
assrt.ok(items and x in items, x + ' not in completions for: ' + arguments[0])
|
|
107
|
+
|
|
108
|
+
check_completions('', 'return', 'A')
|
|
109
|
+
check_completions('Array.', 'isArray', 'apply')
|
|
110
|
+
send_text('x = ""\ny = []'), clear()
|
|
111
|
+
check_completions('x.', 'substr', 'trim')
|
|
112
|
+
check_completions('y.', 'concat', 'push')
|
|
113
|
+
check_completions('x.sl', 'slice')
|
|
114
|
+
send_text('y = {"x":1}'), clear()
|
|
115
|
+
check_completions('y.', 'x')
|
|
116
|
+
|
|
117
|
+
# Test docstrings
|
|
118
|
+
clear()
|
|
119
|
+
send_text('def ds():\n "xxx"\n\n')
|
|
120
|
+
clear()
|
|
121
|
+
check('ds.__doc__', "'xxx'")
|
package/test/scoped_flags.pyj
CHANGED
|
@@ -1,76 +1,76 @@
|
|
|
1
|
-
# vim:fileencoding=utf-8
|
|
2
|
-
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
-
|
|
4
|
-
a = {1:1}
|
|
5
|
-
assrt.ok(not isinstance(a, dict))
|
|
6
|
-
|
|
7
|
-
from __python__ import dict_literals, overload_getitem
|
|
8
|
-
a = {1:1}
|
|
9
|
-
assrt.ok(isinstance(a, dict))
|
|
10
|
-
assrt.equal(a[1], 1)
|
|
11
|
-
a[2] = 2
|
|
12
|
-
assrt.equal(a[2], 2)
|
|
13
|
-
a[2] += 4
|
|
14
|
-
assrt.equal(a[2], 6)
|
|
15
|
-
assrt.deepEqual(list(a.keys()), [1, 2])
|
|
16
|
-
from __python__ import no_dict_literals, no_overload_getitem
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
a = {1:1}
|
|
20
|
-
assrt.ok(not isinstance(a, dict))
|
|
21
|
-
|
|
22
|
-
def f():
|
|
23
|
-
from __python__ import dict_literals
|
|
24
|
-
a = {1:1}
|
|
25
|
-
assrt.ok(isinstance(a, dict))
|
|
26
|
-
|
|
27
|
-
a = {1:1}
|
|
28
|
-
assrt.ok(not isinstance(a, dict))
|
|
29
|
-
|
|
30
|
-
class S:
|
|
31
|
-
from __python__ import bound_methods
|
|
32
|
-
|
|
33
|
-
def __init__(self):
|
|
34
|
-
self.a = 3
|
|
35
|
-
|
|
36
|
-
def val(self):
|
|
37
|
-
return self.a if self else None
|
|
38
|
-
|
|
39
|
-
f = S().val
|
|
40
|
-
assrt.equal(f(), S().val())
|
|
41
|
-
|
|
42
|
-
class U:
|
|
43
|
-
|
|
44
|
-
def __init__(self):
|
|
45
|
-
self.a = 3
|
|
46
|
-
|
|
47
|
-
def val(self):
|
|
48
|
-
return self.a if self else None
|
|
49
|
-
|
|
50
|
-
f = U().val
|
|
51
|
-
assrt.equal(f(), None)
|
|
52
|
-
|
|
53
|
-
class C:
|
|
54
|
-
|
|
55
|
-
def __init__(self):
|
|
56
|
-
self.a = 3
|
|
57
|
-
|
|
58
|
-
def uval1(self):
|
|
59
|
-
return self.a if self else None
|
|
60
|
-
|
|
61
|
-
from __python__ import bound_methods
|
|
62
|
-
|
|
63
|
-
def bval(self):
|
|
64
|
-
return self.a
|
|
65
|
-
|
|
66
|
-
from __python__ import no_bound_methods
|
|
67
|
-
|
|
68
|
-
def uval2(self):
|
|
69
|
-
return self.a if self else None
|
|
70
|
-
|
|
71
|
-
c = C()
|
|
72
|
-
u1, u2 = c.uval1, c.uval2
|
|
73
|
-
f = c.bval
|
|
74
|
-
assrt.equal(u1(), None)
|
|
75
|
-
assrt.equal(u2(), None)
|
|
76
|
-
assrt.equal(f(), 3)
|
|
1
|
+
# vim:fileencoding=utf-8
|
|
2
|
+
# License: BSD Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
|
3
|
+
|
|
4
|
+
a = {1:1}
|
|
5
|
+
assrt.ok(not isinstance(a, dict))
|
|
6
|
+
|
|
7
|
+
from __python__ import dict_literals, overload_getitem
|
|
8
|
+
a = {1:1}
|
|
9
|
+
assrt.ok(isinstance(a, dict))
|
|
10
|
+
assrt.equal(a[1], 1)
|
|
11
|
+
a[2] = 2
|
|
12
|
+
assrt.equal(a[2], 2)
|
|
13
|
+
a[2] += 4
|
|
14
|
+
assrt.equal(a[2], 6)
|
|
15
|
+
assrt.deepEqual(list(a.keys()), [1, 2])
|
|
16
|
+
from __python__ import no_dict_literals, no_overload_getitem
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
a = {1:1}
|
|
20
|
+
assrt.ok(not isinstance(a, dict))
|
|
21
|
+
|
|
22
|
+
def f():
|
|
23
|
+
from __python__ import dict_literals
|
|
24
|
+
a = {1:1}
|
|
25
|
+
assrt.ok(isinstance(a, dict))
|
|
26
|
+
|
|
27
|
+
a = {1:1}
|
|
28
|
+
assrt.ok(not isinstance(a, dict))
|
|
29
|
+
|
|
30
|
+
class S:
|
|
31
|
+
from __python__ import bound_methods
|
|
32
|
+
|
|
33
|
+
def __init__(self):
|
|
34
|
+
self.a = 3
|
|
35
|
+
|
|
36
|
+
def val(self):
|
|
37
|
+
return self.a if self else None
|
|
38
|
+
|
|
39
|
+
f = S().val
|
|
40
|
+
assrt.equal(f(), S().val())
|
|
41
|
+
|
|
42
|
+
class U:
|
|
43
|
+
|
|
44
|
+
def __init__(self):
|
|
45
|
+
self.a = 3
|
|
46
|
+
|
|
47
|
+
def val(self):
|
|
48
|
+
return self.a if self else None
|
|
49
|
+
|
|
50
|
+
f = U().val
|
|
51
|
+
assrt.equal(f(), None)
|
|
52
|
+
|
|
53
|
+
class C:
|
|
54
|
+
|
|
55
|
+
def __init__(self):
|
|
56
|
+
self.a = 3
|
|
57
|
+
|
|
58
|
+
def uval1(self):
|
|
59
|
+
return self.a if self else None
|
|
60
|
+
|
|
61
|
+
from __python__ import bound_methods
|
|
62
|
+
|
|
63
|
+
def bval(self):
|
|
64
|
+
return self.a
|
|
65
|
+
|
|
66
|
+
from __python__ import no_bound_methods
|
|
67
|
+
|
|
68
|
+
def uval2(self):
|
|
69
|
+
return self.a if self else None
|
|
70
|
+
|
|
71
|
+
c = C()
|
|
72
|
+
u1, u2 = c.uval1, c.uval2
|
|
73
|
+
f = c.bval
|
|
74
|
+
assrt.equal(u1(), None)
|
|
75
|
+
assrt.equal(u2(), None)
|
|
76
|
+
assrt.equal(f(), 3)
|
package/test/tuples.pyj
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# globals: assrt
|
|
2
|
+
# vim:fileencoding=utf-8
|
|
3
|
+
#
|
|
4
|
+
# tuples.pyj
|
|
5
|
+
# Tests for tuple literal syntax: (a, b), (a,), and ().
|
|
6
|
+
|
|
7
|
+
ae = assrt.equal
|
|
8
|
+
ade = assrt.deepEqual
|
|
9
|
+
ok = assrt.ok
|
|
10
|
+
|
|
11
|
+
# ── 1. Empty tuple ────────────────────────────────────────────────────────────
|
|
12
|
+
t = ()
|
|
13
|
+
ok(Array.isArray(t))
|
|
14
|
+
ae(t.length, 0)
|
|
15
|
+
|
|
16
|
+
# ── 2. Single-element tuple with trailing comma ───────────────────────────────
|
|
17
|
+
t1 = (42,)
|
|
18
|
+
ok(Array.isArray(t1))
|
|
19
|
+
ae(t1.length, 1)
|
|
20
|
+
ae(t1[0], 42)
|
|
21
|
+
|
|
22
|
+
t_str = ('hello',)
|
|
23
|
+
ae(t_str[0], 'hello')
|
|
24
|
+
|
|
25
|
+
# ── 3. Two-element tuple ──────────────────────────────────────────────────────
|
|
26
|
+
t2 = (1, 2)
|
|
27
|
+
ok(Array.isArray(t2))
|
|
28
|
+
ae(t2.length, 2)
|
|
29
|
+
ae(t2[0], 1)
|
|
30
|
+
ae(t2[1], 2)
|
|
31
|
+
|
|
32
|
+
# ── 4. Three-element tuple ────────────────────────────────────────────────────
|
|
33
|
+
t3 = ('a', 'b', 'c')
|
|
34
|
+
ae(t3.length, 3)
|
|
35
|
+
ae(t3[0], 'a')
|
|
36
|
+
ae(t3[1], 'b')
|
|
37
|
+
ae(t3[2], 'c')
|
|
38
|
+
|
|
39
|
+
# ── 5. Trailing comma on multi-element tuple ──────────────────────────────────
|
|
40
|
+
t_trail = (10, 20, 30,)
|
|
41
|
+
ae(t_trail.length, 3)
|
|
42
|
+
ae(t_trail[0], 10)
|
|
43
|
+
ae(t_trail[2], 30)
|
|
44
|
+
|
|
45
|
+
# ── 6. Tuple in an expression context (return, assignment) ────────────────────
|
|
46
|
+
def make_pair():
|
|
47
|
+
return (99, 100)
|
|
48
|
+
|
|
49
|
+
p = make_pair()
|
|
50
|
+
ae(p[0], 99)
|
|
51
|
+
ae(p[1], 100)
|
|
52
|
+
|
|
53
|
+
# ── 7. Tuple unpacking works the same as array unpacking ─────────────────────
|
|
54
|
+
a, b = (10, 20)
|
|
55
|
+
ae(a, 10)
|
|
56
|
+
ae(b, 20)
|
|
57
|
+
|
|
58
|
+
# ── 8. Nested tuples ─────────────────────────────────────────────────────────
|
|
59
|
+
nested = ((1, 2), (3, 4))
|
|
60
|
+
ae(nested.length, 2)
|
|
61
|
+
ae(nested[0][0], 1)
|
|
62
|
+
ae(nested[0][1], 2)
|
|
63
|
+
ae(nested[1][0], 3)
|
|
64
|
+
ae(nested[1][1], 4)
|
|
65
|
+
|
|
66
|
+
# ── 9. Tuple as function argument ─────────────────────────────────────────────
|
|
67
|
+
def first(t):
|
|
68
|
+
return t[0]
|
|
69
|
+
|
|
70
|
+
ae(first((7, 8, 9)), 7)
|
|
71
|
+
|
|
72
|
+
# ── 10. isinstance with tuple of types ────────────────────────────────────────
|
|
73
|
+
ok(isinstance(42, (int, str)))
|
|
74
|
+
ok(isinstance('hi', (int, str)))
|
|
75
|
+
ok(not isinstance(3.14, (int, str)))
|
|
76
|
+
|
|
77
|
+
# ── 11. Tuple expressions with complex elements ───────────────────────────────
|
|
78
|
+
x = 5
|
|
79
|
+
tup = (x + 1, x * 2, x if x > 0 else 0)
|
|
80
|
+
ae(tup[0], 6)
|
|
81
|
+
ae(tup[1], 10)
|
|
82
|
+
ae(tup[2], 5)
|
|
83
|
+
|
|
84
|
+
# ── 12. Parenthesised single expression is NOT a tuple ───────────────────────
|
|
85
|
+
plain = (42)
|
|
86
|
+
ae(plain, 42)
|
|
87
|
+
ok(not Array.isArray(plain))
|
|
88
|
+
|
|
89
|
+
# ── 13. Tuple used in for-loop unpacking ─────────────────────────────────────
|
|
90
|
+
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
|
|
91
|
+
result = []
|
|
92
|
+
for num, name in pairs:
|
|
93
|
+
result.push(str(num) + ':' + name)
|
|
94
|
+
ae(result[0], '1:one')
|
|
95
|
+
ae(result[1], '2:two')
|
|
96
|
+
ae(result[2], '3:three')
|