Beispiel #1
0
    def test_tree_of_transformations(self):
        # ARRANGE #

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'the_to_upper_transformer', string_transformers.to_uppercase())

        keep_line_1 = StringTransformerSymbolContext.of_primitive(
            'keep_line_1', string_transformers.keep_single_line(1))

        keep_line_2 = StringTransformerSymbolContext.of_primitive(
            'keep_line_2', string_transformers.keep_single_line(2))

        equals_1st = StringMatcherSymbolContext.of_primitive(
            'equals_1st', string_matchers.EqualsConstant('1ST\n'))

        equals_2nd = StringMatcherSymbolContext.of_primitive(
            'equals_2nd', string_matchers.EqualsConstant('2ND\n'))

        model__original = '1st\n2nd\n'

        symbol_contexts = [
            to_upper_transformer,
            keep_line_1,
            equals_1st,
            keep_line_2,
            equals_2nd,
        ]

        # ACT & ASSERT #

        integration_check.CHECKER__PARSE_FULL.check__w_source_variants(
            self,
            args2.Transformed(
                to_upper_transformer.name__sym_ref_syntax,
                args2.Parentheses(
                    args2.conjunction([
                        args2.Parentheses(
                            args2.Transformed(
                                keep_line_1.name__sym_ref_syntax,
                                args2.SymbolReference(equals_1st.name),
                            )),
                        args2.Parentheses(
                            args2.Transformed(
                                keep_line_2.name__sym_ref_syntax,
                                args2.SymbolReference(equals_2nd.name),
                            )),
                    ]))).as_arguments,
            model_constructor.of_str(self, model__original),
            arrangement_w_tcds(symbols=SymbolContext.symbol_table_of_contexts(
                symbol_contexts)),
            Expectation(
                ParseExpectation(
                    symbol_references=SymbolContext.
                    references_assertion_of_contexts(symbol_contexts)),
                ExecutionExpectation(
                    main_result=asrt_matching_result.matches_value(True))))
Beispiel #2
0
 def runTest(self):
     string_transformer = StringTransformerSymbolContext.of_primitive(
         'the_string_transformer', string_transformers.arbitrary())
     for case in string_matcher_failing_validation_cases.failing_validation_cases(
     ):
         with self.subTest(validation_case=case.name):
             symbol_context = case.value.symbol_context
             integration_check.CHECKER__PARSE_FULL.check__w_source_variants(
                 self,
                 args2.Transformed(
                     string_transformer.name,
                     args2.SymbolReference(
                         symbol_context.name)).as_arguments,
                 model_constructor.must_not_be_used,
                 Arrangement(
                     symbols=SymbolContext.symbol_table_of_contexts([
                         string_transformer,
                         symbol_context,
                     ])),
                 Expectation(
                     ParseExpectation(
                         symbol_references=asrt.matches_sequence([
                             string_transformer.reference_assertion,
                             symbol_context.reference_assertion,
                         ]), ),
                     ExecutionExpectation(
                         validation=case.value.expectation, ),
                 ),
             )
Beispiel #3
0
    def _file_contents_cases(self) -> InvalidDestinationFileTestCasesData:
        arbitrary_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        symbols = arbitrary_transformer.symbol_table

        relativity_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        src_file = relativity_conf.path_abs_stx_of_name('src-file.txt')

        contents_abs_stx_builder = string_source_abs_stx.TransformableAbsStxBuilder(
            string_source_abs_stx.StringSourceOfFileAbsStx(src_file))

        src_file_in_hds_contents = relativity_conf.populator_for_relativity_option_root(
            DirContents([File.empty(src_file.name)]))

        file_contents_cases = [
            NameAndValue(
                'contents of existing file / without transformation',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    contents_abs_stx_builder.without_transformation())),
            NameAndValue(
                'contents of existing file / with transformation',
                fs_abs_stx.FileContentsExplicitAbsStx(
                    fs_abs_stx.ModificationType.CREATE,
                    contents_abs_stx_builder.with_transformation(
                        arbitrary_transformer.abstract_syntax))),
        ]

        return InvalidDestinationFileTestCasesData(file_contents_cases,
                                                   symbols,
                                                   src_file_in_hds_contents)
Beispiel #4
0
    def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess):
        # ARRANGE #
        named_transformer = StringTransformerSymbolContext.of_primitive(
            'the_transformer', every_line_empty())

        original_file_contents = 'some\ntext'

        symbols = SymbolTable(
            {named_transformer.name: named_transformer.symbol_table_container})

        expected_symbol_reference_to_transformer = is_reference_to_string_transformer(
            named_transformer.name)

        expected_symbol_usages = asrt.matches_sequence(
            [expected_symbol_reference_to_transformer])

        self._check_with_source_variants(
            test_configuration.arguments_for(
                args(
                    '{transform_option} {the_transformer} {maybe_not} {empty}',
                    the_transformer=named_transformer.name,
                    maybe_not=maybe_not.
                    nothing__if_positive__not_option__if_negative)),
            model_constructor.of_str(self, original_file_contents),
            arrangement_w_tcds(post_population_action=
                               MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY,
                               symbols=symbols),
            Expectation(
                ParseExpectation(symbol_references=expected_symbol_usages),
                ExecutionExpectation(
                    main_result=maybe_not.pass__if_positive__fail__if_negative,
                ),
            ))
