tdc_helper.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. return list(set(lst))
  15. def get_test_categories(alltests):
  16. """ Discover all unique test categories present in the test case file. """
  17. ucat = []
  18. for t in alltests:
  19. ucat.extend(get_unique_item(t['category']))
  20. ucat = get_unique_item(ucat)
  21. return ucat
  22. def list_test_cases(testlist):
  23. """ Print IDs and names of all test cases. """
  24. for curcase in testlist:
  25. print(curcase['id'] + ': (' + ', '.join(curcase['category']) + ") " + curcase['name'])
  26. def list_categories(testlist):
  27. """ Show all categories that are present in a test case file. """
  28. categories = set(map(lambda x: x['category'], testlist))
  29. print("Available categories:")
  30. print(", ".join(str(s) for s in categories))
  31. print("")
  32. def print_list(cmdlist):
  33. """ Print a list of strings prepended with a tab. """
  34. for l in cmdlist:
  35. if (type(l) == list):
  36. print("\t" + str(l[0]))
  37. else:
  38. print("\t" + str(l))
  39. def print_sll(items):
  40. print("\n".join(str(s) for s in items))
  41. def print_test_case(tcase):
  42. """ Pretty-printing of a given test case. """
  43. print('\n==============\nTest {}\t{}\n'.format(tcase['id'], tcase['name']))
  44. for k in tcase.keys():
  45. if (isinstance(tcase[k], list)):
  46. print(k + ":")
  47. print_list(tcase[k])
  48. else:
  49. if not ((k == 'id') or (k == 'name')):
  50. print(k + ": " + str(tcase[k]))