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.

57 lines
2.0 KiB

  1. __author__ = 'Chris'
  2. import unittest
  3. import code_prep
  4. class TestCodePrep(unittest.TestCase):
  5. def test_split_line(self):
  6. line = "parser = ArgumentParser(description='Example Argparse Program')"
  7. self.assertEqual("parser", code_prep.split_line(line)[0])
  8. self.assertEqual("ArgumentParser(description='Example Argparse Program')", code_prep.split_line(line)[1])
  9. def test_update_parser_varname_assigns_new_name_to_parser_var(self):
  10. line = ["parser = ArgumentParser(description='Example Argparse Program')"]
  11. self.assertEqual(
  12. "jarser = ArgumentParser(description='Example Argparse Program')",
  13. code_prep.update_parser_varname('jarser', line)[0]
  14. )
  15. def test_update_parser_varname_assigns_new_name_to_parser_var__multiline(self):
  16. lines = '''
  17. import argparse
  18. from argparse import ArgumentParser
  19. parser = ArgumentParser(description='Example Argparse Program')
  20. parser.parse_args()
  21. '''.split('\n')
  22. self.assertEqual(
  23. "jarser = ArgumentParser(description='Example Argparse Program')",
  24. code_prep.update_parser_varname('jarser', lines)[2]
  25. )
  26. def test_take_imports_drops_all_non_imports_statements(self):
  27. lines = '''
  28. import argparse
  29. from argparse import ArgumentParser
  30. parser = ArgumentParser(description='Example Argparse Program')
  31. parser.parse_args()
  32. '''.split('\n')[1:]
  33. self.assertEqual(2, len(list(code_prep.take_imports(lines))))
  34. self.assertEqual('import argparse', list(code_prep.take_imports(lines))[0])
  35. def test_drop_imports_excludes_all_imports_statements(self):
  36. lines = '''
  37. import argparse
  38. from argparse import ArgumentParser
  39. parser = ArgumentParser(description='Example Argparse Program')
  40. parser.parse_args()
  41. '''.split('\n')[1:]
  42. self.assertEqual(2, len(list(code_prep.take_imports(lines))))
  43. self.assertEqual('parser.parse_args()', list(code_prep.drop_imports(lines))[1])
  44. if __name__ == "__main__":
  45. #import sys;sys.argv = ['', 'Test.testName']
  46. unittest.main()