Beispiel #5
0
    def test_transformation_SHOULD_apply_only_to_matcher_argument(self):
        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'the_to_upper_transformer', string_transformers.to_uppercase())

        model = contents_transformation.TransformedContentsSetup(
            original='text',
            transformed='TEXT',
        )

        integration_check.CHECKER__PARSE_FULL.check__w_source_variants(
            self,
            args2.conjunction([
                args2.Parentheses(
                    args2.Transformed(
                        to_upper_transformer.name,
                        args2.Equals.eq_string(model.transformed)), ),
                args2.Equals.eq_string(model.original),
            ]).as_arguments, model_constructor.of_str(self, model.original),
            arrangement_w_tcds(symbols=to_upper_transformer.symbol_table),
            Expectation(
                ParseExpectation(
                    symbol_references=asrt.matches_singleton_sequence(
                        is_reference_to_string_transformer(
                            to_upper_transformer.name))),
                ExecutionExpectation(
                    main_result=asrt_matching_result.matches_value(True))))
Beispiel #6
0
 def _sym_ctx_of(name: str, may_dep: bool) -> SymbolContext:
     return StringTransformerSymbolContext.of_primitive(
         name,
         string_transformers.StringTransformerFromLinesTransformation(
             name,
             lambda x: x,
             is_identity=False,
             transformation_may_depend_on_external_resources=may_dep,
         ))
Beispiel #7
0
    def runTest(self):
        # ARRANGE #

        src_file = fs.File('source-file.txt', 'contents of source file')
        src_rel_opt_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        expected_file = fs.File('a-file-name.txt', src_file.contents.upper())
        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT)

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)),
            to_upper_transformer.abstract_syntax,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            transformed_file_syntax,
        )

        expected_non_hds_contents = dst_rel_opt_conf.assert_root_dir_contains_exactly(
            fs.DirContents([expected_file]))

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        tcds_contents=src_rel_opt_conf.
                        populator_for_relativity_option_root(
                            DirContents([src_file])),
                        symbols=symbols,
                    ),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=expected_non_hds_contents,
                    ))
Beispiel #8
0
    def runTest(self):
        text_printed_by_program = 'single line of output'

        expected_file_contents = text_printed_by_program.upper()
        expected_file = fs.File('dst-file.txt', expected_file_contents)

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TO_UPPER_CASE',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP)

        program_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.FullProgramAbsStx(
                program_abs_stx.ProgramOfPythonInterpreterAbsStx.
                of_execute_python_src_string(
                    py_programs.single_line_pgm_that_prints_to(
                        ProcOutputFile.STDOUT, text_printed_by_program)),
                transformation=to_upper_transformer.abstract_syntax,
            ))
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            program_syntax,
        )

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        symbols=symbols),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=non_hds_dir_contains_exactly(
                            dst_rel_opt_conf.root_dir__non_hds,
                            fs.DirContents([expected_file])),
                    ))
Beispiel #9
0
    def test__with_transformer(self):
        # ARRANGE #
        src_file = fs.File('source-file.txt', 'contents of source file')
        expected_file = fs.File('a-file-name.txt', src_file.contents.upper())

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_rel_opt_conf = ALLOWED_SRC_FILE_RELATIVITIES__BEFORE_ACT[0]
        dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT)

        expected_non_hds_contents = self._expected_non_hds_contents(
            dst_rel_opt_conf,
            expected_file,
            src_rel_opt_conf,
            src_file,
        )

        transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)),
            to_upper_transformer.abstract_syntax,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_rel_opt_conf.path_abs_stx_of_name(expected_file.file_name),
            transformed_file_contents_abs_stx,
        )

        symbols = to_upper_transformer.symbol_table

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            with self.subTest(phase_is_after_act=phase_is_after_act):
                # ACT & ASSERT #
                checker.check__abs_stx__std_layouts_and_source_variants(
                    self, instruction_syntax,
                    Arrangement.phase_agnostic(
                        tcds=TcdsArrangement(
                            pre_population_action=
                            SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN,
                            tcds_contents=src_rel_opt_conf.
                            populator_for_relativity_option_root(
                                DirContents([src_file])),
                        ),
                        symbols=symbols,
                    ),
                    MultiSourceExpectation.phase_agnostic(
                        main_result=IS_SUCCESS,
                        main_side_effects_on_sds=expected_non_hds_contents,
                        symbol_usages=to_upper_transformer.usages_assertion,
                    ))
