Skip to content

Commit

Permalink
Splitting testing functions off sage_helper.py into testing.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
unhyperbolic committed Jul 27, 2024
1 parent bd2ab00 commit 89a9bc5
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 143 deletions.
8 changes: 4 additions & 4 deletions python/exterior_to_link/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
def run_doctests(verbose=False, print_info=True):
globs = {'Manifold': snappy.Manifold,
'Triangulation': snappy.Triangulation}
results = snappy.sage_helper.doctest_modules(modules,
verbose=verbose,
extraglobs=globs,
print_info=print_info)
results = snappy.testing.doctest_modules(modules,
verbose=verbose,
extraglobs=globs,
print_info=print_info)
return results


Expand Down
3 changes: 2 additions & 1 deletion python/ptolemy/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from snappy.ptolemy.processFileBase import get_manifold
from snappy.ptolemy import __path__ as ptolemy_paths
from snappy.ptolemy.coordinates import PtolemyCannotBeCheckedError
from snappy.sage_helper import _within_sage, doctest_modules
from snappy.sage_helper import _within_sage
from snappy.testing import doctest_modules
from snappy.pari import pari
import bz2
import os
Expand Down
134 changes: 2 additions & 132 deletions python/sage_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@
_within_sage = True
except ImportError:
_within_sage = False
import decorator

import sys
import doctest
import re
import types

from .numeric_output_checker import NumericOutputChecker


class SageNotAvailable(Exception):
pass


if _within_sage:
def sage_method(function):
function._sage_method = True
Expand All @@ -41,13 +31,14 @@ def sage_method(function):
from sage.rings.complex_number import create_ComplexNumber

else:
import decorator

def _sage_method(function, *args, **kw):
raise SageNotAvailable('Sorry, this feature requires using SnapPy inside Sage.')

def sage_method(function):
return decorator.decorator(_sage_method, function)


# Not currently used, but could be exploited by an interpreter to hide
# sage_methods when in plain Python.

Expand All @@ -61,124 +52,3 @@ def sage_methods(obj):
except AttributeError:
pass
return ans


# Used for doctesting
_gui_status = {}

try:
from snappy.gui import Tk_
_gui_status['tk'] = True
except ImportError:
_gui_status['tk'] = False
if _gui_status['tk']:
try:
import snappy.CyOpenGL
_gui_status['cyopengl'] = True
except:
_gui_status['cyopengl'] = False
else:
_gui_status['cyopengl'] = False
_gui_status['fake_root'] = False


def cyopengl_works():
if not _gui_status['cyopengl']:
return False
# if we are running the tests from the snappy app the default root will
# already exist -- it will be the tkterminal window. Otherwise, we open
# a root window here to serve as the master of all of the GUI windows
# which get created during testing.
if _gui_status['tk'] and not Tk_._default_root:
try:
root = Tk_.Tk()
if sys.platform not in ('linux', 'linux2'):
root.withdraw()
except:
# tkinter loads OK but is not able to get a display.
_gui_status['tk'] = _gui_status['cyopengl'] = False
return _gui_status['cyopengl']


def tk_root():
if _gui_status['tk']:
return Tk_._default_root
else:
return None


def root_is_fake():
return _gui_status['fake_root']


class DocTestParser(doctest.DocTestParser):
_use_cyopengl_initialized = False
_use_cyopengl = False
use_modernopengl = True
use_sage = False

def parse(self, string, name='<string>'):
string = re.subn(
r'#doctest: \+CYOPENGL',
'' if DocTestParser._use_cyopengl else '#doctest: +SKIP',
string)[0]

string = re.subn(
r'#doctest: \+CYMODERNOPENGL',
(''
if (DocTestParser._use_cyopengl and
DocTestParser.use_modernopengl)
else '#doctest: +SKIP'),
string)[0]

if DocTestParser.use_sage:
string = re.subn(r'(\n\s*)sage:|(\A\s*)sage:',
r'\g<1>>>>',
string)[0]
return doctest.DocTestParser.parse(self, string, name)


DocTestParser.use_sage = _within_sage

if _within_sage:
globs = {'PSL': sage.all.PSL, 'BraidGroup': sage.all.BraidGroup}
else:
globs = {}


def print_results(module, results):
root = tk_root()
# Platform specific hacks to make running the tests work.
if root and not root_is_fake():
if sys.platform in ('linux', 'linux2'):
root.deiconify()
root.update_idletasks()
else:
root.update()
print(module.__name__ + ':')
print(' %s failures out of %s tests.' % (results.failed,
results.attempted))


def doctest_modules(modules, verbose=False, print_info=True, extraglobs={}):
finder = doctest.DocTestFinder(parser=DocTestParser())
# full_extraglobals = dict(globs.items() + extraglobs.items())
full_extraglobals = globs.copy()
full_extraglobals.update(extraglobs)
failed, attempted = 0, 0
for module in modules:
if isinstance(module, types.ModuleType):
runner = doctest.DocTestRunner(checker=NumericOutputChecker(), verbose=verbose)
for test in finder.find(module, extraglobs=full_extraglobals):
runner.run(test)
result = runner.summarize()
else:
result = module(verbose=verbose)
failed += result.failed
attempted += result.attempted
if print_info:
print_results(module, result)

