def test_symbol_reference_in_dst_file_argument(self):
        dst_path_symbol = ConstantSuffixPathDdvSymbolContext(
            'dst_path_symbol', RelOptionType.REL_ACT, 'dst-file.txt',
            ACCEPTED_DST_RELATIVITY_VARIANTS)
        string_value = rich_str_abs_stx.HereDocAbsStx(
            'single line in here doc\n')
        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            dst_path_symbol.abstract_syntax,
            string_source_abs_stx.StringSourceOfStringAbsStx(string_value),
        )

        expected_file = fs.File(dst_path_symbol.path_suffix,
                                string_value.value)
        integration_check.CHECKER__BEFORE_ACT.check__abs_stx(
            self, instruction_syntax,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(
                    pre_population_action=
                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ),
                symbols=dst_path_symbol.symbol_table,
            ),
            Expectation.phase_agnostic(
                main_result=IS_SUCCESS,
                symbol_usages=asrt.matches_singleton_sequence(
                    dst_path_symbol.reference_assertion),
                main_side_effects_on_sds=dir_contains_exactly(
                    dst_path_symbol.rel_option_type,
                    fs.DirContents([expected_file])),
            ))
Exemple #2
0
 def test_assignment_of_symbols_and_constant_text_within_soft_quotes(self):
     # ARRANGE #
     referred_symbol1 = SymbolWithReferenceSyntax('referred_symbol_1')
     referred_symbol2 = SymbolWithReferenceSyntax('referred_symbol_2')
     assigned_symbol = StringSymbolContext.of_sdv(
         'defined_symbol',
         string_sdv_from_fragments([
             symbol(referred_symbol1.name),
             constant(' between '),
             symbol(referred_symbol2.name),
         ]))
     source = single_line_source('{string_type} {name} = {soft_quote}{sym_ref1} between {sym_ref2}{soft_quote}',
                                 soft_quote=SOFT_QUOTE_CHAR,
                                 name=assigned_symbol.name,
                                 sym_ref1=referred_symbol1,
                                 sym_ref2=referred_symbol2)
     expectation = Expectation.phase_agnostic(
         symbol_usages=asrt.matches_sequence([
             assigned_symbol.assert_matches_definition_of_sdv,
         ]),
         symbols_after_main=assert_symbol_table_is_singleton(
             assigned_symbol.name,
             assigned_symbol.value.assert_matches_container_of_sdv,
         )
     )
     # ACT & ASSERT #
     INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #3
0
 def test(self):
     instruction_argument = src2(
         ValueType.PATH, 'ASSIGNED_NAME',
         '{rel_symbol} REFERENCED_SYMBOL component')
     for source in equivalent_source_variants__with_source_check__consume_last_line(
             self, instruction_argument):
         expected_path_sdv = path_sdvs.rel_symbol(
             SymbolReference(
                 'REFERENCED_SYMBOL',
                 ReferenceRestrictionsOnDirectAndIndirect(
                     PathAndRelativityRestriction(
                         type_parser.REL_OPTIONS_CONFIGURATION.
                         accepted_relativity_variants))),
             path_part_sdvs.from_constant_str('component'))
         expected_symbol_value = PathSymbolValueContext.of_sdv(
             expected_path_sdv)
         expected_symbol = PathSymbolContext('ASSIGNED_NAME',
                                             expected_symbol_value)
         INSTRUCTION_CHECKER.check(
             self, source, Arrangement.phase_agnostic(),
             Expectation.phase_agnostic(
                 symbol_usages=asrt.matches_sequence(
                     [expected_symbol.assert_matches_definition_of_sdv]),
                 symbols_after_main=assert_symbol_table_is_singleton(
                     expected_symbol.name, expected_symbol_value.
                     assert_matches_container_of_sdv)))
