tdc_helper.py 2.0 KB

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