aboutsummaryrefslogtreecommitdiff
path: root/bestflags/mock_task.py
blob: 059536cd338d7b99eecfac4b70b2d7bf677ee24e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""This module defines the common mock task used by varies unit tests.

Part of the Chrome build flags optimization.
"""

__author__ = 'yuhenglong@google.com (Yuheng Long)'

# Pick an integer at random.
POISONPILL = 975


class MockTask(object):
  """This class emulates an actual task.

  It does not do the actual work, but simply returns the result as given when
  this task is constructed.
  """

  def __init__(self, stage, identifier, cost=0):
    """Set up the results for this task.

    Args:
      stage: the stage of this test is in.
      identifier: the identifier of this task.
      cost: the mock cost of this task.

      The _pre_cost field stored the cost. Once this task is performed, i.e., by
      calling the work method, the _cost field will have this cost. The stage
      field verifies that the module being tested and the unitest are in the
      same stage. If the unitest does not care about cost of this task, the cost
      parameter should be leaved blank.
    """

    self._identifier = identifier
    self._pre_cost = cost
    self._stage = stage

  def __eq__(self, other):
    if isinstance(other, MockTask):
      return (self._identifier == other.GetIdentifier(self._stage) and
              self._cost == other.GetResult(self._stage))
    return False

  def GetIdentifier(self, stage):
    assert stage == self._stage
    return self._identifier

  def SetResult(self, stage, cost):
    assert stage == self._stage
    self._cost = cost

  def Work(self, stage):
    assert stage == self._stage
    self._cost = self._pre_cost

  def GetResult(self, stage):
    assert stage == self._stage
    return self._cost

  def Done(self, stage):
    """Indicates whether the task has been performed."""

    assert stage == self._stage
    return '_cost' in self.__dict__