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 {} {}'
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")
def testAlternateMarkerOutsideOfAlternate(self): seq = madlib.parse("foo|bar") self.assertEquals(madlib.expand(seq), "foo|bar")
def testCrazyScenario2(self): seq = madlib.parse("{{a|{|{x|}b}}foo") chooser = lambda p: p[-1] self.assertEquals(madlib.expand(seq, chooser=chooser), "bfoo")
def testEmptyString(self): seq = madlib.parse("") self.assertEquals(madlib.expand(seq), "")
def testNestedAlternates(self): seq = madlib.parse("a{x{y|y}z{q}") self.assertEquals(madlib.expand(seq), "axyzq")
def testCrazyScenario1(self): seq = madlib.parse("{{a|b}|{|grot}}foo") chooser = lambda p: p[0] self.assertEquals(madlib.expand(seq, chooser=chooser), "afoo")
def testAlternate(self): seq = madlib.parse("a{x|x}b") self.assertEquals(madlib.expand(seq), "axb")
def testNestedSimpleAlternate(self): seq = madlib.parse("a{x{y}z}b") self.assertEquals(madlib.expand(seq), "axyzb")
def testAlternateOfEmpty(self): seq = madlib.parse("foo{|}bar") self.assertEquals(madlib.expand(seq), "foobar")
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)
def testSimpleStringWithEmptyAlternate(self): seq = madlib.parse("ab{}cd") self.assertEquals(madlib.expand(seq), "abcd")
def testSimpleAlternate(self): seq = madlib.parse("{x}") self.assertEquals(madlib.expand(seq), "x")
def testEmptyAlternative(self): seq = madlib.parse("{}") self.assertEquals(madlib.expand(seq), "")
def testSimpleString(self): seq = madlib.parse("hello") self.assertEquals(madlib.expand(seq), "hello")
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 {} {}'