forked from adafruit/circuitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
66 lines (55 loc) · 1.89 KB
/
Copy pathcode.py
File metadata and controls
66 lines (55 loc) · 1.89 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
# Test supervisor.get_setting() parsing of settings.toml values.
# Copy settings.toml to the root of the board, then run this script.
# All lines should print PASS.
import math
import supervisor
def check(key, expected):
got = supervisor.get_setting(key)
if type(got) is not type(expected):
print(
f"FAIL {key}: expected type {type(expected).__name__}, got {type(got).__name__} ({got!r})"
)
return
if isinstance(expected, float) and math.isnan(expected):
ok = math.isnan(got)
else:
ok = got == expected
if ok:
print(f"PASS {key}")
else:
print(f"FAIL {key}: expected {expected!r}, got {got!r}")
check("str_val", "hello")
check("int_val", 42)
check("neg_int_val", -7)
check("hex_int_val", 31)
check("bool_true", True)
check("bool_false", False)
check("float_val", 3.14)
check("neg_float_val", -1.5)
check("sci_float_val", 6.626e-34)
# math.nan and math.inf are not always available
check("pos_inf", float("inf"))
check("neg_inf", -float("inf"))
check("pos_nan", float("nan"))
def check_bad(key):
try:
got = supervisor.get_setting(key)
print(f"FAIL {key}: expected ValueError, got {got!r}")
except ValueError:
print(f"PASS {key} raises ValueError")
check_bad("bad_word") # unquoted non-boolean word
check_bad("bad_float_junk") # float with trailing garbage
check_bad("bad_inf_junk") # inf with trailing characters
check_bad("bad_lone_sign") # bare + with nothing after it
# Missing key returns default
got = supervisor.get_setting("no_such_key", "default")
if got == "default":
print("PASS missing key default")
else:
print(f"FAIL missing key default: got {got!r}")
# Invalid value raises ValueError
try:
supervisor.get_setting("str_val") # quoted string is valid
print("PASS str via get_setting")
except ValueError:
print("FAIL str via get_setting raised ValueError unexpectedly")