This repository has been archived by the owner on Jan 7, 2019. It is now read-only.
/
tests.py
141 lines (116 loc) · 4.9 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from twisted.internet.defer import inlineCallbacks
from src.utils.test_utils import DottTestCase
from src.daemons.server.commands.parser import CommandParser, ParsedCommand
from src.daemons.server.commands.cmdtable import CommandTable, DuplicateCommandException
from src.daemons.server.commands.command import BaseCommand
class CommandTableTests(DottTestCase):
@inlineCallbacks
def setUp(self):
yield super(CommandTableTests, self).setUp()
self.table = CommandTable()
@inlineCallbacks
def tearDown(self):
yield super(CommandTableTests, self).tearDown()
del self.table
def test_add_and_lookup(self):
"""
Tests some simple success cases. A fake command is added to the
command table, we perform some lookups.
"""
# Create a fake command to add to the command table.
cmd = BaseCommand()
# This is the full name for the command.
cmd.name = 'test'
# The command can also be called with these values.
cmd.aliases = ['t']
# Add command to command table.
self.table.add_command(cmd)
# Perform a name-based lookup for the command. Result should be the
# BaseCommand() instance we created earlier.
self.assertIs(self.table.match_name('test'), cmd)
# Same as above, but this time look for alias matches.
self.assertIs(self.table.match_alias('t'), cmd)
# Now take a step back and create a fake parsed command (from the user).
# This is as if a user typed 'test'.
parsed = ParsedCommand('test', [], [])
# Hand the command off to the command table and ask it to return
# a match, if there is one. This should match the previously created
# test command.
self.assertIs(self.table.lookup_command(parsed), cmd)
# Now make the input something that definitely isn't in the command
# table. The return value should be None, meaning no match.
parsed.command_str = 'invalid'
self.assertEqual(self.table.lookup_command(parsed), None)
def test_add_duplicate_name(self):
"""
Tries to add a duplicate (name) command.
"""
cmd = BaseCommand()
cmd.name = 'test'
self.table.add_command(cmd)
cmd2 = BaseCommand()
cmd2.name = 'test'
# This is a duplicate, should raise exception.
self.assertRaises(DuplicateCommandException, self.table.add_command, cmd2)
def test_add_duplicate_alias(self):
"""
Tries to add a duplicate (alias) command.
"""
cmd = BaseCommand()
cmd.name = 'cmd'
cmd.aliases = ['l', 't']
self.table.add_command(cmd)
cmd2 = BaseCommand()
cmd2.name = 'cmd2'
cmd2.aliases = ['g', 't']
# This is a duplicate, should raise exception.
self.assertRaises(DuplicateCommandException, self.table.add_command, cmd2)
class CommandParserTests(DottTestCase):
@inlineCallbacks
def setUp(self):
yield super(CommandParserTests, self).setUp()
self.parser = CommandParser()
@inlineCallbacks
def tearDown(self):
yield super(CommandParserTests, self).tearDown()
del self.parser
def test_simple_command(self):
"""
Parses a simple command with no args.
"""
parsed = self.parser.parse('look')
self.assertIsInstance(parsed, ParsedCommand)
self.assertEquals(parsed.command_str, 'look')
self.assertEquals(parsed.arguments, set([]))
self.assertEquals(parsed.switches, set([]))
def test_command_with_arguments(self):
"""
Throw some arguments in as well.
"""
parsed = self.parser.parse('look ship')
self.assertEquals(parsed.command_str, 'look')
self.assertEquals(parsed.arguments, {'ship'})
self.assertEquals(parsed.switches, set([]))
parsed = self.parser.parse('look ship hi')
self.assertEquals(parsed.arguments, {'ship', 'hi'})
def test_command_with_switches_and_arguments(self):
"""
The whole she-bang.
"""
parsed = self.parser.parse('look/quiet ship')
self.assertEquals(parsed.command_str, 'look')
self.assertEquals(parsed.arguments, {'ship'})
self.assertEquals(parsed.switches, {'quiet'})
def test_poses(self):
"""
Poses have some shortcuts that are handled differently. Test those
here, for the MUX/MUSH/MOO folks.
"""
parsed = self.parser.parse(':taunts you.')
self.assertEquals(parsed.command_str, 'emote')
self.assertEquals(parsed.arguments, {'taunts', 'you.'})
self.assertEquals(parsed.switches, set([]))
parsed = self.parser.parse(";'s face is weird.")
self.assertEquals(parsed.command_str, 'emote')
self.assertEquals(parsed.arguments, {"'s", 'face', 'is', 'weird.'})
self.assertEquals(parsed.switches, {'nospace'})