twisted/klein — pull request #45 by exarkun (2014-01-24 15:33:27+00:00)
Title: Comparable Klein and KleinResource

For unit testing my Klein-using application, it seems it would be very useful to be able to compare KleinResource instances. For example, here is how I was previously testing the anonymous case of my `IRealm` implementation:

```
def assertIsAnonymousResource(self, resource):
"""
Assert that C{resource} is the object that represents the v2
anonymous avatar.
"""
# TODO: It's hard to make a good assertion in this test. What
# exactly should we assert about it? The idea here is that we
# at least know that the anonymous avatar supports the
# "versions" endpoint so maybe if we get a 200 response back
# from an attempt to render that, we have the right object.
request = dummyRequest(b"GET", b"/versions", Headers(), b"")
render(resource, request)
self.assertEqual(OK, request._code)
```

With equality on KleinResource, I can test it like this instead:

```
def assertIsAnonymousResource(self, resource):
"""
Assert that C{resource} is the object that represents the v2 anonymous
avatar.
"""
self.assertEqual(
_AnonymousUserV2(versionService=self.versionService).app.resource(),
resource)
```

I much prefer the latter.

A potential downside is that Klein is full of magic and the semantics of equality are therefore somewhat magical. I suppose it's possible people will be surprised by the behavior this branch implements. Documenting the behavior might mitigate this somewhat but I didn't notice a `doc` directory in the repository and didn't feel like starting one.