Beispiel #10
0
    def _check_of_invalid_src_file(
            self, is_after_act_2_every_src_file_rel_conf: Callable[
                [bool], Sequence[RelativityOptionConfiguration]],
            step_of_expected_failure: Step):
        # ARRANGE #
        transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )
        symbols = transformer.symbol_table

        dst_file_rel_conf = conf_rel_any(RelOptionType.REL_TMP)

        expectation_ = self._expect_failure_in(step_of_expected_failure)

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            for src_file_rel_conf in is_after_act_2_every_src_file_rel_conf(
                    phase_is_after_act):
                contents_builder = string_source_abs_stx.TransformableAbsStxBuilder(
                    string_source_abs_stx.StringSourceOfFileAbsStx(
                        src_file_rel_conf.path_abs_stx_of_name(
                            self.src_file_name)))
                for actual_src_file_variant in self.src_file_variants:
                    for contents_arguments in contents_builder.with_and_without_transformer_cases(
                            transformer.abstract_syntax):
                        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                            dst_file_rel_conf.path_abs_stx_of_name(
                                'dst-file.txt'),
                            contents_arguments.value,
                        )
                        # ACT & ASSERT #
                        checker.check__abs_stx__std_layouts_and_source_variants(
                            self,
                            instruction_syntax,
                            Arrangement.phase_agnostic(
                                tcds=TcdsArrangement(
                                    pre_population_action=
                                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN,
                                    tcds_contents=src_file_rel_conf.
                                    populator_for_relativity_option_root(
                                        actual_src_file_variant.value),
                                ),
                                symbols=symbols,
                            ),
                            expectation_,
                            sub_test_identifiers={
                                'phase_is_after_act': phase_is_after_act,
                                'contents': contents_arguments.name,
                                'relativity_of_src_path':
                                src_file_rel_conf.name,
                            })
    def runTest(self):
        # ARRANGE #

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_file = fs.File('src-file.txt', 'contents of source file')
        src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        expected_contents = src_file.contents.upper()

        path_relativity_variants = PathRelativityVariants({src_file_rel_conf.relativity}, True)
        checker = integration_check.checker(RelOptionsConfiguration(path_relativity_variants,
                                                                    src_file_rel_conf.relativity))

        src_file_symbol = ConstantSuffixPathDdvSymbolContext(
            'SRC_FILE_SYMBOL',
            src_file_rel_conf.relativity,
            src_file.name,
            path_relativity_variants,
        )
        string_source_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(src_file_symbol.abstract_syntax),
            to_upper_transformer.abstract_syntax,
        )
        symbol_table = SymbolContext.symbol_table_of_contexts([
            src_file_symbol,
            to_upper_transformer,
        ])

        # ACT & ASSERT #
        checker.check__abs_stx__layouts__std_source_variants__wo_input(
            self,
            OptionallyOnNewLine(string_source_syntax),
            arrangement_w_tcds(
                symbols=symbol_table,
                hds_contents=src_file_rel_conf.populator_for_relativity_option_root__hds(
                    DirContents([src_file]))
            ),
            MultiSourceExpectation.of_prim__const(
                asrt_string_source.pre_post_freeze__matches_str__const(
                    expected_contents,
                    may_depend_on_external_resources=True,
                ),
                symbol_references=asrt.matches_sequence([
                    src_file_symbol.reference_assertion__path_or_string,
                    to_upper_transformer.reference_assertion,
                ]),
            ),
        )
Beispiel #12
0
    def runTest(self):
        # ARRANGE #
        symbol_1 = NameAndValue('symbol_1_name', 'added by 1st transformer')
        symbol_2 = NameAndValue('symbol_2_name', 'added by 2nd transformer')
        symbol_3 = NameAndValue('symbol_3_name', 'added by 3rd transformer')

        cases = [
            NameAndValue('2 transformers', [
                symbol_1,
                symbol_2,
            ]),
            NameAndValue('3 transformers', [
                symbol_1,
                symbol_2,
                symbol_3,
            ]),
        ]
        for case in cases:
            sequenced_transformer_symbols = case.value

            arguments = st_args.syntax_for_sequence_of_transformers(
                [symbol.name for symbol in sequenced_transformer_symbols])

            initial_line = 'initial line\n'
            initial_model_lines = [initial_line]

            expected_output_lines = [initial_line] + [
                symbol.value + '\n' for symbol in sequenced_transformer_symbols
            ]

            symbol_contexts = [
                StringTransformerSymbolContext.of_primitive(
                    symbol.name, string_transformers.add_line(symbol.value))
                for symbol in sequenced_transformer_symbols
            ]
            # ACT & ASSERT #

            with self.subTest(case.name):
                integration_check.CHECKER__PARSE_FULL.check__w_source_variants(
                    self, Arguments(arguments),
                    model_constructor.of_lines(self, initial_model_lines),
                    arrangement_w_tcds(
                        symbols=SymbolContext.symbol_table_of_contexts(
                            symbol_contexts)),
                    expectation_of_successful_execution_2(
                        output_lines=expected_output_lines,
                        symbol_references=SymbolContext.
                        references_assertion_of_contexts(symbol_contexts),
                        may_depend_on_external_resources=False,
                        is_identity_transformer=False,
                    ))
