Beispiel #1
0
def main():
    try:
        doctest.register_optionflag
    except:
        print(
            "\n"
            "The regression test suite requires a more recent version of\n"
            "doctest (e.g., the version that ships with Python 2.4 or 2.5).\n"
            "Please place a new version of doctest on your path before \n"
            "running the test suite.\n")
        return

    PY24 = doctest.register_optionflag('PYTHON2.4')
    """Flag indicating that a doctest example requires Python 2.4+"""

    PY25 = doctest.register_optionflag('PYTHON2.5')
    """Flag indicating that a doctest example requires Python 2.5+"""
    class DocTestParser(doctest.DocTestParser):
        """
        Custom doctest parser that adds support for two new flags
        +PYTHON2.4 and +PYTHON2.5.
        """
        def parse(self, string, name='<string>'):
            pieces = doctest.DocTestParser.parse(self, string, name)
            for i, val in enumerate(pieces):
                if (isinstance(val, doctest.Example) and
                    ((val.options.get(PY24, False) and sys.version[:2] <
                      (2, 4)) or
                     (val.options.get(PY25, False) and sys.version[:2] <
                      (2, 5)))):
                    pieces[i] = doctest.Example('1', '1')
            return pieces

    # Turn on debugging.
    epydoc.DEBUG = True

    # Options for doctest:
    options = doctest.ELLIPSIS
    doctest.set_unittest_reportflags(doctest.REPORT_UDIFF)

    # Use a custom parser
    parser = DocTestParser()

    # Find all test cases.
    tests = []
    testdir = os.path.join(os.path.split(__file__)[0])
    if testdir == '': testdir = '.'
    for filename in os.listdir(testdir):
        if (filename.endswith('.doctest')
                and check_requirements(os.path.join(testdir, filename))):
            tests.append(
                doctest.DocFileSuite(filename,
                                     optionflags=options,
                                     parser=parser))

    # Run all test cases.
    unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(tests))
Beispiel #2
0
def main():
    try:
        doctest.register_optionflag
    except:
        print ("\n"
            "The regression test suite requires a more recent version of\n"
            "doctest (e.g., the version that ships with Python 2.4 or 2.5).\n"
            "Please place a new version of doctest on your path before \n"
            "running the test suite.\n")
        return
                          
    
    PY24 = doctest.register_optionflag('PYTHON2.4')
    """Flag indicating that a doctest example requires Python 2.4+"""
    
    PY25 = doctest.register_optionflag('PYTHON2.5')
    """Flag indicating that a doctest example requires Python 2.5+"""
    
    class DocTestParser(doctest.DocTestParser):
        """
        Custom doctest parser that adds support for two new flags
        +PYTHON2.4 and +PYTHON2.5.
        """
        def parse(self, string, name='<string>'):
            pieces = doctest.DocTestParser.parse(self, string, name)
            for i, val in enumerate(pieces):
                if (isinstance(val, doctest.Example) and
                    ((val.options.get(PY24, False) and
                      sys.version[:2] < (2,4)) or
                     (val.options.get(PY25, False) and
                      sys.version[:2] < (2,5)))):
                    pieces[i] = doctest.Example('1', '1')
            return pieces

    # Turn on debugging.
    epydoc.DEBUG = True
    
    # Options for doctest:
    options = doctest.ELLIPSIS
    doctest.set_unittest_reportflags(doctest.REPORT_UDIFF)

    # Use a custom parser
    parser = DocTestParser()
    
    # Find all test cases.
    tests = []
    testdir = os.path.join(os.path.split(__file__)[0])
    if testdir == '': testdir = '.'
    for filename in os.listdir(testdir):
        if (filename.endswith('.doctest') and
            check_requirements(os.path.join(testdir, filename))):
            tests.append(doctest.DocFileSuite(filename, optionflags=options,
                                              parser=parser))
            
    # Run all test cases.
    unittest.TextTestRunner(verbosity=2).run(unittest.TestSuite(tests))
Beispiel #3
0
def _get_allow_unicode_flag():
    """
    Registers and returns the ALLOW_UNICODE flag.
    """
    import doctest

    return doctest.register_optionflag("ALLOW_UNICODE")
Beispiel #4
0
def _get_allow_unicode_flag() -> int:
    """
    Registers and returns the ALLOW_UNICODE flag.
    """
    import doctest

    return doctest.register_optionflag("ALLOW_UNICODE")
Beispiel #5
0
def _get_allow_bytes_flag() -> int:
    """
    Registers and returns the ALLOW_BYTES flag.
    """
    import doctest

    return doctest.register_optionflag("ALLOW_BYTES")
Beispiel #6
0
def _get_number_flag() -> int:
    """
    Registers and returns the NUMBER flag.
    """
    import doctest

    return doctest.register_optionflag("NUMBER")
Beispiel #7
0
def _get_allow_bytes_flag():
    """
    Registers and returns the ALLOW_BYTES flag.
    """
    import doctest

    return doctest.register_optionflag("ALLOW_BYTES")
Beispiel #8
0
def check_multi_rank_docstr(module):
    # supply customized flag ONLY_CHECK_RANK_{x} for docstr
    check_flags = [
        doctest.register_optionflag(f"ONLY_CHECK_RANK_{i}")
        for i in range(oneflow.env.get_world_size())
    ]
    finder = doctest.DocTestFinder()
    runner = doctest.DebugRunner(CondSkipChecker(check_flags))
    for test in finder.find(module, module.__name__):
        runner.run(test)
Beispiel #9
0
    def run(testObject, name_space=None):
        """
        Runs this doc test executor with the given test object in the given
        namespace.

        @param testObject: object to test. If this is a string, it is run as
          is; otherwise, its C{__doc__} attribute is used
        @type testObject: string or an arbitrary object with a C{__doc__}
          attribute
        @param name_space: namespace to execute the test in (defaults to the
          C{__main__} module)
        @type name_space: dictionary mapping names (strings) to arbitrary
          objects
        @note: when passing in a custom L{name_space} argument, beware that the
          doctest runner also inserts the C{__builtin__} module into your
          namespace
        """
        # Register our special IGNORE_TRAILING_WHITESPACE option.
        flag = doctest.register_optionflag('IGNORE_TRAILING_WHITESPACE')
        # Set up a doc tester with our special dict.
        custom_name_space = _DocTestExecutor._NonCopyableNonClearableDict()
        finder = doctest.DocTestFinder()
        if isinstance(testObject, basestring):
            name = 'doctest'
        else:
            name = testObject.__name__
        tests = finder.find(testObject, name=name, globs=custom_name_space)
        checker = \
           _DocTestExecutor._TrailingWhitespaceInsensitiveOutputChecker(flag)
        tester = doctest.DocTestRunner(checker=checker,
                                        verbose=False)
        for test in tests:
            # Run the doc test(s).
            tester.run(test)
        # Now, update our name space with our newly created variables for
        # further interactive use (but *only* with those to avoid overwriting
        # things, e.g. default instances!).
        if name_space is None:
            name_space = sys.modules['__main__'].__dict__
        for key, value in custom_name_space.items():
            if not key in name_space:
                name_space[key] = value