Exemple #4
0
    def test_assignment_of_single_symbol_reference(self):
        # ARRANGE #
        referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
        expected_sdv = string_sdv_from_fragments([symbol(referred_symbol.name),
                                                  constant('\n')])
        assigned_symbol = StringSymbolContext.of_sdv(
            'defined_symbol',
            expected_sdv,
        )
        sb = SB.new_with(referred_symbol=referred_symbol,
                         name_of_defined_symbol=assigned_symbol.name)

        source = sb.lines(
            here_doc_lines(first_line_start='{string_type} {name_of_defined_symbol} = ',
                           marker='EOF',
                           contents_lines=[str(referred_symbol)])
        )
        # EXPECTATION #
        expectation = Expectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence([
                assigned_symbol.assert_matches_definition_of_sdv,
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                assigned_symbol.name,
                assigned_symbol.value.assert_matches_container_of_sdv,
            )
        )
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #5
0
    def test_assignment_of_single_constant_line(self):
        value_str = 'value'
        symbol_name = 'name1'
        sb = SB.new_with(value_str=value_str,
                         symbol_name=symbol_name)

        source = sb.lines(
            here_doc_lines(first_line_start='{string_type} {symbol_name} = ',
                           marker='EOF',
                           contents_lines=['{value_str}']) +
            ['following line']
        )
        # EXPECTATION #
        expected_symbol = StringConstantSymbolContext(symbol_name, value_str + '\n')
        expectation = Expectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence([
                expected_symbol.assert_matches_definition_of_sdv
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                symbol_name,
                expected_symbol.value.assert_matches_container_of_sdv,
            ),
            source=asrt_source.is_at_beginning_of_line(4)
        )
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #6
0
 def test_destination_already_exists_in_destination_directory(self):
     src = 'src-file-name'
     dst = 'dst-dir-name'
     home_dir_contents = DirContents([(File.empty(src))])
     cwd_dir_contents = DirContents([Dir(dst,
                                         [File.empty(src)])])
     for parser_case in _EXECUTION_CHECKERS:
         with self.subTest(parser=parser_case.name):
             # ACT & ASSERT #
             parser_case.value.check(
                 self,
                 args.copy(
                     defs.DEFAULT_SRC_REL_OPT.path_argument_of_rel_name(src),
                     defs.DEFAULT_DST_REL_OPT.path_argument_of_rel_name(dst)
                 ).as_remaining_source,
                 Arrangement.phase_agnostic(
                     tcds=TcdsArrangement(
                         pre_population_action=MAKE_SUB_DIR_OF_SDS_CURRENT_DIRECTORY,
                         hds_contents=defs.DEFAULT_SRC_REL_OPT.populator_for_relativity_option_root__hds(
                             home_dir_contents),
                         sds_contents=defs.DEFAULT_DST_REL_OPT.populator_for_relativity_option_root__sds(
                             cwd_dir_contents
                         ),
                     )
                 ),
                 Expectation.phase_agnostic(
                     main_result=asrt_text_doc.is_any_text(),
                 ),
             )
Exemple #7
0
 def test_source(self):
     self._check(
         PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION,
         single_line_source(),
         Arrangement.phase_agnostic(),
         Expectation.phase_agnostic(source=asrt.IsInstance(ParseSource)),
     )
Exemple #8
0
    def test(self):

        expected_defined_symbol = ConstantSuffixPathDdvSymbolContext(
            'name', RelOptionType.REL_ACT, 'component')
        argument_cases = [
            NameAndValue('value on same line', '{rel_act} {suffix}'),
            NameAndValue('value on following line',
                         '{new_line} {rel_act} {suffix}'),
        ]

        for argument_case in argument_cases:
            with self.subTest(arguments=argument_case.name):
                instruction_argument = src2(
                    ValueType.PATH,
                    expected_defined_symbol.name,
                    argument_case.value,
                    suffix=expected_defined_symbol.path_suffix)
                for source in equivalent_source_variants__with_source_check__consume_last_line(
                        self, instruction_argument):
                    INSTRUCTION_CHECKER.check(
                        self, source, Arrangement.phase_agnostic(),
                        Expectation.phase_agnostic(
                            symbol_usages=asrt.matches_singleton_sequence(
                                expected_defined_symbol.
                                assert_matches_definition_of_sdv),
                            symbols_after_main=assert_symbol_table_is_singleton(
                                expected_defined_symbol.name,
                                expected_defined_symbol.value.
                                assert_matches_container_of_sdv)))