if print_info:
print('\nAll doctests:\n %s failures out of %s tests.' % (failed, attempted))
return doctest.TestResults(failed, attempted)
2 changes: 1 addition & 1 deletion python/snap/character_varieties.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def total_answer_length(I):


if __name__ == "__main__":
from snappy.sage_helper import _within_sage, doctest_modules
from snappy.testing import doctest_modules
import sys
current_module = sys.modules[__name__]
doctest_modules([current_module])
3 changes: 2 additions & 1 deletion python/snap/test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..sage_helper import _within_sage, doctest_modules
from ..sage_helper import _within_sage
from ..testing import doctest_modules
from ..pari import pari
import snappy
import snappy.snap as snap
Expand Down
7 changes: 4 additions & 3 deletions python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import snappy.exterior_to_link.test
import snappy.pari

from snappy.sage_helper import (_within_sage, doctest_modules, cyopengl_works,
tk_root, root_is_fake, DocTestParser)
from snappy.sage_helper import _within_sage
from snappy.testing import (doctest_modules, cyopengl_works,
tk_root, root_is_fake, DocTestParser)
from snappy import numeric_output_checker
modules = []

Expand Down Expand Up @@ -195,7 +196,7 @@ def runtests(verbose=False,
# doctests to fail.
snappy.pari.allocatemem(2**24, 2**25, silent=True)

DocTestParser.use_modernopengl = use_modernopengl
DocTestParser.use_cymodernopengl = use_modernopengl

result = doctest_modules(modules, verbose=verbose)
if not quick:
Expand Down
120 changes: 120 additions & 0 deletions python/testing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from .numeric_output_checker import NumericOutputChecker
from .sage_helper import _within_sage

import re
import sys
import doctest
import types


# Used for doctesting
_gui_status = {}

try:
from snappy.gui import Tk_
_gui_status['tk'] = True
except ImportError:
_gui_status['tk'] = False
if _gui_status['tk']:
try:
import snappy.CyOpenGL
_gui_status['cyopengl'] = True
except:
_gui_status['cyopengl'] = False
else:
_gui_status['cyopengl'] = False
_gui_status['fake_root'] = False

def cyopengl_works():
if not _gui_status['cyopengl']:
return False
# if we are running the tests from the snappy app the default root will
# already exist -- it will be the tkterminal window. Otherwise, we open
# a root window here to serve as the master of all of the GUI windows
# which get created during testing.
if _gui_status['tk'] and not Tk_._default_root:
try:
root = Tk_.Tk()
if sys.platform not in ('linux', 'linux2'):
root.withdraw()
except:
# tkinter loads OK but is not able to get a display.
_gui_status['tk'] = _gui_status['cyopengl'] = False
return _gui_status['cyopengl']


def tk_root():
if _gui_status['tk']:
return Tk_._default_root
else:
return None


def root_is_fake():
return _gui_status['fake_root']

class DocTestParser(doctest.DocTestParser):
use_cyopengl = False
use_cymodernopengl = True
use_sage = _within_sage

def parse(self, string, name='<string>'):
string = re.subn(
r'#doctest: \+CYOPENGL',
'' if DocTestParser.use_cyopengl else '#doctest: +SKIP',
string)[0]

string = re.subn(
r'#doctest: \+CYMODERNOPENGL',
(''
if (DocTestParser.use_cyopengl and
DocTestParser.use_cymodernopengl)
else '#doctest: +SKIP'),
string)[0]

if DocTestParser.use_sage:
string = re.subn(r'(\n\s*)sage:|(\A\s*)sage:',
r'\g<1>>>>',
string)[0]
return doctest.DocTestParser.parse(self, string, name)

if _within_sage:
import sage.all
globs = {'PSL': sage.all.PSL, 'BraidGroup': sage.all.BraidGroup}
else:
globs = {}

def print_results(module, results):
root = tk_root()
# Platform specific hacks to make running the tests work.
if root and not root_is_fake():
if sys.platform in ('linux', 'linux2'):
root.deiconify()
root.update_idletasks()
else:
root.update()
print(module.__name__ + ':')
print(' %s failures out of %s tests.' % (results.failed,
results.attempted))

def doctest_modules(modules, verbose=False, print_info=True, extraglobs={}):
finder = doctest.DocTestFinder(parser=DocTestParser())
full_extraglobals = globs.copy()
full_extraglobals.update(extraglobs)
failed, attempted = 0, 0
for module in modules:
if isinstance(module, types.ModuleType):
runner = doctest.DocTestRunner(checker=NumericOutputChecker(), verbose=verbose)
for test in finder.find(module, extraglobs=full_extraglobals):
runner.run(test)
result = runner.summarize()
else:
result = module(verbose=verbose)
failed += result.failed
attempted += result.attempted
if print_info:
print_results(module, result)

if print_info:
print('\nAll doctests:\n %s failures out of %s tests.' % (failed, attempted))
return doctest.TestResults(failed, attempted)
2 changes: 1 addition & 1 deletion python/verify/test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from snappy import verify, Manifold
from snappy.geometric_structure import cusp_neighborhood
from snappy.verify import upper_halfspace, volume
from snappy.sage_helper import _within_sage, doctest_modules
from snappy.testing import doctest_modules
import sys
import getopt

Expand Down

0 comments on commit 89a9bc5

Please sign in to comment.