Guest User

Untitled

a guest
Jul 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """actions.tests -- tests for action commands.
  3.  
  4. Copyright 2010 David Eyk. All rights reserved.
  5. """
  6. import re
  7. from django.test import TestCase
  8.  
  9. from . import base
  10. from . import actions
  11. from . import social
  12. from . import attributes
  13.  
  14. from things import models as things
  15.  
  16.  
  17. class CommandParserMeta(type):
  18. """Create tests for each command/expected_response pair on the class.
  19. """
  20. slug_cp = re.compile(r'[^a-zA-Z_]+')
  21.  
  22. def __new__(meta, classname, bases, attrs):
  23. klass = super(CommandParserMeta, meta).__new__(meta, classname, bases, attrs)
  24.  
  25. for command, expected_result in klass.command_result:
  26. command_slug = 'test_%s' % meta.slug_cp.sub('_', command)
  27.  
  28. # Build basic parser test
  29. setattr(klass, command_slug, meta.buildBasicParserTest(command, expected_result))
  30.  
  31. return klass
  32.  
  33. @staticmethod
  34. def buildBasicParserTest(command, expected_result):
  35. def test(self):
  36. """Can we parse '%s'?
  37. """ % command
  38. result = sorted(dict(self.action.parse(command)).iteritems())
  39. expected = sorted(expected_result.iteritems())
  40. self.assertEqual(result, expected)
  41. return test
  42.  
  43.  
  44. class CommandParserTest(TestCase):
  45. __metaclass__ = CommandParserMeta
  46.  
  47. action = None
  48.  
  49. def setUp(self):
  50. self.actor = things.Actor.new()
  51.  
  52. command_result = ()
  53.  
  54.  
  55. class LookParserTest(CommandParserTest):
  56. action = actions.Look
  57.  
  58. command_result = (
  59. ('look', dict(command='look', target='here')),
  60. ('l', dict(command='l', target='here')),
  61. ('look me', dict(command='look', target='me')),
  62. )
  63.  
  64.  
  65. class ExamineParserTest(CommandParserTest):
  66. action = actions.Examine
  67.  
  68. command_result = (
  69. ('examine', dict(command='examine', target='here')),
  70. ('ex', dict(command='ex', target='here')),
  71. ('ex me', dict(command='ex', target='me')),
  72. )
  73.  
  74.  
  75. class DigParserTest(CommandParserTest):
  76. action = actions.Dig
  77.  
  78. command_result = (
  79. ('@dig Waterfall', dict(command='@dig', target='Waterfall')),
  80. ('@dig/teleport Waterfall', dict(command='@dig', teleport='teleport', target='Waterfall')),
  81. ('@dig Waterfall = West', dict(command='@dig', target='Waterfall',
  82. outgoing_names=['West'])),
  83. ('@dig Waterfall = West;W', dict(command='@dig', target='Waterfall',
  84. outgoing_names=['West', 'W'])),
  85. ('@dig Waterfall = West;W, East;E', dict(command='@dig', target='Waterfall',
  86. outgoing_names=['West', 'W'], incoming_names=['East', 'E'])),
  87. )
  88.  
  89.  
  90. class SayParserTest(CommandParserTest):
  91. action = social.Say
  92.  
  93. command_result = (
  94. ('say Hello, world!', dict(command='say', utterance="Hello, world!")),
  95. ('"Hello, world!', dict(command='"', utterance="Hello, world!")),
  96. )
  97.  
  98.  
  99. class PoseParserTest(CommandParserTest):
  100. action = social.Pose
  101.  
  102. command_result = (
  103. ('pose drops his hat', dict(command='pose', action="drops his hat")),
  104. (':drops his hat', dict(command=':', action="drops his hat")),
  105. )
  106.  
  107.  
  108. class SpacelessPoseParserTest(CommandParserTest):
  109. action = social.SpacelessPose
  110.  
  111. command_result = (
  112. ("spose 's hat blows away in the wind", dict(command='spose', action="'s hat blows away in the wind")),
  113. (";'s hat blows away in the wind", dict(command=';', action="'s hat blows away in the wind")),
  114. )
  115.  
  116.  
  117. class SetParserTest(CommandParserTest):
  118. action = attributes.Set
  119.  
  120. command_result = (
  121. ("@set desc me = Tall, dark, mysterious.", dict(command='@set', attr='desc', target='me', value="Tall, dark, mysterious.")),
  122. )
  123.  
  124.  
  125. class DescParserTest(CommandParserTest):
  126. action = attributes.Desc
  127.  
  128. command_result = (
  129. ("@desc me = Tall, dark, mysterious.", dict(command='@desc', target='me', desc="Tall, dark, mysterious.")),
  130. )
Add Comment
Please, Sign In to add comment