Exemple #9
0
 def test(self):
     with tmp_dir() as abs_path_of_dir_containing_last_file_base_name:
         fs_location_info = FileSystemLocationInfo(
             FileLocationInfo(
                 abs_path_of_dir_containing_last_file_base_name))
         instruction_argument = src2(ValueType.PATH, 'name',
                                     '{rel_source_file} component')
         for source in equivalent_source_variants__with_source_check__consume_last_line(
                 self, instruction_argument):
             expected_path_sdv = path_sdvs.constant(
                 path_ddvs.rel_abs_path(
                     abs_path_of_dir_containing_last_file_base_name,
                     path_ddvs.constant_path_part('component')))
             expected_symbol_value = PathSymbolValueContext.of_sdv(
                 expected_path_sdv)
             expected_symbol = PathSymbolContext('name',
                                                 expected_symbol_value)
             INSTRUCTION_CHECKER.check(
                 self, source,
                 Arrangement.phase_agnostic(
                     fs_location_info=fs_location_info),
                 Expectation.phase_agnostic(
                     symbol_usages=asrt.matches_singleton_sequence(
                         expected_symbol.assert_matches_definition_of_sdv),
                     symbols_after_main=assert_symbol_table_is_singleton(
                         'name', expected_symbol_value.
                         assert_matches_container_of_sdv)))
Exemple #10
0
    def test_successful_step_sequence(self):
        validate_pre_sds = 'validate_pre_sds'
        validate_post_sds = 'validate_post_sds'
        main = 'main'

        expected_recordings = [
            validate_pre_sds,
            validate_post_sds,
            main,
        ]
        recorder = []

        def recording_of(s: str) -> Callable[[InstructionEnvironmentForPostSdsStep,
                                              InstructionSettings,
                                              OsServices],
                                             None]:
            def ret_val(*args):
                recorder.append(s)

            return ret_val

        instruction_that_records_steps = instruction_embryo_that__phase_agnostic(
            validate_pre_sds_initial_action=recording_of(validate_pre_sds),
            validate_post_sds_initial_action=recording_of(validate_post_sds),
            main_initial_action=recording_of(main))
        self._check(
            ParserThatGives(instruction_that_records_steps),
            single_line_source(),
            Arrangement.phase_agnostic(),
            Expectation.phase_agnostic())

        self.assertEqual(expected_recordings,
                         recorder,
                         'step execution sequence')
Exemple #11
0
 def test_fail_if_instruction_is_not_phase_agnostic(self):
     with self.assertRaises(utils.TestError):
         self._check(
             ParserThatGives(instruction_embryo_that__setup_phase_aware()),
             single_line_source(),
             Arrangement.phase_agnostic(),
             Expectation.phase_agnostic(),
         )
Exemple #12
0
 def test_that_default_expectation_assumes_no_symbol_usages(self):
     unexpected_symbol_usages = [
         data_references.reference_to__on_direct_and_indirect('symbol_name')]
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(
             instruction_embryo_that__phase_agnostic(
                 symbol_usages=do_return(unexpected_symbol_usages))),
         Arrangement.phase_agnostic(),
         Expectation.phase_agnostic(),
     )
    def _test_symbol_reference_in_dst_file_and_contents(
            self, symbol_ref_syntax_2_contents_arguments: Callable[
                [str], StringSourceAbsStx],
            symbol_value_2_expected_contents: Callable[[str], str]):
        sub_dir_symbol = ConstantSuffixPathDdvSymbolContext(
            'sub_dir_symbol',
            RelOptionType.REL_ACT,
            'sub-dir',
            ACCEPTED_DST_RELATIVITY_VARIANTS,
        )
        contents_symbol = StringConstantSymbolContext(
            'contents_symbol_name',
            'contents symbol value',
            default_restrictions=asrt_rest.is__w_str_rendering(),
        )

        expected_file_contents = symbol_value_2_expected_contents(
            contents_symbol.str_value)

        expected_file = fs.File('a-file-name.txt', expected_file_contents)

        symbols = [sub_dir_symbol, contents_symbol]
        expected_symbol_references = SymbolContext.references_assertion_of_contexts(
            symbols)
        symbol_table = SymbolContext.symbol_table_of_contexts(symbols)

        contents_arguments = symbol_ref_syntax_2_contents_arguments(
            symbol_reference_syntax_for_name(contents_symbol.name))

        instruction_syntax = instr_abs_stx.create_w_explicit_contents(
            path_abs_stx.PathStringAbsStx.of_plain_components(
                [sub_dir_symbol.name__sym_ref_syntax, expected_file.name]),
            contents_arguments,
        )

        integration_check.CHECKER__AFTER_ACT.check__abs_stx(
            self, instruction_syntax,
            Arrangement.phase_agnostic(
                tcds=TcdsArrangement(
                    pre_population_action=
                    SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ),
                symbols=symbol_table,
            ),
            Expectation.phase_agnostic(
                main_result=IS_SUCCESS,
                symbol_usages=expected_symbol_references,
                main_side_effects_on_sds=dir_contains_exactly(
                    sub_dir_symbol.rel_option_type,
                    fs.DirContents(
                        [fs.Dir(sub_dir_symbol.path_suffix,
                                [expected_file])])),
            ))