Beispiel #10
0
    def run(testObject, name_space=None):
        """
        Runs this doc test executor with the given test object in the given
        namespace.

        @param testObject: object to test. If this is a string, it is run as
          is; otherwise, its C{__doc__} attribute is used
        @type testObject: string or an arbitrary object with a C{__doc__}
          attribute
        @param name_space: namespace to execute the test in (defaults to the
          C{__main__} module)
        @type name_space: dictionary mapping names (strings) to arbitrary
          objects
        @note: when passing in a custom L{name_space} argument, beware that the
          doctest runner also inserts the C{__builtin__} module into your
          namespace
        """
        # Register our special IGNORE_TRAILING_WHITESPACE option.
        flag = doctest.register_optionflag('IGNORE_TRAILING_WHITESPACE')
        # Set up a doc tester with our special dict.
        custom_name_space = _DocTestExecutor._NonCopyableNonClearableDict()
        finder = doctest.DocTestFinder()
        if isinstance(testObject, basestring):
            name = 'doctest'
        else:
            name = testObject.__name__
        tests = finder.find(testObject, name=name, globs=custom_name_space)
        checker = \
           _DocTestExecutor._TrailingWhitespaceInsensitiveOutputChecker(flag)
        tester = doctest.DocTestRunner(checker=checker, verbose=False)
        for test in tests:
            # Run the doc test(s).
            tester.run(test)
        # Now, update our name space with our newly created variables for
        # further interactive use (but *only* with those to avoid overwriting
        # things, e.g. default instances!).
        if name_space is None:
            name_space = sys.modules['__main__'].__dict__
        for key, value in custom_name_space.items():
            if not key in name_space:
                name_space[key] = value
Beispiel #11
0
def init_precisions(precisions):
    """
    Register flags for given precisions with doctest module.

    Unfortunately, doctest doesn't seem to support a more generic mechanism
    such as "# doctest: +NUMERIC: 6" to specify the precision and we need to
    unroll each precision we want to its own flag.
    """

    global NUMERIC_LIST
    global NUMERIC_DICT
    global ALL_NUMERIC

    for precision in precisions:
        # Check key such that clients having demand for different
        # precisions could register them.
        if precision not in NUMERIC_DICT:
            flag = doctest.register_optionflag('NUMERIC%d' % precision)
            NUMERIC_LIST.append((precision, flag))
            NUMERIC_DICT[precision] = flag
            ALL_NUMERIC |= flag
Beispiel #12
0
def setup(app):
    """Add an "IGNORE_RESULT" doctest optionflag to sphinx.ext.doctest

    Any output following the flag will be ignored. This can be used when the
    result of a method call is not relevant to the doctest or code example::

        >>> p = pathlib.Path('somefile.txt')
        >>> # `write_text` returns the number of bytes written, but we don't care
        >>> p.write_text('foo')
        3
        >>> # Ignore it to make the code example cleaner
        >>> p.write_text('foo') #doctest: +IGNORE_RESULT

    """
    app.setup_extension('sphinx.ext.doctest')

    import doctest
    from sphinx.ext import doctest as sphinx_doctest
    _SphinxDocTestRunner_orig = sphinx_doctest.SphinxDocTestRunner
    sphinx_doctest._SphinxDocTestRunner_orig = _SphinxDocTestRunner_orig
    IGNORE_RESULT = doctest.register_optionflag('IGNORE_RESULT')

    class IgnoreOutputChecker(doctest.OutputChecker):
        def check_output(self, want, got, optionflags):
            if optionflags & IGNORE_RESULT:
                return True
            return super().check_output(want, got, optionflags)

    class MyRunner(_SphinxDocTestRunner_orig):
        def __init__(self, checker=None, verbose=None, optionflags=0):
            checker = IgnoreOutputChecker()
            super().__init__(checker, verbose, optionflags)

    sphinx_doctest.SphinxDocTestRunner = MyRunner

    return {
        'version': '0.1',
        'parallel_read_safe': True,
        'parallel_write_safe': True,
    }
Beispiel #13
0
def register_skipper(flag, test, why, skipWarning=True):
    """Create a new doctest option flag for skipping tests
    
    Parameters
    ----------
    flag : str
      Name of the option flag
    test : function
      A function which should return `True` if the test should be run
    why : str
      Explanation for why the test was skipped (to be used in a string
      "``Skipped %%(count)d doctest examples because %%(why)s``")
    skipWarning : bool
      Whether or not to report on tests skipped by this flag (default `True`)
    """
    global _doctestSkippers

    skipper = _DoctestSkipper(flag=doctest.register_optionflag(flag),
                              test=test,
                              why=why,
                              skipWarning=skipWarning)
    _doctestSkippers.append(skipper)
Beispiel #14
0
def register_skipper(flag, test, why, skipWarning=True):
    """Create a new doctest option flag for skipping tests
    
    Parameters
    ----------
    flag : str
      Name of the option flag
    test : function
      A function which should return `True` if the test should be run
    why : str
      Explanation for why the test was skipped (to be used in a string
      "``Skipped %%(count)d doctest examples because %%(why)s``")
    skipWarning : bool
      Whether or not to report on tests skipped by this flag (default `True`)
    """
    global _doctestSkippers
    
    skipper = _DoctestSkipper(flag=doctest.register_optionflag(flag),
                              test=test,
                              why=why,
                              skipWarning=skipWarning)
    _doctestSkippers.append(skipper)
Beispiel #15
0
Implements a replacement for `doctest.OutputChecker` that handles certain
normalizations of Python expression output.  See the docstring on
`AstropyOutputChecker` for more details.
"""

from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import doctest
import re

# Much of this code, particularly the parts of floating point handling, is
# borrowed from the SymPy project with permission.  See licenses/SYMPY.rst
# for the full SymPy license.

FIX = doctest.register_optionflag('FIX')
FLOAT_CMP = doctest.register_optionflag('FLOAT_CMP')


class AstropyOutputChecker(doctest.OutputChecker):
    """
    - Removes u'' prefixes on string literals
    - Ignores the 'L' suffix on long integers
    - In Numpy dtype strings, removes the leading pipe, i.e. '|S9' ->
      'S9'.  Numpy 1.7 no longer includes it in display.
    - Supports the FLOAT_CMP flag, which parses floating point values
      out of the output and compares their numerical values rather than their
      string representation.  This naturally supports complex numbers as well
      (simply by comparing their real and imaginary parts separately).
    """
Beispiel #16
0
        a space character, then raise ValueError.

        Note: IPython-modified version which takes the input prompt length as a
        parameter, so that prompts of variable length can be dealt with.
        """
        space_idx = indent+ps1_len
        min_len = space_idx+1
        for i, line in enumerate(lines):
            if len(line) >=  min_len and line[space_idx] != ' ':
                raise ValueError('line %r of the docstring for %s '
                                 'lacks blank after %s: %r' %
                                 (lineno+i+1, name,
                                  line[indent:space_idx], line))


SKIP = doctest.register_optionflag('SKIP')


class IPDocTestRunner(doctest.DocTestRunner,object):
    """Test runner that synchronizes the IPython namespace with test globals.
    """

    def run(self, test, compileflags=None, out=None, clear_globs=True):

        # Hack: ipython needs access to the execution context of the example,
        # so that it can propagate user variables loaded by %run into
        # test.globs.  We put them here into our modified %run as a function
        # attribute.  Our new %run will then only make the namespace update
        # when called (rather than unconconditionally updating test.globs here
        # for all examples, most of which won't be calling %run anyway).
        #_ip._ipdoctest_test_globs = test.globs
