def testExpandSimpleMacroErrors(self): f = io.StringIO(u""" PDDM-DEFINE foo(a, b) <a-z> PDDM-DEFINE baz(a) a - a$z """) mc = pddm.MacroCollection(f) test_list = [ # 1. Unknown macro (u'bar()', 'No macro named "bar".'), (u'bar(a)', 'No macro named "bar".'), # 3. Arg mismatch (u'foo()', 'Expected 2 args, got: "foo()".'), (u'foo(a b)', 'Expected 2 args, got: "foo(a b)".'), (u'foo(a,b,c)', 'Expected 2 args, got: "foo(a,b,c)".'), # 6. Unknown option in expansion (u'baz(mumble)', 'Unknown arg option "a$z" while expanding "baz(mumble)".'), ] for idx, (input_str, expected_err) in enumerate(test_list, 1): try: result = mc.Expand(input_str) self.fail('Should throw exception, entry %d' % idx) except pddm.PDDMError as e: self.assertEqual(e.message, expected_err, 'Entry %d failed: %r' % (idx, e))
def testExpandReferences(self): f = io.StringIO(u""" PDDM-DEFINE StartIt() foo(abc, def) foo(ghi, jkl) PDDM-DEFINE foo(a, b) bar(a, int) bar(b, NSString *) PDDM-DEFINE bar(n, t) - (t)n; - (void)set##n$u##:(t)value; """) mc = pddm.MacroCollection(f) expected = """- (int)abc; - (void)setAbc:(int)value; - (NSString *)def; - (void)setDef:(NSString *)value; - (int)ghi; - (void)setGhi:(int)value; - (NSString *)jkl; - (void)setJkl:(NSString *)value; """ self.assertEqual(mc.Expand('StartIt()'), expected)
def testParseOne(self): f = io.StringIO(u"""PDDM-DEFINE foo( ) body""") result = pddm.MacroCollection(f) self.assertEqual(len(result._macros), 1) macro = result._macros.get('foo') self.assertIsNotNone(macro) self.assertEquals(macro.name, 'foo') self.assertEquals(macro.args, tuple()) self.assertEquals(macro.body, 'body')
def testParseBeginIssues(self): test_list = [ # 1. No name ('PDDM-DEFINE\nmumble', 'Failed to parse macro definition: '), # 2. No name (with spaces) ('PDDM-DEFINE \nmumble', 'Failed to parse macro definition: '), # 3. No open paren ('PDDM-DEFINE foo\nmumble', 'Failed to parse macro definition: '), # 4. No close paren ('PDDM-DEFINE foo(\nmumble', 'Failed to parse macro definition: '), # 5. No close paren (with args) ('PDDM-DEFINE foo(a, b\nmumble', 'Failed to parse macro definition: '), # 6. No name before args ('PDDM-DEFINE (a, b)\nmumble', 'Failed to parse macro definition: '), # 7. No name before args ('PDDM-DEFINE foo bar(a, b)\nmumble', 'Failed to parse macro definition: '), # 8. Empty arg name ('PDDM-DEFINE foo(a, ,b)\nmumble', 'Empty arg name in macro definition: '), ('PDDM-DEFINE foo(a,,b)\nmumble', 'Empty arg name in macro definition: '), # 10. Duplicate name ('PDDM-DEFINE foo(a,b,a,c)\nmumble', 'Arg name "a" used more than once in macro definition: '), # 11. Invalid arg name ('PDDM-DEFINE foo(a b,c)\nmumble', 'Invalid arg name "a b" in macro definition: '), ('PDDM-DEFINE foo(a.b,c)\nmumble', 'Invalid arg name "a.b" in macro definition: '), ('PDDM-DEFINE foo(a-b,c)\nmumble', 'Invalid arg name "a-b" in macro definition: '), ('PDDM-DEFINE foo(a,b,c.)\nmumble', 'Invalid arg name "c." in macro definition: '), # 15. Extra stuff after the name ('PDDM-DEFINE foo(a,c) foo\nmumble', 'Failed to parse macro definition: '), ('PDDM-DEFINE foo(a,c) foo)\nmumble', 'Failed to parse macro definition: '), ] for idx, (input_str, expected_prefix) in enumerate(test_list, 1): f = io.StringIO(input_str) try: result = pddm.MacroCollection(f) self.fail('Should throw exception, entry %d' % idx) except pddm.PDDMError as e: self.assertTrue(e.message.startswith(expected_prefix), 'Entry %d failed: %r' % (idx, e))
def testExpandArgOptions(self): f = io.StringIO(""" PDDM-DEFINE bar(a) a-a$S-a$l-a$L-a$u-a$U PDDM-DEFINE-END """) mc = pddm.MacroCollection(f) self.assertEqual(mc.Expand('bar(xYz)'), 'xYz- -xYz-xyz-XYz-XYZ') self.assertEqual(mc.Expand('bar(MnoP)'), 'MnoP- -mnoP-mnop-MnoP-MNOP') # Test empty self.assertEqual(mc.Expand('bar()'), '-----')
def testCatchRecursion(self): f = io.StringIO(""" PDDM-DEFINE foo(a, b) bar(1, a) bar(2, b) PDDM-DEFINE bar(x, y) foo(x, y) """) mc = pddm.MacroCollection(f) try: result = mc.Expand('foo(A,B)') self.fail('Should throw exception! Test failed to catch recursion.') except pddm.PDDMError as e: self.assertEqual(e.message, 'Found macro recursion, invoking "foo(1, A)":\n...while expanding "bar(1, A)".\n...while expanding "foo(A,B)".')
def testParseGeneral(self): # Tests multiple defines, spaces in all places, etc. f = io.StringIO(u""" PDDM-DEFINE noArgs( ) body1 body2 PDDM-DEFINE-END PDDM-DEFINE oneArg(foo) body3 PDDM-DEFINE twoArgs( bar_ , baz ) body4 body5""") result = pddm.MacroCollection(f) self.assertEqual(len(result._macros), 3) macro = result._macros.get('noArgs') self.assertIsNotNone(macro) self.assertEquals(macro.name, 'noArgs') self.assertEquals(macro.args, tuple()) self.assertEquals(macro.body, 'body1\nbody2\n') macro = result._macros.get('oneArg') self.assertIsNotNone(macro) self.assertEquals(macro.name, 'oneArg') self.assertEquals(macro.args, ('foo', )) self.assertEquals(macro.body, 'body3') macro = result._macros.get('twoArgs') self.assertIsNotNone(macro) self.assertEquals(macro.name, 'twoArgs') self.assertEquals(macro.args, ('bar_', 'baz')) self.assertEquals(macro.body, 'body4\nbody5') # Add into existing collection f = io.StringIO(u""" PDDM-DEFINE another(a,b,c) body1 body2""") result.ParseInput(f) self.assertEqual(len(result._macros), 4) macro = result._macros.get('another') self.assertIsNotNone(macro) self.assertEquals(macro.name, 'another') self.assertEquals(macro.args, ('a', 'b', 'c')) self.assertEquals(macro.body, 'body1\nbody2')
def testExpandBasics(self): f = io.StringIO(""" PDDM-DEFINE noArgs( ) body1 body2 PDDM-DEFINE-END PDDM-DEFINE oneArg(a) body3 a PDDM-DEFINE-END PDDM-DEFINE twoArgs(b,c) body4 b c body5 PDDM-DEFINE-END """) mc = pddm.MacroCollection(f) test_list = [ ('noArgs()', 'body1\nbody2\n'), ('oneArg(wee)', 'body3 wee\n'), ('twoArgs(having some, fun)', 'body4 having some fun\nbody5'), # One arg, pass empty. ('oneArg()', 'body3 \n'), # Two args, gets empty in each slot. ('twoArgs(, empty)', 'body4 empty\nbody5'), ('twoArgs(empty, )', 'body4 empty \nbody5'), ('twoArgs(, )', 'body4 \nbody5'), ] for idx, (input_str, expected) in enumerate(test_list, 1): result = mc.Expand(input_str) self.assertEqual(result, expected, 'Entry %d --\n Result: %r\n Expected: %r' % (idx, result, expected))
def testParseDirectiveIssues(self): test_list = [ # Unknown directive ('PDDM-DEFINE foo()\nbody\nPDDM-DEFINED foo\nbaz', 'Hit a line with an unknown directive: '), # End without begin ('PDDM-DEFINE foo()\nbody\nPDDM-DEFINE-END\nPDDM-DEFINE-END\n', 'Got DEFINE-END directive without an active macro: '), # Line not in macro block ('PDDM-DEFINE foo()\nbody\nPDDM-DEFINE-END\nmumble\n', 'Hit a line that wasn\'t a directive and no open macro definition: '), # Redefine macro ('PDDM-DEFINE foo()\nbody\nPDDM-DEFINE foo(a)\nmumble\n', 'Attempt to redefine macro: '), ] for idx, (input_str, expected_prefix) in enumerate(test_list, 1): f = io.StringIO(input_str) try: result = pddm.MacroCollection(f) self.fail('Should throw exception, entry %d' % idx) except pddm.PDDMError as e: self.assertTrue(e.message.startswith(expected_prefix), 'Entry %d failed: %r' % (idx, e))
def testParseEmpty(self): f = io.StringIO(u'') result = pddm.MacroCollection(f) self.assertEqual(len(result._macros), 0)