From 7b9c1053bb75eab197acfcec53e9bb6ea7568a0e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Sun, 21 Apr 2019 08:48:16 -0400 Subject: This method should be a function in context.py --- coverage/context.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'coverage/context.py') diff --git a/coverage/context.py b/coverage/context.py index 25be98c5..22c0e531 100644 --- a/coverage/context.py +++ b/coverage/context.py @@ -3,6 +3,37 @@ """Determine contexts for coverage.py""" + +def combine_context_switchers(context_switchers): + """Create a single context switcher from multiple switchers. + + `context_switchers` is a list of functions that take a frame as an + argument and return a string to use as the new context label. + + Returns a function that composites `context_switchers` functions, or None + if `context_switchers` is an empty list. + + When invoked, the combined switcher calls `context_switchers` one-by-one + until a string is returned. The combined switcher returns None if all + `context_switchers` return None. + """ + if not context_switchers: + return None + + if len(context_switchers) == 1: + return context_switchers[0] + + def should_start_context(frame): + """The combiner for multiple context switchers.""" + for switcher in context_switchers: + new_context = switcher(frame) + if new_context is not None: + return new_context + return None + + return should_start_context + + def should_start_context_test_function(frame): """Is this frame calling a test_* function?""" if frame.f_code.co_name.startswith("test"): -- cgit v1.2.1