|
15 | 15 | limitations under the License.
|
16 | 16 | """
|
17 | 17 | import sys
|
18 |
| -import os |
| 18 | +from os.path import isfile, join |
19 | 19 | import json
|
20 | 20 |
|
21 |
| -ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..", "..")) |
22 |
| -sys.path.insert(0, ROOT) |
| 21 | +import pytest |
23 | 22 |
|
24 |
| -import unittest |
25 | 23 | from tools.memap import MemapParser
|
26 | 24 | from copy import deepcopy
|
27 | 25 |
|
28 | 26 | """
|
29 | 27 | Tests for test_api.py
|
30 | 28 | """
|
31 | 29 |
|
32 |
| -class MemapParserTests(unittest.TestCase): |
| 30 | +@pytest.fixture |
| 31 | +def memap_parser(): |
33 | 32 | """
|
34 |
| - Test cases for Test Api |
| 33 | + Called before each test case |
| 34 | +
|
| 35 | + :return: |
| 36 | + """ |
| 37 | + memap_parser = MemapParser() |
| 38 | + |
| 39 | + memap_parser.modules = { |
| 40 | + "mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": { |
| 41 | + ".text": 1, |
| 42 | + ".data": 2, |
| 43 | + ".bss": 3, |
| 44 | + ".heap": 0, |
| 45 | + ".stack": 0, |
| 46 | + ".interrupts_ram":0, |
| 47 | + ".init":0, |
| 48 | + ".ARM.extab":0, |
| 49 | + ".ARM.exidx":0, |
| 50 | + ".ARM.attributes":0, |
| 51 | + ".eh_frame":0, |
| 52 | + ".init_array":0, |
| 53 | + ".fini_array":0, |
| 54 | + ".jcr":0, |
| 55 | + ".stab":0, |
| 56 | + ".stabstr":0, |
| 57 | + ".ARM.exidx":0, |
| 58 | + ".ARM":0, |
| 59 | + ".interrupts":0, |
| 60 | + ".flash_config":0, |
| 61 | + "unknown":0, |
| 62 | + "OUTPUT":0, |
| 63 | + }, |
| 64 | + "[lib]/libc.a/lib_a-printf.o": { |
| 65 | + ".text": 4, |
| 66 | + ".data": 5, |
| 67 | + ".bss": 6, |
| 68 | + ".heap": 0, |
| 69 | + ".stack": 0, |
| 70 | + ".interrupts_ram":0, |
| 71 | + ".init":0, |
| 72 | + ".ARM.extab":0, |
| 73 | + ".ARM.exidx":0, |
| 74 | + ".ARM.attributes":0, |
| 75 | + ".eh_frame":0, |
| 76 | + ".init_array":0, |
| 77 | + ".fini_array":0, |
| 78 | + ".jcr":0, |
| 79 | + ".stab":0, |
| 80 | + ".stabstr":0, |
| 81 | + ".ARM.exidx":0, |
| 82 | + ".ARM":0, |
| 83 | + ".interrupts":0, |
| 84 | + ".flash_config":0, |
| 85 | + "unknown":0, |
| 86 | + "OUTPUT":0, |
| 87 | + }, |
| 88 | + "main.o": { |
| 89 | + ".text": 7, |
| 90 | + ".data": 8, |
| 91 | + ".bss": 0, |
| 92 | + ".heap": 0, |
| 93 | + ".stack": 0, |
| 94 | + ".interrupts_ram":0, |
| 95 | + ".init":0, |
| 96 | + ".ARM.extab":0, |
| 97 | + ".ARM.exidx":0, |
| 98 | + ".ARM.attributes":0, |
| 99 | + ".eh_frame":0, |
| 100 | + ".init_array":0, |
| 101 | + ".fini_array":0, |
| 102 | + ".jcr":0, |
| 103 | + ".stab":0, |
| 104 | + ".stabstr":0, |
| 105 | + ".ARM.exidx":0, |
| 106 | + ".ARM":0, |
| 107 | + ".interrupts":0, |
| 108 | + ".flash_config":0, |
| 109 | + "unknown":0, |
| 110 | + "OUTPUT":0, |
| 111 | + }, |
| 112 | + "test.o": { |
| 113 | + ".text": 0, |
| 114 | + ".data": 0, |
| 115 | + ".bss": 0, |
| 116 | + ".heap": 0, |
| 117 | + ".stack": 0, |
| 118 | + ".interrupts_ram":0, |
| 119 | + ".init":0, |
| 120 | + ".ARM.extab":0, |
| 121 | + ".ARM.exidx":0, |
| 122 | + ".ARM.attributes":0, |
| 123 | + ".eh_frame":0, |
| 124 | + ".init_array":0, |
| 125 | + ".fini_array":0, |
| 126 | + ".jcr":0, |
| 127 | + ".stab":0, |
| 128 | + ".stabstr":0, |
| 129 | + ".ARM.exidx":0, |
| 130 | + ".ARM":0, |
| 131 | + ".interrupts":0, |
| 132 | + ".flash_config":0, |
| 133 | + "unknown":0, |
| 134 | + "OUTPUT":0, |
| 135 | + }, |
| 136 | + } |
| 137 | + return memap_parser |
| 138 | + |
| 139 | + |
| 140 | +def generate_test_helper(memap_parser, format, depth, file_output=None): |
| 141 | + """ |
| 142 | + Helper that tests that the member variables "modules" is |
| 143 | + unchanged after calling "generate_output" |
| 144 | +
|
| 145 | + :param output_type: type string that is passed to "generate_output" |
| 146 | + :param file_output: path to output file that is passed to "generate_output" |
35 | 147 | """
|
36 | 148 |
|
37 |
| - def setUp(self): |
38 |
| - """ |
39 |
| - Called before each test case |
40 |
| -
|
41 |
| - :return: |
42 |
| - """ |
43 |
| - self.memap_parser = MemapParser() |
44 |
| - |
45 |
| - self.memap_parser.modules = { |
46 |
| - "mbed-os/targets/TARGET/TARGET_MCUS/api/pinmap.o": { |
47 |
| - ".text": 1, |
48 |
| - ".data": 2, |
49 |
| - ".bss": 3, |
50 |
| - ".heap": 0, |
51 |
| - ".stack": 0, |
52 |
| - ".interrupts_ram":0, |
53 |
| - ".init":0, |
54 |
| - ".ARM.extab":0, |
55 |
| - ".ARM.exidx":0, |
56 |
| - ".ARM.attributes":0, |
57 |
| - ".eh_frame":0, |
58 |
| - ".init_array":0, |
59 |
| - ".fini_array":0, |
60 |
| - ".jcr":0, |
61 |
| - ".stab":0, |
62 |
| - ".stabstr":0, |
63 |
| - ".ARM.exidx":0, |
64 |
| - ".ARM":0, |
65 |
| - ".interrupts":0, |
66 |
| - ".flash_config":0, |
67 |
| - "unknown":0, |
68 |
| - "OUTPUT":0, |
69 |
| - }, |
70 |
| - "[lib]/libc.a/lib_a-printf.o": { |
71 |
| - ".text": 4, |
72 |
| - ".data": 5, |
73 |
| - ".bss": 6, |
74 |
| - ".heap": 0, |
75 |
| - ".stack": 0, |
76 |
| - ".interrupts_ram":0, |
77 |
| - ".init":0, |
78 |
| - ".ARM.extab":0, |
79 |
| - ".ARM.exidx":0, |
80 |
| - ".ARM.attributes":0, |
81 |
| - ".eh_frame":0, |
82 |
| - ".init_array":0, |
83 |
| - ".fini_array":0, |
84 |
| - ".jcr":0, |
85 |
| - ".stab":0, |
86 |
| - ".stabstr":0, |
87 |
| - ".ARM.exidx":0, |
88 |
| - ".ARM":0, |
89 |
| - ".interrupts":0, |
90 |
| - ".flash_config":0, |
91 |
| - "unknown":0, |
92 |
| - "OUTPUT":0, |
93 |
| - }, |
94 |
| - "main.o": { |
95 |
| - ".text": 7, |
96 |
| - ".data": 8, |
97 |
| - ".bss": 0, |
98 |
| - ".heap": 0, |
99 |
| - ".stack": 0, |
100 |
| - ".interrupts_ram":0, |
101 |
| - ".init":0, |
102 |
| - ".ARM.extab":0, |
103 |
| - ".ARM.exidx":0, |
104 |
| - ".ARM.attributes":0, |
105 |
| - ".eh_frame":0, |
106 |
| - ".init_array":0, |
107 |
| - ".fini_array":0, |
108 |
| - ".jcr":0, |
109 |
| - ".stab":0, |
110 |
| - ".stabstr":0, |
111 |
| - ".ARM.exidx":0, |
112 |
| - ".ARM":0, |
113 |
| - ".interrupts":0, |
114 |
| - ".flash_config":0, |
115 |
| - "unknown":0, |
116 |
| - "OUTPUT":0, |
117 |
| - }, |
118 |
| - "test.o": { |
119 |
| - ".text": 0, |
120 |
| - ".data": 0, |
121 |
| - ".bss": 0, |
122 |
| - ".heap": 0, |
123 |
| - ".stack": 0, |
124 |
| - ".interrupts_ram":0, |
125 |
| - ".init":0, |
126 |
| - ".ARM.extab":0, |
127 |
| - ".ARM.exidx":0, |
128 |
| - ".ARM.attributes":0, |
129 |
| - ".eh_frame":0, |
130 |
| - ".init_array":0, |
131 |
| - ".fini_array":0, |
132 |
| - ".jcr":0, |
133 |
| - ".stab":0, |
134 |
| - ".stabstr":0, |
135 |
| - ".ARM.exidx":0, |
136 |
| - ".ARM":0, |
137 |
| - ".interrupts":0, |
138 |
| - ".flash_config":0, |
139 |
| - "unknown":0, |
140 |
| - "OUTPUT":0, |
141 |
| - }, |
142 |
| - } |
143 |
| - |
144 |
| - def tearDown(self): |
145 |
| - """ |
146 |
| - Called after each test case |
147 |
| -
|
148 |
| - :return: |
149 |
| - """ |
150 |
| - pass |
151 |
| - |
152 |
| - def generate_test_helper(self, output_type, depth, file_output=None): |
153 |
| - """ |
154 |
| - Helper that ensures that the member variables "modules" is |
155 |
| - unchanged after calling "generate_output" |
156 |
| -
|
157 |
| - :param output_type: type string that is passed to "generate_output" |
158 |
| - :param file_output: path to output file that is passed to "generate_output" |
159 |
| - :return: |
160 |
| - """ |
161 |
| - |
162 |
| - old_modules = deepcopy(self.memap_parser.modules) |
163 |
| - |
164 |
| - self.memap_parser.generate_output(output_type, depth, file_output) |
165 |
| - |
166 |
| - self.assertEqual(self.memap_parser.modules, old_modules, |
167 |
| - "generate_output modified the 'modules' property") |
168 |
| - |
169 |
| - |
170 |
| - def test_report_computed(self): |
171 |
| - """ |
172 |
| - Test ensures the report and summary are computed |
173 |
| -
|
174 |
| - :return: |
175 |
| - """ |
176 |
| - |
177 |
| - self.memap_parser.generate_output('table', 2) |
178 |
| - |
179 |
| - # Report is created after generating output |
180 |
| - self.assertTrue(self.memap_parser.mem_summary) |
181 |
| - self.assertTrue(self.memap_parser.mem_report) |
182 |
| - |
183 |
| - def test_generate_output_table(self): |
184 |
| - """ |
185 |
| - Test ensures that an output of type "table" can be generated correctly |
186 |
| -
|
187 |
| - :return: |
188 |
| - """ |
189 |
| - depth = 2 |
190 |
| - self.generate_test_helper('table', depth) |
191 |
| - |
192 |
| - def test_generate_output_json(self): |
193 |
| - """ |
194 |
| - Test ensures that an output of type "json" can be generated correctly |
195 |
| -
|
196 |
| - :return: |
197 |
| - """ |
198 |
| - file_name = '.json_test_output.json' |
199 |
| - depth = 2 |
200 |
| - self.generate_test_helper('json', depth, file_output=file_name) |
201 |
| - self.assertTrue(os.path.exists(file_name), "Failed to create json file") |
202 |
| - os.remove(file_name) |
203 |
| - |
204 |
| - def test_generate_output_csv_ci(self): |
205 |
| - """ |
206 |
| - Test ensures that an output of type "csv-ci" can be generated correctly |
207 |
| -
|
208 |
| - :return: |
209 |
| - """ |
210 |
| - file_name = '.csv_ci_test_output.csv' |
211 |
| - depth = 2 |
212 |
| - self.generate_test_helper('csv-ci', depth, file_output=file_name) |
213 |
| - self.assertTrue(os.path.exists(file_name), "Failed to create csv-ci file") |
214 |
| - os.remove(file_name) |
215 |
| - |
216 |
| - |
217 |
| -if __name__ == '__main__': |
218 |
| - unittest.main() |
| 149 | + old_modules = deepcopy(memap_parser.modules) |
| 150 | + |
| 151 | + memap_parser.generate_output(format, depth, file_output=file_output) |
| 152 | + |
| 153 | + assert memap_parser.modules == old_modules,\ |
| 154 | + "generate_output modified the 'modules' property" |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize("depth", [1, 2, 20]) |
| 158 | +def test_report_computed(memap_parser, depth): |
| 159 | + """ |
| 160 | + Test that a report and summary are computed |
| 161 | + """ |
| 162 | + |
| 163 | + memap_parser.generate_output('table', depth) |
| 164 | + |
| 165 | + # Report is created after generating output |
| 166 | + assert memap_parser.mem_summary |
| 167 | + assert memap_parser.mem_report |
| 168 | + |
| 169 | + |
| 170 | +@pytest.mark.parametrize("depth", [1, 2, 20]) |
| 171 | +def test_generate_output_table(memap_parser, depth): |
| 172 | + """ |
| 173 | + Test that an output of type "table" can be generated correctly |
| 174 | + """ |
| 175 | + generate_test_helper(memap_parser, 'table', depth) |
| 176 | + |
| 177 | + |
| 178 | +@pytest.mark.parametrize("depth", [1, 2, 20]) |
| 179 | +def test_generate_output_json(memap_parser, tmpdir, depth): |
| 180 | + """ |
| 181 | + Test that an output of type "json" can be generated correctly |
| 182 | + """ |
| 183 | + file_name = str(tmpdir.join('json_test_output.json').realpath()) |
| 184 | + generate_test_helper(memap_parser, 'json', depth, file_name) |
| 185 | + assert isfile(file_name), "Failed to create json file" |
| 186 | + json.load(open(file_name)) |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.parametrize("depth", [1, 2, 20]) |
| 190 | +def test_generate_output_csv_ci(memap_parser, tmpdir, depth): |
| 191 | + """ |
| 192 | + Test ensures that an output of type "csv-ci" can be generated correctly |
| 193 | +
|
| 194 | + :return: |
| 195 | + """ |
| 196 | + file_name = str(tmpdir.join('.csv_ci_test_output.csv').realpath()) |
| 197 | + generate_test_helper(memap_parser, 'csv-ci', depth, file_name) |
| 198 | + assert isfile(file_name), "Failed to create csv-ci file" |
0 commit comments