mock_task.py revision 25cdf79e17a73858ffa28db8a5b387210fea9e25
1# Copyright (c) 2013 The Chromium OS 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"""This module defines the common mock tasks used by various unit tests. 6 7Part of the Chrome build flags optimization. 8""" 9 10__author__ = 'yuhenglong@google.com (Yuheng Long)' 11 12# Pick an integer at random. 13POISONPILL = 975 14 15 16class MockTask(object): 17 """This class emulates an actual task. 18 19 It does not do the actual work, but simply returns the result as given when 20 this task is constructed. 21 """ 22 23 def __init__(self, stage, identifier, cost=0): 24 """Set up the results for this task. 25 26 Args: 27 stage: the stage of this test is in. 28 identifier: the identifier of this task. 29 cost: the mock cost of this task. 30 31 The _cost field stored the cost. Once this task is performed, i.e., by 32 calling the work method or by setting the result from other task, the 33 _cost field will have this cost. The stage field verifies that the module 34 being tested and the unitest are in the same stage. If the unitest does 35 not care about cost of this task, the cost parameter should be leaved 36 blank. 37 """ 38 39 self._identifier = identifier 40 self._cost = cost 41 self._stage = stage 42 43 # Indicate that this method has not been performed yet. 44 self._performed = False 45 46 def __eq__(self, other): 47 if isinstance(other, MockTask): 48 return (self._identifier == other.GetIdentifier(self._stage) and 49 self._cost == other.GetResult(self._stage)) 50 return False 51 52 def GetIdentifier(self, stage): 53 assert stage == self._stage 54 return self._identifier 55 56 def SetResult(self, stage, cost): 57 assert stage == self._stage 58 self._cost = cost 59 self._performed = True 60 61 def Work(self, stage): 62 assert stage == self._stage 63 self._performed = True 64 65 def GetResult(self, stage): 66 assert stage == self._stage 67 return self._cost 68 69 def Done(self, stage): 70 """Indicates whether the task has been performed.""" 71 72 assert stage == self._stage 73 return self._performed 74 75 def LogSteeringCost(self): 76 pass 77 78 79class IdentifierMockTask(MockTask): 80 """This class defines the mock task that does not consider the cost. 81 82 The task instances will be inserted into a set. Therefore the hash and the 83 equal methods are overridden. The unittests that compares identities of the 84 tasks for equality can use this mock task instead of the base mock tack. 85 """ 86 87 def __hash__(self): 88 return self._identifier 89 90 def __eq__(self, other): 91 if isinstance(other, MockTask): 92 return self._identifier == other.GetIdentifier(self._stage) 93 return False 94