|
1
|
|
|
if __name__ == '__main__': |
|
2
|
|
|
import sys |
|
3
|
|
|
sys.path.insert(0, '..') |
|
4
|
|
|
sys.path.insert(0, '../..') |
|
5
|
|
|
|
|
6
|
|
|
from hamcrest.core.core.issame import * |
|
7
|
|
|
|
|
8
|
|
|
from hamcrest.core.string_description import StringDescription |
|
9
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
|
10
|
|
|
import re |
|
11
|
|
|
import unittest |
|
12
|
|
|
|
|
13
|
|
|
__author__ = "Jon Reid" |
|
14
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
|
15
|
|
|
__license__ = "BSD, see License.txt" |
|
16
|
|
|
|
|
17
|
|
|
ADDRESS_FORMAT = r"-?0x[0-9a-fA-F]+L?" |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
class IsSameTest(MatcherTest): |
|
21
|
|
|
|
|
22
|
|
|
def testEvaluatesToTrueIfArgumentIsReferenceToASpecifiedObject(self): |
|
23
|
|
|
o1 = object() |
|
24
|
|
|
o2 = object() |
|
25
|
|
|
|
|
26
|
|
|
self.assert_matches('same', same_instance(o1), o1) |
|
27
|
|
|
self.assert_does_not_match('different', same_instance(o1), o2) |
|
28
|
|
|
|
|
29
|
|
|
def testDescriptionIncludesMemoryAddress(self): |
|
30
|
|
|
description = StringDescription() |
|
31
|
|
|
expected = re.compile("same instance as " + ADDRESS_FORMAT + " 'abc'") |
|
32
|
|
|
|
|
33
|
|
|
description.append_description_of(same_instance('abc')); |
|
34
|
|
|
self.assertTrue(expected.match(str(description))) |
|
35
|
|
|
|
|
36
|
|
|
def testSuccessfulMatchDoesNotGenerateMismatchDescription(self): |
|
37
|
|
|
o1 = object() |
|
38
|
|
|
self.assert_no_mismatch_description(same_instance(o1), o1) |
|
39
|
|
|
|
|
40
|
|
|
def testMismatchDescriptionShowsActualArgumentAddress(self): |
|
41
|
|
|
matcher = same_instance('foo') |
|
42
|
|
|
description = StringDescription() |
|
43
|
|
|
expected = re.compile("was " + ADDRESS_FORMAT + " 'hi'") |
|
44
|
|
|
|
|
45
|
|
|
result = matcher.matches('hi', description) |
|
46
|
|
|
self.assertFalse(result, 'Precondition: Matcher should not match item') |
|
47
|
|
|
self.assertTrue(expected.match(str(description))) |
|
48
|
|
|
|
|
49
|
|
|
def testMismatchDescriptionWithNilShouldNotIncludeAddress(self): |
|
50
|
|
|
self.assert_mismatch_description("was <None>", same_instance('foo'), None) |
|
51
|
|
|
|
|
52
|
|
|
def testDescribeMismatch(self): |
|
53
|
|
|
matcher = same_instance('foo') |
|
54
|
|
|
description = StringDescription() |
|
55
|
|
|
expected = re.compile("was " + ADDRESS_FORMAT + " 'hi'") |
|
56
|
|
|
|
|
57
|
|
|
matcher.describe_mismatch('hi', description) |
|
58
|
|
|
expected = re.compile("was " + ADDRESS_FORMAT + " 'hi'") |
|
59
|
|
|
self.assertTrue(expected.match(str(description)), |
|
60
|
|
|
"Expected %s to match %s" % (str(matcher), str(description))) |
|
61
|
|
|
|
|
62
|
|
|
def testDescribeMismatchWithNilShouldNotIncludeAddress(self): |
|
63
|
|
|
self.assert_describe_mismatch("was <None>", same_instance('foo'), None) |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
if __name__ == '__main__': |
|
67
|
|
|
unittest.main() |
|
68
|
|
|
|