Beispiel #17
0
        """
    normalised_text = text
    normalise_map = {
        re.compile(r"\(\s+(\S)"): r"(\1",
        re.compile(r"(\S)\s+\)"): r"\1)",
        re.compile(r",\s*(\S)"): r", \1",
        }
    for search_pattern, replace_pattern in normalise_map.items():
        normalised_text = re.sub(
            search_pattern, replace_pattern, normalised_text)

    return normalised_text

doctest.NORMALIZE_FUNCTION_PARAMETERS = (
    doctest.register_optionflag('NORMALIZE_FUNCTION_PARAMETERS'))

class MinimockOutputChecker(doctest.OutputChecker, object):
    """ Class for matching output of MiniMock objects against expectations """

    def check_output(self, want, got, optionflags):
        if (optionflags & doctest.NORMALIZE_FUNCTION_PARAMETERS):
            want = normalise_function_parameters(want)
            got = normalise_function_parameters(got)
        output_match = super(MinimockOutputChecker, self).check_output(
            want, got, optionflags)
        return output_match
    check_output.__doc__ = doctest.OutputChecker.check_output.__doc__


class TestCase(unittest.TestCase):
# TODO: 
#    - optional round-trip check (global setting)
#    - "inject" pandoc.read errors in the doctest so that we can catch them.
#      Mmmm we probably can't do it, unless we do some fugly stack patching.
#      So what ? pandoc.read on any output JSON generated by pandoc is not 
#      supposed to fail ... Implement a "safe" read on top of it ?
#      Return a reference error message ? (maybe the full traceback?)
#      And make sure that after that, it is properly handled?

# ------------------------------------------------------------------------------

# Global Variables
check_round_trip = True

# New Doctest Directive: PANDOC 
PANDOC = doctest.register_optionflag("PANDOC")
doctest.PANDOC = PANDOC
doctest.__all__.append("PANDOC")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | PANDOC

# Helpers
from subprocess import Popen, PIPE
import json
def to_json(txt):
    p = Popen(["pandoc", "-tjson"], 
              stdout=PIPE, stdin=PIPE, stderr=PIPE)
    json_string = p.communicate(input=txt.encode("utf-8"))[0].decode("utf-8")
    json_doc = json.loads(json_string)
    return json_doc

def linebreak(text, length=80):
Beispiel #19
0
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import doctest
import re

import numpy as np

from ..extern import six
from ..extern.six.moves import zip

# Much of this code, particularly the parts of floating point handling, is
# borrowed from the SymPy project with permission.  See licenses/SYMPY.rst
# for the full SymPy license.

FIX = doctest.register_optionflag('FIX')
FLOAT_CMP = doctest.register_optionflag('FLOAT_CMP')
IGNORE_OUTPUT = doctest.register_optionflag('IGNORE_OUTPUT')
IGNORE_OUTPUT_2 = doctest.register_optionflag('IGNORE_OUTPUT_2')
IGNORE_OUTPUT_3 = doctest.register_optionflag('IGNORE_OUTPUT_3')


class AstropyOutputChecker(doctest.OutputChecker):
    """
    - Removes u'' prefixes on string literals
    - Ignores the 'L' suffix on long integers
    - In Numpy dtype strings, removes the leading pipe, i.e. '|S9' ->
      'S9'.  Numpy 1.7 no longer includes it in display.
    - Supports the FLOAT_CMP flag, which parses floating point values
      out of the output and compares their numerical values rather than their
      string representation.  This naturally supports complex numbers as well
Beispiel #20
0
        # Can't use super here because doctest.OutputChecker is not a
        # new-style class.
        return self._original_output_checker.check_output(self, want, got, flags)

    def output_difference(self, want, got, flags):
        if flags & FIX:
            want, got = self.do_fixes(want, got)
        # Can't use super here because doctest.OutputChecker is not a
        # new-style class.
        return self._original_output_checker.output_difference(self, want, got, flags)


# We monkey-patch in our replacement doctest OutputChecker.  Not
# great, but there isn't really an API to replace the checker when
# using doctest.testfile, unfortunately.
FIX = doctest.register_optionflag("FIX")
doctest.OutputChecker = OutputCheckerFix


REMOTE_DATA = doctest.register_optionflag("REMOTE_DATA")


def pytest_configure(config):
    treat_deprecations_as_exceptions()

    # Monkeypatch to deny access to remote resources unless explicitly told
    # otherwise
    if not config.getoption("remote_data"):
        turn_off_internet(verbose=config.option.verbose)

    doctest_plugin = config.pluginmanager.getplugin("doctest")
Beispiel #21
0
from zope.security.interfaces import IInteraction
from zope.security.interfaces import ISecurityPolicy

import z3c.form
from z3c.form import browser, button, converter, datamanager, error, field
from z3c.form import form, interfaces, term, validator, widget
from z3c.form import contentprovider
from z3c.form import outputchecker
from z3c.form import tests
from z3c.form.browser import radio, select, text, textarea

import lxml.html
import lxml.doctestcompare

# register lxml doctest option flags
lxml.doctestcompare.NOPARSE_MARKUP = register_optionflag('NOPARSE_MARKUP')

outputChecker = outputchecker.OutputChecker(
    patterns =(
        # Py3 compatibility.
        (re.compile("u('.*?')"), r"\1"),
        (re.compile("b('.*?')"), r"\1"),
        (re.compile("__builtin__"), r"builtins"),
        (re.compile("<type"), r"<class"),
        (re.compile("set\(\[(.*?)\]\)"), r"{\1}"),
    )
)

