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