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