class TestingFileUploadDataConverter(converter.FileUploadDataConverter):
    """A special file upload data converter that works with testing."""
    zope.component.adapts(
Beispiel #22
0
def setup():
    global session, engine, metadata, trans
    session, engine, metadata = setup_database()
    user = setup_records(session)
    #this may be needed if we end up testing the provider with doctests
    #session.commit()


def check_output_xml(want, got, optionsflags):
    return eq_xml(want, got)

from doctest import OutputChecker
original_check_output = OutputChecker.check_output
import doctest

XML_OUTPUT = doctest.register_optionflag("XML")

class MyOutputChecker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
        if optionflags & XML_OUTPUT:
            return check_output_xml(want, got, optionflags)
        return original_check_output(self, want, got, optionflags)

doctest.OutputChecker = MyOutputChecker

def teardown():
    session.rollback()

def test_doctests():
    global session, metadata
    import unittest
Beispiel #23
0
    for file in p.rglob("*.py"):
        # Construct path as string for import, for instance "internals.frame"
        # The -3 drops the ".py"
        file_name_import = ".".join(file.relative_to(p).parts)[:-3]
        temp_module = importlib.import_module(p.name + "." + file_name_import)
        yield temp_module


if __name__ == "__main__":
    # set to True to just run the code, and do not check any output. Will still report errors if the code is invalid
    IGNORE_RESULT_ALL = True

    # Below the implementation if the IGNORE_RESULT directive
    # You can ignore the result of a doctest by adding "doctest: +IGNORE_RESULT" into the code block
    # The difference with SKIP is that if the code errors on running, that will still be reported.
    IGNORE_RESULT = doctest.register_optionflag("IGNORE_RESULT")

    OutputChecker = doctest.OutputChecker

    class CustomOutputChecker(OutputChecker):
        def check_output(self, want: str, got: str, optionflags: Any) -> bool:
            if IGNORE_RESULT_ALL:
                return True
            if IGNORE_RESULT & optionflags:
                return True
            else:
                return OutputChecker.check_output(self, want, got, optionflags)

    doctest.OutputChecker = CustomOutputChecker  # type: ignore

    # We want to be relaxed about whitespace, but strict on True vs 1
Beispiel #24
0
"""
Implements a replacement for `doctest.OutputChecker` that handles certain
normalizations of Python expression output.  See the docstring on
`AstropyOutputChecker` for more details.
"""

import doctest
import re
import numpy as np


# Much of this code, particularly the parts of floating point handling, is
# borrowed from the SymPy project with permission.  See licenses/SYMPY.rst
# for the full SymPy license.

FIX = doctest.register_optionflag('FIX')
FLOAT_CMP = doctest.register_optionflag('FLOAT_CMP')
IGNORE_OUTPUT = doctest.register_optionflag('IGNORE_OUTPUT')
IGNORE_OUTPUT_2 = doctest.register_optionflag('IGNORE_OUTPUT_2')
IGNORE_OUTPUT_3 = doctest.register_optionflag('IGNORE_OUTPUT_3')


class AstropyOutputChecker(doctest.OutputChecker):
    """
    - Removes u'' prefixes on string literals
    - Ignores the 'L' suffix on long integers
    - In Numpy dtype strings, removes the leading pipe, i.e. '|S9' ->
      'S9'.  Numpy 1.7 no longer includes it in display.
    - Supports the FLOAT_CMP flag, which parses floating point values
      out of the output and compares their numerical values rather than their
      string representation.  This naturally supports complex numbers as well
Beispiel #25
0
min_versions = {}
for line in metadata.requires('astropy'):
    req = Requirement(line.split(';')[0])
    min_versions[req.name.lower()] = str(req.specifier)


# This is added to the end of RST files - a good place to put substitutions to
# be used globally.
with open("common_links.txt", "r") as cl:
    rst_epilog += cl.read().format(minimum_python=__minimum_python_version__,
                                   **min_versions)

# Manually register doctest options since matplotlib 3.5 messed up allowing them
# from pytest-doctestplus
IGNORE_OUTPUT = doctest.register_optionflag('IGNORE_OUTPUT')
REMOTE_DATA = doctest.register_optionflag('REMOTE_DATA')
FLOAT_CMP = doctest.register_optionflag('FLOAT_CMP')

# Whether to create cross-references for the parameter types in the
# Parameters, Other Parameters, Returns and Yields sections of the docstring.
numpydoc_xref_param_type = True

# Words not to cross-reference. Most likely, these are common words used in
# parameter type descriptions that may be confused for classes of the same
# name. The base set comes from sphinx-astropy. We add more here.
numpydoc_xref_ignore.update({
    "mixin",
    "Any",  # aka something that would be annotated with `typing.Any`
    # needed in subclassing numpy  # TODO! revisit
    "Arguments", "Path",
Beispiel #26
0
    key = ''.join(chr(i) for i in range(16)).encode('utf-8')
    plaintext = ''.join(chr(i) for i in range(64)).encode('utf-8')
    for i in range(64):
        assert SipHash_2_4(key, plaintext[:i]).hexdigest() == vectors[i], \
            'failed on test no %i' % i

    # Internal doctests
    #
    # To maintain compatibility with both python 2.x and 3.x in tests
    # we need to do a trick. Python 2.x doesn't like b'' notation,
    # Python 3.x doesn't have 2222L long integers notation. To
    # overcome that we'll pipe both results as well as the intended
    # doctest output through an `eval` function before comparison. To
    # do it we need to monkeypatch the OutputChecker:
    import doctest
    EVAL_FLAG = doctest.register_optionflag("EVAL")
    OrigOutputChecker = doctest.OutputChecker

    def relaxed_eval(s):
        if s.strip():
            return eval(s)
        else:
            return None

    class MyOutputChecker:
        def __init__(self):
            self.orig = OrigOutputChecker()

        def check_output(self, want, got, optionflags):
            if optionflags & EVAL_FLAG:
                return relaxed_eval(got) == relaxed_eval(want)
Beispiel #27
0
    """

    testing.buildoutSetUp(test)
    import tempfile
    target_dir = tempfile.mkdtemp('.z3c.autoinclude.test-installs')
    install_projects(test_packages, target_dir)


def testTearDown(test):
    from testdirective.zcml import clear_test_log
    clear_test_log()

    testing.buildoutTearDown(test)


IGNORECASE = doctest.register_optionflag('IGNORECASE')

class IgnoreCaseChecker(doctest.OutputChecker):
    def check_output(self, want, got, optionflags):
        if optionflags & IGNORECASE:
            want, got = want.lower(), got.lower()
            #print repr(want), repr(got), optionflags, IGNORECASE
        return doctest.OutputChecker.check_output(self, want, got, optionflags)

def test_suite():

    from pprint import pprint
    suite = doctest.DocFileSuite('../utils.txt',
                                 '../dependency.txt',
                                 '../plugin.txt',
                                 setUp=testSetUp,
# Copyright (c) 2004 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
import sys
import doctest


IGNORE_EXCEPTION_MODULE_IN_PYTHON2 = doctest.register_optionflag(
    'IGNORE_EXCEPTION_MODULE_IN_PYTHON2')
IGNORE_EXCEPTION_MODULE_IN_PYTHON2_HINT = """\
===============================================================
HINT:
  The optionflag IGNORE_EXCEPTION_MODULE_IN_PYTHON2 is set.
  You seem to test traceback output.
  If you are indeed, make sure to use the full dotted name of
  the exception class like Python 3 displays,
  even though you are running the tests in Python 2.
  The exception message needs to be last line (and thus not
  split over multiple lines).
==============================================================="""


class OutputChecker(doctest.OutputChecker):
    """Pattern-normalizing outout checker
Beispiel #29
0
        s = s.strip()
        if s and s[0] in ("'", '"') and s[-1] in ("'", '"'):
            s = s[1:-1]
        return s

    want = clean_string(want)
    got = clean_string(got)
    if not want or not got:
        return False, "something is missing"
    want = etree.fromstring(want)
    got = etree.fromstring(got)

    return _assertXMLElementEqual(want, got, optionflags)


XMLDATA = doctest.register_optionflag("XMLDATA")

XMLDATA_IGNOREORDER = doctest.register_optionflag("XMLDATA_IGNOREORDER")

XMLDATA_IGNOREMISSINGATTRIBUTES = doctest.register_optionflag(
    "XMLDATA_IGNOREMISSINGATTRIBUTES")

XMLDATA_IGNOREEXTRAATTRIBUTES = doctest.register_optionflag(
    "XMLDATA_IGNOREEXTRAATTRIBUTES")

class XMLOutputChecker(doctest.OutputChecker):

    def check_output(self, want, got, optionflags):
        if optionflags & XMLDATA or optionflags & XMLDATA_IGNOREORDER:
            if want and got: # it only makes sense to compare actual data.
                result, msg = _assertXMLElement(want, got, optionflags)
Beispiel #30
0
flags = doctest.ELLIPSIS
timeout = 600

filenames = sys.argv[1:]
if os.name == 'nt':
    notinwindows = set([
        'src/cysignals/pysignals.pyx', 'src/cysignals/alarm.pyx',
        'src/cysignals/pselect.pyx'
    ])

    filenames = [f for f in filenames if f not in notinwindows]

# Add an option to flag doctests which should be skipped depending on
# the platform
SKIP_WINDOWS = doctest.register_optionflag("SKIP_WINDOWS")
SKIP_CYGWIN = doctest.register_optionflag("SKIP_CYGWIN")
SKIP_POSIX = doctest.register_optionflag("SKIP_POSIX")

skipflags = set()

if os.name == 'posix':
    skipflags.add(SKIP_POSIX)
elif os.name == 'nt':
    skipflags.add(SKIP_WINDOWS)
if sys.platform == 'cygwin':
    skipflags.add(SKIP_CYGWIN)


class CysignalsDocTestParser(DocTestParser):
    def parse(self, *args, **kwargs):
Beispiel #31
0
try:
    from html import escape as html_escape
except ImportError:
    from cgi import escape as html_escape

__all__ = ['PARSE_HTML', 'PARSE_XML', 'NOPARSE_MARKUP', 'LXMLOutputChecker',
           'LHTMLOutputChecker', 'install', 'temp_install']

try:
    _basestring = basestring
except NameError:
    _basestring = (str, bytes)

_IS_PYTHON_3 = sys.version_info[0] >= 3

PARSE_HTML = doctest.register_optionflag('PARSE_HTML')
PARSE_XML = doctest.register_optionflag('PARSE_XML')
NOPARSE_MARKUP = doctest.register_optionflag('NOPARSE_MARKUP')

OutputChecker = doctest.OutputChecker

def strip(v):
    if v is None:
        return None
    else:
        return v.strip()

def norm_whitespace(v):
    return _norm_whitespace_re.sub(' ', v)

_html_parser = etree.HTMLParser(recover=False, remove_blank_text=True)
Beispiel #32
0
import doctest

import numpy as np
import pkg_resources

# import all packages
from . import audio, evaluation, features, io, ml, models, processors, utils

# define a version variable
__version__ = pkg_resources.get_distribution("madmom").version

# Create a doctest output checker that optionally ignores the unicode string
# literal.

# declare the new doctest directives
_IGNORE_UNICODE = doctest.register_optionflag("IGNORE_UNICODE")
doctest.IGNORE_UNICODE = _IGNORE_UNICODE
doctest.__all__.append("IGNORE_UNICODE")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | _IGNORE_UNICODE

_NORMALIZE_ARRAYS = doctest.register_optionflag("NORMALIZE_ARRAYS")
doctest.NORMALIZE_ARRAYS = _NORMALIZE_ARRAYS
doctest.__all__.append("NORMALIZE_ARRAYS")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | _NORMALIZE_ARRAYS

_doctest_OutputChecker = doctest.OutputChecker


class _OutputChecker(_doctest_OutputChecker):
    """
    Output checker which enhances `doctest.OutputChecker` to compare doctests
Beispiel #33
0
import docutils.core
from argparse import ArgumentParser
from contextlib import contextmanager, redirect_stderr
from doctest import NORMALIZE_WHITESPACE, ELLIPSIS, IGNORE_EXCEPTION_DETAIL

from docutils.parsers.rst import directives
from pkg_resources import parse_version

import sphinx
import numpy as np

sys.path.insert(
    0, os.path.join(os.path.dirname(__file__), '..', 'doc', 'sphinxext'))
from numpydoc.docscrape_sphinx import get_doc_object

SKIPBLOCK = doctest.register_optionflag('SKIPBLOCK')

if parse_version(sphinx.__version__) >= parse_version('1.5'):
    # Enable specific Sphinx directives
    from sphinx.directives.other import SeeAlso, Only
    directives.register_directive('seealso', SeeAlso)
    directives.register_directive('only', Only)
else:
    # Remove sphinx directives that don't run without Sphinx environment.
    # Sphinx < 1.5 installs all directives on import...
    directives._directives.pop('versionadded', None)
    directives._directives.pop('versionchanged', None)
    directives._directives.pop('moduleauthor', None)
    directives._directives.pop('sectionauthor', None)
    directives._directives.pop('codeauthor', None)
    directives._directives.pop('toctree', None)
Beispiel #34
0
try:
    from epydoc.cli import TerminalController
except ImportError:
    class TerminalController:

        def __getattr__(self, attr): return ''

term = TerminalController()

######################################################################
# Code
######################################################################

# If we're using py24, then ignore the +SKIP directive.
if sys.version_info[:2] < (2, 5):
    register_optionflag('SKIP')


def strip_quotes(s):
    s = s.strip()
    while (s and (s[0] in "ur") and (s[-1] in "'\"")):
        s = s[1:]
    while (s and (s[0] in "'\"" and (s[0] == s[-1]))):
        s = s[1:-1]
    s = s.strip()
    return s


def find_class(s, index):
    lines = s[:index].split('\n')
    while lines:
import doctest
import sys

SKIP_ON_PY2 = doctest.register_optionflag('SKIP_ON_PY2')
SKIP_ON_PY3 = doctest.register_optionflag('SKIP_ON_PY3')

_skip = None

if sys.version_info.major == 2:
    _skip = SKIP_ON_PY2
elif sys.version_info.major == 3:
    _skip = SKIP_ON_PY3


def PyVersionAwareDocTestSuite(*args, **kwargs):
    """A specialism of doctest.DocTestSuite providing the ability to SKIP
    doctests based on the Python major version.

    Two new doctest option flags are made available:
    SKIP_ON_PY2 and SKIP_ON_PY3.

    These cause the SKIP option flag to be set if the Python version matches.

    >>> print("Never")      # doctest: +SKIP
    Never
    >>> print("Not Py2")    # doctest: +SKIP_ON_PY2
    Not Py2
    >>> print("Not Py3")    # doctest: +SKIP_ON_PY3
    Not Py3

import sys
import re
import doctest
import cgi

__all__ = ['PARSE_HTML', 'PARSE_XML', 'NOPARSE_MARKUP', 'LXMLOutputChecker',
           'LHTMLOutputChecker', 'install', 'temp_install']

try:
    _basestring = basestring
except NameError:
    _basestring = (str, bytes)

_IS_PYTHON_3 = sys.version_info[0] >= 3

PARSE_HTML = doctest.register_optionflag('PARSE_HTML')
PARSE_XML = doctest.register_optionflag('PARSE_XML')
NOPARSE_MARKUP = doctest.register_optionflag('NOPARSE_MARKUP')

OutputChecker = doctest.OutputChecker

def strip(v):
    if v is None:
        return None
    else:
        return v.strip()

def norm_whitespace(v):
    return _norm_whitespace_re.sub(' ', v)

_html_parser = etree.HTMLParser(recover=False, remove_blank_text=True)
Beispiel #37
0
#!/usr/bin/python
import doctest
import gc
import glob
import os
import re
import sys
import shutil
import tempfile
import unittest


NODES_VARY = doctest.register_optionflag('NODES_VARY')
RANDOM_OUTPUT = doctest.register_optionflag('RANDOM_OUTPUT')


class RandomOutputChecker(doctest.OutputChecker):

    def check_output(self, want, got, optionflags):
        if optionflags & RANDOM_OUTPUT:
            return True
        return doctest.OutputChecker.check_output(self, want, got, optionflags)


class IgnoreNodeCountChecker(RandomOutputChecker):
    _r = re.compile('\(\d+ nodes\)$', re.MULTILINE)

    def check_output(self, want, got, optionflags):
        if optionflags & NODES_VARY:
            want = self._r.sub('(X nodes)', want)
            got = self._r.sub('(X nodes)', got)
Beispiel #38
0
import doctest, re, sys, subprocess, time, socket, traceback
GLOBAL_FLAGS = 0
LOG_TEST = False
PASS = doctest.register_optionflag("PASS")

old_OutputChecker_check_output = doctest.OutputChecker.check_output
def _custom_check_output(self, want, got, optionflags):
   if optionflags & PASS:
      return True

   return old_OutputChecker_check_output(self, want, got, optionflags)

doctest.OutputChecker.check_output = _custom_check_output


JS_SESSION_ADDRESS = ('', 5001)

class DocTestJSParser(doctest.DocTestParser):
   '''This is an adaptation of the original parser. Instead of
      be using '>>>' for the interactive session and '#' for commenting
      a line, they are replaced by 'js>' and '//' respectively, so they
      can be used with a javascript session.'''
   _EXAMPLE_RE = re.compile(r'''
        # Source consists of a PS1 line followed by zero or more PS2 lines.
        (?P<source>
            (?:^(?P<indent> [ ]*) js>    .*)        # PS1 line
            (?:\n           [ ]*  \.\.\. .*)*)      # PS2 lines
        \n?
        # Want consists of any non-blank lines that do not start with PS1.
        (?P<want> (?:(?![ ]*$)    # Not a blank line
                     (?![ ]*js>)  # Not a line starting with PS1
# -*- coding: utf-8 -*-
from __future__ import print_function
from nose.suite import ContextList
import re
import sys
import os
import codecs
import doctest
from nose.plugins.base import Plugin
from nose.util import tolist, anyp
from nose.plugins.doctests import Doctest, log, DocFileCase

ALLOW_UNICODE = doctest.register_optionflag('ALLOW_UNICODE')

class _UnicodeOutputChecker(doctest.OutputChecker):
    _literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)

    def _remove_u_prefixes(self, txt):
        return re.sub(self._literal_re, r'\1\2', txt)

    def check_output(self, want, got, optionflags):
        res = doctest.OutputChecker.check_output(self, want, got, optionflags)
        if res:
            return True
        if not (optionflags & ALLOW_UNICODE):
            return False

        # ALLOW_UNICODE is active and want != got
        cleaned_want = self._remove_u_prefixes(want)
        cleaned_got = self._remove_u_prefixes(got)
        res = doctest.OutputChecker.check_output(self, cleaned_want, cleaned_got, optionflags)
Beispiel #40
0
from __future__ import absolute_import

import re
import os

from doctest import register_optionflag

import numpy as np
# Import for testing structured array reprs
from numpy import array  # noqa

from nipy.utils import _NoValue
from ..fixes.numpy.testing.noseclasses import (NumpyDoctest,
                                               NumpyOutputChecker)

IGNORE_OUTPUT = register_optionflag('IGNORE_OUTPUT')
SYMPY_EQUAL = register_optionflag('SYMPY_EQUAL')
STRUCTARR_EQUAL = register_optionflag('STRUCTARR_EQUAL')
STRIP_ARRAY_REPR = register_optionflag('STRIP_ARRAY_REPR')
IGNORE_DTYPE = register_optionflag('IGNORE_DTYPE')
NOT_EQUAL = register_optionflag('NOT_EQUAL')
FP_4DP =  register_optionflag('FP_4DP')
FP_6DP =  register_optionflag('FP_6DP')

FP_REG = re.compile(r'(?<![0-9a-zA-Z_.])'
                    r'(\d+\.\d+)'
                    r'(e[+-]?\d+)?'
                    r'(?![0-9a-zA-Z_.])')

def round_numbers(in_str, precision):
    """ Replace fp numbers in `in_str` with numbers rounded to `precision`
Beispiel #41
0
from guild import _api as gapi
from guild import cli
from guild import config as configlib
from guild import guildfile
from guild import index2 as indexlib
from guild import init
from guild import op_util
from guild import run as runlib
from guild import util

PLATFORM = platform.system()

TEST_NAME_WIDTH = 27

NORMALIZE_PATHS = doctest.register_optionflag("NORMALIZE_PATHS")
STRIP_U = doctest.register_optionflag("STRIP_U")
STRIP_L = doctest.register_optionflag("STRIP_L")
WINDOWS = doctest.register_optionflag("WINDOWS")
WINDOWS_ONLY = doctest.register_optionflag("WINDOWS_ONLY")
STRIP_ANSI_FMT = doctest.register_optionflag("STRIP_ANSI_FMT")
PY2 = doctest.register_optionflag("PY2")
PY3 = doctest.register_optionflag("PY3")


def run_all(skip=None):
    return run(all_tests(), skip)


def all_tests():
    test_pattern = os.path.join(tests_dir(), "*.md")
Beispiel #42
0
import guild

from guild import _api as gapi
from guild import cli
from guild import config as configlib
from guild import guildfile
from guild import index2
from guild import init
from guild import run_util
from guild import util

PLATFORM = platform.system()

TEST_NAME_WIDTH = 27

DONT_NORMALIZE_PATH = doctest.register_optionflag("DONT_NORMALIZE_PATH")
SKIP_WINDOWS = doctest.register_optionflag("SKIP_WINDOWS")


def run_all(skip=None):
    return run(all_tests(), skip)


def all_tests():
    test_pattern = os.path.join(tests_dir(), "*.md")
    return sorted(
        [_test_name_from_path(path) for path in glob.glob(test_pattern)])


def tests_dir():
    return os.path.join(os.path.dirname(__file__), "tests")
Beispiel #43
0
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import doctest
from unittest import mock

from lxml import doctestcompare
import six

CHECKER = doctestcompare.LXMLOutputChecker()
PARSE_XML = doctest.register_optionflag('PARSE_XML')


class RequestSideEffect(object):
    def __init__(self):
        self.actions = []
        self.started = False

    def append(self, resp=None, ex=None):
        if not self.started:
            self.actions.append((resp, ex))

    def __call__(self, *args, **kwargs):
        if not self.started:
            self.started = True
            self.actions.reverse()
#!/usr/bin/env python
import sys
import doctest

# handle platform specific issues
WINDOWS = doctest.register_optionflag("WINDOWS")
LINUX = doctest.register_optionflag("LINUX")
SKIP = doctest.register_optionflag("SKIP")

# handle size specific issues
import ctypes
c_int_name = ctypes.c_int.__name__

base = doctest.DocTestRunner
class MyDocTestRunner(base):
    def run(self, test, compileflags=None, out=None, clear_globs=True):
        examples = test.examples[:]
        for ex in test.examples:
            if WINDOWS in ex.options and sys.platform != "win32":
                examples.remove(ex)
            elif LINUX in ex.options and not sys.platform.startswith("linux"):
                examples.remove(ex)
            elif SKIP in ex.options:
                examples.remove(ex)
##            elif "printf(" in ex.source:
##                # handle that doctest doesn't catch printf's output
##                lines = ex.want.splitlines()
##                try:
##                    int(lines[-1])
##                except ValueError:
##                    pass
Beispiel #45
0
# -*- coding: utf-8 -*-
from __future__ import print_function
import re
import sys
import os
import codecs
import doctest
from nose.util import tolist, anyp
from nose.plugins.base import Plugin
from nose.suite import ContextList
from nose.plugins.doctests import Doctest, log, DocFileCase

ALLOW_UNICODE = doctest.register_optionflag('ALLOW_UNICODE')


class _UnicodeOutputChecker(doctest.OutputChecker):
    _literal_re = re.compile(r"(\W|^)[uU]([rR]?[\'\"])", re.UNICODE)

    def _remove_u_prefixes(self, txt):
        return re.sub(self._literal_re, r'\1\2', txt)

    def check_output(self, want, got, optionflags):
        res = doctest.OutputChecker.check_output(self, want, got, optionflags)
        if res:
            return True
        if not (optionflags & ALLOW_UNICODE):
            return False

        # ALLOW_UNICODE is active and want != got
        cleaned_want = self._remove_u_prefixes(want)
        cleaned_got = self._remove_u_prefixes(got)
Beispiel #46
0
#!/usr/bin/env python3
# flake8: noqa

"""Run the tests with https://pytest.org."""

import doctest as doctest
from unittest import mock

import platform
import sys

NO_EXE = doctest.register_optionflag('NO_EXE')

ARGS = [#'--skip-exe',
        #'--only-exe',
        #'--collect-only',
        #'--verbose',
        #'--pdb',
        #'--exitfirst',  # a.k.a. -x
        #'-W', 'error',
        #'--doctest-report none',
        #'--cov-append',
        ]


class NoExeChecker(doctest.OutputChecker):

    def check_output(self, want, got, optionflags, *args, **kwargs) -> bool:
        if optionflags & NO_EXE:
            return True
        return super().check_output(want, got, optionflags, *args, **kwargs)
Beispiel #47
0
deprecated_methods = defaultdict(set)

try:
    from epydoc.cli import TerminalController
except ImportError:
    class TerminalController:
        def __getattr__(self, attr): return ''

term = TerminalController()

######################################################################
# Code
######################################################################

# If we're using py24, then ignore the +SKIP directive.
if sys.version_info[:2] < (2,5): register_optionflag('SKIP')

def strip_quotes(s):
    s = s.strip()
    while (s and (s[0] in "ur") and (s[-1] in "'\"")):
        s = s[1:]
    while (s and (s[0] in "'\"" and (s[0] == s[-1]))):
        s = s[1:-1]
    s = s.strip()
    return s

def find_class(s, index):
    lines = s[:index].split('\n')
    while lines:
        m = CLASS_DEF_RE.match(lines[-1])
        if m: return m.group(1)+'.'
class SageNotAvailable(Exception):
    pass

if _within_sage:
    def sage_method(function):
        function._sage_method = True
        return function
else:
    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)


SNAPPY_FLAG = doctest.register_optionflag('SNAPPY')

def filter_out_snappy(pieces):
    ans = []
    for piece in pieces:
        if _have_snappy or not isinstance(piece, doctest.Example):
            ans.append(piece)
        elif not piece.options.get(SNAPPY_FLAG, False):
            ans.append(piece)
    return ans
            
        
if _within_sage:
    class DocTestParser(doctest.DocTestParser):
        def parse(self, string, name='<string>'):
            string = re.subn('([\n\A]\s*)sage:', '\g<1>>>>', string)[0]
Beispiel #49
0
from plone.testing import layered
from plone.testing.zca import UNIT_TESTING
import doctest
import re
import sys
import unittest


DOCFILES = [
    'message.txt',
    'fields.txt',
    'supermodel.txt',
]

SKIP_PYTHON_2 = doctest.register_optionflag('SKIP_PYTHON_2')
SKIP_PYTHON_3 = doctest.register_optionflag('SKIP_PYTHON_3')
IGNORE_U = doctest.register_optionflag('IGNORE_U')
IGNORE_B = doctest.register_optionflag('IGNORE_B')


class PolyglotOutputChecker(doctest.OutputChecker):

    def check_output(self, want, got, optionflags):
        if optionflags & SKIP_PYTHON_3 and sys.version_info >= (3,):
            return True
        elif optionflags & SKIP_PYTHON_2:
            return True

        if hasattr(self, '_toAscii'):
            got = self._toAscii(got)
            want = self._toAscii(want)
Beispiel #50
0
    """

    testing.buildoutSetUp(test)
    import tempfile
    target_dir = tempfile.mkdtemp('.z3c.autoinclude.test-installs')
    install_projects(test_packages, target_dir)


def testTearDown(test):
    from testdirective.zcml import clear_test_log
    clear_test_log()

    testing.buildoutTearDown(test)


IGNORECASE = doctest.register_optionflag('IGNORECASE')


class IgnoreCaseChecker(renormalizing.RENormalizing, object):
    def __init__(self):
        super(IgnoreCaseChecker, self).__init__([
            # Python 3 drops the u'' prefix on unicode strings
            (re.compile(r"u('[^']*')"), r"\1"),
            # Python 3 adds module name to exceptions
            (re.compile("pkg_resources.DistributionNotFound"),
             r"DistributionNotFound"),
        ])

    def check_output(self, want, got, optionflags):
        if optionflags & IGNORECASE:
            want, got = want.lower(), got.lower()
Beispiel #51
0
import doctest
import os

import numpy as np
import pytest

import hail as hl

SKIP_OUTPUT_CHECK = doctest.register_optionflag('SKIP_OUTPUT_CHECK')


@pytest.fixture(autouse=True)
def patch_doctest_check_output(monkeypatch):
    # FIXME: remove once test output matches docs
    base_check_output = doctest.OutputChecker.check_output

    def patched_check_output(self, want, got, optionflags):
        return ((not want) or (want.strip() == 'None')
                or (SKIP_OUTPUT_CHECK & optionflags) or base_check_output(
                    self, want, got,
                    optionflags | doctest.NORMALIZE_WHITESPACE))

    monkeypatch.setattr('doctest.OutputChecker.check_output',
                        patched_check_output)
    yield
    monkeypatch.undo()


@pytest.fixture(scope="session", autouse=True)
def init(doctest_namespace):
    # This gets run once per process -- must avoid race conditions
Beispiel #52
0
""" Custom doctester based on Numpy doctester
"""
import re
from doctest import register_optionflag
import numpy as np

from ..fixes.numpy.testing.noseclasses import NumpyDoctest, NumpyOutputChecker

IGNORE_OUTPUT = register_optionflag('IGNORE_OUTPUT')
SYMPY_EQUAL = register_optionflag('SYMPY_EQUAL')
STRIP_ARRAY_REPR = register_optionflag('STRIP_ARRAY_REPR')
NOT_EQUAL = register_optionflag('NOT_EQUAL')
FP_4DP = register_optionflag('FP_4DP')
FP_6DP = register_optionflag('FP_6DP')

FP_REG = re.compile(r'(?<![0-9a-zA-Z_.])'
                    r'(\d+\.\d+)'
                    r'(e[+-]?\d+)?'
                    r'(?![0-9a-zA-Z_.])')


def round_numbers(in_str, precision):
    """ Replace fp numbers in `in_str` with numbers rounded to `precision`

    Parameters
    ----------
    in_str : str
        string possibly containing floating point numbers
    precision : int
        number of decimal places to round to
Beispiel #53
0
    # pylint: disable=missing-docstring
    # sets up the environment for doctests (when run through nose)
    np.set_printoptions(precision=5, edgeitems=2, suppress=True)


def teardown():
    # pylint: disable=missing-docstring
    # restore the environment after doctests (when run through nose)
    np.set_printoptions(**_NP_PRINT_OPTIONS)


# Create a doctest output checker that optionally ignores the unicode string
# literal.

# declare the new doctest directives
IGNORE_UNICODE = doctest.register_optionflag("IGNORE_UNICODE")
doctest.IGNORE_UNICODE = IGNORE_UNICODE
doctest.__all__.append("IGNORE_UNICODE")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | IGNORE_UNICODE

NORMALIZE_ARRAYS = doctest.register_optionflag("NORMALIZE_ARRAYS")
doctest.NORMALIZE_ARRAYS = NORMALIZE_ARRAYS
doctest.__all__.append("NORMALIZE_ARRAYS")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | NORMALIZE_ARRAYS

_doctest_OutputChecker = doctest.OutputChecker


class MadmomOutputChecker(_doctest_OutputChecker):
    """
    Output checker which enhances `doctest.OutputChecker` to compare doctests
Beispiel #54
0
    # If the repeat option is set, we add a fixture for the repeat count and
    # parametrize the tests over the repeats. Solution adapted from:
    # http://stackoverflow.com/q/21764473/180783

    if metafunc.config.option.repeat is not None:
        count = int(metafunc.config.option.repeat)
        metafunc.fixturenames.append('tmp_ct')
        metafunc.parametrize('tmp_ct', range(count))


# We monkey-patch in our replacement doctest OutputChecker.  Not
# great, but there isn't really an API to replace the checker when
# using doctest.testfile, unfortunately.
doctest.OutputChecker = AstropyOutputChecker

REMOTE_DATA = doctest.register_optionflag('REMOTE_DATA')


def pytest_configure(config):
    treat_deprecations_as_exceptions()

    config.getini('markers').append(
        'remote_data: Run tests that require data from remote servers')

    # Monkeypatch to deny access to remote resources unless explicitly told
    # otherwise
    if not config.getoption('remote_data'):
        turn_off_internet(verbose=config.option.verbose)

    doctest_plugin = config.pluginmanager.getplugin('doctest')
    if (doctest_plugin is None or config.option.doctestmodules or
Beispiel #55
0
#    under the License.
from __future__ import unicode_literals

import doctest
import logging

import mock
import six
from lxml import doctestcompare

__author__ = 'Jay Xu'

log = logging.getLogger(__name__)

CHECKER = doctestcompare.LXMLOutputChecker()
PARSE_XML = doctest.register_optionflag('PARSE_XML')


class RequestSideEffect(object):
    def __init__(self):
        self.actions = []
        self.started = False

    def append(self, resp=None, ex=None):
        if not self.started:
            self.actions.append((resp, ex))

    def __call__(self, *args, **kwargs):
        if not self.started:
            self.started = True
            self.actions.reverse()
        return self._original_output_checker.check_output(
            self, want, got, flags)

    def output_difference(self, want, got, flags):
        if flags & FIX:
            want, got = self.do_fixes(want, got)
        # Can't use super here because doctest.OutputChecker is not a
        # new-style class.
        return self._original_output_checker.output_difference(
            self, want, got, flags)


# We monkey-patch in our replacement doctest OutputChecker.  Not
# great, but there isn't really an API to replace the checker when
# using doctest.testfile, unfortunately.
FIX = doctest.register_optionflag('FIX')
doctest.OutputChecker = OutputCheckerFix


REMOTE_DATA = doctest.register_optionflag('REMOTE_DATA')


def pytest_configure(config):
    treat_deprecations_as_exceptions()

    # Monkeypatch to deny access to remote resources unless explicitly told
    # otherwise
    if not config.getoption('remote_data'):
        turn_off_internet(verbose=config.option.verbose)

    doctest_plugin = config.pluginmanager.getplugin('doctest')
Beispiel #57
0
    key = ''.join(chr(i) for i in range(16)).encode('utf-8')
    plaintext = ''.join(chr(i) for i in range(64)).encode('utf-8')
    for i in range(64):
        assert SipHash_2_4(key, plaintext[:i]).hexdigest() == vectors[i], \
            'failed on test no %i' % i

    # Internal doctests
    #
    # To maintain compatibility with both python 2.x and 3.x in tests
    # we need to do a trick. Python 2.x doesn't like b'' notation,
    # Python 3.x doesn't have 2222L long integers notation. To
    # overcome that we'll pipe both results as well as the intended
    # doctest output through an `eval` function before comparison. To
    # do it we need to monkeypatch the OutputChecker:
    import doctest
    EVAL_FLAG = doctest.register_optionflag("EVAL")
    OrigOutputChecker = doctest.OutputChecker

    def relaxed_eval(s):
        if s.strip():
            return eval(s)
        else:
            return None

    class MyOutputChecker:
        def __init__(self):
            self.orig = OrigOutputChecker()

        def check_output(self, want, got, optionflags):
            if optionflags & EVAL_FLAG:
                return relaxed_eval(got) == relaxed_eval(want)
Beispiel #58
0
        a space character, then raise ValueError.

        Note: IPython-modified version which takes the input prompt length as a
        parameter, so that prompts of variable length can be dealt with.
        """
        space_idx = indent + ps1_len
        min_len = space_idx + 1
        for i, line in enumerate(lines):
            if len(line) >= min_len and line[space_idx] != ' ':
                raise ValueError(
                    'line %r of the docstring for %s '
                    'lacks blank after %s: %r' %
                    (lineno + i + 1, name, line[indent:space_idx], line))


SKIP = doctest.register_optionflag('SKIP')


class IPDocTestRunner(doctest.DocTestRunner, object):
    """Test runner that synchronizes the IPython namespace with test globals.
    """
    def run(self, test, compileflags=None, out=None, clear_globs=True):

        # Hack: ipython needs access to the execution context of the example,
        # so that it can propagate user variables loaded by %run into
        # test.globs.  We put them here into our modified %run as a function
        # attribute.  Our new %run will then only make the namespace update
        # when called (rather than unconditionally updating test.globs here
        # for all examples, most of which won't be calling %run anyway).
        #_ip._ipdoctest_test_globs = test.globs
        #_ip._ipdoctest_test_filename = test.filename
Beispiel #59
0
        a space character, then raise ValueError.

        Note: IPython-modified version which takes the input prompt length as a
        parameter, so that prompts of variable length can be dealt with.
        """
        space_idx = indent + ps1_len
        min_len = space_idx + 1
        for i, line in enumerate(lines):
            if len(line) >= min_len and line[space_idx] != " ":
                raise ValueError(
                    "line %r of the docstring for %s "
                    "lacks blank after %s: %r" % (lineno + i + 1, name, line[indent:space_idx], line)
                )


SKIP = doctest.register_optionflag("SKIP")


class IPDocTestRunner(doctest.DocTestRunner, object):
    """Test runner that synchronizes the IPython namespace with test globals.
    """

    def run(self, test, compileflags=None, out=None, clear_globs=True):

        # Hack: ipython needs access to the execution context of the example,
        # so that it can propagate user variables loaded by %run into
        # test.globs.  We put them here into our modified %run as a function
        # attribute.  Our new %run will then only make the namespace update
        # when called (rather than unconconditionally updating test.globs here
        # for all examples, most of which won't be calling %run anyway).
        # _ip._ipdoctest_test_globs = test.globs