def generic_test_main(filename,
                      func,
                      comp=test_utils.equal_to,
                      res_printer=None):
    """
    :param filename - file with test data
    :param func - function to be tested
    :param comp - custom comparator. A function that accepts
        (expected, computed result) and returns a boolean value
    :param res_printer - function for customized printing
    """
    try:
        parser = argparse.ArgumentParser()
        parser.add_argument('--test_data_dir', nargs='?', const=True, type=str)
        parser.add_argument(
            '--run_all_tests', default=False, action='store_true')
        args = parser.parse_args()
        if args.test_data_dir:
            if not os.path.isdir(args.test_data_dir):
                raise RuntimeError(
                    '--test_data_dir argument "{}" is not a directory'.format(
                        args.test_data_dir))
        else:
            args.test_data_dir = test_utils.get_default_test_data_dir_path()

        test_data_path = os.path.join(args.test_data_dir, filename)

        timeout = 0
        stop_on_error = not args.run_all_tests
        test_utils.run_tests(test_data_path,
                             generic_test_handler.GenericTestHandler(
                                 func, comp=comp), timeout, stop_on_error,
                             res_printer)
    except RuntimeError as e:
        print('\nCritical error: {}'.format(e), file=sys.stderr)
    def from_command_line(test_file, test_data_file, timeout_seconds,
                          num_failed_tests_before_stop, commandline_args):
        # Set num_failed_tests_before_stop to 0, means users want to run as
        # many as tests in one run.
        if num_failed_tests_before_stop == 0:
            num_failed_tests_before_stop = float('inf')

        config = TestConfig(test_file, test_data_file, timeout_seconds,
                            num_failed_tests_before_stop)

        parser = argparse.ArgumentParser()
        parser.add_argument(
            '--test-data-dir',
            nargs='?',
            const=True,
            type=str,
            help='path to test_data directory')
        parser.add_argument(
            '--no-verbose',
            dest='verbose',
            default=True,
            action='store_false',
            help='suppress failure description on test failure')
        parser.add_argument(
            '--force-tty',
            dest='tty_mode',
            default=TriBool.INDETERMINATE,
            action='store_const',
            const=TriBool.TRUE,
            help=
            'enable tty features (like printing output on the same line) '
            'even in case stdout is not a tty device'
        )
        parser.add_argument(
            '--no-tty',
            dest='tty_mode',
            action='store_const',
            const=TriBool.FALSE,
            help='never use tty features')
        parser.add_argument(
            '--force-color',
            dest='color_mode',
            default=TriBool.INDETERMINATE,
            action='store_const',
            const=TriBool.TRUE,
            help='enable colored output even in case stdout is not a tty '
                 'device'
        )
        parser.add_argument(
            '--no-color',
            dest='color_mode',
            action='store_const',
            const=TriBool.FALSE,
            help='never use colored output')
        parser.add_argument(
            '--no-update-js',
            dest='update_js',
            default=False,
            action='store_false',
            help='No update problem_mapping.js')
        args = parser.parse_args(commandline_args)

        config.test_data_dir = args.test_data_dir
        config.verbose = args.verbose
        config.tty_mode = args.tty_mode
        config.color_mode = args.color_mode
        config.update_js = args.update_js

        if config.test_data_dir:
            if not os.path.isdir(config.test_data_dir):
                raise RuntimeError(
                    'CL: --test-data-dir argument ({}) is not a directory'.
                        format(config.test_data_dir))
        else:
            config.test_data_dir = get_default_test_data_dir_path()

        return config
