forked from micropython/micropython-esp32-ulp
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
81 lines (65 loc) · 2.98 KB
/
util.py
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
importos
fromesp32_ulp.utilimportsplit_tokens, validate_expression, file_exists
tests= []
deftest(param):
"""
the @test decorator
"""
tests.append(param)
@test
deftest_split_tokens():
assertsplit_tokens("") == []
assertsplit_tokens("t") == ['t']
assertsplit_tokens("test") == ['test']
assertsplit_tokens("t t") == ['t', ' ', 't']
assertsplit_tokens("t,t") == ['t', ',', 't']
assertsplit_tokens("test(arg)") == ['test', '(', 'arg', ')']
assertsplit_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')']
assertsplit_tokens("test(arg,arg2)") == ['test', '(', 'arg', ',', 'arg2', ')']
assertsplit_tokens(" test( arg, arg2)") == [' ', 'test', '(', ' ', 'arg', ',', ' ', 'arg2', ')']
assertsplit_tokens(" test( arg ) ") == [' ', 'test', '(', ' ', 'arg', ' ', ')', ' ']
assertsplit_tokens("\t test \t ") == ['\t ', 'test', " \t "]
assertsplit_tokens("test\nrow2") == ['test', "\n", "row2"]
# split_token does not support comments. should generally only be used after comments are already stripped
assertsplit_tokens("test(arg /*comment*/)") == ['test', '(', 'arg', ' ', '/', '*', 'comment', '*', '/', ')']
assertsplit_tokens("#test") == ['#', 'test']
@test
deftest_validate_expression():
assertvalidate_expression('') isTrue
assertvalidate_expression('1') isTrue
assertvalidate_expression('1+1') isTrue
assertvalidate_expression('(1+1)') isTrue
assertvalidate_expression('(1+1)*2') isTrue
assertvalidate_expression('(1 + 1)') isTrue
assertvalidate_expression('10 % 2') isTrue
assertvalidate_expression('0x100 << 2') isTrue
assertvalidate_expression('0x100 & ~2') isTrue
assertvalidate_expression('0xabcdef') isTrue
assertvalidate_expression('0x123def') isTrue
assertvalidate_expression('0xABC') isTrue
assertvalidate_expression('0xaBc') isTrue
assertvalidate_expression('0Xabc') isTrue
assertvalidate_expression('0X123ABC') isTrue
assertvalidate_expression('2*3+4/5&6|7') isTrue
assertvalidate_expression('(((((1+1) * 2') isTrue# valid characters, even if expression is not valid
assertvalidate_expression(':') isFalse
assertvalidate_expression('_') isFalse
assertvalidate_expression('=') isFalse
assertvalidate_expression('.') isFalse
assertvalidate_expression('!') isFalse
assertvalidate_expression('123 ^ 4') isFalse# operator not supported for now
assertvalidate_expression('evil()') isFalse
assertvalidate_expression('def cafe()') isFalse# valid hex digits, but potentially dangerous code
assertvalidate_expression('def CAFE()') isFalse
@test
deftest_file_exists():
testfile='.testfile'
withopen(testfile, 'w') asf:
f.write('contents')
assertfile_exists(testfile)
os.remove(testfile)
assertnotfile_exists(testfile)
if__name__=='__main__':
# run all methods marked with @test
fortintests:
t()