Beispiel #13
0
    def runTest(self):
        # ARRANGE #
        named_transformer = StringTransformerSymbolContext.of_primitive(
            _TRANSFORMER_SYMBOL_NAME,
            string_transformers.to_uppercase()
        )

        contents_generator = contents_transformation.TransformedContentsSetup(
            original='some\ntext',
            transformed='SOME\nTEXT',
        )

        symbols = self.rel_opt.symbols.in_arrangement()
        symbols.put(named_transformer.name,
                    named_transformer.symbol_table_container)

        expected_symbol_reference_to_transformer = is_reference_to_string_transformer(named_transformer.name)

        expected_symbol_references = asrt.matches_sequence(
            [expected_symbol_reference_to_transformer] +
            self.rel_opt.symbols.usage_expectation_assertions()
        )
        self._check_with_source_variants(
            test_configuration.arguments_for(
                args(
                    '{transform_option} {transformer} {maybe_not} {equals} '
                    '{file_option} {relativity_option} expected.txt',
                    transformer=named_transformer.name,
                    maybe_not=self.not_opt.nothing__if_positive__not_option__if_negative,
                    relativity_option=self.rel_opt.option_argument)),
            model_constructor.of_str(self, contents_generator.original),
            arrangement_w_tcds(
                tcds_contents=self.rel_opt.populator_for_relativity_option_root(DirContents([
                    File('expected.txt', contents_generator.transformed)
                ])),
                post_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY,
                symbols=symbols,
            ),
            Expectation(
                ParseExpectation(
                    symbol_references=expected_symbol_references,
                ),
                ExecutionExpectation(
                    main_result=self.not_opt.pass__if_positive__fail__if_negative,
                ),
            ),
        )
    def _check_of_invalid_src_file(
            self,
            src_file_relativity: RelOptionType,
            validation: ValidationAssertions,
    ):
        # ARRANGE #
        checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_file_relativity))

        expectation_ = MultiSourceExpectation(
            symbol_references=asrt.anything_goes(),
            execution=ExecutionExpectation(
                validation=validation
            )
        )

        src_file_rel_conf = conf_rel_any(src_file_relativity)

        transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )
        symbols = transformer.symbol_table

        contents_builder = string_source_abs_stx.TransformableAbsStxBuilder(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_file_rel_conf.path_abs_stx_of_name(self.src_file_name)
            )
        )
        for actual_src_file_variant in self.src_file_variants:
            for contents_arguments in contents_builder.with_and_without_transformer_cases(
                    transformer.abstract_syntax):
                with self.subTest(src_file_variant=actual_src_file_variant.name,
                                  contents=contents_arguments.name,
                                  relativity_of_src_path=src_file_rel_conf.option_argument):
                    # ACT & ASSERT #
                    checker.check__abs_stx__layouts__std_source_variants__wo_input(
                        self,
                        OptionallyOnNewLine(contents_arguments.value),
                        arrangement_w_tcds(
                            tcds_contents=src_file_rel_conf.populator_for_relativity_option_root(
                                actual_src_file_variant.value),
                            symbols=symbols,
                        ),
                        expectation_,
                    )
    def test_w_transformation(self):
        str_added_by_transformer = '<string added by transformer>'
        transformer_symbol = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER',
            string_transformers.add(str_added_by_transformer),
        )

        def contents_transformation(s: str) -> str:
            return s + str_added_by_transformer

        # ARRANGE #
        cases: List[NameAndValue[Case]] = [
            NameAndValue('path (within soft quotes)',
                         _path_case(contents_transformation)),
            NameAndValue('list (within soft quotes)',
                         _list_case(contents_transformation)),
        ]

        for case in cases:
            syntax = TransformedStringSourceAbsStx(
                case.value.syntax,
                transformer_symbol.abstract_syntax,
            )
            all_symbols = [case.value.symbol, transformer_symbol]
            # ACT & ASSERT #
            CHECKER.check__abs_stx__layouts__std_source_variants__wo_input(
                self,
                syntax,
                arrangement_w_tcds(
                    symbols=SymbolContext.symbol_table_of_contexts(
                        all_symbols), ),
                MultiSourceExpectation.of_prim(
                    symbol_references=asrt.matches_sequence([
                        references.is_reference_to__string(
                            case.value.symbol.name),
                        transformer_symbol.reference_assertion,
                    ]),
                    primitive=case.value.expectation_of_primitive,
                ),
                sub_test_identifiers={
                    'case': case.name,
                })
    def test__with_transformer(self):
        # ARRANGE #
        src_file = fs.File('source-file.txt', 'contents of source file')
        expected_contents = src_file.contents.upper()

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP)

        transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)
            ),
            to_upper_transformer.abstract_syntax,
        )

        symbols = to_upper_transformer.symbol_table

        checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity))
        # ACT & ASSERT #
        checker.check__abs_stx__layouts__std_source_variants__wo_input(
            self,
            OptionallyOnNewLine(transformed_file_contents_abs_stx),
            arrangement_w_tcds(
                tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root(
                    DirContents([src_file])),
                symbols=symbols,
            ),
            MultiSourceExpectation.of_prim__const(
                asrt_string_source.pre_post_freeze__matches_str__const(
                    expected_contents,
                    may_depend_on_external_resources=True,
                ),
                symbol_references=to_upper_transformer.references_assertion,
            )
        )
Beispiel #17
0
 def runTest(self):
     # ARRANGE #
     program_symbol = ProgramSymbolContext.of_arbitrary_value(
         'PROGRAM_SYMBOL')
     const_true_int_matcher_symbol = IntegerMatcherSymbolContext.of_primitive_constant(
         'CONST_TRUE_INT_MATCHER',
         result=True,
     )
     string_transformer_that_reports_failure_if_applied = StringTransformerSymbolContext.of_primitive(
         'STRING_TRANSFORMER_THAT_MUST_NOT_BE_USED',
         StringTransformerThatFailsTestIfApplied(self))
     all_symbols = [
         program_symbol,
         string_transformer_that_reports_failure_if_applied,
         const_true_int_matcher_symbol,
     ]
     # ACT & ASSERT #
     CHECKER.check__abs_stx(
         self,
         InstructionArguments(
             FullProgramAbsStx(
                 program_symbol.abstract_syntax,
                 transformation=
                 string_transformer_that_reports_failure_if_applied.
                 abstract_syntax),
             const_true_int_matcher_symbol.abstract_syntax,
         ),
         ArrangementPostAct2(
             symbols=SymbolContext.symbol_table_of_contexts(all_symbols),
             process_execution=ProcessExecutionArrangement(
                 os_services_access.new_for_cmd_exe(
                     CommandExecutorThatJustReturnsConstant(1), ), ),
         ),
         Expectation2(
             ParseExpectation(
                 symbol_usages=SymbolContext.usages_assertion_of_contexts(
                     all_symbols), ),
             ExecutionExpectation(main_result=asrt_pfh.is_pass()),
         ))
Beispiel #18
0
    def runTest(self):
        model__original = 'the model text'
        the_model_constructor = model_constructor.of_str(self, model__original)

        string_transformer = StringTransformerSymbolContext.of_primitive(
            'THE_STRING_TRANSFORMER', string_transformers.to_uppercase())
        sm_equals = StringMatcherSymbolContext.of_primitive(
            'STRING_MATCHER_1',
            string_matchers.EqualsConstant(model__original.upper()))
        symbol = [
            string_transformer,
            sm_equals,
        ]

        after_bin_op = 'after bin op'
        sm_conjunction = args2.conjunction([
            args2.SymbolReference(sm_equals.name),
            args2.Custom(after_bin_op),
        ])
        arguments = args2.Transformed(string_transformer.name__sym_ref_syntax,
                                      sm_conjunction)
        integration_check.CHECKER__PARSE_SIMPLE.check(
            self,
            source=arguments.as_remaining_source,
            input_=the_model_constructor,
            arrangement=arrangement_w_tcds(
                symbols=SymbolContext.symbol_table_of_contexts(symbol), ),
            expectation=Expectation(
                ParseExpectation(source=asrt_source.is_at_line(
                    current_line_number=1,
                    remaining_part_of_current_line=' '.join(
                        [sm_conjunction.operator, after_bin_op])),
                                 symbol_references=SymbolContext.
                                 references_assertion_of_contexts(symbol)),
                ExecutionExpectation(
                    main_result=asrt_matching_result.matches_value(True))),
        )
Beispiel #19
0
    def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess):
        # ARRANGE #

        named_transformer = StringTransformerSymbolContext.of_primitive(
            'the_transformer', every_line_empty())

        checked_file = File('actual.txt', 'some\ntext')

        symbols = named_transformer.symbol_table

        expected_symbol_reference_to_transformer = is_reference_to_string_transformer(
            named_transformer.name)

        expected_symbol_usages = asrt.matches_sequence(
            [expected_symbol_reference_to_transformer])

        # ACT & ASSERT #

        self._check_with_source_variants(
            arguments=arguments_for(
                sm_args.args(
                    '{transform_option} {the_transformer} {maybe_not} {empty}',
                    the_transformer=named_transformer.name,
                    maybe_not=maybe_not.
                    nothing__if_positive__not_option__if_negative)),
            model_constructor=integration_check.constant_relative_file_name(
                checked_file.name),
            arrangement=arrangement_w_tcds(
                non_hds_contents=single_file_in_current_dir(checked_file),
                symbols=symbols,
            ),
            expectation=Expectation(
                ParseExpectation(symbol_references=expected_symbol_usages),
                ExecutionExpectation(
                    main_result=maybe_not.pass__if_positive__fail__if_negative,
                ),
            ))
    def test_successful_parse_of_sequence(self):
        # ARRANGE #

        regex_str = 'the_regex'
        replacement_str = 'the_replacement'

        symbol = StringTransformerSymbolContext.of_primitive(
            'the_symbol_name',
            string_transformers.must_not_be_used()
        )

        replace_transformer_syntax = argument_syntax.syntax_for_replace_transformer(regex_str,
                                                                                    replacement_str)

        defined_name = 'defined_name'

        cases = [
            SourceCase('Expression on single line',
                       source=
                       remaining_source(
                           src2(ValueType.STRING_TRANSFORMER,
                                defined_name,
                                argument_syntax.syntax_for_sequence_of_transformers([
                                    symbol.name,
                                    replace_transformer_syntax,
                                ])),
                           following_lines=['following line'],
                       ),
                       source_assertion=asrt_source.is_at_beginning_of_line(2)
                       ),
            SourceCase('Expression on following line',
                       source=
                       remaining_source(
                           src2(ValueType.STRING_TRANSFORMER, defined_name, '{new_line} {transformer_argument}',
                                transformer_argument=argument_syntax.syntax_for_sequence_of_transformers([
                                    symbol.name,
                                    replace_transformer_syntax,
                                ])),
                           following_lines=['following line'],
                       ),
                       source_assertion=asrt_source.is_at_beginning_of_line(3)
                       ),
            SourceCase('1st expr on first line followed by operator, 2nd expr on next line',
                       source=
                       remaining_source(
                           src2(ValueType.STRING_TRANSFORMER, defined_name, '{the_symbol_name} {sequence_operator}',
                                the_symbol_name=symbol.name,
                                sequence_operator=SEQUENCE_OPERATOR_NAME),
                           following_lines=[replace_transformer_syntax],
                       ),
                       source_assertion=asrt_source.source_is_at_end
                       ),
            SourceCase('1st expr on first line followed by operator, 2nd expr on next line, non-exr on 3rd line',
                       source=
                       remaining_source(
                           src2(ValueType.STRING_TRANSFORMER, defined_name, '{the_symbol_name} {sequence_operator}',
                                the_symbol_name=symbol.name,
                                sequence_operator=SEQUENCE_OPERATOR_NAME),
                           following_lines=[replace_transformer_syntax,
                                            'following line'],
                       ),
                       source_assertion=asrt_source.is_at_beginning_of_line(3)
                       ),
        ]
        # EXPECTATION #

        expected_container = matches_container(
            asrt.equals(ValueType.STRING_TRANSFORMER),
            sdv=matches_sdv_of_string_transformer_constant(
                references=asrt.matches_sequence([
                    is_reference_to_string_transformer(symbol.name),
                ]),
                primitive_value=asrt_string_transformer.is_identity_transformer(False),
                symbols=symbol.symbol_table,
            )
        )

        for source_case in cases:
            with self.subTest(source_case.name):
                expectation = Expectation.phase_agnostic(
                    symbol_usages=asrt.matches_sequence([
                        asrt_sym_usage.matches_definition(asrt.equals(defined_name),
                                                          expected_container)
                    ]),
                    symbols_after_main=assert_symbol_table_is_singleton(
                        defined_name,
                        expected_container,
                    ),
                    source=source_case.source_assertion
                )

                # ACT & ASSERT #

                INSTRUCTION_CHECKER.check(self, source_case.source, Arrangement.phase_agnostic(), expectation)
    def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess):
        # ARRANGE #
        string_to_prepend = '.'

        initial_model_contents = '\n'

        model_after_2_transformations = ''.join(
            [string_to_prepend, string_to_prepend, initial_model_contents])

        initial_model = model_constructor.of_str(self, initial_model_contents)

        equals_expected_matcher = StringMatcherSymbolContext.of_primitive(
            'EQUALS_EXPECTED',
            EqualsMatcherTestImpl(model_after_2_transformations))

        prepend_transformer_symbol = StringTransformerSymbolContext.of_primitive(
            'PREPEND_TRANSFORMER',
            string_transformers.of_line_transformer__w_preserved_line_ending(
                'prepend to each line', lambda line:
                (line + string_to_prepend)))

        prepend_trans_arg = str_trans_syntax.syntax_for_transformer_option(
            prepend_transformer_symbol.name)

        trans_and_eq_expected_matcher_source = remaining_source(
            '{prepend_trans_arg} {equals_expected_matcher}'.format(
                prepend_trans_arg=prepend_trans_arg,
                equals_expected_matcher=equals_expected_matcher.name,
            ))

        # ACT & ASSERT #

        parser = sut.parsers().full
        prepend_and_equals_expected_matcher_sdv = parser.parse(
            trans_and_eq_expected_matcher_source)

        prepend_and_equals_expected_matcher = StringMatcherSymbolContext.of_sdv(
            'PREPEND_AND_EQUALS_EXPECTED',
            prepend_and_equals_expected_matcher_sdv)

        symbols = SymbolContext.symbol_table_of_contexts([
            equals_expected_matcher,
            prepend_transformer_symbol,
            prepend_and_equals_expected_matcher,
        ])
        expected_symbol_references = asrt.matches_sequence([
            is_reference_to_string_transformer(
                prepend_transformer_symbol.name),
            is_reference_to_string_matcher(
                prepend_and_equals_expected_matcher.name),
        ])

        self._check_with_source_variants(
            test_configuration.arguments_for(
                args(
                    '{prepend_trans_arg} {maybe_not} {prepend_and_equals_expected_matcher}',
                    prepend_trans_arg=prepend_trans_arg,
                    maybe_not=maybe_not.
                    nothing__if_positive__not_option__if_negative,
                    prepend_and_equals_expected_matcher=
                    prepend_and_equals_expected_matcher.name)), initial_model,
            arrangement_w_tcds(symbols=symbols),
            Expectation(
                ParseExpectation(
                    symbol_references=expected_symbol_references, ),
                ExecutionExpectation(
                    main_result=maybe_not.pass__if_positive__fail__if_negative,
                ),
            ))
Beispiel #22
0
    def runTest(self):
        non_zero_exit_code = 1
        text_printed_by_program = 'the output from the program'

        py_file = File(
            'exit-with-hard-coded-exit-code.py',
            py_programs.py_pgm_with_stdout_stderr_exit_code(
                stdout_output=text_printed_by_program,
                stderr_output='',
                exit_code=non_zero_exit_code,
            ),
        )

        expected_file = fs.File(
            'dst-file.txt',
            text_printed_by_program.upper(),
        )

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TO_UPPER_CASE',
            to_uppercase(),
        )
        symbols = to_upper_transformer.symbol_table

        py_src_file_rel_opt_conf = rel_opt.conf_rel_any(
            RelOptionType.REL_HDS_CASE)
        dst_file_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP)

        program_string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
            ProcOutputFile.STDOUT,
            program_abs_stx.FullProgramAbsStx(
                program_abs_stx.ProgramOfPythonInterpreterAbsStx.
                of_execute_python_src_file(
                    py_src_file_rel_opt_conf.path_abs_stx_of_name(
                        py_file.name)),
                transformation=to_upper_transformer.abstract_syntax,
            ),
            ignore_exit_code=True,
        )
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_file_rel_opt_conf.path_abs_stx_of_name(expected_file.name),
            program_string_source_syntax,
        )

        instruction_checker = self.conf.instruction_checker
        parser = self.conf.parser()
        for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx(
                instruction_syntax):
            with self.subTest(source_case.name):
                source = source_case.value.source
                # ACT #
                instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                source_case.value.expectation.apply_with_message(
                    self, source, 'source-after-parse')
                # ACT & ASSERT #
                instruction_checker.check(
                    self, instruction,
                    self.conf.arrangement(
                        pre_contents_population_action=
                        SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                        symbols=symbols,
                        tcds_contents=py_src_file_rel_opt_conf.
                        populator_for_relativity_option_root(
                            DirContents([py_file]))),
                    self.conf.expect_success(
                        symbol_usages=asrt.matches_sequence([
                            to_upper_transformer.reference_assertion,
                        ]),
                        main_side_effects_on_sds=non_hds_dir_contains_exactly(
                            dst_file_rel_opt_conf.root_dir__non_hds,
                            fs.DirContents([expected_file])),
                    ))
    def test_string_transformer_should_be_parsed_as_simple_expression(self):
        # ARRANGE #
        the_layout = LayoutSpec.of_default()

        src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP)

        src_file = fs.File('source-file.txt', 'contents of source file')
        expected_contents = src_file.contents.upper()

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        str_trans__unused = StringTransformerSymbolReferenceAbsStx('UNUSED_TRANSFORMER')
        transformation_w_infix_op = str_trans_abs_stx.StringTransformerCompositionAbsStx(
            [
                to_upper_transformer.abstract_syntax,
                str_trans__unused,
            ],
            within_parens=False,
            allow_elements_on_separate_lines=False,
        )
        expected_remaining_tokens = TokenSequence.concat([
            TokenSequence.singleton(str_trans_abs_stx.names.SEQUENCE_OPERATOR_NAME),
            str_trans__unused.tokenization(),
        ])
        expected_remaining_source = expected_remaining_tokens.layout(the_layout)

        file_contents_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)
            ),
            transformation_w_infix_op
        )
        checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity))
        # ACT & ASSERT #
        checker.check__abs_stx(
            self,
            file_contents_syntax,
            None,
            arrangement_w_tcds(
                tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root(
                    fs.DirContents([src_file])
                ),
                symbols=to_upper_transformer.symbol_table
            ),
            Expectation.of_prim__const(
                parse=ParseExpectation(
                    source=asrt_source.source_is_not_at_end(
                        remaining_source=asrt.equals(expected_remaining_source)
                    ),
                    symbol_references=to_upper_transformer.references_assertion,
                ),
                primitive=asrt_string_source.pre_post_freeze__matches_str__const(
                    expected_contents,
                    may_depend_on_external_resources=True,
                )
            ),
            the_layout
        )
