def testErrors(self): test_list = [ # 1. Directive within expansion (u'//%PDDM-EXPAND a()\n//%PDDM-BOGUS', 'Ran into directive ("//%PDDM-BOGUS", line 2) while in "//%PDDM-EXPAND a()".' ), (u'//%PDDM-EXPAND a()\n//%PDDM-DEFINE a()\n//%body\n', 'Ran into directive ("//%PDDM-DEFINE", line 2) while in "//%PDDM-EXPAND a()".' ), # 3. Expansion ran off end of file (u'//%PDDM-EXPAND a()\na\nb\n', 'Hit the end of the file while in "//%PDDM-EXPAND a()".'), # 4. Directive within define (u'//%PDDM-DEFINE a()\n//%body\n//%PDDM-BOGUS', 'Ran into directive ("//%PDDM-BOGUS", line 3) while in "//%PDDM-DEFINE a()".' ), (u'//%PDDM-DEFINE a()\n//%body\n//%PDDM-EXPAND-END a()', 'Ran into directive ("//%PDDM-EXPAND-END", line 3) while in "//%PDDM-DEFINE a()".' ), # 6. Directives that shouldn't start sections (u'a\n//%PDDM-DEFINE-END a()\n//a\n', 'Unexpected line 2: "//%PDDM-DEFINE-END a()".'), (u'a\n//%PDDM-EXPAND-END a()\n//a\n', 'Unexpected line 2: "//%PDDM-EXPAND-END a()".'), (u'//%PDDM-BOGUS\n//a\n', 'Unexpected line 1: "//%PDDM-BOGUS".'), ] for idx, (input_str, expected_err) in enumerate(test_list, 1): f = io.StringIO(input_str) try: pddm.SourceFile(f)._ParseFile() 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 testProcessFileWithMacroParseError(self): input_str = """ foo //%PDDM-DEFINE mumble(a_) //%body //%PDDM-DEFINE mumble(x_) //%body2 """ f = io.StringIO(input_str) sf = pddm.SourceFile(f) try: sf.ProcessContent() self.fail('Should throw exception! Test failed to catch macro parsing error.') except pddm.PDDMError as e: self.assertEqual(e.message, 'Attempt to redefine macro: "PDDM-DEFINE mumble(x_)"\n' '...while parsing section that started:\n' ' Line 3: //%PDDM-DEFINE mumble(a_)')
def testProcessFileWithExpandError(self): input_str = u""" foo //%PDDM-DEFINE mumble(a_) //%body //%PDDM-EXPAND foobar(x_) //%PDDM-EXPAND-END """ f = io.StringIO(input_str) sf = pddm.SourceFile(f) try: sf.ProcessContent() self.fail('Should throw exception, entry %d' % idx) except pddm.PDDMError as e: self.assertEqual( e.message, 'No macro named "foobar".\n' '...while expanding "foobar(x_)" from the section that' ' started:\n Line 5: //%PDDM-EXPAND foobar(x_)')
def testBasicParse(self): test_list = [ # 1. no directives ('a\nb\nc', (3,) ), # 2. One define ('a\n//%PDDM-DEFINE foo()\n//%body\nc', (1, 2, 1) ), # 3. Two defines ('a\n//%PDDM-DEFINE foo()\n//%body\n//%PDDM-DEFINE bar()\n//%body2\nc', (1, 4, 1) ), # 4. Two defines with ends ('a\n//%PDDM-DEFINE foo()\n//%body\n//%PDDM-DEFINE-END\n' '//%PDDM-DEFINE bar()\n//%body2\n//%PDDM-DEFINE-END\nc', (1, 6, 1) ), # 5. One expand, one define (that runs to end of file) ('a\n//%PDDM-EXPAND foo()\nbody\n//%PDDM-EXPAND-END\n' '//%PDDM-DEFINE bar()\n//%body2\n', (1, 1, 2) ), # 6. One define ended with an expand. ('a\nb\n//%PDDM-DEFINE bar()\n//%body2\n' '//%PDDM-EXPAND bar()\nbody2\n//%PDDM-EXPAND-END\n', (2, 2, 1) ), # 7. Two expands (one end), one define. ('a\n//%PDDM-EXPAND foo(1)\nbody\n//%PDDM-EXPAND foo(2)\nbody2\n//%PDDM-EXPAND-END\n' '//%PDDM-DEFINE foo()\n//%body2\n', (1, 2, 2) ), ] for idx, (input_str, line_counts) in enumerate(test_list, 1): f = io.StringIO(input_str) sf = pddm.SourceFile(f) sf._ParseFile() self.assertEqual(len(sf._sections), len(line_counts), 'Entry %d -- %d != %d' % (idx, len(sf._sections), len(line_counts))) for idx2, (sec, expected) in enumerate(zip(sf._sections, line_counts), 1): self.assertEqual(sec.num_lines_captured, expected, 'Entry %d, section %d -- %d != %d' % (idx, idx2, sec.num_lines_captured, expected))
def testBasics(self): self.maxDiff = None input_str = u""" //%PDDM-IMPORT-DEFINES ImportFile foo //%PDDM-EXPAND mumble(abc) //%PDDM-EXPAND-END bar //%PDDM-EXPAND mumble(def) //%PDDM-EXPAND mumble(ghi) //%PDDM-EXPAND-END baz //%PDDM-DEFINE mumble(a_) //%a_: getName(a_) """ input_str2 = u""" //%PDDM-DEFINE getName(x_) //%do##x_$u##(int x_); """ expected = u""" //%PDDM-IMPORT-DEFINES ImportFile foo //%PDDM-EXPAND mumble(abc) // This block of code is generated, do not edit it directly. // clang-format off abc: doAbc(int abc); // clang-format on //%PDDM-EXPAND-END mumble(abc) bar //%PDDM-EXPAND mumble(def) // This block of code is generated, do not edit it directly. // clang-format off def: doDef(int def); // clang-format on //%PDDM-EXPAND mumble(ghi) // This block of code is generated, do not edit it directly. // clang-format off ghi: doGhi(int ghi); // clang-format on //%PDDM-EXPAND-END (2 expansions) baz //%PDDM-DEFINE mumble(a_) //%a_: getName(a_) """ expected_stripped = u""" //%PDDM-IMPORT-DEFINES ImportFile foo //%PDDM-EXPAND mumble(abc) //%PDDM-EXPAND-END mumble(abc) bar //%PDDM-EXPAND mumble(def) //%PDDM-EXPAND mumble(ghi) //%PDDM-EXPAND-END (2 expansions) baz //%PDDM-DEFINE mumble(a_) //%a_: getName(a_) """ def _Resolver(name): self.assertEqual(name, 'ImportFile') return io.StringIO(input_str2) f = io.StringIO(input_str) sf = pddm.SourceFile(f, _Resolver) sf.ProcessContent() self.assertEqual(sf.processed_content, expected) # Feed it through and nothing should change. f2 = io.StringIO(sf.processed_content) sf2 = pddm.SourceFile(f2, _Resolver) sf2.ProcessContent() self.assertEqual(sf2.processed_content, expected) self.assertEqual(sf2.processed_content, sf.processed_content) # Test stripping (with the original input and expanded version). f2 = io.StringIO(input_str) sf2 = pddm.SourceFile(f2) sf2.ProcessContent(strip_expansion=True) self.assertEqual(sf2.processed_content, expected_stripped) f2 = io.StringIO(sf.processed_content) sf2 = pddm.SourceFile(f2, _Resolver) sf2.ProcessContent(strip_expansion=True) self.assertEqual(sf2.processed_content, expected_stripped)