Skip to content

Commit

Permalink
Testing: making some of the various test.py scripts more consistent. …
Browse files Browse the repository at this point in the history
…Centralizing some of the code in testing.py. Checking that __doc__ string really exists in test_cases.py. Avoiding relative imports in test.py so that they can be called.
  • Loading branch information
unhyperbolic committed Jul 27, 2024
1 parent 09f5e69 commit f043312
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 180 deletions.
22 changes: 22 additions & 0 deletions python/drilling/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from snappy import testing
import snappy

from snappy import drilling
import snappy.drilling.test_cases

modules = [
drilling,
drilling.test_cases
]

def run_doctests(verbose=False, print_info=False):
globs = {'Manifold': snappy.Manifold}
return testing.doctest_modules(modules,
verbose=verbose,
print_info=print_info,
extraglobs=globs)

run_doctests.__name__ = drilling.__name__

if __name__ == '__main__':
testing.run_doctests_as_main(run_doctests)
3 changes: 3 additions & 0 deletions python/drilling/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,6 @@
"""

from . import drill_words_implementation

if not __doc__:
raise Exception("doc string with tests was not recognized.")
56 changes: 21 additions & 35 deletions python/exterior_to_link/test.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,31 @@
from snappy import testing
import snappy
import sys
import getopt
from . import (rational_linear_algebra,
pl_utils,
barycentric_geometry,
hyp_utils,
link_projection,
mcomplex_with_memory,
mcomplex_with_expansion,
mcomplex_with_link,
simplify_to_base_tri,
put_in_S3,
main)
from . import __name__ as module_name

modules = [rational_linear_algebra,
pl_utils,
barycentric_geometry,
hyp_utils,
link_projection,
mcomplex_with_memory,
mcomplex_with_expansion,
mcomplex_with_link,
simplify_to_base_tri,
put_in_S3,
main]
from snappy import exterior_to_link

modules = [
exterior_to_link.rational_linear_algebra,
exterior_to_link.pl_utils,
exterior_to_link.barycentric_geometry,
exterior_to_link.hyp_utils,
exterior_to_link.link_projection,
exterior_to_link.mcomplex_with_memory,
exterior_to_link.mcomplex_with_expansion,
exterior_to_link.mcomplex_with_link,
exterior_to_link.simplify_to_base_tri,
exterior_to_link.put_in_S3,
exterior_to_link.main
]

def run_doctests(verbose=False, print_info=False):
globs = {'Manifold': snappy.Manifold,
'Triangulation': snappy.Triangulation}
results = snappy.testing.doctest_modules(modules,
verbose=verbose,
extraglobs=globs,
print_info=print_info)
return results
return testing.doctest_modules(modules,
verbose=verbose,
print_info=print_info,
extraglobs=globs)

run_doctests.__name__ = module_name
run_doctests.__name__ = exterior_to_link.__name__

if __name__ == '__main__':
optlist, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
verbose = len(optlist) > 0
results = run_doctests(verbose, print_info=True)
sys.exit(results.failed)
testing.run_doctests_as_main(run_doctests)
24 changes: 24 additions & 0 deletions python/len_spec/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from snappy import testing
import snappy

from snappy import len_spec
import snappy.len_spec.test_cases

modules = [
len_spec,
len_spec.word,
len_spec.length_spectrum_geodesic_info,
len_spec.test_cases
]

def run_doctests(verbose=False, print_info=False):
globs = {'Manifold': snappy.Manifold}
return testing.doctest_modules(modules,
verbose=verbose,
print_info=print_info,
extraglobs=globs)

run_doctests.__name__ = len_spec.__name__

if __name__ == '__main__':
testing.run_doctests_as_main(run_doctests)
5 changes: 5 additions & 0 deletions python/len_spec/test_cases.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
IMPORTANT: Python only recognises this as a doc string if there is
nothing before it. In particular, add any includes after the doc string.
>>> from snappy import Manifold
>>> M = Manifold("m125(3,4)(0,0)")
Expand All @@ -18,3 +20,6 @@
1.63203771292969 + 2.30009520293758*I
"""

