Created
December 12, 2010 21:47
-
-
Save eykd/738365 to your computer and use it in GitHub Desktop.
An example of using metaclasses to enable declarative tests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""actions.tests -- tests for action commands. | |
Copyright 2010 David Eyk. All rights reserved. | |
""" | |
import re | |
from django.test import TestCase | |
from . import base | |
from . import actions | |
from . import social | |
from . import attributes | |
from things import models as things | |
class CommandParserMeta(type): | |
"""Create tests for each command/expected_response pair on the class. | |
""" | |
slug_cp = re.compile(r'[^a-zA-Z_]+') | |
def __new__(meta, classname, bases, attrs): | |
klass = super(CommandParserMeta, meta).__new__(meta, classname, bases, attrs) | |
for command, expected_result in klass.command_result: | |
command_slug = 'test_%s' % meta.slug_cp.sub('_', command) | |
# Build basic parser test | |
setattr(klass, command_slug, meta.buildBasicParserTest(command, expected_result)) | |
return klass | |
@staticmethod | |
def buildBasicParserTest(command, expected_result): | |
def test(self): | |
"""Can we parse '%s'? | |
""" % command | |
result = sorted(dict(self.action.parse(command)).iteritems()) | |
expected = sorted(expected_result.iteritems()) | |
self.assertEqual(result, expected) | |
return test | |
class CommandParserTest(TestCase): | |
__metaclass__ = CommandParserMeta | |
action = None | |
def setUp(self): | |
self.actor = things.Actor.new() | |
command_result = () | |
class LookParserTest(CommandParserTest): | |
action = actions.Look | |
command_result = ( | |
('look', dict(command='look', target='here')), | |
('l', dict(command='l', target='here')), | |
('look me', dict(command='look', target='me')), | |
) | |
class ExamineParserTest(CommandParserTest): | |
action = actions.Examine | |
command_result = ( | |
('examine', dict(command='examine', target='here')), | |
('ex', dict(command='ex', target='here')), | |
('ex me', dict(command='ex', target='me')), | |
) | |
class DigParserTest(CommandParserTest): | |
action = actions.Dig | |
command_result = ( | |
('@dig Waterfall', dict(command='@dig', target='Waterfall')), | |
('@dig/teleport Waterfall', dict(command='@dig', teleport='teleport', target='Waterfall')), | |
('@dig Waterfall = West', dict(command='@dig', target='Waterfall', | |
outgoing_names=['West'])), | |
('@dig Waterfall = West;W', dict(command='@dig', target='Waterfall', | |
outgoing_names=['West', 'W'])), | |
('@dig Waterfall = West;W, East;E', dict(command='@dig', target='Waterfall', | |
outgoing_names=['West', 'W'], incoming_names=['East', 'E'])), | |
) | |
class SayParserTest(CommandParserTest): | |
action = social.Say | |
command_result = ( | |
('say Hello, world!', dict(command='say', utterance="Hello, world!")), | |
('"Hello, world!', dict(command='"', utterance="Hello, world!")), | |
) | |
class PoseParserTest(CommandParserTest): | |
action = social.Pose | |
command_result = ( | |
('pose drops his hat', dict(command='pose', action="drops his hat")), | |
(':drops his hat', dict(command=':', action="drops his hat")), | |
) | |
class SpacelessPoseParserTest(CommandParserTest): | |
action = social.SpacelessPose | |
command_result = ( | |
("spose 's hat blows away in the wind", dict(command='spose', action="'s hat blows away in the wind")), | |
(";'s hat blows away in the wind", dict(command=';', action="'s hat blows away in the wind")), | |
) | |
class SetParserTest(CommandParserTest): | |
action = attributes.Set | |
command_result = ( | |
("@set desc me = Tall, dark, mysterious.", dict(command='@set', attr='desc', target='me', value="Tall, dark, mysterious.")), | |
) | |
class DescParserTest(CommandParserTest): | |
action = attributes.Desc | |
command_result = ( | |
("@desc me = Tall, dark, mysterious.", dict(command='@desc', target='me', desc="Tall, dark, mysterious.")), | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment