def _compile_and_run(self, lang, format, template_file,
                         expected_generated_code_file, input_file):
        code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
        exec_file, exec_args = self._exec_file_and_args(lang)
        compile_cmd = self._compile_command(lang, code_file)
        args = CodeGenArgs(template=load_text_file(template_file),
                           format_=format,
                           constants=ProblemConstantSet(123, "yes", "NO"),
                           config=CodeStyleConfig(lang=lang.name))
        code = lang.default_code_generator(args)
        # to remove version strings from test resources
        code = re.sub(r'Generated by \d+.\d+.\d+', 'Generated by x.y.z', code)
        self.compare_two_texts_ignoring_trailing_spaces(
            load_text_file(expected_generated_code_file), code)
        create_code(code, code_file)
        try:
            print("Executing:", compile_cmd)
            print(run_command(compile_cmd, self.temp_dir))

            print("Run program:", [exec_file] + exec_args)
            exec_result = run_program(exec_file, input_file, 2, exec_args,
                                      self.temp_dir)
        finally:
            self._clean_up(lang)

        print("== stdout ==")
        print(exec_result.output)
        print("== stderr ==")
        print(exec_result.stderr)

        self.assertEqual(exec_result.status.NORMAL, exec_result.status)
        return exec_result
    def _compile_and_run(self, lang, format, template_file,
                         expected_generated_code_file, input_file):
        code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
        exec_file, exec_args = self._exec_file_and_args(lang)
        compile_cmd = self._compile_command(lang, code_file)

        args = CodeGenArgs(template=load_text_file(template_file),
                           format_=format,
                           constants=ProblemConstantSet(123, "yes", "NO"),
                           config=CodeStyleConfig())

        code = lang.default_code_generator(args)
        self.compare_two_texts_ignoring_trailing_spaces(
            load_text_file(expected_generated_code_file), code)
        create_code(code, code_file)
        print(run_command(compile_cmd, self.temp_dir))
        exec_result = run_program(exec_file, input_file, 2, exec_args,
                                  self.temp_dir)
        print("== stdout ==")
        print(exec_result.output)
        print("== stderr ==")
        print(exec_result.stderr)

        self.assertEqual(exec_result.status.NORMAL, exec_result.status)
        return exec_result
    def _compile_and_run(self, lang, format, template_file, expected_generated_code_file, input_file):
        code_file = os.path.join(self.temp_dir, lang.source_code_name("main"))
        exec_file, exec_args = self._exec_file_and_args(lang)
        compile_cmd = self._compile_command(lang, code_file)

        args = CodeGenArgs(
            template=load_text_file(template_file),
            format_=format,
            constants=ProblemConstantSet(123, "yes", "NO"),
            config=CodeStyleConfig()
        )

        code = lang.default_code_generator(args)
        # to remove version strings from test resources
        code = re.sub(r'Generated by \d+.\d+.\d+', 'Generated by x.y.z', code)
        self.compare_two_texts_ignoring_trailing_spaces(
            load_text_file(expected_generated_code_file), code)
        create_code(code, code_file)
        print(run_command(compile_cmd, self.temp_dir))
        exec_result = run_program(
            exec_file, input_file, 2, exec_args, self.temp_dir)
        print("== stdout ==")
        print(exec_result.output)
        print("== stderr ==")
        print(exec_result.stderr)

        self.assertEqual(exec_result.status.NORMAL, exec_result.status)
        return exec_result
    def test_default_code_generators_and_templates(self):
        def _full_path(filename):
            return os.path.join(RESOURCE_DIR,
                                "test_default_code_generators_and_templates",
                                filename)

        input_file = _full_path("echo_test_input.txt")
        expected_output_file = _full_path("echo_test_output.txt")
        pred_result = predict_format(
            ProblemContent(load_text_file(
                _full_path("echo_test_format.txt")), [
                    Sample(load_text_file(_full_path("echo_test_input.txt")),
                           None)
                ]))

        for lang in ALL_LANGUAGES:
            expected_default_generated_code_file = _full_path(
                os.path.join(
                    lang.name,
                    lang.source_code_name("expected_default_generated_code")))

            # 1. Compile test with default templates

            self._compile_and_run(lang, pred_result.format,
                                  lang.default_template_path,
                                  expected_default_generated_code_file,
                                  input_file)

            # 2. Echo test, which tests custom templates having echo output of input

            exec_result = self._compile_and_run(
                lang, pred_result.format,
                _full_path(
                    os.path.join(lang.name,
                                 lang.source_code_name("echo_template"))),
                _full_path(
                    os.path.join(
                        lang.name,
                        lang.source_code_name(
                            "expected_echo_generated_code"))), input_file)
            self.assertEqual(load_text_file(expected_output_file),
                             exec_result.output)
    def test_default_code_generators_and_templates(self):
        def _full_path(filename):
            return os.path.join(RESOURCE_DIR, "test_default_code_generators_and_templates", filename)

        input_file = _full_path("echo_test_input.txt")
        expected_output_file = _full_path("echo_test_output.txt")
        pred_result = predict_format(
            ProblemContent(
                load_text_file(_full_path("echo_test_format.txt")),
                [Sample(load_text_file(_full_path("echo_test_input.txt")), None)]))

        for lang in ALL_LANGUAGES:
            expected_default_generated_code_file = _full_path(
                os.path.join(lang.name, lang.source_code_name("expected_default_generated_code")))

            # 1. Compile test with default templates

            self._compile_and_run(
                lang,
                pred_result.format,
                lang.default_template_path,
                expected_default_generated_code_file,
                input_file
            )

            # 2. Echo test, which tests custom templates having echo output of input

            exec_result = self._compile_and_run(
                lang,
                pred_result.format,
                _full_path(os.path.join(
                    lang.name, lang.source_code_name("echo_template"))),
                _full_path(os.path.join(lang.name, lang.source_code_name(
                    "expected_echo_generated_code"))),
                input_file
            )
            self.assertEqual(load_text_file(
                expected_output_file), exec_result.output)
        result.constant_set = ProblemConstantSet()
        return
    try:
        result.modulo = predict_modulo(result.problem_content.original_html)
    except Exception as e:
        result.modulo_error = e

    result.yes_str, result.no_str = predict_yes_no(
        result.problem_content.original_html)

    result.constant_set = ProblemConstantSet(mod=result.modulo,
                                             yes_str=result.yes_str,
                                             no_str=result.no_str)


