From b721a38b3930a90cdb0b526dfc9b94c7b0f18cd9 Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Wed, 13 Feb 2013 22:03:56 +0100 Subject: [PATCH] Fix Pyflakes tests for Python 2.5, and add a reporter --- flake8/tests/test_flakes.py | 43 ++++++++++++++++++++++++++++++++----- flake8/tests/test_mccabe.py | 1 + 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/flake8/tests/test_flakes.py b/flake8/tests/test_flakes.py index b007955..79ac253 100644 --- a/flake8/tests/test_flakes.py +++ b/flake8/tests/test_flakes.py @@ -1,8 +1,30 @@ +# -*- coding: utf-8 -*- +import sys + from unittest import TestCase from pyflakes.api import check -code = """ +class FlakesTestReporter(object): + def __init__(self): + self.messages = [] + self.flakes = self.messages.append + + def unexpectedError(self, filename, msg): + self.flakes('[unexpectedError] %s: %s' % (filename, msg)) + + def syntaxError(self, filename, msg, lineno, offset, text): + self.flakes('[syntaxError] %s:%d: %s' % (filename, lineno, msg)) + + +code0 = """ +try: + pass +except ValueError, err: + print(err) +""" + +code1 = """ try: pass except ValueError as err: @@ -48,14 +70,25 @@ except foo.SomeException: class TestFlake(TestCase): def test_exception(self): - for c in (code, code2, code3): - warnings = check(code, '(stdin)') + codes = [code1, code2, code3] + if sys.version_info < (2, 6): + codes[0] = code0 + elif sys.version_info < (3,): + codes.insert(0, code0) + for code in codes: + reporter = FlakesTestReporter() + warnings = check(code, '(stdin)', reporter) + self.assertFalse(reporter.messages) self.assertEqual(warnings, 0) def test_from_import_exception_in_scope(self): - warnings = check(code_from_import_exception, '(stdin)') + reporter = FlakesTestReporter() + warnings = check(code_from_import_exception, '(stdin)', reporter) + self.assertFalse(reporter.messages) self.assertEqual(warnings, 0) def test_import_exception_in_scope(self): - warnings = check(code_import_exception, '(stdin)') + reporter = FlakesTestReporter() + warnings = check(code_import_exception, '(stdin)', reporter) + self.assertFalse(reporter.messages) self.assertEqual(warnings, 0) diff --git a/flake8/tests/test_mccabe.py b/flake8/tests/test_mccabe.py index 1814699..a220c83 100644 --- a/flake8/tests/test_mccabe.py +++ b/flake8/tests/test_mccabe.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- import unittest import sys try: