Something went wrong. Try again.
Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470#!/usr/bin/env python3
"""Unit tests for struct/union member extractor class."""
import osimport reimport unittestimport sys
from unittest.mock import MagicMock
SRC_DIR = os.path.dirname(os.path.realpath(__file__))sys.path.insert(0, os.path.join(SRC_DIR, "../lib/python"))
from kdoc.c_lex import CToken, CTokenizerfrom unittest_helper import run_unittest
## List of tests.## The code will dynamically generate one test for each key on this dictionary.#def tokens_to_list(tokens): tuples = []
for tok in tokens: if tok.kind == CToken.SPACE: continue
tuples += [(tok.kind, tok.value, tok.level)]
return tuples
def make_tokenizer_test(name, data): """ Create a test named ``name`` using parameters given by ``data`` dict. """
def test(self): """In-lined lambda-like function to run the test"""
# # Check if logger is working # if "log_msg" in data: with self.assertLogs() as cm: tokenizer = CTokenizer(data["source"])
msg_found = False for result in cm.output: if data["log_msg"] in result: msg_found = True
self.assertTrue(msg_found, f"Missing log {data['log_msg']}")
return
# # Check if tokenizer is producing expected results # tokens = CTokenizer(data["source"]).tokens
result = tokens_to_list(tokens) expected = tokens_to_list(data["expected"])
self.assertEqual(result, expected, msg=f"{name}")
return test
#: Tokenizer tests.TESTS_TOKENIZER = { "__run__": make_tokenizer_test,
"basic_tokens": { "source": """ int a; // comment float b = 1.23; """, "expected": [ CToken(CToken.NAME, "int"), CToken(CToken.NAME, "a"), CToken(CToken.ENDSTMT, ";"), CToken(CToken.COMMENT, "// comment"), CToken(CToken.NAME, "float"), CToken(CToken.NAME, "b"), CToken(CToken.OP, "="), CToken(CToken.NUMBER, "1.23"), CToken(CToken.ENDSTMT, ";"), ], },
"depth_counters": { "source": """ struct X { int arr[10]; func(a[0], (b + c)); } """, "expected": [ CToken(CToken.STRUCT, "struct"), CToken(CToken.NAME, "X"), CToken(CToken.BEGIN, "{", brace_level=1),
CToken(CToken.NAME, "int", brace_level=1), CToken(CToken.NAME, "arr", brace_level=1), CToken(CToken.BEGIN, "[", brace_level=1, bracket_level=1), CToken(CToken.NUMBER, "10", brace_level=1, bracket_level=1), CToken(CToken.END, "]", brace_level=1), CToken(CToken.ENDSTMT, ";", brace_level=1), CToken(CToken.NAME, "func", brace_level=1), CToken(CToken.BEGIN, "(", brace_level=1, paren_level=1), CToken(CToken.NAME, "a", brace_level=1, paren_level=1), CToken(CToken.BEGIN, "[", brace_level=1, paren_level=1, bracket_level=1), CToken(CToken.NUMBER, "0", brace_level=1, paren_level=1, bracket_level=1), CToken(CToken.END, "]", brace_level=1, paren_level=1), CToken(CToken.PUNC, ",", brace_level=1, paren_level=1), CToken(CToken.BEGIN, "(", brace_level=1, paren_level=2), CToken(CToken.NAME, "b", brace_level=1, paren_level=2), CToken(CToken.OP, "+", brace_level=1, paren_level=2), CToken(CToken.NAME, "c", brace_level=1, paren_level=2), CToken(CToken.END, ")", brace_level=1, paren_level=1), CToken(CToken.END, ")", brace_level=1), CToken(CToken.ENDSTMT, ";", brace_level=1), CToken(CToken.END, "}"), ], },
"mismatch_error": { "source": "int a$ = 5;", # $ is illegal "log_msg": "Unexpected token", },}
def make_private_test(name, data): """ Create a test named ``name`` using parameters given by ``data`` dict. """
def test(self): """In-lined lambda-like function to run the test""" tokens = CTokenizer(data["source"]) result = str(tokens)
# # Avoid whitespace false positives # result = re.sub(r"\s++", " ", result).strip() expected = re.sub(r"\s++", " ", data["trimmed"]).strip()
msg = f"failed when parsing this source:\n{data['source']}" self.assertEqual(result, expected, msg=msg)
return test
#: Tests to check if CTokenizer is handling properly public/private comments.TESTS_PRIVATE = { # # Simplest case: no private. Ensure that trimming won't affect struct # "__run__": make_private_test, "no private": { "source": """ struct foo { int a; int b; int c; }; """, "trimmed": """ struct foo { int a; int b; int c; }; """, },
# # Play "by the books" by always having a public in place #
"balanced_private": { "source": """ struct foo { int a; /* private: */ int b; /* public: */ int c; }; """, "trimmed": """ struct foo { int a; int c; }; """, },
"balanced_non_greddy_private": { "source": """ struct foo { int a; /* private: */ int b; /* public: */ int c; /* private: */ int d; /* public: */ int e;
}; """, "trimmed": """ struct foo { int a; int c; int e; }; """, },
"balanced_inner_private": { "source": """ struct foo { struct { int a; /* private: ignore below */ int b; /* public: but this should not be ignored */ }; int b; }; """, "trimmed": """ struct foo { struct { int a; }; int b; }; """, },
# # Test what happens if there's no public after private place #
"unbalanced_private": { "source": """ struct foo { int a; /* private: */ int b; int c; }; """, "trimmed": """ struct foo { int a; }; """, },
"unbalanced_inner_private": { "source": """ struct foo { struct { int a; /* private: ignore below */ int b; /* but this should not be ignored */ }; int b; }; """, "trimmed": """ struct foo { struct { int a; }; int b; }; """, },
"unbalanced_struct_group_tagged_with_private": { "source": """ struct page_pool_params { struct_group_tagged(page_pool_params_fast, fast, unsigned int order; unsigned int pool_size; int nid; struct device *dev; struct napi_struct *napi; enum dma_data_direction dma_dir; unsigned int max_len; unsigned int offset; }; struct_group_tagged(page_pool_params_slow, slow, struct net_device *netdev; unsigned int queue_idx; unsigned int flags; /* private: used by test code only */ void (*init_callback)(netmem_ref netmem, void *arg); void *init_arg; }; }; """, "trimmed": """ struct page_pool_params { struct_group_tagged(page_pool_params_fast, fast, unsigned int order; unsigned int pool_size; int nid; struct device *dev; struct napi_struct *napi; enum dma_data_direction dma_dir; unsigned int max_len; unsigned int offset; }; struct_group_tagged(page_pool_params_slow, slow, struct net_device *netdev; unsigned int queue_idx; unsigned int flags; }; }; """, },
"unbalanced_two_struct_group_tagged_first_with_private": { "source": """ struct page_pool_params { struct_group_tagged(page_pool_params_slow, slow, struct net_device *netdev; unsigned int queue_idx; unsigned int flags; /* private: used by test code only */ void (*init_callback)(netmem_ref netmem, void *arg); void *init_arg; }; struct_group_tagged(page_pool_params_fast, fast, unsigned int order; unsigned int pool_size; int nid; struct device *dev; struct napi_struct *napi; enum dma_data_direction dma_dir; unsigned int max_len; unsigned int offset; }; }; """, "trimmed": """ struct page_pool_params { struct_group_tagged(page_pool_params_slow, slow, struct net_device *netdev; unsigned int queue_idx; unsigned int flags; }; struct_group_tagged(page_pool_params_fast, fast, unsigned int order; unsigned int pool_size; int nid; struct device *dev; struct napi_struct *napi; enum dma_data_direction dma_dir; unsigned int max_len; unsigned int offset; }; }; """, }, "unbalanced_without_end_of_line": { "source": """ \ struct page_pool_params { \ struct_group_tagged(page_pool_params_slow, slow, \ struct net_device *netdev; \ unsigned int queue_idx; \ unsigned int flags; /* private: used by test code only */ void (*init_callback)(netmem_ref netmem, void *arg); \ void *init_arg; \ }; \ struct_group_tagged(page_pool_params_fast, fast, \ unsigned int order; \ unsigned int pool_size; \ int nid; \ struct device *dev; \ struct napi_struct *napi; \ enum dma_data_direction dma_dir; \ unsigned int max_len; \ unsigned int offset; \ }; \ }; """, "trimmed": """ struct page_pool_params { struct_group_tagged(page_pool_params_slow, slow, struct net_device *netdev; unsigned int queue_idx; unsigned int flags; }; struct_group_tagged(page_pool_params_fast, fast, unsigned int order; unsigned int pool_size; int nid; struct device *dev; struct napi_struct *napi; enum dma_data_direction dma_dir; unsigned int max_len; unsigned int offset; }; }; """, },}
#: Dict containing all test groups fror CTokenizerTESTS = { "TestPublicPrivate": TESTS_PRIVATE, "TestTokenizer": TESTS_TOKENIZER,}
def setUp(self): self.maxDiff = None
def build_test_class(group_name, table): """ Dynamically creates a class instance using type() as a generator for a new class derivated from unittest.TestCase.
We're opting to do it inside a function to avoid the risk of changing the globals() dictionary. """
class_dict = { "setUp": setUp }
run = table["__run__"]
for test_name, data in table.items(): if test_name == "__run__": continue
class_dict[f"test_{test_name}"] = run(test_name, data)
cls = type(group_name, (unittest.TestCase,), class_dict)
return cls.__name__, cls
## Create classes and add them to the global dictionary#for group, table in TESTS.items(): t = build_test_class(group, table) globals()[t[0]] = t[1]
## main#if __name__ == "__main__": run_unittest(__file__)