Repository navigation
Expand file tree
/
Copy pathcode.py
More file actions
82 lines (71 loc) · 2.71 KB
/
Copy pathcode.py
File metadata and controls
82 lines (71 loc) · 2.71 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
# 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("hex_leading_zero_val", 15) # TOML allows leading zeros after 0x
check("neg_hex_int_val", -17) # not TOML, but accepted
check("oct_int_val", 15)
check("bin_int_val", 17)
check("underscore_int_val", 1000) # used to fall back to float 1000.0
check("zero_int_val", 0)
check("signed_zero_val", 0)
check("bool_true", True)
check("bool_false", False)
check("float_val", 3.14)
check("neg_float_val", -1.5)
check("frac_float_val", 0.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
check_bad("bad_leading_zero") # TOML does not allow leading zeros
check_bad("bad_leading_zero_neg") # also after a sign
check_bad("bad_leading_zero_08") # not octal, so it used to fall back to float
check_bad("bad_leading_zero_float") # nor on the integer part of a float
check_bad("bad_hex_no_digits") # prefix with no digits
check_bad("bad_hex_overflow") # does not fit in a 32-bit int
check_bad("bad_digit_at") # '@' is not a digit
check_bad("bad_unicode_underscore") # no underscores in \u escapes
# 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")