Exemple #14
0
 def runTest(self):
     # ARRANGE #
     test_setup = StdinCheckWithProgramWExitCode0ForSuccess()
     # ACT & ASSERT #
     EXECUTION_CHECKER.check__abs_stx(
         self,
         test_setup.program_that_checks_stdin__syntax(
             'the contents of stdin'),
         Arrangement.phase_agnostic(tcds=TcdsArrangement(
             tcds_contents=test_setup.tcds_contents, ), ),
         Expectation.phase_agnostic(main_result=result_assertions.equals(
             test_setup.exit_code_of_successful_application, None)),
     )
Exemple #15
0
    def test_symbol_references(self):
        file_to_interpret = fs.File(
            'python-logic_symbol_utils.py',
            python_program_that_exits_with_code_given_as_first_cl_arg)
        file_to_interpret_symbol = StringConstantSymbolContext(
            'file_to_interpret_symbol', file_to_interpret.file_name)
        python_interpreter_symbol = StringConstantSymbolContext(
            'python_interpreter_symbol', sys.executable)
        exit_code_symbol = StringIntConstantSymbolContext(
            'exit_code_symbol', 72)

        argument = ' {python_interpreter} {interpret_option} {file_to_interpret}  "{exit_code}"'.format(
            python_interpreter=python_interpreter_symbol.name__sym_ref_syntax,
            interpret_option=args.option(
                syntax_elements.EXISTING_FILE_OPTION_NAME).as_str,
            file_to_interpret=file_to_interpret_symbol.name__sym_ref_syntax,
            exit_code=exit_code_symbol.name__sym_ref_syntax,
        )

        following_line = 'following line'
        source = remaining_source(argument, [following_line])

        arrangement = Arrangement.phase_agnostic(
            tcds=TcdsArrangement(tcds_contents=TcdsPopulatorForRelOptionType(
                path_relativities.ALL_REL_OPTIONS_ARG_CONFIG.options.
                default_option, fs.DirContents([file_to_interpret])), ),
            symbols=SymbolContext.symbol_table_of_contexts([
                python_interpreter_symbol,
                file_to_interpret_symbol,
                exit_code_symbol,
            ]),
        )

        expectation = Expectation.phase_agnostic(
            source=assert_source(current_line_number=asrt.equals(2),
                                 column_index=asrt.equals(0)),
            symbol_usages=asrt.matches_sequence([
                python_interpreter_symbol.usage_assertion__path_or_string(
                    syntax_elements.EXE_FILE_REL_OPTION_ARG_CONF.options.
                    accepted_relativity_variants),
                file_to_interpret_symbol.usage_assertion__path_or_string(
                    path_relativities.ALL_REL_OPTIONS_ARG_CONFIG.options.
                    accepted_relativity_variants),
                exit_code_symbol.usage_assertion__w_str_rendering,
            ]),
            main_result=result_assertions.equals(exit_code_symbol.int_value,
                                                 ''),
        )

        parser = sut.embryo_parser('instruction-name')
        embryo_check.check(self, parser, source, arrangement, expectation)