Example #3
0
    def from_command_line(
        test_file,
        test_data_file,
        timeout_seconds,
        num_failed_tests_before_stop,
        commandline_args,
    ):
        # Set num_failed_tests_before_stop to 0, means users want to run as many as tests in one run.
        if num_failed_tests_before_stop == 0:
            num_failed_tests_before_stop = float("inf")

        config = TestConfig(
            test_file,
            test_data_file,
            timeout_seconds,
            num_failed_tests_before_stop,
        )

        parser = argparse.ArgumentParser()
        parser.add_argument(
            "--test-data-dir",
            nargs="?",
            const=True,
            type=str,
            help="path to test_data directory",
        )
        parser.add_argument(
            "--force-tty",
            dest="tty_mode",
            default=TriBool.INDETERMINATE,
            action="store_const",
            const=TriBool.TRUE,
            help=
            "enable tty features (like printing output on the same line) even in case stdout is not a tty device",
        )
        parser.add_argument(
            "--no-tty",
            dest="tty_mode",
            action="store_const",
            const=TriBool.FALSE,
            help="never use tty features",
        )
        parser.add_argument(
            "--force-color",
            dest="color_mode",
            default=TriBool.INDETERMINATE,
            action="store_const",
            const=TriBool.TRUE,
            help=
            "enable colored output even in case stdout is not a tty device",
        )
        parser.add_argument(
            "--no-color",
            dest="color_mode",
            action="store_const",
            const=TriBool.FALSE,
            help="never use colored output",
        )
        parser.add_argument(
            "--no-update-js",
            dest="update_js",
            default=True,
            action="store_false",
            help="no update problem_mapping.js",
        )
        parser.add_argument(
            "--no-complexity",
            dest="analyze_complexity",
            default=False,
            action="store_false",
            help="no asymptotic complexity estimation",
        )
        args = parser.parse_args(commandline_args)

        config.test_data_dir = args.test_data_dir
        config.tty_mode = args.tty_mode
        config.color_mode = args.color_mode
        config.update_js = args.update_js
        config.analyze_complexity = args.analyze_complexity

        if config.test_data_dir:
            if not os.path.isdir(config.test_data_dir):
                raise RuntimeError(
                    "CL: --test-data-dir argument ({}) is not a directory".
                    format(config.test_data_dir))
        else:
            config.test_data_dir = get_default_test_data_dir_path()

        return config
    def from_command_line(test_file, test_data_file, timeout_seconds,
                          num_failed_tests_before_stop, commandline_args):
        # Set num_failed_tests_before_stop to 0, means users want to run as many as tests in one run.
        if num_failed_tests_before_stop == 0:
            num_failed_tests_before_stop = float('inf')

        config = TestConfig(test_file, test_data_file, timeout_seconds,
                            num_failed_tests_before_stop)

        parser = argparse.ArgumentParser()
        parser.add_argument(
            '--test-data-dir',
            nargs='?',
            const=True,
            type=str,
            help='path to test_data directory')
        parser.add_argument(
            '--no-verbose',
            dest='verbose',
            default=True,
            action='store_false',
            help='suppress failure description on test failure')
        parser.add_argument(
            '--force-tty',
            dest='tty_mode',
            default=TriBool.INDETERMINATE,
            action='store_const',
            const=TriBool.TRUE,
            help=
            'enable tty features (like printing output on the same line) even in case stdout is not a tty device'
        )
        parser.add_argument(
            '--no-tty',
            dest='tty_mode',
            action='store_const',
            const=TriBool.FALSE,
            help='never use tty features')
        parser.add_argument(
            '--force-color',
            dest='color_mode',
            default=TriBool.INDETERMINATE,
            action='store_const',
            const=TriBool.TRUE,
            help='enable colored output even in case stdout is not a tty device'
        )
        parser.add_argument(
            '--no-color',
            dest='color_mode',
            action='store_const',
            const=TriBool.FALSE,
            help='never use colored output')
        parser.add_argument(
            '--no-update-js',
            dest='update_js',
            default=False,
            action='store_false',
            help='No update problem_mapping.js')
        args = parser.parse_args(commandline_args)

        config.test_data_dir = args.test_data_dir
        config.verbose = args.verbose
        config.tty_mode = args.tty_mode
        config.color_mode = args.color_mode
        config.update_js = args.update_js

        if config.test_data_dir:
            if not os.path.isdir(config.test_data_dir):
                raise RuntimeError(
                    'CL: --test-data-dir argument ({}) is not a directory'.
                    format(config.test_data_dir))
        else:
            config.test_data_dir = get_default_test_data_dir_path()

        return config