forked from micropython/micropython
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_basics.py
More file actions
139 lines (105 loc) · 3.5 KB
/
test_basics.py
File metadata and controls
139 lines (105 loc) · 3.5 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
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
# SPDX-FileCopyrightText: 2025 Scott Shawcroft for Adafruit Industries
# SPDX-License-Identifier: MIT
"""Test basic native_sim functionality."""
import pytest
@pytest.mark.circuitpy_drive(None)
def test_blank_flash_hello_world(circuitpython):
"""Test that an erased flash shows code.py output header."""
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "Board ID:native_native_sim" in output
assert "UID:" in output
assert "code.py output:" in output
assert "Hello World" in output
assert "done" in output
# --- PTY Input Tests ---
INPUT_CODE = """\
import sys
print("ready")
char = sys.stdin.read(1)
print(f"received: {repr(char)}")
print("done")
"""
@pytest.mark.circuitpy_drive({"code.py": INPUT_CODE})
def test_basic_serial_input(circuitpython):
"""Test reading single character from serial via PTY write."""
circuitpython.serial.wait_for("ready")
circuitpython.serial.write("A")
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "ready" in output
assert "received: 'A'" in output
assert "done" in output
INPUT_FUNC_CODE = """\
print("ready")
name = input("Enter name: ")
print(f"hello {name}")
print("done")
"""
@pytest.mark.circuitpy_drive({"code.py": INPUT_FUNC_CODE})
def test_input_function(circuitpython):
"""Test the built-in input() function with PTY input."""
circuitpython.serial.wait_for("Enter name:")
circuitpython.serial.write("World\r")
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "ready" in output
assert "Enter name:" in output
assert "hello World" in output
assert "done" in output
INTERRUPT_CODE = """\
import time
print("starting")
for i in range(100):
print(f"loop {i}")
time.sleep(0.1)
print("completed")
"""
@pytest.mark.circuitpy_drive({"code.py": INTERRUPT_CODE})
def test_ctrl_c_interrupt(circuitpython):
"""Test sending Ctrl+C (0x03) to interrupt running code."""
circuitpython.serial.wait_for("loop 5")
circuitpython.serial.write("\x03")
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "starting" in output
assert "loop 5" in output
assert "KeyboardInterrupt" in output
assert "completed" not in output
IMPORT_PRECEDENCE_CODE = """\
import fake_lib
print("done")
"""
@pytest.mark.circuitpy_drive(
{
"code.py": IMPORT_PRECEDENCE_CODE,
"fake_lib/a_spritesheet.bmp": b"",
"fake_lib.py": 'print("hello fake_lib.py")\n',
}
)
def test_py_file_wins_over_namespace_dir(circuitpython):
"""#10614: a .py module beats a sibling directory lacking __init__.py."""
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "hello fake_lib.py" in output
assert "done" in output
RELOAD_CODE = """\
print("first run")
import time
time.sleep(1)
print("done")
"""
@pytest.mark.circuitpy_drive({"code.py": RELOAD_CODE})
@pytest.mark.code_py_runs(2)
def test_ctrl_d_soft_reload(circuitpython):
"""Test sending Ctrl+D (0x04) to trigger soft reload."""
circuitpython.serial.wait_for("first run")
circuitpython.serial.write("\x04")
circuitpython.wait_until_done()
# Should see "first run" appear multiple times due to reload
# or see a soft reboot message
output = circuitpython.serial.all_output
assert "first run" in output
# The soft reload should restart the code before "done" is printed
assert "done" in output
assert output.count("first run") > 1