Mercurial > sjsbox
changeset 4:0203cae3947f
Added Bunch, improved tests
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Mon, 31 May 2010 10:23:11 -0700 |
parents | b935781e3f89 |
children | ebeab25bca50 |
files | sjsbox/bunch.py tests/test_server.py |
diffstat | 2 files changed, 55 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sjsbox/bunch.py Mon May 31 10:23:11 2010 -0700 @@ -0,0 +1,30 @@ +# Taken from Paver's paver.options module. + +class Bunch(dict): + """A dictionary that provides attribute-style access.""" + + def __repr__(self): + keys = self.keys() + keys.sort() + args = ', '.join(['%s=%r' % (key, self[key]) for key in keys]) + return '%s(%s)' % (self.__class__.__name__, args) + + def __getitem__(self, key): + item = dict.__getitem__(self, key) + if callable(item): + return item() + return item + + def __getattr__(self, name): + try: + return self[name] + except KeyError: + raise AttributeError(name) + + __setattr__ = dict.__setitem__ + + def __delattr__(self, name): + try: + del self[name] + except KeyError: + raise AttributeError(name)
--- a/tests/test_server.py Mon May 31 09:35:42 2010 -0700 +++ b/tests/test_server.py Mon May 31 10:23:11 2010 -0700 @@ -1,13 +1,30 @@ +import unittest + import sjsbox.server +from sjsbox.bunch import Bunch + +class ServerTests(unittest.TestCase): + def test_boxes(self): + foo = Bunch( + mtime = 0, + name = 'foo.js', + contents = 'function handle() { return 404; }' + ) -def test_boxes(): - class MockFile(object): - mtime = 0 - name = 'foo.js' - contents = 'function handle() { return 404; }' - - boxes = sjsbox.server.Boxes([MockFile()]) - boxes.shutdown() + boxes = sjsbox.server.Boxes([foo]) + try: + self.assertEqual(boxes['foo'].handle('GET', '/'), + 404) + boxes.update() + self.assertEqual(boxes['foo'].handle('GET', '/'), + 404) + foo.mtime = 1 + foo.contents = 'function handle() { return "yo"; }'; + boxes.update() + self.assertEqual(boxes['foo'].handle('GET', '/'), + u'yo') + finally: + boxes.shutdown() def test_app(): sjsbox.server.App(None)