Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: tests/super_mox.py

Issue 392006: Cleanup the unit tests by mocking more system functions. (Closed)
Patch Set: Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tests/revert_unittest.py ('k') | tests/trychange_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2009 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2009 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """Simplify unit tests based on pymox.""" 6 """Simplify unit tests based on pymox."""
7 7
8 import __builtin__
8 import os 9 import os
9 import random 10 import random
10 import string 11 import string
11 from pymox import mox 12 import subprocess
13 import sys
14 from pymox import mox
12 15
13 16
14 class SuperMoxTestBase(mox.MoxTestBase): 17 class IsOneOf(mox.Comparator):
18 def __init__(self, keys):
19 self._keys = keys
20
21 def equals(self, rhs):
22 return rhs in self._keys
23
24 def __repr__(self):
25 return '<sequence or map containing \'%s\'>' % str(self._keys)
26
27
28 class SuperMoxBaseTestBase(mox.MoxTestBase):
29 """Base class with some additional functionalities. People will usually want
30 to use SuperMoxTestBase instead."""
15 # Backup the separator in case it gets mocked 31 # Backup the separator in case it gets mocked
16 _OS_SEP = os.sep 32 _OS_SEP = os.sep
17 _RANDOM_CHOICE = random.choice 33 _RANDOM_CHOICE = random.choice
18 _RANDOM_RANDINT = random.randint 34 _RANDOM_RANDINT = random.randint
19 _STRING_LETTERS = string.letters 35 _STRING_LETTERS = string.letters
20 36
21 ## Some utilities for generating arbitrary arguments. 37 ## Some utilities for generating arbitrary arguments.
22 def String(self, max_length): 38 def String(self, max_length):
23 return ''.join([self._RANDOM_CHOICE(self._STRING_LETTERS) 39 return ''.join([self._RANDOM_CHOICE(self._STRING_LETTERS)
24 for x in xrange(self._RANDOM_RANDINT(1, max_length))]) 40 for x in xrange(self._RANDOM_RANDINT(1, max_length))])
(...skipping 25 matching lines...) Expand all
50 # Skip over members starting with '_' since they are usually not meant to 66 # Skip over members starting with '_' since they are usually not meant to
51 # be for public use. 67 # be for public use.
52 actual_members = [x for x in sorted(dir(object)) 68 actual_members = [x for x in sorted(dir(object))
53 if not x.startswith('_')] 69 if not x.startswith('_')]
54 expected_members = sorted(members) 70 expected_members = sorted(members)
55 if actual_members != expected_members: 71 if actual_members != expected_members:
56 diff = ([i for i in actual_members if i not in expected_members] + 72 diff = ([i for i in actual_members if i not in expected_members] +
57 [i for i in expected_members if i not in actual_members]) 73 [i for i in expected_members if i not in actual_members])
58 print diff 74 print diff
59 self.assertEqual(actual_members, expected_members) 75 self.assertEqual(actual_members, expected_members)
76
77 def UnMock(self, object, name):
78 """Restore an object inside a test."""
79 for (parent, old_child, child_name) in self.mox.stubs.cache:
80 if parent == object and child_name == name:
81 setattr(parent, child_name, old_child)
82 break
83
84
85 class SuperMoxTestBase(SuperMoxBaseTestBase):
86 def setUp(self):
87 """Patch a few functions with know side-effects."""
88 SuperMoxBaseTestBase.setUp(self)
89 #self.mox.StubOutWithMock(__builtin__, 'open')
90 self.mox.StubOutWithMock(os, 'chdir')
91 self.mox.StubOutWithMock(os, 'chown')
92 self.mox.StubOutWithMock(os, 'close')
93 #self.mox.StubOutWithMock(os, 'closerange')
94 self.mox.StubOutWithMock(os, 'dup')
95 self.mox.StubOutWithMock(os, 'dup2')
96 self.mox.StubOutWithMock(os, 'fchdir')
97 #self.mox.StubOutWithMock(os, 'fchmod')
98 #self.mox.StubOutWithMock(os, 'fchown')
99 self.mox.StubOutWithMock(os, 'fdopen')
100 self.mox.StubOutWithMock(os, 'getcwd')
101 self.mox.StubOutWithMock(os, 'getpid')
102 self.mox.StubOutWithMock(os, 'lseek')
103 self.mox.StubOutWithMock(os, 'makedirs')
104 self.mox.StubOutWithMock(os, 'mkdir')
105 self.mox.StubOutWithMock(os, 'open')
106 self.mox.StubOutWithMock(os, 'popen')
107 self.mox.StubOutWithMock(os, 'popen2')
108 self.mox.StubOutWithMock(os, 'popen3')
109 self.mox.StubOutWithMock(os, 'popen4')
110 self.mox.StubOutWithMock(os, 'read')
111 self.mox.StubOutWithMock(os, 'remove')
112 self.mox.StubOutWithMock(os, 'removedirs')
113 self.mox.StubOutWithMock(os, 'rename')
114 self.mox.StubOutWithMock(os, 'renames')
115 self.mox.StubOutWithMock(os, 'rmdir')
116 self.mox.StubOutWithMock(os, 'symlink')
117 self.mox.StubOutWithMock(os, 'system')
118 self.mox.StubOutWithMock(os, 'tmpfile')
119 self.mox.StubOutWithMock(os, 'walk')
120 self.mox.StubOutWithMock(os, 'write')
121 self.mox.StubOutWithMock(os.path, 'abspath')
122 self.mox.StubOutWithMock(os.path, 'exists')
123 self.mox.StubOutWithMock(os.path, 'getsize')
124 self.mox.StubOutWithMock(os.path, 'isdir')
125 self.mox.StubOutWithMock(os.path, 'isfile')
126 self.mox.StubOutWithMock(os.path, 'islink')
127 self.mox.StubOutWithMock(os.path, 'ismount')
128 self.mox.StubOutWithMock(os.path, 'lexists')
129 self.mox.StubOutWithMock(os.path, 'realpath')
130 self.mox.StubOutWithMock(os.path, 'samefile')
131 self.mox.StubOutWithMock(os.path, 'walk')
132 self.mox.StubOutWithMock(subprocess, 'call')
133 self.mox.StubOutWithMock(subprocess, 'Popen')
134 #self.mox.StubOutWithMock(sys, 'stderr')
135 self.mox.StubOutWithMock(sys, 'stdin')
136 self.mox.StubOutWithMock(sys, 'stdout')
OLDNEW
« no previous file with comments | « tests/revert_unittest.py ('k') | tests/trychange_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698