1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
if __name__ == '__main__':
import sys
sys.path.insert(0, '..')
sys.path.insert(0, '../..')
import sys
print '\n'.join(sys.path)
from hamcrest.library.object.hasproperty import *
from hamcrest.core.core.isequal import equal_to
from hamcrest_unit_test.matcher_test import MatcherTest
import unittest
__author__ = "Chris Rose"
__copyright__ = "Copyright 2011 hamcrest.org"
__license__ = "BSD, see License.txt"
class OnePropertyOldStyle:
field = 'value'
class OnePropertyNewStyle(object):
field = 'value'
def __repr__(self):
return 'OnePropertyNewStyle'
def __str__(self):
return repr(self)
class OverridingOldStyle:
def __getattr__(self, name):
if name != 'field':
raise AttributeError(name)
return 'value'
class OverridingNewStyleGetAttr(object):
def __getattr__(self, name):
if name != 'field':
raise AttributeError(name)
return 'value'
class OverridingNewStyleGetAttribute(object):
def __getattr__(self, name):
if name != 'field':
raise AttributeError(name)
return 'value'
class HasPropertyTest(MatcherTest):
def testHasPropertyWithoutValueMatcher(self):
self.assert_matches('old-style direct',
has_property('field'), OnePropertyOldStyle())
self.assert_matches('old-style direct',
has_property('field'), OnePropertyNewStyle())
self.assert_matches('old-style direct',
has_property('field'), OverridingOldStyle())
self.assert_matches('old-style direct',
has_property('field'), OverridingNewStyleGetAttr())
self.assert_matches('old-style direct',
has_property('field'), OverridingNewStyleGetAttribute())
def testHasPropertyWithoutValueMatcherNegative(self):
self.assert_does_not_match('old-style direct',
has_property('not_there'), OnePropertyOldStyle())
self.assert_does_not_match('old-style direct',
has_property('not_there'), OnePropertyNewStyle())
self.assert_does_not_match('old-style direct',
has_property('not_there'), OverridingOldStyle())
self.assert_does_not_match('old-style direct',
has_property('not_there'), OverridingNewStyleGetAttr())
self.assert_does_not_match('old-style direct',
has_property('not_there'), OverridingNewStyleGetAttribute())
def testHasPropertyWithValueMatcher(self):
self.assert_matches('old-style direct',
has_property('field', 'value'), OnePropertyOldStyle())
self.assert_matches('old-style direct',
has_property('field', 'value'), OnePropertyNewStyle())
self.assert_matches('old-style direct',
has_property('field', 'value'), OverridingOldStyle())
self.assert_matches('old-style direct',
has_property('field', 'value'), OverridingNewStyleGetAttr())
self.assert_matches('old-style direct',
has_property('field', 'value'), OverridingNewStyleGetAttribute())
def testHasPropertyWithValueMatcherNegative(self):
self.assert_does_not_match('old-style direct',
has_property('field', 'not the value'), OnePropertyOldStyle())
self.assert_does_not_match('old-style direct',
has_property('field', 'not the value'), OnePropertyNewStyle())
self.assert_does_not_match('old-style direct',
has_property('field', 'not the value'), OverridingOldStyle())
self.assert_does_not_match('old-style direct',
has_property('field', 'not the value'), OverridingNewStyleGetAttr())
self.assert_does_not_match('old-style direct',
has_property('field', 'not the value'), OverridingNewStyleGetAttribute())
def testDescription(self):
self.assert_description("an object with a property 'field' matching ANYTHING",
has_property('field'))
self.assert_description("an object with a property 'field' matching 'value'",
has_property('field', 'value'))
def testDescribeMissingProperty(self):
self.assert_mismatch_description("<OnePropertyNewStyle> did not have the 'not_there' property",
has_property('not_there'), OnePropertyNewStyle())
def testDescribePropertyValueMismatch(self):
self.assert_mismatch_description("property 'field' was 'value'",
has_property('field', 'another_value'), OnePropertyNewStyle())
def testMismatchDescription(self):
self.assert_describe_mismatch("<OnePropertyNewStyle> did not have the 'not_there' property",
has_property('not_there'),
OnePropertyNewStyle())
def testNoMismatchDescriptionOnMatch(self):
self.assert_no_mismatch_description(has_property('field', 'value'), OnePropertyNewStyle())
if __name__ == '__main__':
unittest.main()
|