mock_task.py revision 16d7a5204e347855b1c3a68c982c22f931a12866
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 task used by varies 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 _pre_cost field stored the cost. Once this task is performed, i.e., by
32      calling the work method, the _cost field will have this cost. The stage
33      field verifies that the module being tested and the unitest are in the
34      same stage. If the unitest does not care about cost of this task, the cost
35      parameter should be leaved blank.
36    """
37
38    self._identifier = identifier
39    self._pre_cost = cost
40    self._stage = stage
41
42  def __eq__(self, other):
43    if isinstance(other, MockTask):
44      return (self._identifier == other.GetIdentifier(self._stage) and
45              self._cost == other.GetResult(self._stage))
46    return False
47
48  def GetIdentifier(self, stage):
49    assert stage == self._stage
50    return self._identifier
51
52  def SetResult(self, stage, cost):
53    assert stage == self._stage
54    self._cost = cost
55
56  def Work(self, stage):
57    assert stage == self._stage
58    self._cost = self._pre_cost
59
60  def GetResult(self, stage):
61    assert stage == self._stage
62    return self._cost
63
64  def Done(self, stage):
65    """Indicates whether the task has been performed."""
66
67    assert stage == self._stage
68    return '_cost' in self.__dict__
69