/
test_iniParser.py
executable file
·169 lines (120 loc) · 3.27 KB
/
test_iniParser.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
from buildutil.iniParser import IniParser
import os
iniFilepath = "/tmp/buildutil/test.ini"
def write(parser, testArray):
for line in testArray:
if len(line) == 3:
section = line[0]
key = line[1]
value = line[2]
parser.write(section, key, value, update=False)
def check(parser, section, key, value):
rv = parser.read(section, key)
assert rv == value
def checkTestArray(parser, testArray, col):
for line in testArray:
len(line) == col
if len(line) == col:
section = line[0]
key = line[1]
value = line[2]
check(parser, section, key, value)
def start():
# Make sure the inifile is removed before the test
if os.path.exists(iniFilepath):
os.remove(iniFilepath)
assert os.path.exists(iniFilepath) == False
# Init parser
parser = IniParser(iniFilepath)
assert os.path.exists(iniFilepath) == True
return parser
def cleanup():
assert os.path.exists(iniFilepath) == True
if os.path.exists(iniFilepath):
os.remove(iniFilepath)
assert os.path.exists(iniFilepath) == False
############################################################
def test_types():
col = 3
testArray = [
# Section Key Value
["DEFAULT", "String", "Hello" ],
["DEFAULT", "True", True ],
["DEFAULT", "False", False ],
["DEFAULT", "Int", 1 ],
["DEFAULT", "Float", 3.14 ],
["DEFAULT", "None", None ]
]
parser = start()
write(parser, testArray)
checkTestArray(parser, testArray, col)
cleanup()
def test_invalid():
col = 3
testArray = [
# Section Key Value
["DEFAULT", "", "b2" ],
["", "", "c3" ],
["DEFAULT", None, "d4" ],
[None, None, "f6" ],
]
parser = start()
write(parser, testArray)
expectedArray = [
# Section Key Value
#["DEFAULT", "", "b2" ],
#["", "", "c3" ],
#["DEFAULT", None, "d4" ],
#[None, None, "f6" ],
]
checkTestArray(parser, expectedArray, col)
cleanup()
def test_noneSection():
parser = start()
parser.write(None, "test", "out")
check(parser, "DEFAULT", "test", "out")
cleanup()
def test_emptySection():
parser = start()
parser.write("", "test", "out")
check(parser, "DEFAULT", "test", "out")
cleanup()
def test_writtenBoolean():
col = 3
testArray = [
# Section Key Value
["VALID", "Invalid1", True ],
["VALID", "Invalid2", False ]
["VALID", "Invalid3", "True" ],
["VALID", "Invalid4", "False" ]
]
parser = start()
write(parser, testArray)
checkTestArray(parser, testArray, col)
cleanup()
def test_writtenBoolean():
col = 3
testArray = [
# Section Key Value
["VALID", "Invalid1", "1" ],
["VALID", "Invalid2", "3.14" ]
]
parser = start()
write(parser, testArray)
#checkTestArray(parser, testArray, col)
cleanup()
def test_writeUpdate():
parser = start()
parser.write("UPDATE", "test", "Original", update=False)
check(parser, "UPDATE", "test", "Original")
parser.write("UPDATE", "test", "Updated", update=False)
check(parser, "UPDATE", "test", "Original")
parser.write("UPDATE", "test", "Updated", update=False)
check(parser, "UPDATE", "test", "Original")
cleanup()
def main():
test_types()
test_writeUpdate()
if __name__ == "__main__":
main()