def test_backtraces(self): el = ErrorLogger() expected_output = '''\ a shorter backtrace [b.mwel: line 2, column 5; via line 3, column 5] a long backtrace [a.mwel: line 1, column 4; via b.mwel: line 2, column 5; via line 3, column 6] no backtrace [line 4, column 6] another backtrace to a file [a.mwel: line 1, column 3; via b.mwel: line 2, column 4] backtrace to a file [a.mwel: line 1, column 3; via b.mwel: line 3, column 4] ''' el('backtrace to a file', lineno = (1, 3), colno = (3, 4), filename = ('a.mwel', 'b.mwel')) el('no backtrace', lineno=4, colno=6) el('a long backtrace', lineno = (1, 2, 3), colno = (4, 5, 6), filename = ('a.mwel', 'b.mwel', '')) el('a shorter backtrace', lineno = (2, 3), colno = (5, 5), filename = ('b.mwel', '')) el('another backtrace to a file', lineno = (1, 2), colno = (3, 4), filename = ('a.mwel', 'b.mwel')) file = io.StringIO() el.print_errors(file) self.assertEqual(expected_output, file.getvalue())
def test_basic(self): el = ErrorLogger() expected_output = '''\ negative lineno, no colno zero lineno, no colno positive lineno, negative colno positive lineno, zero colno positive lineno, positive colno [line 1, column 1] positive lineno, no colno positive lineno, positive colno [line 15, column 9] positive lineno, positive colno [line 20, column 8] positive lineno, positive colno [line 20, column 10] no lineno, no colno ''' el('no lineno, no colno', token='?') el('negative lineno, no colno', lineno=-1) el('zero lineno, no colno', lineno=0) el('positive lineno, no colno', lineno=1) el('positive lineno, negative colno', lineno=1, colno=-1) el('positive lineno, zero colno', lineno=1, colno=0) el('positive lineno, positive colno', lineno=1, colno=1) el('positive lineno, positive colno', lineno=20, colno=10) el('positive lineno, positive colno', lineno=15, colno=9) el('positive lineno, positive colno', lineno=20, colno=8) file = io.StringIO() el.print_errors(file) self.assertEqual(expected_output, file.getvalue())
class ErrorLoggerMixin(object): def setUp(self): super(ErrorLoggerMixin, self).setUp() self.error_logger = ErrorLogger() def with_error_check(self, func): @contextmanager def func_wrapper(*args, **kwargs): yield func(*args, **kwargs) self.assertNoErrors() return func_wrapper def assertNoErrors(self): if self.error_logger.errors: msg = io.StringIO() msg.write('input contained unexpected errors:\n') self.error_logger.print_errors(msg) self.fail(msg.getvalue()) def assertError(self, msg=None, token=None, lineno=None, colno=None, filename=''): if not self.error_logger.errors: self.fail('expected error was not detected') e = self.error_logger.errors.popleft() self.assertEqual(5, len(e)) if msg: self.assertEqual(msg, e.msg) elif token: self.assertEqual('Invalid syntax at %r' % str(token), e.msg) if token: self.assertEqual(token, e.token) if lineno is not None: self.assertEqual(lineno, e.lineno) if colno is not None: self.assertEqual(colno, e.colno) self.assertEqual(filename, e.filename)
def test_filenames(self): el = ErrorLogger() expected_output = '''\ no file 1 [line 1, column 1] no file 2 [line 2, column 2] no file 3 [line 8, column 8] file1 1 [file1.mwel: line 5, column 5] file1 2 [file1.mwel: line 6, column 6] file1 3 [file1.mwel] another file [another_file: line 100, column 100] file2 1 [file2: line 3, column 3] file2 2 [file2: line 4, column 4] file2 3 [file2: line 7, column 7] ''' el('no file 1', lineno=1, colno=1) el('no file 2', lineno=2, colno=2) with el.filename('dir1/file2'): el('file2 1', lineno=3, colno=3) el('file2 2', lineno=4, colno=4) with el.filename('/dir2/dir3/file1.mwel'): el('file1 1', lineno=5, colno=5) el('file1 2', lineno=6, colno=6) el('another file', lineno=100, colno=100, filename='another_file') el('file1 3') self.assertEqual('/dir2/dir3/file1.mwel', el.current_filename) el('file2 3', lineno=7, colno=7) self.assertEqual('dir1/file2', el.current_filename) el('no file 3', lineno=8, colno=8) self.assertEqual('', el.current_filename) file = io.StringIO() el.print_errors(file) self.assertEqual(expected_output, file.getvalue())
def setUp(self): super(ErrorLoggerMixin, self).setUp() self.error_logger = ErrorLogger()