Пример #1
0
def test_word_parse():
    """test function to parse keys to user from rax text file
    """
    keys, stripped = madlib.parse(
        "This is an {adjective} and also {adjective} {noun} of a test.")
    assert keys == ['adjective', 'adjective', 'noun']
    assert stripped == 'This is an {} and also {} {} of a test.'
def test_parse():

    prompts, stripped = madlib.parse(
        'It was a {Adjective} and {Adjective} {Noun}')

    assert prompts == ['Adjective', 'Adjective', 'Noun']

    assert stripped == 'It was a {} and {} {}'
Пример #3
0
 def testAlternateLeftOpen(self):
     """TODO: this should fail to parse"""
     seq = madlib.parse("x{y|")
     chooser = lambda p: p[-1]
     self.assertEquals(madlib.expand(seq, chooser=chooser), "xy")
Пример #4
0
 def testAlternateMarkerOutsideOfAlternate(self):
     seq = madlib.parse("foo|bar")
     self.assertEquals(madlib.expand(seq), "foo|bar")
Пример #5
0
 def testCrazyScenario2(self):
     seq = madlib.parse("{{a|{|{x|}b}}foo")
     chooser = lambda p: p[-1]
     self.assertEquals(madlib.expand(seq, chooser=chooser), "bfoo")
Пример #6
0
 def testEmptyString(self):
     seq = madlib.parse("")
     self.assertEquals(madlib.expand(seq), "")
Пример #7
0
 def testNestedAlternates(self):
     seq = madlib.parse("a{x{y|y}z{q}")
     self.assertEquals(madlib.expand(seq), "axyzq")
Пример #8
0
 def testCrazyScenario1(self):
     seq = madlib.parse("{{a|b}|{|grot}}foo")
     chooser = lambda p: p[0]
     self.assertEquals(madlib.expand(seq, chooser=chooser), "afoo")
Пример #9
0
 def testAlternate(self):
     seq = madlib.parse("a{x|x}b")
     self.assertEquals(madlib.expand(seq), "axb")
Пример #10
0
 def testNestedSimpleAlternate(self):
     seq = madlib.parse("a{x{y}z}b")
     self.assertEquals(madlib.expand(seq), "axyzb")
Пример #11
0
 def testAlternateOfEmpty(self):
     seq = madlib.parse("foo{|}bar")
     self.assertEquals(madlib.expand(seq), "foobar")
Пример #12
0
import re
import os
from madlib import read_template, parse, newWords, merge

fillPattern = re.compile(r'{.*?}')
dirname = os.path.dirname(__file__)
writeFile = os.path.join(dirname, '../assets/madlib_complete.txt')
readFile = os.path.join(dirname, '../assets/madlib_template.txt')

print('Greetings! Let\'s have some fun with MadLibs. I will ask you for some words by grammatical family and then place them into a prebuilt story to find some funny results.')

template = read_template(readFile)
fillIns = parse(template)
userInput = newWords(fillIns)
finished = open(writeFile, 'wt')
finished.write(merge(template, userInput))
finished.close()
print(template)
Пример #13
0
 def testSimpleStringWithEmptyAlternate(self):
     seq = madlib.parse("ab{}cd")
     self.assertEquals(madlib.expand(seq), "abcd")
Пример #14
0
 def testSimpleAlternate(self):
     seq = madlib.parse("{x}")
     self.assertEquals(madlib.expand(seq), "x")
Пример #15
0
 def testEmptyAlternative(self):
     seq = madlib.parse("{}")
     self.assertEquals(madlib.expand(seq), "")
Пример #16
0
 def testSimpleString(self):
     seq = madlib.parse("hello")
     self.assertEquals(madlib.expand(seq), "hello")
Пример #17
0
def test_parse():
    """Test whether parse works with simple input."""
    prompts, stripped = madlib.parse(
        'It was a {Adjective} and {Adjective} {Noun}')
    assert prompts == ['Adjective', 'Adjective', 'Noun']
    assert stripped == 'It was a {} and {} {}'