xref: /linux/tools/testing/selftests/tc-testing/tdc_helper.py (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
176b903eeSLucas Bates"""
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
376b903eeSLucas Batestdc_helper.py - tdc helper functions
476b903eeSLucas Bates
576b903eeSLucas BatesCopyright (C) 2017 Lucas Bates <lucasb@mojatatu.com>
676b903eeSLucas Bates"""
776b903eeSLucas Bates
876b903eeSLucas Batesdef get_categorized_testlist(alltests, ucat):
976b903eeSLucas Bates    """ Sort the master test list into categories. """
1076b903eeSLucas Bates    testcases = dict()
1176b903eeSLucas Bates
1276b903eeSLucas Bates    for category in ucat:
1376b903eeSLucas Bates        testcases[category] = list(filter(lambda x: category in x['category'], alltests))
1476b903eeSLucas Bates
1576b903eeSLucas Bates    return(testcases)
1676b903eeSLucas Bates
1776b903eeSLucas Bates
1876b903eeSLucas Batesdef get_unique_item(lst):
197ae677fbSBrenda J. Butler    """ For a list, return a list of the unique items in the list. """
20*489ce2f4SLucas Bates    if len(lst) > 1:
2176b903eeSLucas Bates        return list(set(lst))
22*489ce2f4SLucas Bates    else:
23*489ce2f4SLucas Bates        return lst
2476b903eeSLucas Bates
2576b903eeSLucas Bates
2676b903eeSLucas Batesdef get_test_categories(alltests):
2776b903eeSLucas Bates    """ Discover all unique test categories present in the test case file. """
2876b903eeSLucas Bates    ucat = []
2976b903eeSLucas Bates    for t in alltests:
3076b903eeSLucas Bates        ucat.extend(get_unique_item(t['category']))
3176b903eeSLucas Bates        ucat = get_unique_item(ucat)
3276b903eeSLucas Bates    return ucat
3376b903eeSLucas Bates
3476b903eeSLucas Batesdef list_test_cases(testlist):
3576b903eeSLucas Bates    """ Print IDs and names of all test cases. """
3676b903eeSLucas Bates    for curcase in testlist:
3776b903eeSLucas Bates        print(curcase['id'] + ': (' + ', '.join(curcase['category']) + ") " + curcase['name'])
3876b903eeSLucas Bates
3976b903eeSLucas Bates
4076b903eeSLucas Batesdef list_categories(testlist):
4176b903eeSLucas Bates    """ Show all categories that are present in a test case file. """
4276b903eeSLucas Bates    categories = set(map(lambda x: x['category'], testlist))
4376b903eeSLucas Bates    print("Available categories:")
4476b903eeSLucas Bates    print(", ".join(str(s) for s in categories))
4576b903eeSLucas Bates    print("")
4676b903eeSLucas Bates
4776b903eeSLucas Bates
4876b903eeSLucas Batesdef print_list(cmdlist):
4976b903eeSLucas Bates    """ Print a list of strings prepended with a tab. """
5076b903eeSLucas Bates    for l in cmdlist:
5176b903eeSLucas Bates        if (type(l) == list):
5276b903eeSLucas Bates            print("\t" + str(l[0]))
5376b903eeSLucas Bates        else:
5476b903eeSLucas Bates            print("\t" + str(l))
5576b903eeSLucas Bates
5676b903eeSLucas Bates
5776b903eeSLucas Batesdef print_sll(items):
5876b903eeSLucas Bates    print("\n".join(str(s) for s in items))
5976b903eeSLucas Bates
6076b903eeSLucas Bates
6176b903eeSLucas Batesdef print_test_case(tcase):
6276b903eeSLucas Bates    """ Pretty-printing of a given test case. """
63f87c7f64SBrenda J. Butler    print('\n==============\nTest {}\t{}\n'.format(tcase['id'], tcase['name']))
6476b903eeSLucas Bates    for k in tcase.keys():
656c26c3fbSBrenda J. Butler        if (isinstance(tcase[k], list)):
6676b903eeSLucas Bates            print(k + ":")
6776b903eeSLucas Bates            print_list(tcase[k])
6876b903eeSLucas Bates        else:
69f87c7f64SBrenda J. Butler            if not ((k == 'id') or (k == 'name')):
70f87c7f64SBrenda J. Butler                print(k + ": " + str(tcase[k]))
71