1# Copyright (c) 2012 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Annotations for python-driven tests.""" 6 7import os 8 9 10class AnnotatedFunctions(object): 11 """A container for annotated methods.""" 12 _ANNOTATED = {} 13 14 @staticmethod 15 def _AddFunction(annotation, function): 16 """Adds an annotated to function to our container. 17 18 Args: 19 annotation: the annotation string. 20 function: the function. 21 Returns: 22 The function passed in. 23 """ 24 module_name = os.path.splitext(os.path.basename( 25 function.__globals__['__file__']))[0] 26 qualified_function_name = '.'.join([module_name, function.func_name]) 27 function_list = AnnotatedFunctions._ANNOTATED.get(annotation, []) 28 function_list.append(qualified_function_name) 29 AnnotatedFunctions._ANNOTATED[annotation] = function_list 30 return function 31 32 @staticmethod 33 def IsAnnotated(annotation, qualified_function_name): 34 """True if function name (module.function) contains the annotation. 35 36 Args: 37 annotation: the annotation string. 38 qualified_function_name: the qualified function name. 39 Returns: 40 True if module.function contains the annotation. 41 """ 42 return qualified_function_name in AnnotatedFunctions._ANNOTATED.get( 43 annotation, []) 44 45 @staticmethod 46 def GetTestAnnotations(qualified_function_name): 47 """Returns a list containing all annotations for the given function. 48 49 Args: 50 qualified_function_name: the qualified function name. 51 Returns: 52 List of all annotations for this function. 53 """ 54 return [annotation 55 for annotation, tests in AnnotatedFunctions._ANNOTATED.iteritems() 56 if qualified_function_name in tests] 57 58 59# The following functions are annotations used for the python driven tests. 60def Smoke(function): 61 return AnnotatedFunctions._AddFunction('Smoke', function) 62 63 64def SmallTest(function): 65 return AnnotatedFunctions._AddFunction('SmallTest', function) 66 67 68def MediumTest(function): 69 return AnnotatedFunctions._AddFunction('MediumTest', function) 70 71 72def LargeTest(function): 73 return AnnotatedFunctions._AddFunction('LargeTest', function) 74 75 76def FlakyTest(function): 77 return AnnotatedFunctions._AddFunction('FlakyTest', function) 78 79 80def DisabledTest(function): 81 return AnnotatedFunctions._AddFunction('DisabledTest', function) 82 83 84def Feature(feature_list): 85 def _AddFeatures(function): 86 for feature in feature_list: 87 AnnotatedFunctions._AddFunction('Feature' + feature, function) 88 return AnnotatedFunctions._AddFunction('Feature', function) 89 return _AddFeatures 90