Advertisement
DeaD_EyE

test_sequence homework 2

Apr 9th, 2018
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.56 KB | None | 0 0
  1. ''' test framework for string to nums beginner challenge
  2.  
  3.     Copy this, using the raw view (or download) to an editor of your choice
  4.     and insert your function(s) at the top of the code ensuring the function
  5.     you want to test takes one string argument and returns a list
  6.  
  7.     Edit the last line, the call to the test function test_funcs() and place
  8.     the name of each function, without its trailing (), in the call to test_funcs,
  9.     with commas between each function name if testing more than one function.
  10. '''
  11.  
  12. def test_funcs(*funcs):
  13.     import traceback
  14.     tests = [('1-5,7,9,10-13', [1, 2, 3, 4, 5, 7, 9, 10, 11, 12, 13]),
  15.              ('5, 10-6, 4-8,3', [3, 4, 5, 6, 7, 8, 9, 10]),
  16.              ('1023,675,43, 23400-23395, 5-12', [5,6,7,8,9,10,11,12,43,675,1023,23395,23396,23397,23398,23399,23400]),
  17.              ('1, 3 -5, 6-, -, 7, -9 , 16 - 20,23',
  18.               [1, 3, 4, 5, 7, 16, 17, 18, 19, 20, 23]),
  19.              ('2, 4, num, ;5, a-b', [2, 4])
  20.              ]
  21.     for func in funcs:
  22.         print(f'\n\nTesting function: {func.__name__}\n')
  23.         print('-----------')
  24.         for test, assertion in tests:
  25.             print(f'Testing: {test}')
  26.             try:
  27.                 result = func(test)
  28.             except ValueError as errmsg:
  29.                 print(errmsg)
  30.             except Exception as e:
  31.                 print(f'ERROR: {traceback.format_exc()}')
  32.             else:
  33.                 try:
  34.                     assert sorted(set(result)) == assertion
  35.                 except AssertionError:
  36.                     print(
  37.                         f'*** ERROR -> for {test}\n\texpected {assertion},\n\treceived {result}')
  38.                 else:
  39.                     print(result, end='')
  40.                     if result != sorted(set(result)):
  41.                         print(' ** result not ordered/unique')
  42.                     else:
  43.                         print()
  44.             print('-----------')
  45.  
  46.  
  47. if __name__ == "__main__":
  48.     def test_funcs(*funcs):
  49.         import traceback
  50.         tests = [('1-5,7,9,10-13', [1, 2, 3, 4, 5, 7, 9, 10, 11, 12, 13]),
  51.                  ('5, 10-6, 4-8,3', [3, 4, 5, 6, 7, 8, 9, 10]),
  52.                  ('1023,675,43, 23400-23395, 5-12', [5,6,7,8,9,10,11,12,43,675,1023,23395,23396,23397,23398,23399,23400]),
  53.                  ('1, 3 -5, 6-, -, 7, -9 , 16 - 20,23',
  54.                   [1, 3, 4, 5, 7, 16, 17, 18, 19, 20, 23]),
  55.                  ('2, 4, num, ;5, a-b', [2, 4])
  56.                  ]
  57.         for func in funcs:
  58.             print(f'\n\nTesting function: {func.__name__}\n')
  59.             print('-----------')
  60.             for test, assertion in tests:
  61.                 print(f'Testing: {test}')
  62.                 try:
  63.                     result = func(test)
  64.                 except ValueError as errmsg:
  65.                     print(errmsg)
  66.                 except Exception as e:
  67.                     print(f'ERROR: {traceback.format_exc()}')
  68.                 else:
  69.                     try:
  70.                         assert sorted(set(result)) == assertion
  71.                     except AssertionError:
  72.                         print(
  73.                             f'*** ERROR -> for {test}\n\texpected {assertion},\n\treceived {result}')
  74.                     else:
  75.                         print(result, end='')
  76.                         if result != sorted(set(result)):
  77.                             print(' ** result not ordered/unique')
  78.                         else:
  79.                             print()
  80.                 print('-----------')
  81.  
  82.  
  83. if __name__ == "__main__":
  84.     from seq import safe_adv_range
  85.     test_funcs(safe_adv_range)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement