You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
9.3 KiB

  1. # '''
  2. # Created on Feb 2, 2014
  3. #
  4. # @author: Chris
  5. #
  6. # TODO:
  7. # - test no argparse module
  8. # - test argparse in main
  9. # - test argparse in try/catch
  10. # -
  11. #
  12. # '''
  13. #
  14. # import os
  15. # import ast
  16. # import unittest
  17. # from gooey.python_bindings import source_parser
  18. #
  19. #
  20. # basic_pyfile = \
  21. # '''
  22. # import os
  23. #
  24. # def say_jello():
  25. # print "Jello!"
  26. #
  27. # def main():
  28. # print "hello!"
  29. # parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  30. # parser.add_argument("filename", help="filename")
  31. # parser.add_argument("-r", "--recursive", dest="recurse", action="store_true",
  32. # help="recurse into subfolders [default: %(default)s]")
  33. # parser.add_argument("-v", "--verbose", dest="verbose", action="count",
  34. # help="set verbosity level [default: %(default)s]")
  35. # parser.add_argument("-i", "--include", action="append",
  36. # help="only include paths matching this regex pattern. Note: exclude is given preference over include. [default: %(default)s]",
  37. # metavar="RE")
  38. # parser.add_argument("-m", "--mycoolargument", help="mycoolargument")
  39. # parser.add_argument("-e", "--exclude", dest="exclude",
  40. # help="exclude paths matching this regex pattern. [default: %(default)s]", metavar="RE")
  41. # parser.add_argument('-V', '--version', action='version')
  42. # parser.add_argument('-T', '--tester', choices=['yes', 'no'])
  43. # parser.add_argument(dest="paths", help="paths to folder(s) with source file(s) [default: %(default)s]",
  44. # metavar="path", nargs='+')
  45. #
  46. # if __name__ == '__main__':
  47. # main()
  48. # '''
  49. #
  50. #
  51. #
  52. # class TestSourceParser(unittest.TestCase):
  53. # PATH = os.path.join(os.path.dirname(__file__), 'examples')
  54. #
  55. # def module_path(self, name):
  56. # return os.path.join(self.PATH, name)
  57. #
  58. # def setUp(self):
  59. # self._mockapp = self.module_path('examples.py')
  60. # self._module_with_noargparse = self.module_path('module_with_no_argparse.py')
  61. # self._module_with_arparse_in_try = self.module_path('TODO.py')
  62. # self._module_with_argparse_in_main = self.module_path('example_argparse_souce_in_main.py')
  63. #
  64. # def test_should_throw_parser_exception_if_no_argparse_found_in_module(self):
  65. # with self.assertRaises(source_parser.ParserError):
  66. # source_parser.parse_source_file(self._module_with_noargparse)
  67. #
  68. #
  69. # def test_find_main(self):
  70. # example_source = '''
  71. # def main(): pass
  72. # '''
  73. # nodes = ast.parse(example_source)
  74. # main_node = source_parser.find_main(nodes)
  75. # self.assertEqual('main', main_node.name)
  76. #
  77. #
  78. # def test_find_main_throws_exception_if_not_found(self):
  79. # example_source = '''
  80. # def some_cool_function_that_is_not_main(): pass
  81. # '''
  82. # with self.assertRaises(source_parser.ParserError):
  83. # nodes = ast.parse(example_source)
  84. # main_node = source_parser.find_main(nodes)
  85. # self.assertEqual('main', main_node.name)
  86. #
  87. #
  88. # def test_find_try_blocks_finds_all_tryblock_styles(self):
  89. # example_source = '''
  90. # try: a = 1
  91. # except: pass
  92. #
  93. # try: pass
  94. # finally: pass
  95. #
  96. # try: pass
  97. # except: pass
  98. # else: pass
  99. # '''
  100. # nodes = ast.parse(example_source)
  101. # try_blocks = source_parser.find_try_blocks(nodes)
  102. # self.assertEqual(3, len(try_blocks))
  103. #
  104. #
  105. # def test_find_try_blocks_returns_empty_if_no_blocks_present(self):
  106. # example_source = 'def main(): pass'
  107. # nodes = ast.parse(example_source)
  108. # result = source_parser.find_try_blocks(nodes)
  109. # self.assertEqual(list(), result)
  110. #
  111. # def test_find_argparse_located_object_when_imported_by_direct_name(self):
  112. # example_source = '''
  113. # def main():
  114. # parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  115. # '''
  116. # nodes = ast.parse(example_source)
  117. # main_node = source_parser.find_main(nodes)
  118. # self.assertEqual('main', main_node.name)
  119. # containing_block = source_parser.find_block_containing_argparse([main_node])
  120. # self.assertTrue(containing_block is not None)
  121. #
  122. # def test_find_argparse_located_object_when_access_through_module_dot_notation(self):
  123. # example_source = '''
  124. # def main():
  125. # parser = argparse.ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  126. # '''
  127. # nodes = ast.parse(example_source)
  128. # main_node = source_parser.find_main(nodes)
  129. # self.assertEqual('main', main_node.name)
  130. # containing_block = source_parser.find_block_containing_argparse([main_node])
  131. # self.assertTrue(containing_block is not None)
  132. #
  133. # def test_find_argparse_locates_assignment_stmnt_in_main(self):
  134. # nodes = ast.parse(source_parser._openfile(self._module_with_argparse_in_main))
  135. # main_node = source_parser.find_main(nodes)
  136. # self.assertEqual('main', main_node.name)
  137. # containing_block = source_parser.find_block_containing_argparse([main_node])
  138. # self.assertTrue(containing_block is not None)
  139. # self.assertEqual('main', containing_block.name)
  140. #
  141. #
  142. # def test_find_argparse_locates_assignment_stmnt_in_try_block(self):
  143. # nodes = ast.parse(source_parser._openfile(self._module_with_arparse_in_try))
  144. # main_node = source_parser.find_main(nodes)
  145. # self.assertEqual('main', main_node.name)
  146. # try_nodes = source_parser.find_try_blocks(main_node)
  147. # self.assertTrue(len(try_nodes) > 0)
  148. # containing_block = source_parser.find_block_containing_argparse([main_node] + try_nodes)
  149. # self.assertEqual(ast.TryExcept, type(containing_block))
  150. #
  151. #
  152. # def test_find_argparse_throws_exception_if_not_found(self):
  153. # with self.assertRaises(source_parser.ParserError):
  154. # nodes = ast.parse(source_parser._openfile(self._module_with_noargparse))
  155. # main_node = source_parser.find_main(nodes)
  156. # self.assertEqual('main', main_node.name)
  157. # try_nodes = source_parser.find_try_blocks(main_node)
  158. # containing_block = source_parser.find_block_containing_argparse([main_node] + try_nodes)
  159. #
  160. #
  161. # def test_has_instantiator_returns_true_if_object_found(self):
  162. # source = '''
  163. # parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  164. # parser.add_argument("filename", help="filename")
  165. # '''
  166. # nodes = ast.parse(source)
  167. # self.assertTrue(source_parser.has_instantiator(nodes.body[0], 'ArgumentParser'))
  168. #
  169. #
  170. # def test_has_instantiator_returns_false_if_object_not_found(self):
  171. # source = '''
  172. # parser = NopeParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  173. # parser.add_argument("filename", help="filename")
  174. # '''
  175. # nodes = ast.parse(source)
  176. # self.assertFalse(source_parser.has_instantiator(nodes.body[0], 'ArgumentParser'))
  177. #
  178. # def test_has_assignment_returns_true_if_object_found(self):
  179. # source = '''
  180. # parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  181. # parser.add_argument("filename", help="filename")
  182. # '''
  183. # nodes = ast.parse(source)
  184. # self.assertTrue(source_parser.has_assignment(nodes.body[1], 'add_argument'))
  185. #
  186. # def test_has_assignment_returns_false_if_object_not_found(self):
  187. # source = '''
  188. # parser = ArgumentParser(description='Example Argparse Program', formatter_class=RawDescriptionHelpFormatter)
  189. # parser.add_argument("filename", help="filename")
  190. # '''
  191. # nodes = ast.parse(source)
  192. # self.assertFalse(source_parser.has_instantiator(nodes.body[1], 'add_argument'))
  193. #
  194. # def test_parser_identifies_import_module(self):
  195. # source = '''
  196. # import os
  197. # import itertools
  198. # from os import path
  199. # '''
  200. # import _ast
  201. # nodes = ast.parse(source)
  202. # module_imports = source_parser.get_nodes_by_instance_type(nodes, _ast.Import)
  203. # self.assertEqual(2, len(module_imports))
  204. #
  205. # def test_parser_identifies_import_from(self):
  206. # source = '''
  207. # import os
  208. # import itertools
  209. # from os import path
  210. # from gooey.gooey_decorator import Gooey
  211. # '''
  212. # import _ast
  213. # nodes = ast.parse(source)
  214. # from_imports = source_parser.get_nodes_by_instance_type(nodes, _ast.ImportFrom)
  215. # self.assertEqual(2, len(from_imports))
  216. #
  217. #
  218. # def test_get_indent_return_indent_amount_for_tabs_and_spaces(self):
  219. # spaced_lines = ["def main"," def main"," def main"," def main"]
  220. # expected_indent = ["", " ", " ", " "]
  221. # for line, expected in zip(spaced_lines, expected_indent):
  222. # self.assertEqual(expected, source_parser.get_indent(line))
  223. #
  224. # # def test_parse_source_file__file_with_argparse_in_main__succesfully_finds_and_returns_ast_obejcts(self):
  225. # # ast_objects = source_parser.parse_source_file(self._module_with_argparse_in_main)
  226. # # for obj in ast_objects:
  227. # # self.assertTrue(type(obj) in (ast.Assign, ast.Expr))
  228. # #
  229. # # def test_parse_source_file__file_with_argparse_in_try_block__succesfully_finds_and_returns_ast_obejcts(self):
  230. # # ast_objects = source_parser.parse_source_file(self._module_with_arparse_in_try)
  231. # # for obj in ast_objects:
  232. # # self.assertTrue(type(obj) in (ast.Assign, ast.Expr))
  233. #
  234. #
  235. # if __name__ == "__main__":
  236. # #import sys;sys.argv = ['', 'Test.testName']
  237. # unittest.main()
  238. #