tests/pymox/mox_test.py
author Mario Ferraro <fadinlight@gmail.com>
Thu, 09 Jul 2009 10:16:29 +0200
changeset 2575 e319fd52cbc4
parent 1000 9af147fc1f1c
permissions -rwxr-xr-x
Extended tooltip timeout to 10 seconds for all fields. This is because the tooltip currently timesout to fast for people to read the whole stories they can contain. Especially because Surveys can have custom tooltips. Reviewed by: Lennard de Rijk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1000
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.4
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     2
#
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     3
# Unit tests for Mox.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     4
#
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     5
# Copyright 2008 Google Inc.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     6
#
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     7
# Licensed under the Apache License, Version 2.0 (the "License");
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     8
# you may not use this file except in compliance with the License.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
     9
# You may obtain a copy of the License at
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    10
#
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    11
#      http://www.apache.org/licenses/LICENSE-2.0
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    12
#
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    13
# Unless required by applicable law or agreed to in writing, software
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    14
# distributed under the License is distributed on an "AS IS" BASIS,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    15
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    16
# See the License for the specific language governing permissions and
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    17
# limitations under the License.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    18
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    19
import cStringIO
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    20
import unittest
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    21
import re
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    22
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    23
import mox
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    24
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    25
import mox_test_helper
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    26
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    27
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    28
class ExpectedMethodCallsErrorTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    29
  """Test creation and string conversion of ExpectedMethodCallsError."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    30
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    31
  def testAtLeastOneMethod(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    32
    self.assertRaises(ValueError, mox.ExpectedMethodCallsError, [])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    33
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    34
  def testOneError(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    35
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    36
    method(1, 2).AndReturn('output')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    37
    e = mox.ExpectedMethodCallsError([method])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    38
    self.assertEqual(
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    39
        "Verify: Expected methods never called:\n"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    40
        "  0.  testMethod(1, 2) -> 'output'",
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    41
        str(e))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    42
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    43
  def testManyErrors(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    44
    method1 = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    45
    method1(1, 2).AndReturn('output')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    46
    method2 = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    47
    method2(a=1, b=2, c="only named")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    48
    method3 = mox.MockMethod("testMethod2", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    49
    method3().AndReturn(44)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    50
    method4 = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    51
    method4(1, 2).AndReturn('output')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    52
    e = mox.ExpectedMethodCallsError([method1, method2, method3, method4])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    53
    self.assertEqual(
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    54
        "Verify: Expected methods never called:\n"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    55
        "  0.  testMethod(1, 2) -> 'output'\n"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    56
        "  1.  testMethod(a=1, b=2, c='only named') -> None\n"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    57
        "  2.  testMethod2() -> 44\n"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    58
        "  3.  testMethod(1, 2) -> 'output'",
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    59
        str(e))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    60
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    61
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    62
class OrTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    63
  """Test Or correctly chains Comparators."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    64
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    65
  def testValidOr(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    66
    """Or should be True if either Comparator returns True."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    67
    self.assert_(mox.Or(mox.IsA(dict), mox.IsA(str)) == {})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    68
    self.assert_(mox.Or(mox.IsA(dict), mox.IsA(str)) == 'test')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    69
    self.assert_(mox.Or(mox.IsA(str), mox.IsA(str)) == 'test')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    70
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    71
  def testInvalidOr(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    72
    """Or should be False if both Comparators return False."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    73
    self.failIf(mox.Or(mox.IsA(dict), mox.IsA(str)) == 0)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    74
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    75
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    76
class AndTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    77
  """Test And correctly chains Comparators."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    78
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    79
  def testValidAnd(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    80
    """And should be True if both Comparators return True."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    81
    self.assert_(mox.And(mox.IsA(str), mox.IsA(str)) == '1')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    82
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    83
  def testClauseOneFails(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    84
    """And should be False if the first Comparator returns False."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    85
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    86
    self.failIf(mox.And(mox.IsA(dict), mox.IsA(str)) == '1')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    87
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    88
  def testAdvancedUsage(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    89
    """And should work with other Comparators.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    90
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    91
    Note: this test is reliant on In and ContainsKeyValue.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    92
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    93
    test_dict = {"mock" : "obj", "testing" : "isCOOL"}
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    94
    self.assert_(mox.And(mox.In("testing"),
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    95
                           mox.ContainsKeyValue("mock", "obj")) == test_dict)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    96
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    97
  def testAdvancedUsageFails(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    98
    """Note: this test is reliant on In and ContainsKeyValue."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
    99
    test_dict = {"mock" : "obj", "testing" : "isCOOL"}
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   100
    self.failIf(mox.And(mox.In("NOTFOUND"),
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   101
                          mox.ContainsKeyValue("mock", "obj")) == test_dict)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   102
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   103
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   104
class SameElementsAsTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   105
  """Test SameElementsAs correctly identifies sequences with same elements."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   106
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   107
  def testSortedLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   108
    """Should return True if two lists are exactly equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   109
    self.assert_(mox.SameElementsAs([1, 2.0, 'c']) == [1, 2.0, 'c'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   110
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   111
  def testUnsortedLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   112
    """Should return True if two lists are unequal but have same elements."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   113
    self.assert_(mox.SameElementsAs([1, 2.0, 'c']) == [2.0, 'c', 1])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   114
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   115
  def testUnhashableLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   116
    """Should return True if two lists have the same unhashable elements."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   117
    self.assert_(mox.SameElementsAs([{'a': 1}, {2: 'b'}]) ==
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   118
                 [{2: 'b'}, {'a': 1}])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   119
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   120
  def testEmptyLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   121
    """Should return True for two empty lists."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   122
    self.assert_(mox.SameElementsAs([]) == [])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   123
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   124
  def testUnequalLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   125
    """Should return False if the lists are not equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   126
    self.failIf(mox.SameElementsAs([1, 2.0, 'c']) == [2.0, 'c'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   127
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   128
  def testUnequalUnhashableLists(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   129
    """Should return False if two lists with unhashable elements are unequal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   130
    self.failIf(mox.SameElementsAs([{'a': 1}, {2: 'b'}]) == [{2: 'b'}])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   131
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   132
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   133
class ContainsKeyValueTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   134
  """Test ContainsKeyValue correctly identifies key/value pairs in a dict.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   135
  """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   136
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   137
  def testValidPair(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   138
    """Should return True if the key value is in the dict."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   139
    self.assert_(mox.ContainsKeyValue("key", 1) == {"key": 1})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   140
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   141
  def testInvalidValue(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   142
    """Should return False if the value is not correct."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   143
    self.failIf(mox.ContainsKeyValue("key", 1) == {"key": 2})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   144
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   145
  def testInvalidKey(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   146
    """Should return False if they key is not in the dict."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   147
    self.failIf(mox.ContainsKeyValue("qux", 1) == {"key": 2})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   148
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   149
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   150
class InTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   151
  """Test In correctly identifies a key in a list/dict"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   152
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   153
  def testItemInList(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   154
    """Should return True if the item is in the list."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   155
    self.assert_(mox.In(1) == [1, 2, 3])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   156
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   157
  def testKeyInDict(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   158
    """Should return True if the item is a key in a dict."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   159
    self.assert_(mox.In("test") == {"test" : "module"})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   160
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   161
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   162
class NotTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   163
  """Test Not correctly identifies False predicates."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   164
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   165
  def testItemInList(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   166
    """Should return True if the item is NOT in the list."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   167
    self.assert_(mox.Not(mox.In(42)) == [1, 2, 3])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   168
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   169
  def testKeyInDict(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   170
    """Should return True if the item is NOT a key in a dict."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   171
    self.assert_(mox.Not(mox.In("foo")) == {"key" : 42})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   172
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   173
  def testInvalidKeyWithNot(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   174
    """Should return False if they key is NOT in the dict."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   175
    self.assert_(mox.Not(mox.ContainsKeyValue("qux", 1)) == {"key": 2})
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   176
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   177
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   178
class StrContainsTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   179
  """Test StrContains correctly checks for substring occurrence of a parameter.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   180
  """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   181
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   182
  def testValidSubstringAtStart(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   183
    """Should return True if the substring is at the start of the string."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   184
    self.assert_(mox.StrContains("hello") == "hello world")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   185
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   186
  def testValidSubstringInMiddle(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   187
    """Should return True if the substring is in the middle of the string."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   188
    self.assert_(mox.StrContains("lo wo") == "hello world")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   189
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   190
  def testValidSubstringAtEnd(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   191
    """Should return True if the substring is at the end of the string."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   192
    self.assert_(mox.StrContains("ld") == "hello world")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   193
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   194
  def testInvaildSubstring(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   195
    """Should return False if the substring is not in the string."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   196
    self.failIf(mox.StrContains("AAA") == "hello world")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   197
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   198
  def testMultipleMatches(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   199
    """Should return True if there are multiple occurances of substring."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   200
    self.assert_(mox.StrContains("abc") == "ababcabcabcababc")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   201
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   202
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   203
class RegexTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   204
  """Test Regex correctly matches regular expressions."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   205
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   206
  def testIdentifyBadSyntaxDuringInit(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   207
    """The user should know immediately if a regex has bad syntax."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   208
    self.assertRaises(re.error, mox.Regex, '(a|b')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   209
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   210
  def testPatternInMiddle(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   211
    """Should return True if the pattern matches at the middle of the string.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   212
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   213
    This ensures that re.search is used (instead of re.find).
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   214
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   215
    self.assert_(mox.Regex(r"a\s+b") == "x y z a b c")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   216
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   217
  def testNonMatchPattern(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   218
    """Should return False if the pattern does not match the string."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   219
    self.failIf(mox.Regex(r"a\s+b") == "x y z")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   220
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   221
  def testFlagsPassedCorrectly(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   222
    """Should return True as we pass IGNORECASE flag."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   223
    self.assert_(mox.Regex(r"A", re.IGNORECASE) == "a")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   224
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   225
  def testReprWithoutFlags(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   226
    """repr should return the regular expression pattern."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   227
    self.assert_(repr(mox.Regex(r"a\s+b")) == "<regular expression 'a\s+b'>")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   228
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   229
  def testReprWithFlags(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   230
    """repr should return the regular expression pattern and flags."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   231
    self.assert_(repr(mox.Regex(r"a\s+b", flags=4)) ==
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   232
                 "<regular expression 'a\s+b', flags=4>")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   233
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   234
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   235
class IsATest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   236
  """Verify IsA correctly checks equality based upon class type, not value."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   237
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   238
  def testEqualityValid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   239
    """Verify that == correctly identifies objects of the same type."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   240
    self.assert_(mox.IsA(str) == 'test')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   241
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   242
  def testEqualityInvalid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   243
    """Verify that == correctly identifies objects of different types."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   244
    self.failIf(mox.IsA(str) == 10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   245
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   246
  def testInequalityValid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   247
    """Verify that != identifies objects of different type."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   248
    self.assert_(mox.IsA(str) != 10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   249
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   250
  def testInequalityInvalid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   251
    """Verify that != correctly identifies objects of the same type."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   252
    self.failIf(mox.IsA(str) != "test")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   253
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   254
  def testEqualityInListValid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   255
    """Verify list contents are properly compared."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   256
    isa_list = [mox.IsA(str), mox.IsA(str)]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   257
    str_list = ["abc", "def"]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   258
    self.assert_(isa_list == str_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   259
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   260
  def testEquailtyInListInvalid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   261
    """Verify list contents are properly compared."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   262
    isa_list = [mox.IsA(str),mox.IsA(str)]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   263
    mixed_list = ["abc", 123]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   264
    self.failIf(isa_list == mixed_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   265
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   266
  def testSpecialTypes(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   267
    """Verify that IsA can handle objects like cStringIO.StringIO."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   268
    isA = mox.IsA(cStringIO.StringIO())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   269
    stringIO = cStringIO.StringIO()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   270
    self.assert_(isA == stringIO)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   271
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   272
class IsAlmostTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   273
  """Verify IsAlmost correctly checks equality of floating point numbers."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   274
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   275
  def testEqualityValid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   276
    """Verify that == correctly identifies nearly equivalent floats."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   277
    self.assertEquals(mox.IsAlmost(1.8999999999), 1.9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   278
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   279
  def testEqualityInvalid(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   280
    """Verify that == correctly identifies non-equivalent floats."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   281
    self.assertNotEquals(mox.IsAlmost(1.899), 1.9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   282
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   283
  def testEqualityWithPlaces(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   284
    """Verify that specifying places has the desired effect."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   285
    self.assertNotEquals(mox.IsAlmost(1.899), 1.9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   286
    self.assertEquals(mox.IsAlmost(1.899, places=2), 1.9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   287
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   288
  def testNonNumericTypes(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   289
    """Verify that IsAlmost handles non-numeric types properly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   290
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   291
    self.assertNotEquals(mox.IsAlmost(1.8999999999), '1.9')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   292
    self.assertNotEquals(mox.IsAlmost('1.8999999999'), 1.9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   293
    self.assertNotEquals(mox.IsAlmost('1.8999999999'), '1.9')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   294
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   295
class MockMethodTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   296
  """Test class to verify that the MockMethod class is working correctly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   297
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   298
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   299
    self.expected_method = mox.MockMethod("testMethod", [], False)(['original'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   300
    self.mock_method = mox.MockMethod("testMethod", [self.expected_method],
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   301
                                        True)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   302
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   303
  def testAndReturnNoneByDefault(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   304
    """Should return None by default."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   305
    return_value = self.mock_method(['original'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   306
    self.assert_(return_value == None)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   307
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   308
  def testAndReturnValue(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   309
    """Should return a specificed return value."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   310
    expected_return_value = "test"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   311
    self.expected_method.AndReturn(expected_return_value)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   312
    return_value = self.mock_method(['original'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   313
    self.assert_(return_value == expected_return_value)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   314
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   315
  def testAndRaiseException(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   316
    """Should raise a specified exception."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   317
    expected_exception = Exception('test exception')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   318
    self.expected_method.AndRaise(expected_exception)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   319
    self.assertRaises(Exception, self.mock_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   320
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   321
  def testWithSideEffects(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   322
    """Should call state modifier."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   323
    local_list = ['original']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   324
    def modifier(mutable_list):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   325
      self.assertTrue(local_list is mutable_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   326
      mutable_list[0] = 'mutation'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   327
    self.expected_method.WithSideEffects(modifier).AndReturn(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   328
    self.mock_method(local_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   329
    self.assertEquals('mutation', local_list[0])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   330
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   331
  def testEqualityNoParamsEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   332
    """Methods with the same name and without params should be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   333
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   334
    self.assertEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   335
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   336
  def testEqualityNoParamsNotEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   337
    """Methods with different names and without params should not be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   338
    expected_method = mox.MockMethod("otherMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   339
    self.failIfEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   340
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   341
  def testEqualityParamsEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   342
    """Methods with the same name and parameters should be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   343
    params = [1, 2, 3]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   344
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   345
    expected_method._params = params
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   346
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   347
    self.mock_method._params = params
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   348
    self.assertEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   349
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   350
  def testEqualityParamsNotEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   351
    """Methods with the same name and different params should not be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   352
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   353
    expected_method._params = [1, 2, 3]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   354
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   355
    self.mock_method._params = ['a', 'b', 'c']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   356
    self.failIfEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   357
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   358
  def testEqualityNamedParamsEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   359
    """Methods with the same name and same named params should be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   360
    named_params = {"input1": "test", "input2": "params"}
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   361
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   362
    expected_method._named_params = named_params
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   363
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   364
    self.mock_method._named_params = named_params
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   365
    self.assertEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   366
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   367
  def testEqualityNamedParamsNotEqual(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   368
    """Methods with the same name and diffnamed params should not be equal."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   369
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   370
    expected_method._named_params = {"input1": "test", "input2": "params"}
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   371
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   372
    self.mock_method._named_params = {"input1": "test2", "input2": "params2"}
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   373
    self.failIfEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   374
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   375
  def testEqualityWrongType(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   376
    """Method should not be equal to an object of a different type."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   377
    self.failIfEqual(self.mock_method, "string?")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   378
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   379
  def testObjectEquality(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   380
    """Equality of objects should work without a Comparator"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   381
    instA = TestClass();
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   382
    instB = TestClass();
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   383
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   384
    params = [instA, ]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   385
    expected_method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   386
    expected_method._params = params
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   387
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   388
    self.mock_method._params = [instB, ]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   389
    self.assertEqual(self.mock_method, expected_method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   390
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   391
  def testStrConversion(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   392
    method = mox.MockMethod("f", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   393
    method(1, 2, "st", n1=8, n2="st2")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   394
    self.assertEqual(str(method), ("f(1, 2, 'st', n1=8, n2='st2') -> None"))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   395
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   396
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   397
    method(1, 2, "only positional")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   398
    self.assertEqual(str(method), "testMethod(1, 2, 'only positional') -> None")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   399
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   400
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   401
    method(a=1, b=2, c="only named")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   402
    self.assertEqual(str(method),
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   403
                     "testMethod(a=1, b=2, c='only named') -> None")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   404
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   405
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   406
    method()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   407
    self.assertEqual(str(method), "testMethod() -> None")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   408
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   409
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   410
    method(x="only 1 parameter")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   411
    self.assertEqual(str(method), "testMethod(x='only 1 parameter') -> None")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   412
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   413
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   414
    method().AndReturn('return_value')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   415
    self.assertEqual(str(method), "testMethod() -> 'return_value'")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   416
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   417
    method = mox.MockMethod("testMethod", [], False)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   418
    method().AndReturn(('a', {1: 2}))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   419
    self.assertEqual(str(method), "testMethod() -> ('a', {1: 2})")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   420
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   421
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   422
class MockAnythingTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   423
  """Verify that the MockAnything class works as expected."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   424
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   425
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   426
    self.mock_object = mox.MockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   427
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   428
  def testSetupMode(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   429
    """Verify the mock will accept any call."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   430
    self.mock_object.NonsenseCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   431
    self.assert_(len(self.mock_object._expected_calls_queue) == 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   432
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   433
  def testReplayWithExpectedCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   434
    """Verify the mock replays method calls as expected."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   435
    self.mock_object.ValidCall()          # setup method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   436
    self.mock_object._Replay()            # start replay mode
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   437
    self.mock_object.ValidCall()          # make method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   438
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   439
  def testReplayWithUnexpectedCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   440
    """Unexpected method calls should raise UnexpectedMethodCallError."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   441
    self.mock_object.ValidCall()          # setup method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   442
    self.mock_object._Replay()             # start replay mode
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   443
    self.assertRaises(mox.UnexpectedMethodCallError,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   444
                      self.mock_object.OtherValidCall)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   445
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   446
  def testVerifyWithCompleteReplay(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   447
    """Verify should not raise an exception for a valid replay."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   448
    self.mock_object.ValidCall()          # setup method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   449
    self.mock_object._Replay()             # start replay mode
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   450
    self.mock_object.ValidCall()          # make method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   451
    self.mock_object._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   452
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   453
  def testVerifyWithIncompleteReplay(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   454
    """Verify should raise an exception if the replay was not complete."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   455
    self.mock_object.ValidCall()          # setup method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   456
    self.mock_object._Replay()             # start replay mode
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   457
    # ValidCall() is never made
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   458
    self.assertRaises(mox.ExpectedMethodCallsError, self.mock_object._Verify)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   459
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   460
  def testSpecialClassMethod(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   461
    """Verify should not raise an exception when special methods are used."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   462
    self.mock_object[1].AndReturn(True)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   463
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   464
    returned_val = self.mock_object[1]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   465
    self.assert_(returned_val)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   466
    self.mock_object._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   467
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   468
  def testNonzero(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   469
    """You should be able to use the mock object in an if."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   470
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   471
    if self.mock_object:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   472
      pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   473
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   474
  def testNotNone(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   475
    """Mock should be comparable to None."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   476
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   477
    if self.mock_object is not None:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   478
      pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   479
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   480
    if self.mock_object is None:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   481
      pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   482
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   483
  def testEquals(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   484
    """A mock should be able to compare itself to another object."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   485
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   486
    self.assertEquals(self.mock_object, self.mock_object)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   487
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   488
  def testEqualsMockFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   489
    """Verify equals identifies unequal objects."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   490
    self.mock_object.SillyCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   491
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   492
    self.assertNotEquals(self.mock_object, mox.MockAnything())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   493
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   494
  def testEqualsInstanceFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   495
    """Verify equals identifies that objects are different instances."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   496
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   497
    self.assertNotEquals(self.mock_object, TestClass())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   498
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   499
  def testNotEquals(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   500
    """Verify not equals works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   501
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   502
    self.assertFalse(self.mock_object != self.mock_object)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   503
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   504
  def testNestedMockCallsRecordedSerially(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   505
    """Test that nested calls work when recorded serially."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   506
    self.mock_object.CallInner().AndReturn(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   507
    self.mock_object.CallOuter(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   508
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   509
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   510
    self.mock_object.CallOuter(self.mock_object.CallInner())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   511
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   512
    self.mock_object._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   513
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   514
  def testNestedMockCallsRecordedNested(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   515
    """Test that nested cals work when recorded in a nested fashion."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   516
    self.mock_object.CallOuter(self.mock_object.CallInner().AndReturn(1))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   517
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   518
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   519
    self.mock_object.CallOuter(self.mock_object.CallInner())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   520
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   521
    self.mock_object._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   522
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   523
  def testIsCallable(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   524
    """Test that MockAnything can even mock a simple callable.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   525
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   526
    This is handy for "stubbing out" a method in a module with a mock, and
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   527
    verifying that it was called.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   528
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   529
    self.mock_object().AndReturn('mox0rd')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   530
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   531
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   532
    self.assertEquals('mox0rd', self.mock_object())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   533
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   534
    self.mock_object._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   535
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   536
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   537
class MethodCheckerTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   538
  """Tests MockMethod's use of MethodChecker method."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   539
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   540
  def testNoParameters(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   541
    method = mox.MockMethod('NoParameters', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   542
                            CheckCallTestClass.NoParameters)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   543
    method()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   544
    self.assertRaises(AttributeError, method, 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   545
    self.assertRaises(AttributeError, method, 1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   546
    self.assertRaises(AttributeError, method, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   547
    self.assertRaises(AttributeError, method, 1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   548
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   549
  def testOneParameter(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   550
    method = mox.MockMethod('OneParameter', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   551
                            CheckCallTestClass.OneParameter)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   552
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   553
    method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   554
    method(a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   555
    self.assertRaises(AttributeError, method, b=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   556
    self.assertRaises(AttributeError, method, 1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   557
    self.assertRaises(AttributeError, method, 1, a=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   558
    self.assertRaises(AttributeError, method, 1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   559
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   560
  def testTwoParameters(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   561
    method = mox.MockMethod('TwoParameters', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   562
                            CheckCallTestClass.TwoParameters)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   563
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   564
    self.assertRaises(AttributeError, method, 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   565
    self.assertRaises(AttributeError, method, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   566
    self.assertRaises(AttributeError, method, b=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   567
    method(1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   568
    method(1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   569
    method(a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   570
    method(b=2, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   571
    self.assertRaises(AttributeError, method, b=2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   572
    self.assertRaises(AttributeError, method, a=1, b=2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   573
    self.assertRaises(AttributeError, method, 1, 2, 3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   574
    self.assertRaises(AttributeError, method, 1, 2, 3, 4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   575
    self.assertRaises(AttributeError, method, 3, a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   576
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   577
  def testOneDefaultValue(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   578
    method = mox.MockMethod('OneDefaultValue', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   579
                            CheckCallTestClass.OneDefaultValue)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   580
    method()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   581
    method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   582
    method(a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   583
    self.assertRaises(AttributeError, method, b=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   584
    self.assertRaises(AttributeError, method, 1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   585
    self.assertRaises(AttributeError, method, 1, a=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   586
    self.assertRaises(AttributeError, method, 1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   587
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   588
  def testTwoDefaultValues(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   589
    method = mox.MockMethod('TwoDefaultValues', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   590
                            CheckCallTestClass.TwoDefaultValues)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   591
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   592
    self.assertRaises(AttributeError, method, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   593
    self.assertRaises(AttributeError, method, 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   594
    self.assertRaises(AttributeError, method, 1, d=4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   595
    self.assertRaises(AttributeError, method, 1, d=4, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   596
    method(1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   597
    method(a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   598
    method(1, 2, 3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   599
    method(1, 2, 3, 4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   600
    method(1, 2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   601
    method(1, 2, c=3, d=4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   602
    method(1, 2, d=4, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   603
    method(d=4, c=3, a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   604
    self.assertRaises(AttributeError, method, 1, 2, 3, 4, 5)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   605
    self.assertRaises(AttributeError, method, 1, 2, e=9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   606
    self.assertRaises(AttributeError, method, a=1, b=2, e=9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   607
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   608
  def testArgs(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   609
    method = mox.MockMethod('Args', [], False, CheckCallTestClass.Args)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   610
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   611
    self.assertRaises(AttributeError, method, 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   612
    method(1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   613
    method(a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   614
    method(1, 2, 3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   615
    method(1, 2, 3, 4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   616
    self.assertRaises(AttributeError, method, 1, 2, a=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   617
    self.assertRaises(AttributeError, method, 1, 2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   618
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   619
  def testKwargs(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   620
    method = mox.MockMethod('Kwargs', [], False, CheckCallTestClass.Kwargs)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   621
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   622
    method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   623
    method(1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   624
    method(a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   625
    method(b=2, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   626
    self.assertRaises(AttributeError, method, 1, 2, 3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   627
    self.assertRaises(AttributeError, method, 1, 2, a=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   628
    method(1, 2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   629
    method(a=1, b=2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   630
    method(c=3, a=1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   631
    method(a=1, b=2, c=3, d=4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   632
    self.assertRaises(AttributeError, method, 1, 2, 3, 4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   633
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   634
  def testArgsAndKwargs(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   635
    method = mox.MockMethod('ArgsAndKwargs', [], False,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   636
                            CheckCallTestClass.ArgsAndKwargs)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   637
    self.assertRaises(AttributeError, method)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   638
    method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   639
    method(1, 2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   640
    method(1, 2, 3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   641
    method(a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   642
    method(1, b=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   643
    self.assertRaises(AttributeError, method, 1, a=2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   644
    method(b=2, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   645
    method(c=3, b=2, a=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   646
    method(1, 2, c=3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   647
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   648
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   649
class CheckCallTestClass(object):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   650
  def NoParameters(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   651
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   652
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   653
  def OneParameter(self, a):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   654
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   655
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   656
  def TwoParameters(self, a, b):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   657
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   658
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   659
  def OneDefaultValue(self, a=1):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   660
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   661
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   662
  def TwoDefaultValues(self, a, b, c=1, d=2):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   663
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   664
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   665
  def Args(self, a, b, *args):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   666
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   667
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   668
  def Kwargs(self, a, b=2, **kwargs):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   669
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   670
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   671
  def ArgsAndKwargs(self, a, *args, **kwargs):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   672
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   673
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   674
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   675
class MockObjectTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   676
  """Verify that the MockObject class works as exepcted."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   677
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   678
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   679
    self.mock_object = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   680
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   681
  def testSetupModeWithValidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   682
    """Verify the mock object properly mocks a basic method call."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   683
    self.mock_object.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   684
    self.assert_(len(self.mock_object._expected_calls_queue) == 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   685
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   686
  def testSetupModeWithInvalidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   687
    """UnknownMethodCallError should be raised if a non-member method is called.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   688
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   689
    # Note: assertRaises does not catch exceptions thrown by MockObject's
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   690
    # __getattr__
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   691
    try:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   692
      self.mock_object.InvalidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   693
      self.fail("No exception thrown, expected UnknownMethodCallError")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   694
    except mox.UnknownMethodCallError:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   695
      pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   696
    except Exception:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   697
      self.fail("Wrong exception type thrown, expected UnknownMethodCallError")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   698
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   699
  def testReplayWithInvalidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   700
    """UnknownMethodCallError should be raised if a non-member method is called.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   701
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   702
    self.mock_object.ValidCall()          # setup method call
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   703
    self.mock_object._Replay()             # start replay mode
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   704
    # Note: assertRaises does not catch exceptions thrown by MockObject's
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   705
    # __getattr__
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   706
    try:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   707
      self.mock_object.InvalidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   708
      self.fail("No exception thrown, expected UnknownMethodCallError")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   709
    except mox.UnknownMethodCallError:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   710
      pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   711
    except Exception:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   712
      self.fail("Wrong exception type thrown, expected UnknownMethodCallError")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   713
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   714
  def testIsInstance(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   715
    """Mock should be able to pass as an instance of the mocked class."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   716
    self.assert_(isinstance(self.mock_object, TestClass))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   717
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   718
  def testFindValidMethods(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   719
    """Mock should be able to mock all public methods."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   720
    self.assert_('ValidCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   721
    self.assert_('OtherValidCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   722
    self.assert_('MyClassMethod' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   723
    self.assert_('MyStaticMethod' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   724
    self.assert_('_ProtectedCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   725
    self.assert_('__PrivateCall' not in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   726
    self.assert_('_TestClass__PrivateCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   727
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   728
  def testFindsSuperclassMethods(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   729
    """Mock should be able to mock superclasses methods."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   730
    self.mock_object = mox.MockObject(ChildClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   731
    self.assert_('ValidCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   732
    self.assert_('OtherValidCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   733
    self.assert_('MyClassMethod' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   734
    self.assert_('ChildValidCall' in self.mock_object._known_methods)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   735
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   736
  def testAccessClassVariables(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   737
    """Class variables should be accessible through the mock."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   738
    self.assert_('SOME_CLASS_VAR' in self.mock_object._known_vars)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   739
    self.assert_('_PROTECTED_CLASS_VAR' in self.mock_object._known_vars)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   740
    self.assertEquals('test_value', self.mock_object.SOME_CLASS_VAR)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   741
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   742
  def testEquals(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   743
    """A mock should be able to compare itself to another object."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   744
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   745
    self.assertEquals(self.mock_object, self.mock_object)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   746
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   747
  def testEqualsMockFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   748
    """Verify equals identifies unequal objects."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   749
    self.mock_object.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   750
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   751
    self.assertNotEquals(self.mock_object, mox.MockObject(TestClass))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   752
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   753
  def testEqualsInstanceFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   754
    """Verify equals identifies that objects are different instances."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   755
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   756
    self.assertNotEquals(self.mock_object, TestClass())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   757
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   758
  def testNotEquals(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   759
    """Verify not equals works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   760
    self.mock_object._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   761
    self.assertFalse(self.mock_object != self.mock_object)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   762
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   763
  def testMockSetItem_ExpectedSetItem_Success(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   764
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   765
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   766
    In this test, _Verify() succeeds.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   767
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   768
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   769
    dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   770
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   771
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   772
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   773
    dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   774
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   775
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   776
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   777
  def testMockSetItem_ExpectedSetItem_NoSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   778
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   779
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   780
    In this test, _Verify() fails.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   781
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   782
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   783
    dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   784
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   785
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   786
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   787
    # NOT doing dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   788
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   789
    self.assertRaises(mox.ExpectedMethodCallsError, dummy._Verify)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   790
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   791
  def testMockSetItem_ExpectedNoSetItem_Success(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   792
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   793
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   794
    In this test, _Verify() succeeds.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   795
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   796
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   797
    # NOT doing dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   798
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   799
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   800
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   801
    def call(): dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   802
    self.assertRaises(mox.UnexpectedMethodCallError, call)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   803
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   804
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   805
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   806
  def testMockSetItem_ExpectedNoSetItem_NoSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   807
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   808
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   809
    In this test, _Verify() fails.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   810
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   811
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   812
    # NOT doing dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   813
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   814
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   815
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   816
    # NOT doing dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   817
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   818
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   819
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   820
  def testMockSetItem_ExpectedSetItem_NonmatchingParameters(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   821
    """Test that __setitem__() fails if other parameters are expected."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   822
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   823
    dummy['X'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   824
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   825
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   826
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   827
    def call(): dummy['wrong'] = 'Y'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   828
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   829
    self.assertRaises(mox.UnexpectedMethodCallError, call)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   830
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   831
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   832
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   833
  def testMockGetItem_ExpectedGetItem_Success(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   834
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   835
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   836
    In this test, _Verify() succeeds.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   837
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   838
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   839
    dummy['X'].AndReturn('value')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   840
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   841
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   842
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   843
    self.assertEqual(dummy['X'], 'value')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   844
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   845
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   846
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   847
  def testMockGetItem_ExpectedGetItem_NoSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   848
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   849
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   850
    In this test, _Verify() fails.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   851
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   852
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   853
    dummy['X'].AndReturn('value')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   854
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   855
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   856
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   857
    # NOT doing dummy['X']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   858
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   859
    self.assertRaises(mox.ExpectedMethodCallsError, dummy._Verify)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   860
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   861
  def testMockGetItem_ExpectedNoGetItem_NoSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   862
    """Test that __setitem__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   863
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   864
    In this test, _Verify() succeeds.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   865
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   866
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   867
    # NOT doing dummy['X']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   868
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   869
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   870
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   871
    def call(): return dummy['X']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   872
    self.assertRaises(mox.UnexpectedMethodCallError, call)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   873
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   874
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   875
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   876
  def testMockGetItem_ExpectedGetItem_NonmatchingParameters(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   877
    """Test that __setitem__() fails if other parameters are expected."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   878
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   879
    dummy['X'].AndReturn('value')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   880
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   881
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   882
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   883
    def call(): return dummy['wrong']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   884
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   885
    self.assertRaises(mox.UnexpectedMethodCallError, call)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   886
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   887
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   888
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   889
  def testMockContains_ExpectedContains_Success(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   890
    """Test that __contains__ gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   891
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   892
    In this test, _Verify() succeeds.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   893
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   894
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   895
    dummy.__contains__('X').AndReturn(True)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   896
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   897
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   898
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   899
    self.failUnless('X' in dummy)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   900
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   901
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   902
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   903
  def testMockContains_ExpectedContains_NoSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   904
    """Test that __contains__() gets mocked in Dummy.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   905
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   906
    In this test, _Verify() fails.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   907
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   908
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   909
    dummy.__contains__('X').AndReturn('True')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   910
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   911
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   912
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   913
    # NOT doing 'X' in dummy
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   914
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   915
    self.assertRaises(mox.ExpectedMethodCallsError, dummy._Verify)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   916
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   917
  def testMockContains_ExpectedContains_NonmatchingParameter(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   918
    """Test that __contains__ fails if other parameters are expected."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   919
    dummy = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   920
    dummy.__contains__('X').AndReturn(True)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   921
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   922
    dummy._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   923
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   924
    def call(): return 'Y' in dummy
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   925
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   926
    self.assertRaises(mox.UnexpectedMethodCallError, call)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   927
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   928
    dummy._Verify()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   929
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   930
class MoxTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   931
  """Verify Mox works correctly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   932
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   933
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   934
    self.mox = mox.Mox()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   935
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   936
  def testCreateObject(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   937
    """Mox should create a mock object."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   938
    mock_obj = self.mox.CreateMock(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   939
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   940
  def testVerifyObjectWithCompleteReplay(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   941
    """Mox should replay and verify all objects it created."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   942
    mock_obj = self.mox.CreateMock(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   943
    mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   944
    mock_obj.ValidCallWithArgs(mox.IsA(TestClass))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   945
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   946
    mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   947
    mock_obj.ValidCallWithArgs(TestClass("some_value"))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   948
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   949
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   950
  def testVerifyObjectWithIncompleteReplay(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   951
    """Mox should raise an exception if a mock didn't replay completely."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   952
    mock_obj = self.mox.CreateMock(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   953
    mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   954
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   955
    # ValidCall() is never made
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   956
    self.assertRaises(mox.ExpectedMethodCallsError, self.mox.VerifyAll)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   957
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   958
  def testEntireWorkflow(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   959
    """Test the whole work flow."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   960
    mock_obj = self.mox.CreateMock(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   961
    mock_obj.ValidCall().AndReturn("yes")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   962
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   963
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   964
    ret_val = mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   965
    self.assertEquals("yes", ret_val)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   966
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   967
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   968
  def testCallableObject(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   969
    """Test recording calls to a callable object works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   970
    mock_obj = self.mox.CreateMock(CallableClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   971
    mock_obj("foo").AndReturn("qux")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   972
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   973
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   974
    ret_val = mock_obj("foo")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   975
    self.assertEquals("qux", ret_val)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   976
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   977
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   978
  def testCallOnNonCallableObject(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   979
    """Test that you cannot call a non-callable object."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   980
    mock_obj = self.mox.CreateMock(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   981
    self.assertRaises(TypeError, mock_obj)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   982
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   983
  def testCallableObjectWithBadCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   984
    """Test verifying calls to a callable object works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   985
    mock_obj = self.mox.CreateMock(CallableClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   986
    mock_obj("foo").AndReturn("qux")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   987
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   988
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   989
    self.assertRaises(mox.UnexpectedMethodCallError, mock_obj, "ZOOBAZ")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   990
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   991
  def testUnorderedGroup(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   992
    """Test that using one unordered group works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   993
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   994
    mock_obj.Method(1).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   995
    mock_obj.Method(2).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   996
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   997
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   998
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
   999
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1000
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1001
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1002
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1003
  def testUnorderedGroupsInline(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1004
    """Unordered groups should work in the context of ordered calls."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1005
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1006
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1007
    mock_obj.Method(1).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1008
    mock_obj.Method(2).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1009
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1010
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1011
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1012
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1013
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1014
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1015
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1016
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1017
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1018
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1019
  def testMultipleUnorderdGroups(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1020
    """Multiple unoreded groups should work."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1021
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1022
    mock_obj.Method(1).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1023
    mock_obj.Method(2).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1024
    mock_obj.Foo().InAnyOrder('group2')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1025
    mock_obj.Bar().InAnyOrder('group2')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1026
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1027
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1028
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1029
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1030
    mock_obj.Bar()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1031
    mock_obj.Foo()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1032
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1033
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1034
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1035
  def testMultipleUnorderdGroupsOutOfOrder(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1036
    """Multiple unordered groups should maintain external order"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1037
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1038
    mock_obj.Method(1).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1039
    mock_obj.Method(2).InAnyOrder()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1040
    mock_obj.Foo().InAnyOrder('group2')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1041
    mock_obj.Bar().InAnyOrder('group2')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1042
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1043
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1044
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1045
    self.assertRaises(mox.UnexpectedMethodCallError, mock_obj.Bar)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1046
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1047
  def testUnorderedGroupWithReturnValue(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1048
    """Unordered groups should work with return values."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1049
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1050
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1051
    mock_obj.Method(1).InAnyOrder().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1052
    mock_obj.Method(2).InAnyOrder().AndReturn(10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1053
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1054
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1055
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1056
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1057
    actual_two = mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1058
    actual_one = mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1059
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1060
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1061
    self.assertEquals(9, actual_one)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1062
    self.assertEquals(10, actual_two)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1063
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1064
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1065
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1066
  def testUnorderedGroupWithComparator(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1067
    """Unordered groups should work with comparators"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1068
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1069
    def VerifyOne(cmd):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1070
      if not isinstance(cmd, str):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1071
        self.fail('Unexpected type passed to comparator: ' + str(cmd))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1072
      return cmd == 'test'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1073
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1074
    def VerifyTwo(cmd):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1075
      return True
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1076
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1077
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1078
    mock_obj.Foo(['test'], mox.Func(VerifyOne), bar=1).InAnyOrder().\
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1079
        AndReturn('yes test')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1080
    mock_obj.Foo(['test'], mox.Func(VerifyTwo), bar=1).InAnyOrder().\
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1081
        AndReturn('anything')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1082
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1083
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1084
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1085
    mock_obj.Foo(['test'], 'anything', bar=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1086
    mock_obj.Foo(['test'], 'test', bar=1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1087
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1088
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1089
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1090
  def testMultipleTimes(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1091
    """Test if MultipleTimesGroup works."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1092
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1093
    mock_obj.Method(1).MultipleTimes().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1094
    mock_obj.Method(2).AndReturn(10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1095
    mock_obj.Method(3).MultipleTimes().AndReturn(42)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1096
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1097
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1098
    actual_one = mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1099
    second_one = mock_obj.Method(1) # This tests MultipleTimes.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1100
    actual_two = mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1101
    actual_three = mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1102
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1103
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1104
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1105
    self.assertEquals(9, actual_one)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1106
    self.assertEquals(9, second_one) # Repeated calls should return same number.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1107
    self.assertEquals(10, actual_two)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1108
    self.assertEquals(42, actual_three)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1109
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1110
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1111
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1112
  def testMultipleTimesUsingIsAParameter(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1113
    """Test if MultipleTimesGroup works with a IsA parameter."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1114
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1115
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1116
    mock_obj.Method(mox.IsA(str)).MultipleTimes("IsA").AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1117
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1118
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1119
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1120
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1121
    actual_one = mock_obj.Method("1")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1122
    second_one = mock_obj.Method("2") # This tests MultipleTimes.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1123
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1124
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1125
    self.assertEquals(9, actual_one)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1126
    self.assertEquals(9, second_one) # Repeated calls should return same number.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1127
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1128
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1129
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1130
  def testMultipleTimesThreeMethods(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1131
    """Test if MultipleTimesGroup works with three or more methods."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1132
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1133
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1134
    mock_obj.Method(1).MultipleTimes().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1135
    mock_obj.Method(2).MultipleTimes().AndReturn(8)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1136
    mock_obj.Method(3).MultipleTimes().AndReturn(7)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1137
    mock_obj.Method(4).AndReturn(10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1138
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1139
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1140
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1141
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1142
    actual_three = mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1143
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1144
    actual_two = mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1145
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1146
    actual_one = mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1147
    actual_four = mock_obj.Method(4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1148
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1149
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1150
    self.assertEquals(9, actual_one)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1151
    self.assertEquals(8, actual_two)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1152
    self.assertEquals(7, actual_three)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1153
    self.assertEquals(10, actual_four)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1154
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1155
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1156
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1157
  def testMultipleTimesMissingOne(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1158
    """Test if MultipleTimesGroup fails if one method is missing."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1159
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1160
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1161
    mock_obj.Method(1).MultipleTimes().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1162
    mock_obj.Method(2).MultipleTimes().AndReturn(8)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1163
    mock_obj.Method(3).MultipleTimes().AndReturn(7)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1164
    mock_obj.Method(4).AndReturn(10)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1165
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1166
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1167
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1168
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1169
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1170
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1171
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1172
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1173
    mock_obj.Method(2)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1174
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1175
    self.assertRaises(mox.UnexpectedMethodCallError, mock_obj.Method, 4)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1176
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1177
  def testMultipleTimesTwoGroups(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1178
    """Test if MultipleTimesGroup works with a group after a
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1179
    MultipleTimesGroup.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1180
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1181
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1182
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1183
    mock_obj.Method(1).MultipleTimes().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1184
    mock_obj.Method(3).MultipleTimes("nr2").AndReturn(42)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1185
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1186
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1187
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1188
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1189
    actual_one = mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1190
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1191
    actual_three = mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1192
    mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1193
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1194
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1195
    self.assertEquals(9, actual_one)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1196
    self.assertEquals(42, actual_three)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1197
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1198
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1199
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1200
  def testMultipleTimesTwoGroupsFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1201
    """Test if MultipleTimesGroup fails with a group after a
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1202
    MultipleTimesGroup.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1203
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1204
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1205
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1206
    mock_obj.Method(1).MultipleTimes().AndReturn(9)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1207
    mock_obj.Method(3).MultipleTimes("nr2").AndReturn(42)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1208
    mock_obj.Close()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1209
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1210
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1211
    mock_obj.Open()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1212
    actual_one = mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1213
    mock_obj.Method(1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1214
    actual_three = mock_obj.Method(3)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1215
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1216
    self.assertRaises(mox.UnexpectedMethodCallError, mock_obj.Method, 1)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1217
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1218
  def testWithSideEffects(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1219
    """Test side effect operations actually modify their target objects."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1220
    def modifier(mutable_list):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1221
      mutable_list[0] = 'mutated'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1222
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1223
    mock_obj.ConfigureInOutParameter(['original']).WithSideEffects(modifier)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1224
    mock_obj.WorkWithParameter(['mutated'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1225
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1226
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1227
    local_list = ['original']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1228
    mock_obj.ConfigureInOutParameter(local_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1229
    mock_obj.WorkWithParameter(local_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1230
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1231
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1232
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1233
  def testWithSideEffectsException(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1234
    """Test side effect operations actually modify their target objects."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1235
    def modifier(mutable_list):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1236
      mutable_list[0] = 'mutated'
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1237
    mock_obj = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1238
    method = mock_obj.ConfigureInOutParameter(['original'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1239
    method.WithSideEffects(modifier).AndRaise(Exception('exception'))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1240
    mock_obj.WorkWithParameter(['mutated'])
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1241
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1242
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1243
    local_list = ['original']
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1244
    self.failUnlessRaises(Exception,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1245
                          mock_obj.ConfigureInOutParameter,
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1246
                          local_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1247
    mock_obj.WorkWithParameter(local_list)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1248
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1249
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1250
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1251
  def testStubOutMethod(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1252
    """Test that a method is replaced with a MockAnything."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1253
    test_obj = TestClass()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1254
    # Replace OtherValidCall with a mock.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1255
    self.mox.StubOutWithMock(test_obj, 'OtherValidCall')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1256
    self.assert_(isinstance(test_obj.OtherValidCall, mox.MockAnything))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1257
    test_obj.OtherValidCall().AndReturn('foo')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1258
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1259
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1260
    actual = test_obj.OtherValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1261
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1262
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1263
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1264
    self.assertEquals('foo', actual)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1265
    self.failIf(isinstance(test_obj.OtherValidCall, mox.MockAnything))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1266
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1267
  def testStubOutObject(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1268
    """Test than object is replaced with a Mock."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1269
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1270
    class Foo(object):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1271
      def __init__(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1272
        self.obj = TestClass()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1273
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1274
    foo = Foo()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1275
    self.mox.StubOutWithMock(foo, "obj")
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1276
    self.assert_(isinstance(foo.obj, mox.MockObject))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1277
    foo.obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1278
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1279
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1280
    foo.obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1281
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1282
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1283
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1284
    self.failIf(isinstance(foo.obj, mox.MockObject))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1285
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1286
  def testForgotReplayHelpfulMessage(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1287
    """If there is an AttributeError on a MockMethod, give users a helpful msg.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1288
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1289
    foo = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1290
    bar = self.mox.CreateMockAnything()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1291
    foo.GetBar().AndReturn(bar)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1292
    bar.ShowMeTheMoney()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1293
    # Forgot to replay!
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1294
    try:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1295
      foo.GetBar().ShowMeTheMoney()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1296
    except AttributeError, e:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1297
      self.assertEquals('MockMethod has no attribute "ShowMeTheMoney". '
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1298
          'Did you remember to put your mocks in replay mode?', str(e))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1299
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1300
class ReplayTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1301
  """Verify Replay works properly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1302
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1303
  def testReplay(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1304
    """Replay should put objects into replay mode."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1305
    mock_obj = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1306
    self.assertFalse(mock_obj._replay_mode)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1307
    mox.Replay(mock_obj)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1308
    self.assertTrue(mock_obj._replay_mode)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1309
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1310
class MoxTestBaseTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1311
  """Verify that all tests in a class derived from MoxTestBase are wrapped."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1312
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1313
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1314
    self.mox = mox.Mox()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1315
    self.test_mox = mox.Mox()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1316
    self.result = unittest.TestResult()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1317
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1318
  def tearDown(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1319
    # In case one of our tests fail before UnsetStubs is called.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1320
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1321
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1322
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1323
  def _setUpTestClass(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1324
    """Replacement for setUp in the test class instance.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1325
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1326
    Assigns a mox.Mox instance as the mox attribute of the test class instance.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1327
    This replacement Mox instance is under our control before setUp is called
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1328
    in the test class instance.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1329
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1330
    self.test.mox = self.test_mox
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1331
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1332
  def _CreateTest(self, test_name):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1333
    """Create a test from our example mox class.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1334
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1335
    The created test instance is assigned to this instances test attribute.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1336
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1337
    self.test = mox_test_helper.ExampleMoxTest(test_name)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1338
    self.mox.stubs.Set(self.test, 'setUp', self._setUpTestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1339
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1340
  def _VerifySuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1341
    """Run the checks to confirm test method completed successfully."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1342
    self.mox.StubOutWithMock(self.test_mox, 'UnsetStubs')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1343
    self.mox.StubOutWithMock(self.test_mox, 'VerifyAll')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1344
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1345
    self.test_mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1346
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1347
    self.test.run(result=self.result)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1348
    self.assertTrue(self.result.wasSuccessful())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1349
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1350
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1351
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1352
    self.test_mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1353
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1354
  def testSuccess(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1355
    """Successful test method execution test."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1356
    self._CreateTest('testSuccess')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1357
    self._VerifySuccess()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1358
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1359
  def testExpectedNotCalled(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1360
    """Stubbed out method is not called."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1361
    self._CreateTest('testExpectedNotCalled')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1362
    self.mox.StubOutWithMock(self.test_mox, 'UnsetStubs')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1363
    # Dont stub out VerifyAll - that's what causes the test to fail
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1364
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1365
    self.test_mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1366
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1367
    self.test.run(result=self.result)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1368
    self.failIf(self.result.wasSuccessful())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1369
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1370
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1371
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1372
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1373
  def testUnexpectedCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1374
    """Stubbed out method is called with unexpected arguments."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1375
    self._CreateTest('testUnexpectedCall')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1376
    self.mox.StubOutWithMock(self.test_mox, 'UnsetStubs')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1377
    # Ensure no calls are made to VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1378
    self.mox.StubOutWithMock(self.test_mox, 'VerifyAll')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1379
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1380
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1381
    self.test.run(result=self.result)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1382
    self.failIf(self.result.wasSuccessful())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1383
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1384
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1385
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1386
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1387
  def testFailure(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1388
    """Failing assertion in test method."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1389
    self._CreateTest('testFailure')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1390
    self.mox.StubOutWithMock(self.test_mox, 'UnsetStubs')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1391
    # Ensure no calls are made to VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1392
    self.mox.StubOutWithMock(self.test_mox, 'VerifyAll')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1393
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1394
    self.mox.ReplayAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1395
    self.test.run(result=self.result)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1396
    self.failIf(self.result.wasSuccessful())
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1397
    self.mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1398
    self.mox.VerifyAll()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1399
    self.test_mox.UnsetStubs()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1400
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1401
  def testMixin(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1402
    """Run test from mix-in test class, ensure it passes."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1403
    self._CreateTest('testStat')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1404
    self._VerifySuccess()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1405
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1406
  def testMixinAgain(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1407
    """Run same test as above but from the current test class.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1408
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1409
    This ensures metaclass properly wrapped test methods from all base classes.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1410
    If unsetting of stubs doesn't happen, this will fail.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1411
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1412
    self._CreateTest('testStatOther')
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1413
    self._VerifySuccess()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1414
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1415
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1416
class VerifyTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1417
  """Verify Verify works properly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1418
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1419
  def testVerify(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1420
    """Verify should be called for all objects.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1421
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1422
    This should throw an exception because the expected behavior did not occur.
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1423
    """
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1424
    mock_obj = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1425
    mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1426
    mock_obj._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1427
    self.assertRaises(mox.ExpectedMethodCallsError, mox.Verify, mock_obj)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1428
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1429
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1430
class ResetTest(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1431
  """Verify Reset works properly."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1432
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1433
  def testReset(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1434
    """Should empty all queues and put mocks in record mode."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1435
    mock_obj = mox.MockObject(TestClass)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1436
    mock_obj.ValidCall()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1437
    self.assertFalse(mock_obj._replay_mode)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1438
    mock_obj._Replay()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1439
    self.assertTrue(mock_obj._replay_mode)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1440
    self.assertEquals(1, len(mock_obj._expected_calls_queue))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1441
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1442
    mox.Reset(mock_obj)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1443
    self.assertFalse(mock_obj._replay_mode)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1444
    self.assertEquals(0, len(mock_obj._expected_calls_queue))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1445
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1446
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1447
class MyTestCase(unittest.TestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1448
  """Simulate the use of a fake wrapper around Python's unittest library."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1449
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1450
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1451
    super(MyTestCase, self).setUp()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1452
    self.critical_variable = 42
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1453
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1454
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1455
class MoxTestBaseMultipleInheritanceTest(mox.MoxTestBase, MyTestCase):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1456
  """Test that multiple inheritance can be used with MoxTestBase."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1457
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1458
  def setUp(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1459
    super(MoxTestBaseMultipleInheritanceTest, self).setUp()
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1460
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1461
  def testMultipleInheritance(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1462
    """Should be able to access members created by all parent setUp()."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1463
    self.assert_(isinstance(self.mox, mox.Mox))
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1464
    self.assertEquals(42, self.critical_variable)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1465
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1466
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1467
class TestClass:
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1468
  """This class is used only for testing the mock framework"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1469
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1470
  SOME_CLASS_VAR = "test_value"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1471
  _PROTECTED_CLASS_VAR = "protected value"
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1472
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1473
  def __init__(self, ivar=None):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1474
    self.__ivar = ivar
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1475
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1476
  def __eq__(self, rhs):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1477
    return self.__ivar == rhs
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1478
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1479
  def __ne__(self, rhs):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1480
    return not self.__eq__(rhs)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1481
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1482
  def ValidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1483
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1484
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1485
  def OtherValidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1486
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1487
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1488
  def ValidCallWithArgs(self, *args, **kwargs):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1489
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1490
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1491
  @classmethod
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1492
  def MyClassMethod(cls):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1493
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1494
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1495
  @staticmethod
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1496
  def MyStaticMethod():
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1497
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1498
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1499
  def _ProtectedCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1500
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1501
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1502
  def __PrivateCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1503
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1504
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1505
  def __getitem__(self, key):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1506
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1507
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1508
  def __DoNotMock(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1509
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1510
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1511
  def __getitem__(self, key):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1512
    """Return the value for key."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1513
    return self.d[key]
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1514
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1515
  def __setitem__(self, key, value):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1516
    """Set the value for key to value."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1517
    self.d[key] = value
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1518
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1519
  def __contains__(self, key):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1520
     """Returns True if d contains the key."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1521
     return key in self.d
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1522
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1523
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1524
class ChildClass(TestClass):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1525
  """This inherits from TestClass."""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1526
  def __init__(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1527
    TestClass.__init__(self)
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1528
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1529
  def ChildValidCall(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1530
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1531
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1532
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1533
class CallableClass(object):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1534
  """This class is callable, and that should be mockable!"""
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1535
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1536
  def __init__(self):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1537
    pass
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1538
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1539
  def __call__(self, param):
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1540
    return param
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1541
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1542
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1543
if __name__ == '__main__':
9af147fc1f1c Add pymox to tests folder.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents:
diff changeset
  1544
  unittest.main()