if not __doc__:
raise Exception("doc string with tests was not recognized.")
24 changes: 24 additions & 0 deletions python/raytracing/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from snappy import testing
import snappy

from snappy import raytracing

modules = [
raytracing.cohomology_fractal,
raytracing.geodesic_tube_info,
raytracing.geodesics,
raytracing.ideal_raytracing_data,
raytracing.upper_halfspace_utilities
]

def run_doctests(verbose=False, print_info=False):
globs = {'Manifold': snappy.Manifold}
return testing.doctest_modules(modules,
verbose=verbose,
print_info=print_info,
extraglobs=globs)

run_doctests.__name__ = raytracing.__name__

if __name__ == '__main__':
testing.run_doctests_as_main(run_doctests)
88 changes: 35 additions & 53 deletions python/snap/test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
from ..sage_helper import _within_sage
from ..testing import doctest_modules
from ..pari import pari
from snappy import testing
import snappy

from snappy.sage_helper import _within_sage
from snappy.pari import pari

from snappy import snap
import getopt
import sys

modules = [
snap,
snap.t3mlite.linalg,
snap.t3mlite.mcomplex,
snap.t3mlite.perm4,
snap.t3mlite.spun,
snap.character_varieties,
snap.slice_obs_HKL,
snap.nsagetools,
snap.polished_reps,
snap.interval_reps,
snap.fundamental_polyhedron,
snap.peripheral.dual_cellulation,
snap.peripheral.link,
snap.peripheral.peripheral
]

def run_doctests(verbose=False, print_info=False):
globs = {'Manifold':snappy.Manifold,
'ManifoldHP':snappy.ManifoldHP,
'Triangulation':snappy.Triangulation,
'Mcomplex':snappy.snap.t3mlite.Mcomplex,
'LinkSurface':snappy.snap.peripheral.link.LinkSurface}
return testing.doctest_modules(modules,
verbose=verbose,
print_info=print_info,
extraglobs=globs)

run_doctests.__name__ = snap.__name__

