-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstories_test.py
57 lines (45 loc) · 1.84 KB
/
stories_test.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
#!/usr/bin/env python3
import unittest
import stories
class TemplateTest(unittest.TestCase):
def testFindFields(self):
text = "My {rumpus} ate my {bearclaw}."
correct = {'{rumpus}', '{bearclaw}'}
fields = stories.StoryTemplate(text).fields
self.assertEqual(fields, correct)
def testFindFieldAtStart(self):
text = "{name} likes {name}'s name, which is '{name}'."
correct = {'{name}'}
fields = stories.StoryTemplate(text).fields
self.assertEqual(fields, correct)
def testEmptyField(self):
test = "This has a {} empty field."
with self.assertRaises(stories.ParseError):
stories.StoryTemplate(test)
def testUnclosedField(self):
test = "This has an {unclosed."
with self.assertRaises(stories.ParseError):
stories.StoryTemplate(test)
def testUnopenedField(self):
test = "This has a } busted field."
with self.assertRaises(stories.ParseError):
stories.StoryTemplate(test)
def testReplace(self):
test = "These words {verb} no sense."
fields = {'{verb}': 'balloon'}
self.assertEqual("These words balloon no sense.",
stories.StoryTemplate(test).Populate(fields))
def testHTMLForm(self):
text = "My dog ate my {bearclaw}."
st = stories.StoryTemplate(text)
html = st.HTMLForm(hidden='<input type=hidden name=test value=test>')
self.assertEqual(html, (
'<!DOCTYPE html>\n'
'<title>Story</title>\n'
'<form method=POST>\n'
'<label>bearclaw: <input type=text name="bearclaw"></label><br>\n'
'<input type=hidden name=test value=test>\n'
'<button type=submit>Tell me a story!</button>\n'
'</form>'))
if __name__ == '__main__':
unittest.main()