mock_task.py revision a791546e80cede30d5325bec834b35b99b7e7bfe
1"""This module defines the common mock task used by varies unit tests.
2
3Part of the Chrome build flags optimization.
4"""
5
6__author__ = 'yuhenglong@google.com (Yuheng Long)'
7
8# Pick an integer at random.
9POISONPILL = 975
10
11
12class MockTask(object):
13  """This class emulates an actual task.
14
15  It does not do the actual work, but simply returns the result as given when
16  this task is constructed.
17  """
18
19  def __init__(self, stage, identifier, cost=0):
20    """Set up the results for this task.
21
22    Args:
23      stage: the stage of this test is in.
24      identifier: the identifier of this task.
25      cost: the mock cost of this task.
26
27      The _pre_cost field stored the cost. Once this task is performed, i.e., by
28      calling the work method, the _cost field will have this cost. The stage
29      field verifies that the module being tested and the unitest are in the
30      same stage. If the unitest does not care about cost of this task, the cost
31      parameter should be leaved blank.
32    """
33
34    self._identifier = identifier
35    self._pre_cost = cost
36    self._stage = stage
37
38  def __eq__(self, other):
39    if isinstance(other, MockTask):
40      return (self._identifier == other.GetIdentifier(self._stage) and
41              self._cost == other.GetResult(self._stage))
42    return False
43
44  def GetIdentifier(self, stage):
45    assert stage == self._stage
46    return self._identifier
47
48  def SetResult(self, stage, cost):
49    assert stage == self._stage
50    self._cost = cost
51
52  def Work(self, stage):
53    assert stage == self._stage
54    self._cost = self._pre_cost
55
56  def GetResult(self, stage):
57    assert stage == self._stage
58    return self._cost
59
60  def Done(self, stage):
61    """Indicates whether the task has been performed."""
62
63    assert stage == self._stage
64    return '_cost' in self.__dict__
65