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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
#!/usr/bin/env python3
# Copyright 2018 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Tests for extract_sqlite_api.py.
These tests should be getting picked up by the PRESUBMIT.py in this directory.
"""
from importlib.machinery import SourceFileLoader
import os
import shutil
import sys
import tempfile
import unittest
class ExtractSqliteApiUnittest(unittest.TestCase):
def setUp(self):
self.test_root = tempfile.mkdtemp()
source_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'extract_sqlite_api.py')
self.extractor = SourceFileLoader('extract_api',
source_path).load_module()
def tearDown(self):
if self.test_root:
shutil.rmtree(self.test_root)
def testExtractLineTuples(self):
golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3'), (5, '')]
text_with_newline = "Line1\n\nLine 2 \nLine3\n"
self.assertEqual(
self.extractor.ExtractLineTuples(text_with_newline), golden)
golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3')]
text_without_newline = "Line1\n\nLine 2 \nLine3"
self.assertEqual(
self.extractor.ExtractLineTuples(text_without_newline), golden)
def testExtractPreprocessorDirectives(self):
lines = [
(1, '// Header comment'),
(2, '#define DIRECTIVE 1'),
(3, 'int main() { // \\'),
(4, '}'),
(5, ''),
(6, '#define MULTILINE \\'),
(7, 'MORE_MULTILINE_DIRECTIVE\\'),
(8, 'END_MULTILINE_DIRECTIVE'),
(9, 'void code() { }'),
]
directives, code_lines = self.extractor.ExtractPreprocessorDirectives(
lines)
self.assertEqual(directives, [
'#define DIRECTIVE 1',
'#define MULTILINE \nMORE_MULTILINE_DIRECTIVE\nEND_MULTILINE_DIRECTIVE',
])
self.assertEqual(code_lines, [
(1, '// Header comment'),
(3, 'int main() { // \\'),
(4, '}'),
(5, ''),
(9, 'void code() { }'),
])
def testExtractDefineMacroName(self):
self.assertEqual(
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API 1'))
self.assertEqual(
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API'))
self.assertEqual(
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#define SQLITE_API\n1'))
self.assertEqual(
'SQLITE_API',
self.extractor.ExtractDefineMacroName(
'# define SQLITE_API 1'))
self.assertEqual(
'SQLITE_API',
self.extractor.ExtractDefineMacroName('#\tdefine\tSQLITE_API\t1'))
self.assertEqual(
None,
self.extractor.ExtractDefineMacroName(' #define SQLITE_API 1'))
self.assertEqual(
None,
self.extractor.ExtractDefineMacroName(' #define SQLITE_API() 1'))
self.assertEqual(None, self.extractor.ExtractDefineMacroName(''))
def testRemoveLineComments(self):
self.assertEqual('word;', self.extractor.RemoveLineComments('word;'))
self.assertEqual('', self.extractor.RemoveLineComments(''))
self.assertEqual('', self.extractor.RemoveLineComments('// comment'))
self.assertEqual('',
self.extractor.RemoveLineComments('/* comment */'))
self.assertEqual('word;',
self.extractor.RemoveLineComments('wo/*comment*/rd;'))
self.assertEqual(
'word;*/', self.extractor.RemoveLineComments('wo/*comment*/rd;*/'))
self.assertEqual(
'word;*/',
self.extractor.RemoveLineComments('wo/*/*comment*/rd;*/'))
self.assertEqual(
'word;', self.extractor.RemoveLineComments('wo/*comm//ent*/rd;'))
def testRemoveComments(self):
lines = [
(1, 'code();'),
(2, 'more_code(); /* with comment */ more_code();'),
(3, '/**'),
(4, 'Spec text'),
(5, '**/ spec_code();'),
(6,
'late_code(); /* with comment */ more_late_code(); /* late comment'
),
(7, 'ends here // C++ trap */ code(); // /* C trap'),
(8, 'last_code();'),
]
self.assertEqual(
self.extractor.RemoveComments(lines), [
(1, 'code();'),
(2, 'more_code(); more_code();'),
(3, ''),
(5, ' spec_code();'),
(6, 'late_code(); more_late_code(); '),
(7, ' code(); '),
(8, 'last_code();'),
])
def testToStatementTuples(self):
lines = [(1, 'void function();'), (2, 'int main('),
(3, ' int argc, char* argv) {'),
(4, ' statement1; statement2;'), (5, '}'), (6, 'stat'),
(7, 'ement4; statement5; sta'), (8, 'tem'),
(9, 'ent6; statement7;')]
self.assertEqual(
self.extractor.ToStatementTuples(lines), [
(1, 1, 'void function()'),
(2, 3, 'int main(\n int argc, char* argv)'),
(4, 4, 'statement1'),
(4, 4, 'statement2'),
(5, 5, ''),
(6, 7, 'stat\nement4'),
(7, 7, 'statement5'),
(7, 9, 'sta\ntem\nent6'),
(9, 9, 'statement7'),
])
def testExtractApiExport(self):
self.assertEqual(
'sqlite3_init',
self.extractor.ExtractApiExport(set(), 'SQLITE_API',
'SQLITE_API void sqlite3_init()'))
self.assertEqual(
'sqlite3_sleep',
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API int sqlite3_sleep(int ms)'))
self.assertEqual(
'sqlite3_sleep',
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API long long sqlite3_sleep(int ms)'))
self.assertEqual(
'sqlite3rbu_temp_size',
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu)'
))
self.assertEqual(
'sqlite3_expired',
self.extractor.ExtractApiExport(
set(['SQLITE_DEPRECATED']), 'SQLITE_API',
'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*)'
))
# SQLite's header actually #defines double (in some cases).
self.assertEqual(
'sqlite3_column_double',
self.extractor.ExtractApiExport(
set(['double']), 'SQLITE_API',
'SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol)'
))
self.assertEqual(
'sqlite3_temp_directory',
self.extractor.ExtractApiExport(
set(['SQLITE_EXTERN']), 'SQLITE_API',
'SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory'))
self.assertEqual(
'sqlite3_version',
self.extractor.ExtractApiExport(
set(['SQLITE_EXTERN']), 'SQLITE_API',
'SQLITE_API SQLITE_EXTERN const char sqlite3_version[]'))
self.assertEqual(
None,
self.extractor.ExtractApiExport(
set(['SQLITE_DEPRECATED']), 'SQLITE_API',
'NOT_SQLITE_API struct sqlite_type sqlite3_sleep(int ms)'))
with self.assertRaisesRegex(self.extractor.ExtractError,
'Mixed simple .* and composite'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API void int sqlite3_sleep(int ms)')
with self.assertRaisesRegex(self.extractor.ExtractError,
'Unsupported keyword struct'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API',
'SQLITE_API struct sqlite_type sqlite3_sleep(int ms)')
with self.assertRaisesRegex(self.extractor.ExtractError,
'int\+\+ parsed as type name'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API int++ sqlite3_sleep(int ms)')
with self.assertRaisesRegex(self.extractor.ExtractError,
'sqlite3\+sleep parsed as symbol'):
self.extractor.ExtractApiExport(
set(), 'SQLITE_API', 'SQLITE_API int sqlite3+sleep(int ms)')
def testExportedSymbolLine(self):
self.assertEqual(
'#define sqlite3_sleep chrome_sqlite3_sleep // Line 42',
self.extractor.ExportedSymbolLine(
'chrome_', 'sqlite3_sleep',
(42, 42, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
self.assertEqual(
'#define sqlite3_sleep chrome_sqlite3_sleep // Lines 42-44',
self.extractor.ExportedSymbolLine(
'chrome_', 'sqlite3_sleep',
(42, 44, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
def testExportedExceptionLine(self):
self.assertEqual(
'// TODO: Lines 42-44 -- Something went wrong',
self.extractor.ExportedExceptionLine(
self.extractor.ExtractError('Something went wrong'),
(42, 44, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
def testProcessSource(self):
file_content = '\n'.join([
'/*',
'struct sqlite_type sqlite3_sleep; // Remove comments',
'*/',
'#define SQLITE_DEPRECATED',
'SQLITE_API int sqlite3_sleep(int ms);',
'SQLITE_API struct sqlite_type sqlite3_sleep(int ms);',
'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);',
])
golden_output = [
'// Header',
'#define sqlite3_expired chrome_sqlite3_expired // Line 7',
'#define sqlite3_sleep chrome_sqlite3_sleep // Line 5',
'// TODO: Lines 6-6 -- Unsupported keyword struct',
'// Footer',
]
self.assertEqual(
golden_output,
self.extractor.ProcessSource('SQLITE_API', 'chrome_', '// Header',
'// Footer', file_content))
def testProcessSourceFile(self):
file_content = '\n'.join([
'/*',
'struct sqlite_type sqlite3_sleep; // Remove comments',
'*/',
'#define SQLITE_DEPRECATED',
'SQLITE_API int sqlite3_sleep(int ms);',
'SQLITE_API struct sqlite_type sqlite3_sleep(int ms);',
'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);',
])
golden_output = '\n'.join([
'// Header',
'#define sqlite3_expired chrome_sqlite3_expired // Line 7',
'#define sqlite3_sleep chrome_sqlite3_sleep // Line 5',
'// TODO: Lines 6-6 -- Unsupported keyword struct',
'// Footer',
'',
])
input_file = os.path.join(self.test_root, 'input.h')
output_file = os.path.join(self.test_root, 'macros.h')
with open(input_file, 'w') as f:
f.write(file_content)
self.extractor.ProcessSourceFile('SQLITE_API', 'chrome_', '// Header',
'// Footer', input_file, output_file)
with open(output_file, 'r') as f:
self.assertEqual(f.read(), golden_output)
if __name__ == '__main__':
unittest.main()
|