Beispiel #24
0
    def test_symbol_usages(self):
        # ARRANGE #

        to_upper_transformer = StringTransformerSymbolContext.of_primitive(
            'TRANSFORMER_SYMBOL',
            string_transformers.to_uppercase(),
        )

        src_file = fs.File('src-file.txt', 'contents of source file')
        src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE)

        expected_dst_file = fs.File('dst-file-name.txt',
                                    src_file.contents.upper())

        dst_file_symbol = ConstantSuffixPathDdvSymbolContext(
            'DST_FILE_SYMBOL',
            RelOptionType.REL_TMP,
            expected_dst_file.name,
            sut.REL_OPT_ARG_CONF.options.accepted_relativity_variants,
        )

        for phase_is_after_act in [False, True]:
            checker = integration_check.checker(phase_is_after_act)
            src_file_rel_opt_conf = src_rel_opt_arg_conf_for_phase(
                phase_is_after_act)

            src_file_symbol = ConstantSuffixPathDdvSymbolContext(
                'SRC_FILE_SYMBOL',
                src_file_rel_conf.relativity_option,
                src_file.name,
                src_file_rel_opt_conf.options.accepted_relativity_variants,
            )
            transformed_file_contents = string_source_abs_stx.TransformedStringSourceAbsStx(
                string_source_abs_stx.StringSourceOfFileAbsStx(
                    src_file_symbol.abstract_syntax),
                StringTransformerSymbolReferenceAbsStx(
                    to_upper_transformer.name))
            instruction_syntax = instr_abs_stx.create_w_explicit_contents(
                dst_file_symbol.abstract_syntax,
                transformed_file_contents,
            )
            symbols = SymbolContext.symbol_table_of_contexts([
                dst_file_symbol,
                src_file_symbol,
                to_upper_transformer,
            ])

            with self.subTest(phase_is_after_act=phase_is_after_act):
                # ACT & ASSERT #
                checker.check__abs_stx__std_layouts_and_source_variants(
                    self,
                    instruction_syntax,
                    Arrangement.phase_agnostic(
                        symbols=symbols,
                        tcds=TcdsArrangement(
                            hds_contents=src_file_rel_conf.
                            populator_for_relativity_option_root__hds(
                                DirContents([src_file]))),
                    ),
                    MultiSourceExpectation.phase_agnostic(
                        main_result=IS_SUCCESS,
                        symbol_usages=asrt.matches_sequence([
                            dst_file_symbol.
                            reference_assertion__path_or_string,
                            src_file_symbol.
                            reference_assertion__path_or_string,
                            is_reference_to_string_transformer__usage(
                                to_upper_transformer.name),
                        ]),
                    ),
                )
Beispiel #25
0
                    program_w_complex_str_trans_wo_parentheses,
                    ignore_exit_code=ignore_exit_code,
                )
                with self.subTest(output_file=output_file,
                                  ignore_exit_code=ignore_exit_code):
                    checker.check__abs_stx__wo_input(
                        self,
                        syntax,
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                symbols),
                            tcds_contents=py_file_rel_conf.
                            populator_for_relativity_option_root(
                                DirContents([py_program_file]))),
                        Expectation(
                            ParseExpectation(
                                source=asrt_source.source_is_not_at_end(
                                    remaining_source=asrt.equals(
                                        expected_remaining_source)),
                                symbol_references=SymbolContext.
                                references_assertion_of_contexts(symbols),
                            )),
                        the_layout,
                    )


TO_UPPER_TRANSFORMER_SYMBOL = StringTransformerSymbolContext.of_primitive(
    'TO_UPPER_TRANSFORMER_SYMBOL',
    string_transformers.to_uppercase(),
)
        put,
        source_variants,
        OptionallyOnNewLine(ss_syntax),
        arrangement_w_tcds(),
        MultiSourceExpectation.of_prim__const(
            asrt_string_source.pre_post_freeze__matches_str__const(
                case.expected,
                may_depend_on_external_resources=False,
            )),
        sub_test_identifiers={'name': case.name})


def _check_transformed_w_constant_contents(put: unittest.TestCase, case: Case):
    str_added_by_transformer = '<string added by transformer>'
    transformer_symbol = StringTransformerSymbolContext.of_primitive(
        'TRANSFORMER',
        string_transformers.add(str_added_by_transformer),
    )

    ss_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
        string_source_abs_stx.StringSourceOfStringAbsStx(case.syntax),
        transformer_symbol.abstract_syntax,
    )
    CHECKER.check__abs_stx__layouts__source_variants__wo_input(
        put,
        equivalent_source_variants__for_expr_parse__s__nsc,
        OptionallyOnNewLine(ss_syntax),
        arrangement_w_tcds(symbols=transformer_symbol.symbol_table, ),
        MultiSourceExpectation.of_prim__const(
            symbol_references=transformer_symbol.references_assertion,
            primitive=asrt_string_source.pre_post_freeze__matches_str__const(
                case.expected + str_added_by_transformer,