|
1
|
|
|
if __name__ == '__main__': |
|
2
|
|
|
import sys |
|
3
|
|
|
sys.path.insert(0, '..') |
|
4
|
|
|
sys.path.insert(0, '../..') |
|
5
|
|
|
|
|
6
|
|
|
from hamcrest.library.object.hasproperty import * |
|
7
|
|
|
|
|
8
|
|
|
from hamcrest.core.core.isequal import equal_to |
|
9
|
|
|
from hamcrest_unit_test.matcher_test import MatcherTest |
|
10
|
|
|
import unittest |
|
11
|
|
|
|
|
12
|
|
|
__author__ = "Chris Rose" |
|
13
|
|
|
__copyright__ = "Copyright 2011 hamcrest.org" |
|
14
|
|
|
__license__ = "BSD, see License.txt" |
|
15
|
|
|
|
|
16
|
|
|
class OnePropertyOldStyle: |
|
17
|
|
|
|
|
18
|
|
|
field = 'value' |
|
19
|
|
|
field2 = 'value2' |
|
20
|
|
|
|
|
21
|
|
|
class OnePropertyNewStyle(object): |
|
22
|
|
|
|
|
23
|
|
|
field = 'value' |
|
24
|
|
|
field2 = 'value2' |
|
25
|
|
|
|
|
26
|
|
|
def __repr__(self): |
|
27
|
|
|
return 'OnePropertyNewStyle' |
|
28
|
|
|
|
|
29
|
|
|
def __str__(self): |
|
30
|
|
|
return repr(self) |
|
31
|
|
|
|
|
32
|
|
|
class OverridingOldStyle: |
|
33
|
|
|
|
|
34
|
|
|
def __getattr__(self, name): |
|
35
|
|
|
if name == 'field': |
|
36
|
|
|
return 'value' |
|
37
|
|
|
if name == 'field2': |
|
38
|
|
|
return 'value2' |
|
39
|
|
|
|
|
40
|
|
|
raise AttributeError(name) |
|
41
|
|
|
|
|
42
|
|
|
class OverridingNewStyleGetAttr(object): |
|
43
|
|
|
|
|
44
|
|
|
def __getattr__(self, name): |
|
45
|
|
|
if name == 'field': |
|
46
|
|
|
return 'value' |
|
47
|
|
|
if name == 'field2': |
|
48
|
|
|
return 'value2' |
|
49
|
|
|
|
|
50
|
|
|
raise AttributeError(name) |
|
51
|
|
|
|
|
52
|
|
|
class OverridingNewStyleGetAttribute(object): |
|
53
|
|
|
|
|
54
|
|
|
def __getattribute__(self, name): |
|
55
|
|
|
if name == 'field': |
|
56
|
|
|
return 'value' |
|
57
|
|
|
if name == 'field2': |
|
58
|
|
|
return 'value2' |
|
59
|
|
|
|
|
60
|
|
|
raise AttributeError(name) |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
class ObjectPropertyMatcher(object): |
|
64
|
|
|
|
|
65
|
|
|
match_sets = ( |
|
66
|
|
|
("old-style: %s", OnePropertyOldStyle), |
|
67
|
|
|
('new-style: %s', OnePropertyNewStyle), |
|
68
|
|
|
('old-style, overriding: %s', OverridingOldStyle), |
|
69
|
|
|
('new-style, using getattr: %s', OverridingNewStyleGetAttr), |
|
70
|
|
|
('new-style, using getattribute: %s', OverridingNewStyleGetAttribute), |
|
71
|
|
|
) |
|
72
|
|
|
|
|
73
|
|
|
def assert_matches_for_all_types(self, description, matcher): |
|
74
|
|
|
for description_fmt, target_class in self.match_sets: |
|
75
|
|
|
self.assert_matches(description_fmt % description, |
|
76
|
|
|
matcher, |
|
77
|
|
|
target_class()) |
|
78
|
|
|
|
|
79
|
|
|
def assert_does_not_match_for_all_types(self, description, matcher): |
|
80
|
|
|
for description_fmt, target_class in self.match_sets: |
|
81
|
|
|
self.assert_does_not_match(description_fmt % description, |
|
82
|
|
|
matcher, |
|
83
|
|
|
target_class()) |
|
84
|
|
|
|
|
85
|
|
View Code Duplication |
class HasPropertyTest(MatcherTest, ObjectPropertyMatcher): |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
def testHasPropertyWithoutValueMatcher(self): |
|
88
|
|
|
self.assert_matches_for_all_types('has property with name', |
|
89
|
|
|
has_property('field')) |
|
90
|
|
|
|
|
91
|
|
|
def testHasPropertyWithoutValueMatcherNegative(self): |
|
92
|
|
|
self.assert_does_not_match_for_all_types('has property with name', |
|
93
|
|
|
has_property('not_there')) |
|
94
|
|
|
|
|
95
|
|
|
def testHasPropertyWithValueMatcher(self): |
|
96
|
|
|
self.assert_matches_for_all_types('has property with name and value', |
|
97
|
|
|
has_property('field', 'value')) |
|
98
|
|
|
|
|
99
|
|
|
def testHasPropertyWithValueMatcherNegative(self): |
|
100
|
|
|
self.assert_does_not_match_for_all_types('has property with name', |
|
101
|
|
|
has_property('field', 'not the value')) |
|
102
|
|
|
|
|
103
|
|
|
def testDescription(self): |
|
104
|
|
|
self.assert_description("an object with a property 'field' matching ANYTHING", |
|
105
|
|
|
has_property('field')) |
|
106
|
|
|
self.assert_description("an object with a property 'field' matching 'value'", |
|
107
|
|
|
has_property('field', 'value')) |
|
108
|
|
|
|
|
109
|
|
|
def testDescribeMissingProperty(self): |
|
110
|
|
|
self.assert_mismatch_description("<OnePropertyNewStyle> did not have the 'not_there' property", |
|
111
|
|
|
has_property('not_there'), OnePropertyNewStyle()) |
|
112
|
|
|
|
|
113
|
|
|
def testDescribePropertyValueMismatch(self): |
|
114
|
|
|
self.assert_mismatch_description("property 'field' was 'value'", |
|
115
|
|
|
has_property('field', 'another_value'), OnePropertyNewStyle()) |
|
116
|
|
|
|
|
117
|
|
|
def testMismatchDescription(self): |
|
118
|
|
|
self.assert_describe_mismatch("<OnePropertyNewStyle> did not have the 'not_there' property", |
|
119
|
|
|
has_property('not_there'), |
|
120
|
|
|
OnePropertyNewStyle()) |
|
121
|
|
|
|
|
122
|
|
|
def testNoMismatchDescriptionOnMatch(self): |
|
123
|
|
|
self.assert_no_mismatch_description(has_property('field', 'value'), OnePropertyNewStyle()) |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
class HasPropertiesTest(MatcherTest, ObjectPropertyMatcher): |
|
127
|
|
|
|
|
128
|
|
|
def testMatcherCreationRequiresEvenNumberOfPositionalArguments(self): |
|
129
|
|
|
self.assertRaises(ValueError, has_properties, 'a', 'b', 'c') |
|
130
|
|
|
|
|
131
|
|
|
def testMatchesUsingSingleDictionaryArgument(self): |
|
132
|
|
|
# import pdb; pdb.set_trace() |
|
133
|
|
|
self.assert_matches_for_all_types('matches using a single-argument dictionary', |
|
134
|
|
|
has_properties({'field':'value', 'field2': 'value2'})) |
|
135
|
|
|
|
|
136
|
|
|
def testMatchesUsingKeywordArguments(self): |
|
137
|
|
|
self.assert_matches_for_all_types('matches using a kwarg dict', |
|
138
|
|
|
has_properties(field='value', field2='value2')) |
|
139
|
|
|
|
|
140
|
|
|
if __name__ == '__main__': |
|
141
|
|
|
unittest.main() |
|
142
|
|
|
|