michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Annotations for python-driven tests.""" michael@0: michael@0: import os michael@0: michael@0: michael@0: class AnnotatedFunctions(object): michael@0: """A container for annotated methods.""" michael@0: _ANNOTATED = {} michael@0: michael@0: @staticmethod michael@0: def _AddFunction(annotation, function): michael@0: """Adds an annotated to function to our container. michael@0: michael@0: Args: michael@0: annotation: the annotation string. michael@0: function: the function. michael@0: Returns: michael@0: The function passed in. michael@0: """ michael@0: module_name = os.path.splitext(os.path.basename( michael@0: function.__globals__['__file__']))[0] michael@0: qualified_function_name = '.'.join([module_name, function.func_name]) michael@0: function_list = AnnotatedFunctions._ANNOTATED.get(annotation, []) michael@0: function_list.append(qualified_function_name) michael@0: AnnotatedFunctions._ANNOTATED[annotation] = function_list michael@0: return function michael@0: michael@0: @staticmethod michael@0: def IsAnnotated(annotation, qualified_function_name): michael@0: """True if function name (module.function) contains the annotation. michael@0: michael@0: Args: michael@0: annotation: the annotation string. michael@0: qualified_function_name: the qualified function name. michael@0: Returns: michael@0: True if module.function contains the annotation. michael@0: """ michael@0: return qualified_function_name in AnnotatedFunctions._ANNOTATED.get( michael@0: annotation, []) michael@0: michael@0: @staticmethod michael@0: def GetTestAnnotations(qualified_function_name): michael@0: """Returns a list containing all annotations for the given function. michael@0: michael@0: Args: michael@0: qualified_function_name: the qualified function name. michael@0: Returns: michael@0: List of all annotations for this function. michael@0: """ michael@0: return [annotation michael@0: for annotation, tests in AnnotatedFunctions._ANNOTATED.iteritems() michael@0: if qualified_function_name in tests] michael@0: michael@0: michael@0: # The following functions are annotations used for the python driven tests. michael@0: def Smoke(function): michael@0: return AnnotatedFunctions._AddFunction('Smoke', function) michael@0: michael@0: michael@0: def SmallTest(function): michael@0: return AnnotatedFunctions._AddFunction('SmallTest', function) michael@0: michael@0: michael@0: def MediumTest(function): michael@0: return AnnotatedFunctions._AddFunction('MediumTest', function) michael@0: michael@0: michael@0: def LargeTest(function): michael@0: return AnnotatedFunctions._AddFunction('LargeTest', function) michael@0: michael@0: michael@0: def FlakyTest(function): michael@0: return AnnotatedFunctions._AddFunction('FlakyTest', function) michael@0: michael@0: michael@0: def DisabledTest(function): michael@0: return AnnotatedFunctions._AddFunction('DisabledTest', function) michael@0: michael@0: michael@0: def Feature(feature_list): michael@0: def _AddFeatures(function): michael@0: for feature in feature_list: michael@0: AnnotatedFunctions._AddFunction('Feature' + feature, function) michael@0: return AnnotatedFunctions._AddFunction('Feature', function) michael@0: return _AddFeatures