summaryrefslogtreecommitdiff
path: root/coverage/context.py
diff options
context:
space:
mode:
Diffstat (limited to 'coverage/context.py')
-rw-r--r--coverage/context.py31
1 files changed, 31 insertions, 0 deletions
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"):