def test_no_floats(self):
     want = "text ... text"
     got = "text 1.0 1.2 1.9 text"
     output_checker = keras_doctest_lib.KerasDoctestOutputChecker()
     self.assertTrue(
         output_checker.check_output(want=want,
                                     got=got,
                                     optionflags=doctest.ELLIPSIS))
    def test_warning_messages(self, want, got):
        output_checker = keras_doctest_lib.KerasDoctestOutputChecker()

        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)
    def test_fail_tolerences(self, text, expected_floats):
        extract_floats = keras_doctest_lib._FloatExtractor()
        output_checker = keras_doctest_lib.KerasDoctestOutputChecker()

        (_, 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
    def test_extract_floats(self, text, expected_floats):
        extract_floats = keras_doctest_lib._FloatExtractor()
        output_checker = keras_doctest_lib.KerasDoctestOutputChecker()

        (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
Example #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:
        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=keras_doctest_lib.KerasDoctestOutputChecker(),
                optionflags=(doctest.ELLIPSIS
                             | doctest.NORMALIZE_WHITESPACE
                             | doctest.IGNORE_EXCEPTION_DETAIL
                             | doctest.DONT_ACCEPT_BLANKLINE),
            ))
    return tests
 def test_tf_tensor_numpy_output(self, string, expected_output):
     output_checker = keras_doctest_lib.KerasDoctestOutputChecker()
     output = output_checker._tf_tensor_numpy_output(string)
     self.assertEqual(expected_output, output)