tdc_helper.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """
  2. # SPDX-License-Identifier: GPL-2.0
  3. tdc_helper.py - tdc helper functions
  4. Copyright (C) 2017 Lucas Bates <lucasb@mojatatu.com>
  5. """
  6. def get_categorized_testlist(alltests, ucat):
  7. """ Sort the master test list into categories. """
  8. testcases = dict()
  9. for category in ucat:
  10. testcases[category] = list(filter(lambda x: category in x['category'], alltests))
  11. return(testcases)
  12. def get_unique_item(lst):
  13. """ For a list, return a list of the unique items in the list. """
  14. if len(lst) > 1:
  15. return list(set(lst))
  16. else:
  17. return lst
  18. def get_test_categories(alltests):
  19. """ Discover all unique test categories present in the test case file. """
  20. ucat = []
  21. for t in alltests:
  22. ucat.extend(get_unique_item(t['category']))
  23. ucat = get_unique_item(ucat)
  24. return ucat
  25. def list_test_cases(testlist):
  26. """ Print IDs and names of all test cases. """
  27. for curcase in testlist:
  28. print(curcase['id'] + ': (' + ', '.join(curcase['category']) + ") " + curcase['name'])
  29. def list_categories(testlist):
  30. """Show all unique categories present in the test cases."""
  31. categories = set()
  32. for t in testlist:
  33. if 'category' in t:
  34. categories.update(t['category'])
  35. print("Available categories:")
  36. print(", ".join(sorted(categories)))
  37. print("")
  38. def print_list(cmdlist):
  39. """ Print a list of strings prepended with a tab. """
  40. for l in cmdlist:
  41. if (type(l) == list):
  42. print("\t" + str(l[0]))
  43. else:
  44. print("\t" + str(l))
  45. def print_sll(items):
  46. print("\n".join(str(s) for s in items))
  47. def print_test_case(tcase):
  48. """ Pretty-printing of a given test case. """
  49. print('\n==============\nTest {}\t{}\n'.format(tcase['id'], tcase['name']))
  50. for k in tcase.keys():
  51. if (isinstance(tcase[k], list)):
  52. print(k + ":")
  53. print_list(tcase[k])
  54. else:
  55. if not ((k == 'id') or (k == 'name')):
  56. print(k + ": " + str(tcase[k]))