forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dis.py
More file actions
60 lines (50 loc) · 1.62 KB
/
test_dis.py
File metadata and controls
60 lines (50 loc) · 1.62 KB
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
import subprocess
import sys
import unittest
# This only tests that it prints something in order
# to avoid changing this test if the bytecode changes
# These tests start a new process instead of redirecting stdout because
# stdout is being written to by rust code, which currently can't be
# redirected by reassigning sys.stdout
class TestDis(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.setup = """
import dis
def tested_func(): pass
"""
cls.command = (sys.executable, "-c")
def test_dis(self):
test_code = f"""
{self.setup}
dis.dis(tested_func)
dis.dis("x = 2; print(x)")
"""
result = subprocess.run(
self.command + (test_code,), capture_output=True
)
self.assertNotEqual("", result.stdout.decode())
self.assertEqual("", result.stderr.decode())
def test_disassemble(self):
test_code = f"""
{self.setup}
dis.disassemble(tested_func)
"""
result = subprocess.run(
self.command + (test_code,), capture_output=True
)
# In CPython this would raise an AttributeError, not a
# TypeError because dis is implemented in python in CPython and
# as such the type mismatch wouldn't be caught immeadiately
self.assertIn("TypeError", result.stderr.decode())
test_code = f"""
{self.setup}
dis.disassemble(tested_func.__code__)
"""
result = subprocess.run(
self.command + (test_code,), capture_output=True
)
self.assertNotEqual("", result.stdout.decode())
self.assertEqual("", result.stderr.decode())
if __name__ == "__main__":
unittest.main()