Exemple #16
0
    def test_successful_parse_and_application_of_non_trivial_matcher(self):
        # ARRANGE #

        defined_name = 'defined_name'

        expected_container = matches_container(
            asrt.equals(ValueType.FILES_MATCHER),
            type_sdv_assertions.matches_sdv_of_files_matcher()
        )

        not_num_files_beginning_with_a_eq_1_arg = self._not_num_files_beginning_with_a_eq_1_arg()

        cases = [
            NEA('should match',
                expected=matcher_assertions.is_matching_success(),
                actual=DirContents([
                    File.empty('a.x'),
                    File.empty('a.y'),
                    File.empty('b.x'),
                    File.empty('b.y'),
                ])
                ),
            NEA('should not match',
                expected=matcher_assertions.is_arbitrary_matching_failure(),
                actual=DirContents([
                    File.empty('a.x'),
                    File.empty('b.y'),
                    File.empty('b.x'),
                ])
                ),
        ]

        for case in cases:
            source = single_line_source(
                src2(ValueType.FILES_MATCHER, defined_name, not_num_files_beginning_with_a_eq_1_arg),
            )
            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,
                ),
                instruction_environment=
                AssertApplicationOfMatcherInSymbolTable(defined_name,
                                                        actual_dir_contents=case.actual,
                                                        expected_matcher_result=case.expected),
            )
Exemple #17
0
 def test_that_fails_due_to_missing_symbol_reference(self):
     symbol_usages_of_instruction = []
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(
             instruction_embryo_that__phase_agnostic(
                 symbol_usages=do_return(symbol_usages_of_instruction))),
         Arrangement.phase_agnostic(),
         Expectation.phase_agnostic(
             symbol_usages=asrt.matches_singleton_sequence(
                 matches_data_type_symbol_reference(
                     'symbol_name',
                     reference_restrictions.is_any_type_w_str_rendering()
                 )
             )),
     )
Exemple #18
0
class TestSuccessfulScenarios(unittest.TestCase):
    def test_successful_parse_of_arbitrary_matcher(self):
        defined_name = 'defined_name'
        file_name = 'the-file-name'

        # ARRANGE #

        argument_cases = [
            NameAndValue('value on same line',
                         '{files_condition}'
                         ),
            NameAndValue('value on following line',
                         '{new_line} {files_condition}'
                         ),
        ]

        for case in argument_cases:
            with self.subTest(case.name):
                source = remaining_source(
                    src2(ValueType.FILES_CONDITION, defined_name, case.value,
                         files_condition=arg_syntax.FilesCondition([arg_syntax.FileCondition(file_name)])),
                )

                # EXPECTATION #

                expected_container = matches_container(
                    asrt.equals(ValueType.FILES_CONDITION),
                    type_sdv_assertions.matches_sdv_of_files_condition_constant(
                        primitive_value=asrt_primitive.files_matches({
                            PurePosixPath(file_name): asrt.is_none
                        })
                    )
                )

                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,
                    )
                )

                # ACT & ASSERT #

                INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #19
0
    def test_symbol_references(self):
        python_interpreter_symbol = StringConstantSymbolContext(
            'python_interpreter_symbol', sys.executable)
        execute_program_option_symbol = StringConstantSymbolContext(
            'execute_program_option', '-c')
        exit_code_symbol = StringIntConstantSymbolContext(
            'exit_code_symbol', 87)

        argument = ' {python_interpreter} {execute_program_option} {source_option}   exit({exit_code})  '.format(
            python_interpreter=python_interpreter_symbol.name__sym_ref_syntax,
            execute_program_option=execute_program_option_symbol.
            name__sym_ref_syntax,
            source_option=syntax_elements.
            REMAINING_PART_OF_CURRENT_LINE_AS_LITERAL_MARKER,
            exit_code=exit_code_symbol.name__sym_ref_syntax,
        )

        arrangement = Arrangement.phase_agnostic(
            symbols=SymbolContext.symbol_table_of_contexts([
                python_interpreter_symbol,
                execute_program_option_symbol,
                exit_code_symbol,
            ]),
            tcds=TcdsArrangement(),
        )

        source = remaining_source(argument, ['following line'])

        expectation = Expectation.phase_agnostic(
            source=assert_source(current_line_number=asrt.equals(2),
                                 column_index=asrt.equals(0)),
            symbol_usages=asrt.matches_sequence([
                python_interpreter_symbol.usage_assertion__path_or_string(
                    syntax_elements.EXE_FILE_REL_OPTION_ARG_CONF.options.
                    accepted_relativity_variants),
                execute_program_option_symbol.usage_assertion__w_str_rendering,
                exit_code_symbol.usage_assertion__w_str_rendering,
            ]),
            main_result=result_assertions.equals(exit_code_symbol.int_value,
                                                 ''),
        )

        parser = sut.embryo_parser('instruction-name')
        embryo_check.check(self, parser, source, arrangement, expectation)
