Advertisement
gruntfutuk

Test string to num

Apr 9th, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 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.    
  14.     import traceback
  15.    
  16.     tests = [('1-5,7,9,10-13', [1, 2, 3, 4, 5, 7, 9, 10, 11, 12, 13]),
  17.              ('5, 10-6, 4-8,3', [3, 4, 5, 6, 7, 8, 9, 10]),
  18.              ('1023,675,43, 23400-23395, 5-12', [5,6,7,8,9,10,11,12,43,675,1023,23395,23396,23397,23398,23399,23400]),
  19.              ('1, 3 -5, 6-, -, 7, -9 , 16 - 20,23',
  20.               [1, 3, 4, 5, 7, 16, 17, 18, 19, 20, 23]),
  21.              ('2, 4, num, ;5, a-b', [2, 4])
  22.              ]
  23.    
  24.     for func in funcs:
  25.         print(f'\n\nTesting function: {func.__name__}\n')
  26.         print('-----------')
  27.         for test, assertion in tests:
  28.             print(f'Testing: {test}')
  29.             try:
  30.                 result = func(test)
  31.             except ValueError as errmsg:
  32.                 print(errmsg)
  33.             except Exception as e:
  34.                 print(f'ERROR: {traceback.format_exc()}')
  35.             else:
  36.                 try:
  37.                     assert sorted(set(result)) == assertion
  38.                 except AssertionError:
  39.                     print(
  40.                         f'*** ERROR -> for {test}\n\texpected {assertion},\n\treceived {result}')
  41.                 else:
  42.                     print(result, end='')
  43.                     if result != sorted(set(result)):
  44.                         print(' ** result not ordered/unique')
  45.                     else:
  46.                         print()
  47.             print('-----------')
  48.  
  49.  
  50. if __name__ == "__main__":
  51.     test_funcs(replace_with_name_of_functions_to_test_use_comma_between_each_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement