def verify(self, src, exp, dump=False): ''' Run the verification of a source value against an expected output value. The values are list of strings, each string representing an individual line. An empty list is interpretted as an empty file. And empty string is interpretted as an empty line. ''' # We create one long string for both source and expected values so # that the caller can enumerate each line without adding new lines, # making it easier to see what is being written. srcStr = "\n".join(src) if src: # Add the trailing new line only if there was something in the # "file". srcStr += "\n" expStr = "\n".join(exp) if exp: expStr += "\n" try: l = parseSource(srcStr, self.buf, 0, dump) except AssertionError as ae: self.fail("Internal AssertionError Encountered: %s\n" "Concrete Syntax Tree:\n" "%s\n" % (ae, dumpCst(parser.suite(srcStr),StringIO()).getvalue())) self.assertEqual(l, len(self.buf)) output = "".join(self.buf) self.assertEqual(output, expStr, "Output not quite what was expected:\n" " out: %r\n" " exp: %r\n" "Concrete Syntax Tree:\n" "%s\n" % (output, expStr, dumpCst(parser.suite(srcStr),StringIO()).getvalue()))
def verify(self, src, exp, dump=False): ''' Run the verification of a source value against an expected output value. The values are list of strings, each string representing an individual line. An empty list is interpretted as an empty file. And empty string is interpretted as an empty line. ''' # We create one long string for both source and expected values so # that the caller can enumerate each line without adding new lines, # making it easier to see what is being written. srcStr = "\n".join(src) if src: # Add the trailing new line only if there was something in the # "file". srcStr += "\n" expStr = "\n".join(exp) if exp: expStr += "\n" try: l = parseSource(srcStr, self.buf, 0, dump) except AssertionError as ae: self.fail( "Internal AssertionError Encountered: %s\n" "Concrete Syntax Tree:\n" "%s\n" % (ae, dumpCst(parser.suite(srcStr), StringIO()).getvalue())) self.assertEqual(l, len(self.buf)) output = "".join(self.buf) self.assertEqual( output, expStr, "Output not quite what was expected:\n" " out: %r\n" " exp: %r\n" "Concrete Syntax Tree:\n" "%s\n" % (output, expStr, dumpCst(parser.suite(srcStr), StringIO()).getvalue()))
def testBadStream(self,): x = 0 try: dumpCst(parser.suite("a = 1"), x) except AttributeError as e: pass else: self.fail("Expected a ValueError since we did not give a proper streem")
def testBadStream(self, ): x = 0 try: dumpCst(parser.suite("a = 1"), x) except AttributeError as e: pass else: self.fail( "Expected a ValueError since we did not give a proper streem")
def testGoodStreamIOError(self,): import pprint orig_pprint = pprint.pprint def mockEpipe(obj, stm): e = IOError() e.errno = errno.ENOENT raise e pprint.pprint = mockEpipe try: dumpCst(parser.suite("a = 1"), StringIO()).getvalue() except IOError as e: assert e.errno == errno.ENOENT else: self.fail("Expected IOError raised") finally: pprint.pprint = orig_pprint
def testIssue0009(self): """ Verify dumpCst works on tuples. """ try: from cStringIO import StringIO except ImportError: from io import StringIO out = StringIO() import parser, sys cst = parser.suite("import sys\na = b\n") pycscope.dumpCst(cst.totuple(True), out) output = out.getvalue() if sys.hexversion < 0x03000000: expected = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['import_stmt',\n ['import_name',\n ['NAME', 'import', 1],\n ['dotted_as_names',\n ['dotted_as_name', ['dotted_name', ['NAME', 'sys', 1]]]]]]],\n ['NEWLINE', '', 1]]],\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 2]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 2],\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'b', 2]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 2]]],\n ['NEWLINE', '', 2],\n ['ENDMARKER', '', 2]]\n" else: expected = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['import_stmt',\n ['import_name',\n ['NAME', 'import', 1],\n ['dotted_as_names',\n ['dotted_as_name', ['dotted_name', ['NAME', 'sys', 1]]]]]]],\n ['NEWLINE', '', 1]]],\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist_star_expr',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 2]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 2],\n ['testlist_star_expr',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'b', 2]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 2]]],\n ['NEWLINE', '', 2],\n ['ENDMARKER', '', 2]]\n" print(repr(output)) self.assertEqual(output, expected)
def testGoodStreamIOError(self, ): import pprint orig_pprint = pprint.pprint def mockEpipe(obj, stm): e = IOError() e.errno = errno.ENOENT raise e pprint.pprint = mockEpipe try: dumpCst(parser.suite("a = 1"), StringIO()).getvalue() except IOError as e: assert e.errno == errno.ENOENT else: self.fail("Expected IOError raised") finally: pprint.pprint = orig_pprint
def testGoodStreamBadPipe(self,): import pprint orig_pprint = pprint.pprint def mockEpipe(obj, stm): e = IOError() e.errno = errno.EPIPE raise e pprint.pprint = mockEpipe try: res = dumpCst(parser.suite("a = 1"), StringIO()).getvalue() finally: pprint.pprint = orig_pprint self.assertEquals(res, "")
def testGoodStreamBadPipe(self, ): import pprint orig_pprint = pprint.pprint def mockEpipe(obj, stm): e = IOError() e.errno = errno.EPIPE raise e pprint.pprint = mockEpipe try: res = dumpCst(parser.suite("a = 1"), StringIO()).getvalue() finally: pprint.pprint = orig_pprint self.assertEquals(res, "")
def testGoodStream(self, ): res = dumpCst(parser.suite("a = 1"), StringIO()).getvalue() exp = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 1]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 1],\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NUMBER', '1', 1]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 1]]],\n ['NEWLINE', '', 1],\n ['ENDMARKER', '', 1]]\n" self.assertEquals(res, exp)
def testGoodStream(self,): res = dumpCst(parser.suite("a = 1"), StringIO()).getvalue() exp = "['file_input',\n ['stmt',\n ['simple_stmt',\n ['small_stmt',\n ['expr_stmt',\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NAME', 'a', 1]]]]]]]]]]]]]]]],\n ['EQUAL', '=', 1],\n ['testlist',\n ['test',\n ['or_test',\n ['and_test',\n ['not_test',\n ['comparison',\n ['expr',\n ['xor_expr',\n ['and_expr',\n ['shift_expr',\n ['arith_expr',\n ['term',\n ['factor',\n ['power', ['atom', ['NUMBER', '1', 1]]]]]]]]]]]]]]]]]],\n ['NEWLINE', '', 1]]],\n ['NEWLINE', '', 1],\n ['ENDMARKER', '', 1]]\n" self.assertEquals(res, exp)