Exemple #20
0
class TestSuccessfulScenarios(unittest.TestCase):
    def test_successful_parse_of_arbitrary_matcher(self):
        defined_name = 'defined_name'

        # ARRANGE #

        argument_cases = [
            NameAndValue('value on same line',
                         '{matcher_argument}'
                         ),
            NameAndValue('value on following line',
                         '{new_line} {matcher_argument}'
                         ),
        ]

        for case in argument_cases:
            with self.subTest(case.name):
                source = single_line_source(
                    src2(ValueType.FILES_MATCHER, defined_name, case.value,
                         matcher_argument=arg_syntax.arbitrary_single_line_value_that_must_not_be_quoted()),
                )

                # EXPECTATION #

                expected_container = matches_container(
                    asrt.equals(ValueType.FILES_MATCHER),
                    type_sdv_assertions.matches_sdv_of_files_matcher()
                )

                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,
                    )
                )

                # ACT & ASSERT #

                INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #21
0
 def test_assignment_of_single_symbol_reference(self):
     # ARRANGE #
     referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
     assigned_symbol = StringSymbolContext.of_sdv('defined_symbol',
                                                  string_sdv_from_fragments([symbol(referred_symbol.name)]))
     source = single_line_source('{string_type} {name} = {symbol_reference}',
                                 name=assigned_symbol.name,
                                 symbol_reference=referred_symbol)
     expectation = Expectation.phase_agnostic(
         symbol_usages=asrt.matches_sequence([
             assigned_symbol.assert_matches_definition_of_sdv,
         ]),
         symbols_after_main=assert_symbol_table_is_singleton(
             assigned_symbol.name,
             assigned_symbol.value.assert_matches_container_of_sdv,
         )
     )
     # ACT & ASSERT #
     INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #22
0
def _expect_definition_of(
        defined_name: str,
        symbols_for_evaluation: SymbolTable,
        references: Assertion[Sequence[SymbolReference]],
        expected_contents: str,
        may_depend_on_external_resources: bool,
) -> Expectation[None]:
    sdv_expectation = type_sdv_assertions.matches_sdv(
        asrt.is_instance(StringSourceSdv),
        references=references,
        symbols=symbols_for_evaluation,
        resolved_value=asrt.anything_goes(),
        custom=asrt.is_instance_with(
            StringSourceSdv,
            str_src_check.execution_assertion(
                intgr_arr_exp.Arrangement(
                    symbols=symbols_for_evaluation,
                    tcds=TcdsArrangement(),
                ),
                primitive=str_src_check.primitive__const(
                    asrt_string_source.pre_post_freeze__matches_str__const(
                        expected_contents,
                        may_depend_on_external_resources=may_depend_on_external_resources
                    )
                )
            )
        )
    )
    expected_container = matches_container(
        asrt.equals(ValueType.STRING_SOURCE),
        sdv_expectation,
    )
    return 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,
        )
    )