CPP_TEMPLATE = load_text_file(CPP.default_template_path)
JAVA_TEMPLATE = load_text_file(JAVA.default_template_path)
RUST_TEMPLATE = load_text_file(RUST.default_template_path)
PYTHON_TEMPLATE = load_text_file(PYTHON.default_template_path)
D_TEMPLATE = load_text_file(DLANG.default_template_path)
NIM_TEMPLATE = load_text_file(NIM.default_template_path)


def generate_code(result: QualityResult):
    if result.prediction_result is None:
        result_format = None
    else:
        result_format = result.prediction_result.format

    result.codes["cpp"] = apply_clang(
        CPP.default_code_generator(
        result.no_str_error = Skipped()
        return
    try:
        result.modulo = predict_modulo(result.problem_content.original_html)
    except Exception as e:
        result.modulo_error = e

    result.yes_str, result.no_str = predict_yes_no(
        result.problem_content.original_html)

    result.constant_set = ProblemConstantSet(mod=result.modulo,
                                             yes_str=result.yes_str,
                                             no_str=result.no_str)


CPP_TEMPLATE = load_text_file(CPP.default_template_path)
JAVA_TEMPLATE = load_text_file(JAVA.default_template_path)
RUST_TEMPLATE = load_text_file(RUST.default_template_path)


def generate_code(result: QualityResult):
    if result.prediction_result is not None:
        result.codes["cpp"] = apply_clang(
            CPP.default_code_generator(
                CodeGenArgs(CPP_TEMPLATE, result.prediction_result.format,
                            result.constant_set, CodeStyleConfig())))
        result.codes["java"] = apply_clang(
            JAVA.default_code_generator(
                CodeGenArgs(JAVA_TEMPLATE, result.prediction_result.format,
                            result.constant_set, CodeStyleConfig())))
        result.codes["rust"] = apply_clang(
    try:
        result.modulo = predict_modulo(result.problem_content.original_html)
    except Exception as e:
        result.modulo_error = e

    result.yes_str, result.no_str = predict_yes_no(
        result.problem_content.original_html)

    result.constant_set = ProblemConstantSet(
        mod=result.modulo,
        yes_str=result.yes_str,
        no_str=result.no_str
    )


CPP_TEMPLATE = load_text_file(CPP.default_template_path)
JAVA_TEMPLATE = load_text_file(JAVA.default_template_path)
RUST_TEMPLATE = load_text_file(RUST.default_template_path)
PYTHON_TEMPLATE = load_text_file(PYTHON.default_template_path)


def generate_code(result: QualityResult):
    if result.prediction_result is None:
        result_format = None
    else:
        result_format = result.prediction_result.format

    result.codes["cpp"] = apply_clang(CPP.default_code_generator(CodeGenArgs(
        CPP_TEMPLATE,
        result_format,
        result.constant_set,