Skip to content

Commit

Permalink
Trac #27431: py3: fix misc/unittest
Browse files Browse the repository at this point in the history
where the empty-set-of-Exception was done using None

plus full pep8 cleanup of the modified file

URL: https://trac.sagemath.org/27431
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Mar 7, 2019
2 parents 3aa99d3 + c048c3a commit 769697a
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/sage/misc/sage_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
Unit testing for Sage objects
"""

#*****************************************************************************
# ****************************************************************************
# Copyright (C) 2009 Nicolas M. Thiery <nthiery at users.sf.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
# https://www.gnu.org/licenses/
# ****************************************************************************

import unittest
import sys
Expand Down Expand Up @@ -152,7 +152,7 @@ def __init__(self, instance):
Test suite for Integer Ring
"""
from sage.structure.sage_object import SageObject
if not isinstance(instance, (SageObject,PythonObjectWithTests)):
if not isinstance(instance, (SageObject, PythonObjectWithTests)):
instance = PythonObjectWithTests(instance)
self._instance = instance

Expand All @@ -163,10 +163,10 @@ def __repr__(self):
sage: TestSuite(ZZ)
Test suite for Integer Ring
"""
return "Test suite for %s"%self._instance
return "Test suite for %s" % self._instance


def run(self, category = None, skip = [], catch = True, raise_on_failure = False, **options):
def run(self, category=None, skip=[], catch=True, raise_on_failure=False,
**options):
"""
Run all the tests from this test suite:
Expand Down Expand Up @@ -281,19 +281,19 @@ def _test_b(self, tester): tester.fail()
skip = tuple(skip)

# The class of exceptions that will be caught and reported;
# other exceptions will get through. None catches nothing.
catch_exception = Exception if catch else None
# other exceptions will get through. () catches nothing.
catch_exception = Exception if catch else ()

tester = instance_tester(self._instance, **options)
failed = []
for method_name in dir(self._instance):
if method_name[0:6] == "_test_" and method_name not in skip:
# TODO: improve pretty printing
# could use the doc string of the test method?
tester.info(tester._prefix+"running .%s() . . ."%method_name, newline = False)
tester.info(tester._prefix + "running .%s() . . ." % method_name, newline=False)
test_method = getattr(self._instance, method_name)
try:
test_method(tester = tester)
test_method(tester=tester)
tester.info(" pass")
except catch_exception as e:
failed.append(method_name)
Expand All @@ -302,26 +302,28 @@ def _test_b(self, tester): tester.fail()
# which has already reported the details of
# that failure
if not tester._verbose:
print(tester._prefix+"Failure in {}".format(method_name))
print(tester._prefix + "Failure in {}".format(method_name))
else:
if tester._verbose:
tester.info(" fail")
else:
print(tester._prefix+"Failure in {}:".format(method_name))
print(tester._prefix + "Failure in {}:".format(method_name))
s = traceback.format_exc()
print(tester._prefix + s.strip().replace("\n", "\n"+tester._prefix))
print(tester._prefix + s.strip().replace("\n", "\n" + tester._prefix))
print(tester._prefix + "-" * 60)
if len(failed) > 0:
print(tester._prefix+"The following tests failed: {}".format(", ".join(failed)))
if failed:
print(tester._prefix + "The following tests failed: {}".format(", ".join(failed)))
if raise_on_failure:
raise TestSuiteFailure


class TestSuiteFailure(AssertionError):
pass

def instance_tester(instance, tester = None, **options):

def instance_tester(instance, tester=None, **options):
"""
Returns a gadget attached to ``instance`` providing testing utilities.
Return a gadget attached to ``instance`` providing testing utilities.
EXAMPLES::
Expand Down Expand Up @@ -356,10 +358,11 @@ def instance_tester(instance, tester = None, **options):
if tester is None:
return InstanceTester(instance, **options)
else:
assert len(options) == 0
assert not options
assert tester._instance is instance
return tester


class InstanceTester(unittest.TestCase):
"""
A gadget attached to an instance providing it with testing utilities.
Expand All @@ -383,7 +386,8 @@ class InstanceTester(unittest.TestCase):
# all that much anyways)
longMessage = False

def __init__(self, instance, elements = None, verbose = False, prefix = "", max_runs = 4096, max_samples = None, **options):
def __init__(self, instance, elements=None, verbose=False, prefix="",
max_runs=4096, max_samples=None, **options):
"""
A gadget attached to an instance providing it with testing utilities.
Expand Down Expand Up @@ -420,9 +424,9 @@ def runTest(self):
"""
pass

def info(self, message, newline = True):
def info(self, message, newline=True):
"""
Displays user information
Display user information
EXAMPLES::
Expand All @@ -442,7 +446,7 @@ def info(self, message, newline = True):
"""
if self._verbose:
if newline:
sys.stdout.write(message+"\n")
sys.stdout.write(message + "\n")
else:
sys.stdout.write(message)
sys.stdout.flush()
Expand All @@ -456,14 +460,14 @@ def __repr__(self):
Testing utilities for Integer Ring
"""
return "Testing utilities for %s"%self._instance

return "Testing utilities for %s" % self._instance

def some_elements(self, S=None, repeat=None):
"""
Returns a list (or iterable) of elements of the instance on which
the tests should be run. This is only meaningful for container
objects like parents.
Return a list (or iterable) of elements of the instance on which
the tests should be run.
This is only meaningful for container objects like parents.
INPUT:
Expand Down Expand Up @@ -570,6 +574,7 @@ def some_elements(self, S=None, repeat=None):
from sage.misc.misc import some_tuples
return list(some_tuples(S, repeat, self._max_runs, self._max_samples))


class PythonObjectWithTests(object):
"""
Utility class for running basis tests on a plain Python object
Expand Down

0 comments on commit 769697a

Please sign in to comment.