Exemple #23
0
    def test_successful_parse_and_application_of_non_trivial_matcher(self):
        defined_name = 'defined_name'

        expected_container = matches_container(
            asrt.equals(ValueType.STRING_MATCHER),
            matches_sdv_of_string_matcher())

        not_num_lines_eq_1_matcher_arg = self._not_num_lines_eq_1_matcher_arg()

        cases = [
            NEA('should match',
                expected=matcher_assertions.is_matching_success(),
                actual=lines_content([
                    '1st line',
                    '2nd line',
                ])),
            NEA('should not match',
                expected=matcher_assertions.is_arbitrary_matching_failure(),
                actual='a single line'),
        ]

        for case in cases:
            source = single_line_source(
                src2(ValueType.STRING_MATCHER, defined_name,
                     not_num_lines_eq_1_matcher_arg), )
            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,
                ),
                instruction_environment=AssertApplicationOfMatcherInSymbolTable(
                    self,
                    defined_name,
                    actual_model_contents=case.actual,
                    expected_matcher_result=case.expected),
            )

            with self.subTest(case.name):
Exemple #24
0
    def test(self):
        expected_defined_symbol = ConstantSuffixPathDdvSymbolContext(
            'name', type_parser.REL_OPTIONS_CONFIGURATION.default_option,
            'component')

        instruction_argument = src2(ValueType.PATH,
                                    expected_defined_symbol.name,
                                    expected_defined_symbol.path_suffix)

        for source in equivalent_source_variants__with_source_check__consume_last_line(
                self, instruction_argument):
            INSTRUCTION_CHECKER.check(
                self, source, Arrangement.phase_agnostic(),
                Expectation.phase_agnostic(
                    symbol_usages=asrt.matches_singleton_sequence(
                        expected_defined_symbol.
                        assert_matches_definition_of_sdv),
                    symbols_after_main=assert_symbol_table_is_singleton(
                        'name', expected_defined_symbol.value.
                        assert_matches_container_of_sdv)))
Exemple #25
0
def _expect_definition_of(
        defined_name: str,
        symbols_for_evaluation: SymbolTable,
        model: FileSystemElements,
        references: Assertion[Sequence[SymbolReference]],
        expected_output: Assertion[pathlib.Path],
) -> Expectation[pathlib.Path]:
    sdv_expectation = type_sdv_assertions.matches_sdv(
        asrt.is_instance(FilesSourceSdv),
        references=references,
        symbols=symbols_for_evaluation,
        resolved_value=asrt.anything_goes(),
        custom=asrt.is_instance_with(
            FilesSourceSdv,
            file_src_check.execution_assertion(
                model,
                intgr_arr_exp.Arrangement(
                    symbols=symbols_for_evaluation,
                    tcds=TcdsArrangement(),
                ),
                ExecutionExpectation(
                    main_result=expected_output,
                )
            ),
        )
    )
    expected_container = matches_container(
        asrt.equals(ValueType.FILES_SOURCE),
        sdv_expectation,
    )
    return 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,
        )
    )
Exemple #26
0
    def test_symbol_references(self):
        expected_exit_status = 72
        file_to_interpret = fs.File('python-logic_symbol_utils.py',
                                    py_script_that_exists_with_status(expected_exit_status))
        file_to_interpret_symbol = StringConstantSymbolContext('file_to_interpret_symbol',
                                                               file_to_interpret.file_name)
        python_interpreter_symbol = StringConstantSymbolContext('python_interpreter_symbol', sys.executable)

        argument = ' "{python_interpreter}" {file_to_interpret}'.format(
            python_interpreter=python_interpreter_symbol.name__sym_ref_syntax,
            file_to_interpret=file_to_interpret_symbol.name__sym_ref_syntax,
        )

        following_line = 'following line'
        source = remaining_source(argument, [following_line])

        arrangement = Arrangement.phase_agnostic(
            tcds=TcdsArrangement(
                tcds_contents=TcdsPopulatorForRelOptionType(
                    RelOptionType.REL_ACT,
                    fs.DirContents([file_to_interpret])),
            ),
            symbols=SymbolContext.symbol_table_of_contexts([
                python_interpreter_symbol,
                file_to_interpret_symbol,
            ]),
        )

        expectation = Expectation.phase_agnostic(
            source=assert_source(current_line_number=asrt.equals(2),
                                 column_index=asrt.equals(0)),
            symbol_usages=asrt.matches_sequence([
                python_interpreter_symbol.usage_assertion__w_str_rendering,
                file_to_interpret_symbol.usage_assertion__w_str_rendering,
            ]),
            main_result=result_assertions.equals(expected_exit_status, ''),
        )

        parser = sut.embryo_parser('instruction-name')
        embryo_check.check(self, parser, source, arrangement, expectation)
