示例#1
0
 def test_no_floats(self):
   want = 'text ... text'
   got = 'text 1.0 1.2 1.9 text'
   output_checker = tf_doctest_lib.TfDoctestOutputChecker()
   self.assertTrue(
       output_checker.check_output(
           want=want, got=got, optionflags=doctest.ELLIPSIS))
示例#2
0
  def test_warning_messages(self, want, got):
    output_checker = tf_doctest_lib.TfDoctestOutputChecker()

    output_checker.check_output(
        want=want, got=got, optionflags=doctest.ELLIPSIS)

    example = doctest.Example('None', want=want)
    result = output_checker.output_difference(
        example=example, got=got, optionflags=doctest.ELLIPSIS)
    self.assertIn("doesn't work if *some* of the", result)
示例#3
0
def load_tests(unused_loader, tests, unused_ignore):
    """Loads all the tests in the docstrings and runs them."""

    tf_modules = find_modules()

    if FLAGS.module:
        tf_modules = filter_on_submodules(tf_modules, FLAGS.module)

    if FLAGS.list:
        print('**************************************************')
        for mod in tf_modules:
            print(mod.__name__)
        print('**************************************************')
        return tests

    test_shard_index = int(os.environ.get('TEST_SHARD_INDEX', '0'))
    total_test_shards = int(os.environ.get('TEST_TOTAL_SHARDS', '1'))

    tf_modules = sorted(tf_modules, key=lambda mod: mod.__name__)
    for n, module in enumerate(tf_modules):
        if (n % total_test_shards) != test_shard_index:
            continue

        # If I break the loop comprehension, then the test times out in `small`
        # size.
        if any(
                module.__name__.startswith(package + prefix)  # pylint: disable=g-complex-comprehension
                for prefix in FLAGS.module_prefix_skip
                for package in PACKAGES):
            continue
        testcase = TfTestCase()
        tests.addTests(
            doctest.DocTestSuite(
                module,
                test_finder=doctest.DocTestFinder(exclude_empty=False),
                extraglobs={
                    'tf': tf,
                    'np': np,
                    'os': os
                },
                setUp=testcase.set_up,
                tearDown=testcase.tear_down,
                checker=tf_doctest_lib.TfDoctestOutputChecker(),
                optionflags=(doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
                             | doctest.IGNORE_EXCEPTION_DETAIL
                             | doctest.DONT_ACCEPT_BLANKLINE),
            ))
    return tests
    def test_fail_tolerences(self, text, expected_floats):
        extract_floats = tf_doctest_lib._FloatExtractor()
        output_checker = tf_doctest_lib.TfDoctestOutputChecker()

        (_, extracted_floats) = extract_floats(text)

        # These floats should not match according to allclose
        try:
            self.assertFalse(
                output_checker._allclose(expected_floats, extracted_floats))
        except AssertionError as e:
            msg = ('\n\nThese matched! They should not have.\n'
                   '\n\n  Expected:  {}\n  found:     {}'.format(
                       expected_floats, extracted_floats))
            e.args = (e.args[0] + msg, )
            raise e
示例#5
0
def load_tests(unused_loader, tests, unused_ignore):
    """Loads all the tests in the docstrings and runs them."""

    tf_modules = find_modules()

    if FLAGS.module:
        tf_modules = filter_on_submodules(tf_modules, FLAGS.module)

    if FLAGS.list:
        print('**************************************************')
        for mod in tf_modules:
            print(mod.__name__)
        print('**************************************************')
        return tests

    if FLAGS.file:
        tf_modules = get_module_and_inject_docstring(FLAGS.file)

    for module in tf_modules:
        if any(
                module.__name__.startswith(PACKAGE + prefix)
                for prefix in FLAGS.module_prefix_skip):
            continue
        testcase = TfTestCase()
        tests.addTests(
            doctest.DocTestSuite(
                module,
                test_finder=doctest.DocTestFinder(exclude_empty=False),
                extraglobs={
                    'tf': tf,
                    'np': np,
                    'os': os
                },
                setUp=testcase.set_up,
                tearDown=testcase.tear_down,
                checker=tf_doctest_lib.TfDoctestOutputChecker(),
                optionflags=(doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE
                             | doctest.IGNORE_EXCEPTION_DETAIL
                             | doctest.DONT_ACCEPT_BLANKLINE),
            ))
    return tests
    def test_extract_floats(self, text, expected_floats):
        extract_floats = tf_doctest_lib._FloatExtractor()
        output_checker = tf_doctest_lib.TfDoctestOutputChecker()

        (text_parts, extracted_floats) = extract_floats(text)
        text_with_wildcards = '...'.join(text_parts)

        # Check that the lengths match before doing anything else.
        try:
            self.assertLen(extracted_floats, len(expected_floats))
        except AssertionError as e:
            msg = '\n\n  expected: {}\n  found:     {}'.format(
                expected_floats, extracted_floats)
            e.args = (e.args[0] + msg, )
            raise e

        # The floats should match according to allclose
        try:
            self.assertTrue(
                output_checker._allclose(expected_floats, extracted_floats))
        except AssertionError as e:
            msg = '\n\nexpected:  {}\nfound:     {}'.format(
                expected_floats, extracted_floats)
            e.args = (e.args[0] + msg, )
            raise e

        # The wildcard text should match the input text, according to the
        # OutputChecker base class.
        try:
            self.assertTrue(doctest.OutputChecker().check_output(
                want=text_with_wildcards,
                got=text,
                optionflags=doctest.ELLIPSIS))
        except AssertionError as e:
            msg = '\n\n  expected: {}\n  found:     {}'.format(
                text_with_wildcards, text)
            e.args = (e.args[0] + msg, )
            raise e
 def test_tf_tensor_numpy_output(self, string, expected_output):
     output_checker = tf_doctest_lib.TfDoctestOutputChecker()
     output = output_checker._tf_tensor_numpy_output(string)
     self.assertEqual(expected_output, output)
示例#8
0
 def test_extra_spaces(self, want, got):
     output_checker = tf_doctest_lib.TfDoctestOutputChecker()
     self.assertTrue(
         output_checker.check_output(want=want,
                                     got=got,
                                     optionflags=doctest.ELLIPSIS))