def _test_gluing_equations(manifold, shapes):
"""
Expand Down Expand Up @@ -101,52 +130,5 @@ def big_test():
test_holonomy()
test_fields()


def run_doctests(verbose=False, print_info=False):
from snappy.snap.t3mlite import perm4
from snappy.snap.t3mlite import linalg
from snappy.snap.t3mlite import spun
from snappy.snap.t3mlite import mcomplex
from snappy.snap import slice_obs_HKL
from snappy.snap import character_varieties
from snappy.snap import nsagetools
from snappy.snap import polished_reps
from snappy.snap import interval_reps
from snappy.snap import fundamental_polyhedron
from snappy.snap.peripheral import dual_cellulation
from snappy.snap.peripheral import link
from snappy.snap.peripheral import peripheral

modules = [
perm4,
mcomplex,
linalg,
spun,
character_varieties,
nsagetools,
slice_obs_HKL,
polished_reps,
snap,
interval_reps,
fundamental_polyhedron,
dual_cellulation,
link,
peripheral,
]

globs = {'Manifold':snappy.Manifold,
'ManifoldHP':snappy.ManifoldHP,
'Triangulation':snappy.Triangulation,
'Mcomplex':snappy.snap.t3mlite.Mcomplex,
'LinkSurface':snappy.snap.peripheral.link.LinkSurface
}

return doctest_modules(modules, extraglobs=globs,
verbose=verbose, print_info=print_info)

run_doctests.__name__ = snap.__name__

if __name__ == '__main__':
optlist, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
verbose = len(optlist) > 0
run_doctests(verbose, print_info=True)
testing.run_doctests_as_main(run_doctests)
33 changes: 9 additions & 24 deletions python/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,9 @@
import snappy.tiling.dict_based_set
import snappy.cusps.maximal_cusp_area_matrix
import snappy.cusps.cusp_areas_from_matrix
import snappy.raytracing.cohomology_fractal
import snappy.raytracing.geodesic_tube_info
import snappy.raytracing.geodesics
import snappy.raytracing.ideal_raytracing_data
import snappy.raytracing.upper_halfspace_utilities
import snappy.len_spec
import snappy.len_spec.word
import snappy.len_spec.length_spectrum_geodesic_info
import snappy.len_spec.test_cases
import snappy.drilling
import snappy.drilling.test_cases
import snappy.raytracing.test
import snappy.len_spec.test
import snappy.drilling.test
import snappy.exterior_to_link.test
import snappy.pari

Expand Down Expand Up @@ -64,7 +56,7 @@ def snappy_database_doctester(verbose):
# is updated.
snappy.number.use_field_conversion('snappy')
snappy.number.Number._accuracy_for_testing = 8
ans = doctest_modules([snappy.database], verbose)
ans = doctest_modules([snappy.database], verbose=verbose)
snappy.number.Number._accuracy_for_testing = None
if _within_sage:
snappy.number.use_field_conversion('sage')
Expand Down Expand Up @@ -107,17 +99,9 @@ def spherogram_doctester(verbose):
snappy.tiling.dict_based_set,
snappy.cusps.maximal_cusp_area_matrix,
snappy.cusps.cusp_areas_from_matrix,
snappy.raytracing.cohomology_fractal,
snappy.raytracing.geodesic_tube_info,
snappy.raytracing.geodesics,
snappy.raytracing.ideal_raytracing_data,
snappy.raytracing.upper_halfspace_utilities,
snappy.len_spec,
snappy.len_spec.word,
snappy.len_spec.length_spectrum_geodesic_info,
snappy.len_spec.test_cases,
snappy.drilling,
snappy.drilling.test_cases,
snappy.raytracing.test.run_doctests,
snappy.len_spec.test.run_doctests,
snappy.drilling.test.run_doctests,
snappy.ptolemy.test.run_doctests,
spherogram_doctester,
snappy.verify.test.run_doctests]
Expand Down Expand Up @@ -174,7 +158,8 @@ def runtests(verbose=False,
if not quick:
all_modules += slow_modules

result = doctest_modules(all_modules, verbose=verbose)
result = doctest_modules(
all_modules, verbose=verbose, print_info=True)

if not quick:
print()
Expand Down
14 changes: 10 additions & 4 deletions python/testing.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from .numeric_output_checker import NumericOutputChecker
from .sage_helper import _within_sage

import doctest
import getopt
import re
import sys
import doctest
import types


# Used for doctesting
_gui_status = {}

Expand Down Expand Up @@ -49,7 +49,6 @@ def tk_root():
else:
return None


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

Expand Down Expand Up @@ -97,7 +96,7 @@ def print_results(module, results):
print(' %s failures out of %s tests.' % (results.failed,
results.attempted))

def doctest_modules(modules, verbose=False, print_info=True, extraglobs={}):
def doctest_modules(modules, verbose=False, print_info=False, extraglobs={}):
finder = doctest.DocTestFinder(parser=DocTestParser())
full_extraglobals = globs.copy()
full_extraglobals.update(extraglobs)
Expand All @@ -118,3 +117,10 @@ def doctest_modules(modules, verbose=False, print_info=True, extraglobs={}):
if print_info:
print('\nAll doctests:\n %s failures out of %s tests.' % (failed, attempted))
return doctest.TestResults(failed, attempted)

def run_doctests_as_main(run_doctests):
optlist, args = getopt.getopt(sys.argv[1:], 'v', ['verbose'])
verbose = len(optlist) > 0
results = run_doctests(verbose, print_info=True)
sys.exit(results.failed)

Loading

0 comments on commit f043312

Please sign in to comment.