Exemple #27
0
    def test_successful_parse_of_reference(self):
        defined_name = 'defined_name'

        referenced_symbol = FilesMatcherSymbolContext.of_arbitrary_value('referenced_name')

        symbols = referenced_symbol.symbol_table

        # ARRANGE #

        source = single_line_source(
            src2(ValueType.FILES_MATCHER, defined_name, referenced_symbol.name),
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expected_container = matches_container(
            asrt.equals(ValueType.FILES_MATCHER),
            type_sdv_assertions.matches_sdv_of_files_matcher(
                references=asrt.matches_sequence([
                    referenced_symbol.reference_assertion
                ]),
                symbols=symbols)
        )

        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,
            )
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check(self, source, arrangement, expectation)
Exemple #28
0
    def test_assignment_of_single_constant_word(self):
        argument_cases = [
            NameAndValue('value on same line',
                         '{string_type} name1 = v1'
                         ),
            NameAndValue('value on following line',
                         '{string_type} name1 = {new_line} v1'
                         ),
        ]

        for argument_case in argument_cases:
            with self.subTest(argument_case.name):
                source = arbitrary_string_source(argument_case.value)
                expected_symbol = StringConstantSymbolContext('name1', 'v1')
                expectation = Expectation.phase_agnostic(
                    symbol_usages=asrt.matches_sequence([
                        expected_symbol.assert_matches_definition_of_sdv
                    ]),
                    symbols_after_main=assert_symbol_table_is_singleton(
                        'name1',
                        expected_symbol.value.assert_matches_container_of_sdv,
                    )
                )
                INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #29
0
    def test_successful_parse(self):
        name_pattern = 'the name pattern'
        non_matching_name = 'non-matching name'

        glob_pattern_arguments = file_matcher_arguments(
            name_pattern=name_pattern)
        sut_parser = parse_file_matcher.parsers().full
        expected_glob_pattern_matcher_sdv = sut_parser.parse(
            remaining_source(glob_pattern_arguments))

        expected_glob_pattern_matcher = resolving_helper__fake(
        ).resolve_matcher(expected_glob_pattern_matcher_sdv)

        cases = [
            NIE(
                'name pattern in RHS SHOULD give selection of name pattern',
                asrt_matcher.is_equivalent_to(expected_glob_pattern_matcher, [
                    asrt_matcher.ModelInfo(
                        file_matcher_models.FileMatcherModelForDescribedPath(
                            described_path.new_primitive(
                                pathlib.Path(name_pattern)), )),
                    asrt_matcher.ModelInfo(
                        file_matcher_models.FileMatcherModelForDescribedPath(
                            described_path.new_primitive(
                                pathlib.Path(non_matching_name)), )),
                ]),
                glob_pattern_arguments,
            ),
        ]
        # ARRANGE #
        defined_name = 'defined_name'
        argument_cases = [
            NameAndValue('value on same line', '{selector_argument}'),
            NameAndValue('value on following line',
                         '{new_line} {selector_argument}'),
        ]

        for case in cases:
            for argument_case in argument_cases:
                with self.subTest(case.name, arguments=argument_case.name):
                    source = single_line_source(
                        src2(ValueType.FILE_MATCHER,
                             defined_name,
                             argument_case.value,
                             selector_argument=case.input_value), )

                    expected_container = matches_container(
                        asrt.equals(ValueType.FILE_MATCHER),
                        type_sdv_assertions.matches_sdv_of_file_matcher(
                            references=asrt.is_empty_sequence,
                            primitive_value=case.expected_value))

                    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,
                        ))
                    # ACT & ASSERT #
                    INSTRUCTION_CHECKER.check(self, source,
                                              Arrangement.phase_agnostic(),
                                              expectation)
    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)