Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / lib / partial_mock_unittest.py
1 #!/usr/bin/python
2 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Unittests for the partial_mock test helper code."""
7
8 import os
9 import sys
10
11 sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)),
12                                 '..', '..'))
13 from chromite.lib import cros_test_lib
14 from chromite.lib import partial_mock
15
16 # pylint: disable=W0212
17
18 class ComparatorTest(cros_test_lib.MoxTestCase):
19   """Test Comparitor functionality."""
20   TEST_KEY1 = 'monkey'
21   TEST_KEY2 = 'foon'
22
23   def testEquals(self):
24     """__eq__, __ne__ functionality of Comparator classes."""
25     for cls_name in ['In', 'Regex', 'ListRegex']:
26       cls = getattr(partial_mock, cls_name)
27       obj1 = cls(self.TEST_KEY1)
28       obj2 = cls(self.TEST_KEY1)
29       obj3 = cls(self.TEST_KEY2)
30       self.assertEquals(obj1, obj2)
31       self.assertFalse(obj1 == obj3)
32       self.assertNotEquals(obj1, obj3)
33
34   def testIgnoreEquals(self):
35     """Verify __eq__ functionality for Ignore."""
36     obj1 = partial_mock.Ignore()
37     obj2 = partial_mock.Ignore()
38     self.assertEquals(obj1, obj2)
39     self.assertFalse(obj1 != obj2)
40
41   def testListRegex(self):
42     """Verify ListRegex match functionality."""
43     obj = partial_mock.ListRegex('.*monkey.*')
44     self.assertTrue(obj.Match(['the', 'small monkeys', 'jumped']))
45     self.assertFalse(obj.Match(['the', 'jumped']))
46     self.assertFalse(obj.Match(None))
47     self.assertFalse(obj.Match(1))
48
49
50 class RecursiveCompareTest(cros_test_lib.MoxTestCase):
51   """Test recursive compare functionality."""
52
53   LHS_DICT = {3: 1, 1: 2}
54   RHS_DICT = {1: 2, 3: 1}
55   LIST = [1, 2, 3, 4]
56   TUPLE = (1, 2, 3, 4)
57
58   def TrueHelper(self, lhs, rhs):
59     self.assertTrue(partial_mock._RecursiveCompare(lhs, rhs))
60
61   def FalseHelper(self, lhs, rhs):
62     self.assertFalse(partial_mock._RecursiveCompare(lhs, rhs))
63
64   def testIt(self):
65     """Test basic equality cases."""
66     self.TrueHelper(self.LHS_DICT, self.RHS_DICT)
67     self.TrueHelper({3: self.LIST, 1: self.LHS_DICT},
68                     {1: self.LHS_DICT, 3: self.LIST})
69     self.FalseHelper({1: self.LHS_DICT, 3: self.LIST},
70                      {1: self.LHS_DICT, 3: self.LIST + [5]})
71     self.FalseHelper(self.LIST, self.TUPLE)
72
73   def testUnicode(self):
74     """Test recursively comparing unicode and non-unicode strings."""
75     self.assertTrue(partial_mock._RecursiveCompare(['foo'], [u'foo']))
76
77
78 class ListContainsTest(cros_test_lib.MoxTestCase):
79   """Unittests for ListContains method."""
80
81   L = range(10) + range(10) + [9]
82   STRICTLY_TRUE_LISTS = [range(10), range(9, 10), range(3, 6), range(1), [],
83                          [9, 9]]
84   LOOSELY_TRUE_LISTS = [range(0, 10, 2), range(3, 6, 2), [1, 1]]
85   FALSE_LISTS = [[1.5], [-1], [1, 1, 1], [10], [22], range(6, 11), range(-1, 5)]
86
87   def testStrictContains(self):
88     """Test ListContains with strict=True."""
89     for x in self.STRICTLY_TRUE_LISTS:
90       self.assertTrue(partial_mock.ListContains(x, self.L, strict=True))
91     for x in self.LOOSELY_TRUE_LISTS + self.FALSE_LISTS:
92       self.assertFalse(partial_mock.ListContains(x, self.L, strict=True))
93
94   def testLooseContains(self):
95     """Test ListContains with strict=False."""
96     for x in self.STRICTLY_TRUE_LISTS + self.LOOSELY_TRUE_LISTS:
97       self.assertTrue(partial_mock.ListContains(x, self.L))
98     for x in self.FALSE_LISTS:
99       self.assertFalse(partial_mock.ListContains(x, self.L))
100
101   def testUnicode(self):
102     """Test ListContains with unicode and non-unicode strings."""
103     self.assertTrue(partial_mock.ListContains(['foo'], [u'foo']))
104
105
106 class MockedCallResultsTest(cros_test_lib.MoxTestCase):
107   """Test MockedCallResults functionality."""
108
109   ARGS = ('abc',)
110   LIST_ARGS = ([1, 2, 3, 4],)
111   KWARGS = {'test': 'ing'}
112   NEW_ENTRY = {'new': 'entry'}
113
114   def KwargsHelper(self, result, kwargs, strict=True):
115     self.mr.AddResultForParams(self.ARGS, result, kwargs=kwargs,
116                                strict=strict)
117
118   def setUp(self):
119     self.mr = partial_mock.MockedCallResults('SomeFunction')
120
121   def testNoMock(self):
122     """The call is not mocked."""
123     self.assertRaises(AssertionError, self.mr.LookupResult, self.ARGS)
124
125   def testArgReplacement(self):
126     """Replacing mocks for args-only calls."""
127     self.mr.AddResultForParams(self.ARGS, 1)
128     self.mr.AddResultForParams(self.ARGS, 2)
129     self.assertEquals(2, self.mr.LookupResult(self.ARGS))
130
131   def testKwargsStrictReplacement(self):
132     """Replacing strict kwargs mock with another strict mock."""
133     self.KwargsHelper(1, self.KWARGS)
134     self.KwargsHelper(2, self.KWARGS)
135     self.assertEquals(2, self.mr.LookupResult(self.ARGS, kwargs=self.KWARGS))
136
137   def testKwargsNonStrictReplacement(self):
138     """Replacing strict kwargs mock with nonstrict mock."""
139     self.KwargsHelper(1, self.KWARGS)
140     self.KwargsHelper(2, self.KWARGS, strict=False)
141     self.assertEquals(2, self.mr.LookupResult(self.ARGS, kwargs=self.KWARGS))
142
143   def testListArgLookup(self):
144     """Matching of arguments containing lists."""
145     self.mr.AddResultForParams(self.LIST_ARGS, 1)
146     self.mr.AddResultForParams(self.ARGS, 1)
147     self.assertEquals(1, self.mr.LookupResult(self.LIST_ARGS))
148
149   def testKwargsStrictLookup(self):
150     """Strict lookup fails due to extra kwarg."""
151     self.KwargsHelper(1, self.KWARGS)
152     kwargs = self.NEW_ENTRY
153     kwargs.update(self.KWARGS)
154     self.assertRaises(AssertionError, self.mr.LookupResult, self.ARGS,
155                       kwargs=kwargs)
156
157   def testKwargsNonStrictLookup(self):
158     """"Nonstrict lookup passes with extra kwarg."""
159     self.KwargsHelper(1, self.KWARGS, strict=False)
160     kwargs = self.NEW_ENTRY
161     kwargs.update(self.KWARGS)
162     self.assertEquals(1, self.mr.LookupResult(self.ARGS, kwargs=kwargs))
163
164   def testIgnoreMatching(self):
165     """Deep matching of Ignore objects."""
166     ignore = partial_mock.Ignore()
167     self.mr.AddResultForParams((ignore, ignore), 1, kwargs={'test': ignore})
168     self.assertEquals(
169         1, self.mr.LookupResult(('some', 'values'), {'test': 'bla'}))
170
171   def testRegexMatching(self):
172     """Regex matching."""
173     self.mr.AddResultForParams((partial_mock.Regex('pre.ix'),), 1)
174     self.mr.AddResultForParams((partial_mock.Regex('suffi.'),), 2)
175     self.assertEquals(1, self.mr.LookupResult(('prefix',)))
176     self.assertEquals(2, self.mr.LookupResult(('suffix',)))
177
178   def testMultipleMatches(self):
179     """Lookup matches mutilple results."""
180     self.mr.AddResultForParams((partial_mock.Ignore(),), 1)
181     self.mr.AddResultForParams((partial_mock.In('test'),), 2)
182     self.assertRaises(AssertionError, self.mr.LookupResult, ('test',))
183
184   def testDefaultResult(self):
185     """Test default result matching."""
186     self.mr.SetDefaultResult(1)
187     self.mr.AddResultForParams((partial_mock.In('test'),), 2)
188     self.assertEquals(1, self.mr.LookupResult(self.ARGS))
189     self.assertEquals(2, self.mr.LookupResult(('test',)))
190
191   def _ExampleHook(self, *args, **kwargs):
192     """Example hook for testing."""
193     self.assertEquals(args, self.LIST_ARGS)
194     self.assertEquals(kwargs, self.KWARGS)
195     return 2
196
197   def testHook(self):
198     """Return value of hook is used as the final result."""
199     self.mr.AddResultForParams(self.ARGS, 1, side_effect=self._ExampleHook)
200     self.assertEqual(
201         2, self.mr.LookupResult(self.ARGS, hook_args=self.LIST_ARGS,
202                                 hook_kwargs=self.KWARGS))
203
204   def testDefaultHook(self):
205     """Verify default hooks are used."""
206     self.mr.SetDefaultResult(1, self._ExampleHook)
207     self.mr.AddResultForParams((partial_mock.In('test'),), 3)
208     self.assertEqual(
209         2, self.mr.LookupResult(self.ARGS, hook_args=self.LIST_ARGS,
210                                 hook_kwargs=self.KWARGS))
211     self.assertEquals(3, self.mr.LookupResult(('test',)))
212
213
214 if __name__ == '__main__':
215   cros_test_lib.main()