| 1 |
|
| 2 |
|
| 3 |
""" |
| 4 |
Test custom assertions. |
| 5 |
""" |
| 6 |
|
| 7 |
from sure import expect, assertion, chain, chainproperty |
| 8 |
from sure.magic import is_cpython |
| 9 |
|
| 10 |
|
| 11 |
def test_custom_assertion(): |
| 12 |
"test extending sure with a custom assertion." |
| 13 |
|
| 14 |
class Response(object): |
| 15 |
def __init__(self, return_code): |
| 16 |
self.return_code = return_code |
| 17 |
|
| 18 |
|
| 19 |
@assertion |
| 20 |
def return_code(self, return_code): |
| 21 |
if self.negative: |
| 22 |
assert return_code != self.obj.return_code, "Expected was a return code different from {0}.".format(return_code) |
| 23 |
else: |
| 24 |
assert return_code == self.obj.return_code, "Expected return code is: {0}\nGiven return code was: {1}".format( |
| 25 |
return_code, self.obj.return_code) |
| 26 |
|
| 27 |
return True |
| 28 |
|
| 29 |
|
| 30 |
expect(Response(200)).should.have.return_code(200) |
| 31 |
expect(Response(200)).shouldnt.have.return_code(201) |
| 32 |
|
| 33 |
|
| 34 |
def test_custom_chain_method(): |
| 35 |
"test extending sure with a custom chain method." |
| 36 |
|
| 37 |
class Response(object): |
| 38 |
def __init__(self, headers, return_code): |
| 39 |
self.headers = headers |
| 40 |
self.return_code = return_code |
| 41 |
|
| 42 |
|
| 43 |
@chain |
| 44 |
def header(self, header_name): |
| 45 |
expect(self.obj.headers).should.have.key(header_name) |
| 46 |
return self.obj.headers[header_name] |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
if is_cpython: |
| 54 |
Response({"foo": "bar", "bar": "foo"}, 200).should.have.header("foo").must.be.equal("bar") |
| 55 |
else: |
| 56 |
expect(expect(Response({"foo": "bar", "bar": "foo"}, 200)).should.have.header("foo")).must.be.equal("bar") |
| 57 |
|
| 58 |
|
| 59 |
def test_custom_chain_property(): |
| 60 |
"test extending sure with a custom chain property." |
| 61 |
|
| 62 |
class Response(object): |
| 63 |
magic = 41 |
| 64 |
|
| 65 |
@chainproperty |
| 66 |
def having(self): |
| 67 |
return self |
| 68 |
|
| 69 |
@chainproperty |
| 70 |
def implement(self): |
| 71 |
return self |
| 72 |
|
| 73 |
|
| 74 |
@assertion |
| 75 |
def attribute(self, name): |
| 76 |
has_it = hasattr(self.obj, name) |
| 77 |
if self.negative: |
| 78 |
assert not has_it, "Expected was that object {0} does not have attribute {1}".format( |
| 79 |
self.obj, name) |
| 80 |
else: |
| 81 |
assert has_it, "Expected was that object {0} has attribute {1}".format( |
| 82 |
self.obj, name) |
| 83 |
|
| 84 |
return True |
| 85 |
|
| 86 |
|
| 87 |
expect(Response).having.attribute("magic") |
| 88 |
expect(Response).doesnt.implement.attribute("nomagic") |