PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/third_party/sqlite/scripts/extract_sqlite_api_unittest.py

https://github.com/chromium/chromium
Python | 295 lines | 276 code | 11 blank | 8 comment | 7 complexity | a34cf44364baeb934637c5cf3e2aa028 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. #!/usr/bin/env python3
  2. # Copyright 2018 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Tests for extract_sqlite_api.py.
  6. These tests should be getting picked up by the PRESUBMIT.py in this directory.
  7. """
  8. from importlib.machinery import SourceFileLoader
  9. import os
  10. import shutil
  11. import sys
  12. import tempfile
  13. import unittest
  14. class ExtractSqliteApiUnittest(unittest.TestCase):
  15. def setUp(self):
  16. self.test_root = tempfile.mkdtemp()
  17. source_path = os.path.join(
  18. os.path.dirname(os.path.realpath(__file__)),
  19. 'extract_sqlite_api.py')
  20. self.extractor = SourceFileLoader('extract_api',
  21. source_path).load_module()
  22. def tearDown(self):
  23. if self.test_root:
  24. shutil.rmtree(self.test_root)
  25. def testExtractLineTuples(self):
  26. golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3'), (5, '')]
  27. text_with_newline = "Line1\n\nLine 2 \nLine3\n"
  28. self.assertEqual(
  29. self.extractor.ExtractLineTuples(text_with_newline), golden)
  30. golden = [(1, 'Line1'), (2, ''), (3, 'Line 2'), (4, 'Line3')]
  31. text_without_newline = "Line1\n\nLine 2 \nLine3"
  32. self.assertEqual(
  33. self.extractor.ExtractLineTuples(text_without_newline), golden)
  34. def testExtractPreprocessorDirectives(self):
  35. lines = [
  36. (1, '// Header comment'),
  37. (2, '#define DIRECTIVE 1'),
  38. (3, 'int main() { // \\'),
  39. (4, '}'),
  40. (5, ''),
  41. (6, '#define MULTILINE \\'),
  42. (7, 'MORE_MULTILINE_DIRECTIVE\\'),
  43. (8, 'END_MULTILINE_DIRECTIVE'),
  44. (9, 'void code() { }'),
  45. ]
  46. directives, code_lines = self.extractor.ExtractPreprocessorDirectives(
  47. lines)
  48. self.assertEqual(directives, [
  49. '#define DIRECTIVE 1',
  50. '#define MULTILINE \nMORE_MULTILINE_DIRECTIVE\nEND_MULTILINE_DIRECTIVE',
  51. ])
  52. self.assertEqual(code_lines, [
  53. (1, '// Header comment'),
  54. (3, 'int main() { // \\'),
  55. (4, '}'),
  56. (5, ''),
  57. (9, 'void code() { }'),
  58. ])
  59. def testExtractDefineMacroName(self):
  60. self.assertEqual(
  61. 'SQLITE_API',
  62. self.extractor.ExtractDefineMacroName('#define SQLITE_API 1'))
  63. self.assertEqual(
  64. 'SQLITE_API',
  65. self.extractor.ExtractDefineMacroName('#define SQLITE_API'))
  66. self.assertEqual(
  67. 'SQLITE_API',
  68. self.extractor.ExtractDefineMacroName('#define SQLITE_API\n1'))
  69. self.assertEqual(
  70. 'SQLITE_API',
  71. self.extractor.ExtractDefineMacroName(
  72. '# define SQLITE_API 1'))
  73. self.assertEqual(
  74. 'SQLITE_API',
  75. self.extractor.ExtractDefineMacroName('#\tdefine\tSQLITE_API\t1'))
  76. self.assertEqual(
  77. None,
  78. self.extractor.ExtractDefineMacroName(' #define SQLITE_API 1'))
  79. self.assertEqual(
  80. None,
  81. self.extractor.ExtractDefineMacroName(' #define SQLITE_API() 1'))
  82. self.assertEqual(None, self.extractor.ExtractDefineMacroName(''))
  83. def testRemoveLineComments(self):
  84. self.assertEqual('word;', self.extractor.RemoveLineComments('word;'))
  85. self.assertEqual('', self.extractor.RemoveLineComments(''))
  86. self.assertEqual('', self.extractor.RemoveLineComments('// comment'))
  87. self.assertEqual('',
  88. self.extractor.RemoveLineComments('/* comment */'))
  89. self.assertEqual('word;',
  90. self.extractor.RemoveLineComments('wo/*comment*/rd;'))
  91. self.assertEqual(
  92. 'word;*/', self.extractor.RemoveLineComments('wo/*comment*/rd;*/'))
  93. self.assertEqual(
  94. 'word;*/',
  95. self.extractor.RemoveLineComments('wo/*/*comment*/rd;*/'))
  96. self.assertEqual(
  97. 'word;', self.extractor.RemoveLineComments('wo/*comm//ent*/rd;'))
  98. def testRemoveComments(self):
  99. lines = [
  100. (1, 'code();'),
  101. (2, 'more_code(); /* with comment */ more_code();'),
  102. (3, '/**'),
  103. (4, 'Spec text'),
  104. (5, '**/ spec_code();'),
  105. (6,
  106. 'late_code(); /* with comment */ more_late_code(); /* late comment'
  107. ),
  108. (7, 'ends here // C++ trap */ code(); // /* C trap'),
  109. (8, 'last_code();'),
  110. ]
  111. self.assertEqual(
  112. self.extractor.RemoveComments(lines), [
  113. (1, 'code();'),
  114. (2, 'more_code(); more_code();'),
  115. (3, ''),
  116. (5, ' spec_code();'),
  117. (6, 'late_code(); more_late_code(); '),
  118. (7, ' code(); '),
  119. (8, 'last_code();'),
  120. ])
  121. def testToStatementTuples(self):
  122. lines = [(1, 'void function();'), (2, 'int main('),
  123. (3, ' int argc, char* argv) {'),
  124. (4, ' statement1; statement2;'), (5, '}'), (6, 'stat'),
  125. (7, 'ement4; statement5; sta'), (8, 'tem'),
  126. (9, 'ent6; statement7;')]
  127. self.assertEqual(
  128. self.extractor.ToStatementTuples(lines), [
  129. (1, 1, 'void function()'),
  130. (2, 3, 'int main(\n int argc, char* argv)'),
  131. (4, 4, 'statement1'),
  132. (4, 4, 'statement2'),
  133. (5, 5, ''),
  134. (6, 7, 'stat\nement4'),
  135. (7, 7, 'statement5'),
  136. (7, 9, 'sta\ntem\nent6'),
  137. (9, 9, 'statement7'),
  138. ])
  139. def testExtractApiExport(self):
  140. self.assertEqual(
  141. 'sqlite3_init',
  142. self.extractor.ExtractApiExport(set(), 'SQLITE_API',
  143. 'SQLITE_API void sqlite3_init()'))
  144. self.assertEqual(
  145. 'sqlite3_sleep',
  146. self.extractor.ExtractApiExport(
  147. set(), 'SQLITE_API', 'SQLITE_API int sqlite3_sleep(int ms)'))
  148. self.assertEqual(
  149. 'sqlite3_sleep',
  150. self.extractor.ExtractApiExport(
  151. set(), 'SQLITE_API',
  152. 'SQLITE_API long long sqlite3_sleep(int ms)'))
  153. self.assertEqual(
  154. 'sqlite3rbu_temp_size',
  155. self.extractor.ExtractApiExport(
  156. set(), 'SQLITE_API',
  157. 'SQLITE_API sqlite3_int64 sqlite3rbu_temp_size(sqlite3rbu *pRbu)'
  158. ))
  159. self.assertEqual(
  160. 'sqlite3_expired',
  161. self.extractor.ExtractApiExport(
  162. set(['SQLITE_DEPRECATED']), 'SQLITE_API',
  163. 'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*)'
  164. ))
  165. # SQLite's header actually #defines double (in some cases).
  166. self.assertEqual(
  167. 'sqlite3_column_double',
  168. self.extractor.ExtractApiExport(
  169. set(['double']), 'SQLITE_API',
  170. 'SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol)'
  171. ))
  172. self.assertEqual(
  173. 'sqlite3_temp_directory',
  174. self.extractor.ExtractApiExport(
  175. set(['SQLITE_EXTERN']), 'SQLITE_API',
  176. 'SQLITE_API SQLITE_EXTERN char *sqlite3_temp_directory'))
  177. self.assertEqual(
  178. 'sqlite3_version',
  179. self.extractor.ExtractApiExport(
  180. set(['SQLITE_EXTERN']), 'SQLITE_API',
  181. 'SQLITE_API SQLITE_EXTERN const char sqlite3_version[]'))
  182. self.assertEqual(
  183. None,
  184. self.extractor.ExtractApiExport(
  185. set(['SQLITE_DEPRECATED']), 'SQLITE_API',
  186. 'NOT_SQLITE_API struct sqlite_type sqlite3_sleep(int ms)'))
  187. with self.assertRaisesRegex(self.extractor.ExtractError,
  188. 'Mixed simple .* and composite'):
  189. self.extractor.ExtractApiExport(
  190. set(), 'SQLITE_API',
  191. 'SQLITE_API void int sqlite3_sleep(int ms)')
  192. with self.assertRaisesRegex(self.extractor.ExtractError,
  193. 'Unsupported keyword struct'):
  194. self.extractor.ExtractApiExport(
  195. set(), 'SQLITE_API',
  196. 'SQLITE_API struct sqlite_type sqlite3_sleep(int ms)')
  197. with self.assertRaisesRegex(self.extractor.ExtractError,
  198. 'int\+\+ parsed as type name'):
  199. self.extractor.ExtractApiExport(
  200. set(), 'SQLITE_API', 'SQLITE_API int++ sqlite3_sleep(int ms)')
  201. with self.assertRaisesRegex(self.extractor.ExtractError,
  202. 'sqlite3\+sleep parsed as symbol'):
  203. self.extractor.ExtractApiExport(
  204. set(), 'SQLITE_API', 'SQLITE_API int sqlite3+sleep(int ms)')
  205. def testExportedSymbolLine(self):
  206. self.assertEqual(
  207. '#define sqlite3_sleep chrome_sqlite3_sleep // Line 42',
  208. self.extractor.ExportedSymbolLine(
  209. 'chrome_', 'sqlite3_sleep',
  210. (42, 42, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
  211. self.assertEqual(
  212. '#define sqlite3_sleep chrome_sqlite3_sleep // Lines 42-44',
  213. self.extractor.ExportedSymbolLine(
  214. 'chrome_', 'sqlite3_sleep',
  215. (42, 44, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
  216. def testExportedExceptionLine(self):
  217. self.assertEqual(
  218. '// TODO: Lines 42-44 -- Something went wrong',
  219. self.extractor.ExportedExceptionLine(
  220. self.extractor.ExtractError('Something went wrong'),
  221. (42, 44, 'SQLITE_API int chrome_sqlite3_sleep(int ms)')))
  222. def testProcessSource(self):
  223. file_content = '\n'.join([
  224. '/*',
  225. 'struct sqlite_type sqlite3_sleep; // Remove comments',
  226. '*/',
  227. '#define SQLITE_DEPRECATED',
  228. 'SQLITE_API int sqlite3_sleep(int ms);',
  229. 'SQLITE_API struct sqlite_type sqlite3_sleep(int ms);',
  230. 'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);',
  231. ])
  232. golden_output = [
  233. '// Header',
  234. '#define sqlite3_expired chrome_sqlite3_expired // Line 7',
  235. '#define sqlite3_sleep chrome_sqlite3_sleep // Line 5',
  236. '// TODO: Lines 6-6 -- Unsupported keyword struct',
  237. '// Footer',
  238. ]
  239. self.assertEqual(
  240. golden_output,
  241. self.extractor.ProcessSource('SQLITE_API', 'chrome_', '// Header',
  242. '// Footer', file_content))
  243. def testProcessSourceFile(self):
  244. file_content = '\n'.join([
  245. '/*',
  246. 'struct sqlite_type sqlite3_sleep; // Remove comments',
  247. '*/',
  248. '#define SQLITE_DEPRECATED',
  249. 'SQLITE_API int sqlite3_sleep(int ms);',
  250. 'SQLITE_API struct sqlite_type sqlite3_sleep(int ms);',
  251. 'SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);',
  252. ])
  253. golden_output = '\n'.join([
  254. '// Header',
  255. '#define sqlite3_expired chrome_sqlite3_expired // Line 7',
  256. '#define sqlite3_sleep chrome_sqlite3_sleep // Line 5',
  257. '// TODO: Lines 6-6 -- Unsupported keyword struct',
  258. '// Footer',
  259. '',
  260. ])
  261. input_file = os.path.join(self.test_root, 'input.h')
  262. output_file = os.path.join(self.test_root, 'macros.h')
  263. with open(input_file, 'w') as f:
  264. f.write(file_content)
  265. self.extractor.ProcessSourceFile('SQLITE_API', 'chrome_', '// Header',
  266. '// Footer', input_file, output_file)
  267. with open(output_file, 'r') as f:
  268. self.assertEqual(f.read(), golden_output)
  269. if __name__ == '__main__':
  270. unittest.main()