Example #1
0
    def test_successful_parse_WHEN_rhs_is_empty_THEN_result_SHOULD_be_identity_transformer(self):
        defined_name = 'defined_name'

        # ARRANGE #

        source = single_line_source(
            src('{lines_trans_type} {defined_name} = ',
                defined_name=defined_name),
        )

        # EXPECTATION #

        expected_container = matches_container(
            resolved_value_equals_string_transformer(IdentityStringTransformer())
        )

        expectation = Expectation(
            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 #

        self._check(source, ArrangementWithSds(), expectation)
Example #2
0
    def test_successful_parse_of_arbitrary_matcher(self):
        defined_name = 'defined_name'

        # ARRANGE #

        source = single_line_source(
            src('{string_matcher_type} {defined_name} = {matcher_argument}',
                defined_name=defined_name,
                matcher_argument=arg_syntax.arbitrary_single_line_value_that_must_not_be_quoted()),
        )

        # EXPECTATION #

        expected_container = matches_container(
            matches_string_matcher_resolver()
        )

        expectation = Expectation(
            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 #

        self._check(source, ArrangementWithSds(), expectation)
Example #3
0
    def test_successful_parse_of_regex(self):
        # ARRANGE #

        regex_str = 'the_regex'

        symbol = NameAndValue('the_symbol_name',
                              LineMatcherConstant(False))

        regex_matcher_syntax = argument_syntax.syntax_for_regex_matcher(regex_str)

        matcher_argument = argument_syntax.syntax_for_and([
            symbol.name,
            regex_matcher_syntax,
        ])

        defined_name = 'defined_name'

        source = remaining_source(
            src('{line_match_type} {defined_name} = {matcher_argument}',
                defined_name=defined_name,
                matcher_argument=matcher_argument),
            following_lines=['following line'],
        )

        # EXPECTATION #

        the_and_matcher = LineMatcherAnd([
            symbol.value,
            LineMatcherRegex(re.compile(regex_str)),
        ])

        expected_container = matches_container(
            assertion_on_resolver=
            resolved_value_equals_line_matcher(
                the_and_matcher,
                references=asrt.matches_sequence([
                    is_line_matcher_reference_to(symbol.name),
                ]),
                symbols=SymbolTable({
                    symbol.name: container(LineMatcherConstantResolver(symbol.value)),
                }),
            )
        )

        expectation = Expectation(
            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 #

        self._check(source, ArrangementWithSds(), expectation)
Example #4
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),
            )
Example #5
0
    def test_container_SHOULD_be_given_as_argument_to_assertion_on_container(self):
        # ARRANGE #
        builtin_symbol = StringSymbolContext.of_constant('the name', 's',
                                                         definition_source=None)
        actual_definition = builtin_symbol.definition
        actual_container = actual_definition.symbol_container

        assertion_that_is_expected_to_succeed = asrt.is_(actual_container)

        assertion_to_check = symbol_usage_assertions.matches_definition(
            name=asrt.anything_goes(),
            container=assertion_that_is_expected_to_succeed)
        # ACT & ASSERT #
        assertion_to_check.apply_without_message(self, actual_definition)
Example #6
0
    def test_arbitrary_failure(self):
        # ARRANGE #
        builtin_symbol = StringSymbolContext.of_constant('the name',
                                                         's',
                                                         definition_source=None)
        actual_definition = builtin_symbol.definition
        actual_container = actual_definition.symbol_container

        assertion_that_is_expected_to_succeed = asrt.not_(asrt.is_(actual_container))

        assertion_to_check = symbol_usage_assertions.matches_definition(
            name=asrt.anything_goes(),
            container=assertion_that_is_expected_to_succeed)
        # ACT & ASSERT #
        assert_that_assertion_fails(assertion_to_check, actual_definition)
Example #7
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)
Example #8
0
    def test_successful_parse_and_application_of_non_trivial_matcher(self):
        defined_name = 'defined_name'

        expected_container = matches_container(
            matches_string_matcher_resolver()
        )

        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(
                src('{string_matcher_type} {defined_name} = {matcher_argument}',
                    defined_name=defined_name,
                    matcher_argument=not_num_lines_eq_1_matcher_arg),
            )
            expectation = Expectation(
                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,
                ),
                assertion_on_instruction_environment=
                AssertApplicationOfMatcherInSymbolTable(defined_name,
                                                        actual_model_contents=case.actual,
                                                        expected_matcher_result=case.expected),
            )

            with self.subTest(case.name):
                # ACT & ASSERT #
                self._check(source, ArrangementWithSds(), expectation)
Example #9
0
    def test_assignment_of_program_without_arguments(self):
        python_source = 'exit(72)'

        resolver_of_referred_program = program_resolvers.for_py_source_on_command_line(python_source)

        name_of_defined_symbol = 'the_symbol'

        referred_symbol = NameAndValue(
            'PRE_EXISTING_PROGRAM_SYMBOL',
            resolver_of_referred_program
        )

        symbols = SymbolTable({
            referred_symbol.name:
                symbol_utils.container(referred_symbol.value)
        })

        program = pgm_args.symbol_ref_command_line(sym_ref_args.sym_ref_cmd_line(
            referred_symbol.name))
        source = multi_line_source('{program_type} {defined_symbol} = {program}',
                                   ['following line'],
                                   defined_symbol=name_of_defined_symbol,
                                   program=program)
        expected_symbol_container = asrt_rs.matches_container(
            assertion_on_resolver=asrt_resolver.matches_resolver_of_program(
                references=asrt.matches_sequence([
                    is_program_reference_to(referred_symbol.name)
                ]),
                resolved_program_value=asrt_dir_dep_val.matches_multi_dir_dependent_value(
                    DirDependencies.NONE,
                    lambda tcds: matches_py_source_on_cmd_line_program(python_source)),
                symbols=symbols
            ))
        expectation = Expectation(
            source=asrt_source.is_at_beginning_of_line(2),
            symbol_usages=asrt.matches_sequence([
                asrt_sym_usage.matches_definition(
                    name=asrt.equals(name_of_defined_symbol),
                    container=expected_symbol_container)
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                expected_name=name_of_defined_symbol,
                value_assertion=expected_symbol_container,
            )
        )
        self._check(source, ArrangementWithSds(), expectation)
Example #10
0
    def test_successful_parse_of_reference(self):
        defined_name = 'defined_name'

        referenced_symbol = NameAndValue('referenced_name',
                                         ARBITRARY_RESOLVER)

        symbols = SymbolTable({
            referenced_symbol.name:
                symbol_utils.container(referenced_symbol.value)
        })

        # ARRANGE #

        source = single_line_source(
            src('{string_matcher_type} {defined_name} = {matcher_argument}',
                defined_name=defined_name,
                matcher_argument=referenced_symbol.name),
        )

        arrangement = ArrangementWithSds()

        # EXPECTATION #

        expected_container = matches_container(
            matches_string_matcher_resolver(
                references=asrt.matches_sequence([
                    is_reference_to_string_matcher(referenced_symbol.name)
                ]),
                symbols=symbols)
        )

        expectation = Expectation(
            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 #

        self._check(source, arrangement, expectation)
Example #11
0
def check_matcher_should_be_parsed_as_full_expression(put: unittest.TestCase,
                                                      symbol_1: MatcherTypeSymbolContext,
                                                      symbol_2: MatcherTypeSymbolContext,
                                                      value_type: ValueType,
                                                      ):
    # ARRANGE #
    symbols = [symbol_1, symbol_2]

    value_argument = matcher_argument.disjunction([symbol_1.argument, symbol_2.argument])

    defined_name = 'the_defined_name'

    source = remaining_source(
        src3(value_type,
             defined_name,
             value_argument.as_str,
             ),
    )

    # EXPECTATION #

    expected_symbol_references = SymbolContext.references_assertion_of_contexts(symbols)

    expected_container = matches_container(
        asrt.equals(value_type),
        type_sdv_assertions.matches_sdv_of_file_matcher(
            references=expected_symbol_references,
            primitive_value=asrt.anything_goes(),
            symbols=SymbolContext.symbol_table_of_contexts(symbols)
        )
    )
    expected_symbol_usages = asrt.matches_singleton_sequence(
        asrt_sym_usage.matches_definition(asrt.equals(defined_name), expected_container)
    )

    expected_source = asrt_source.source_is_at_end

    # ACT #

    actual = sut.PARTS_PARSER.parse(ARBITRARY_FS_LOCATION_INFO, source)

    # ASSERT #

    expected_source.apply_with_message(put, source, 'source')
    expected_symbol_usages.apply_with_message(put, actual.symbol_usages, 'symbol usages')
Example #12
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)
Example #13
0
    def runTest(self):
        python_source = 'exit(72)'

        name_of_defined_symbol = 'the_symbol'

        referred_symbol = ProgramSymbolContext.of_sdv(
            'PRE_EXISTING_PROGRAM_SYMBOL',
            program_sdvs.for_py_source_on_command_line(python_source))

        symbols = referred_symbol.symbol_table

        program_abs_stx = ProgramOfSymbolReferenceAbsStx(referred_symbol.name)

        define_symbol_syntax = DefineSymbolWMandatoryValue(
            name_of_defined_symbol,
            ValueType.PROGRAM,
            program_abs_stx,
        )

        expected_symbol_container = matches_container(
            asrt.equals(ValueType.PROGRAM),
            sdv=type_sdv_assertions.matches_sdv_of_program_constant(
                references=asrt.matches_sequence(
                    [referred_symbol.reference_assertion]),
                primitive_value=matches_py_source_on_cmd_line_program(
                    python_source),
                symbols=symbols))
        expectation = MultiSourceExpectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence([
                asrt_sym_usage.matches_definition(
                    name=asrt.equals(name_of_defined_symbol),
                    container=expected_symbol_container)
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                expected_name=name_of_defined_symbol,
                value_assertion=expected_symbol_container,
            ))
        INSTRUCTION_CHECKER.check__abs_stx__layout_and_source_variants(
            self,
            define_symbol_syntax,
            Arrangement.phase_agnostic(),
            expectation,
        )
Example #14
0
    def test_successful_parse(self):
        name_pattern = 'the name pattern'
        cases = [
            ('empty RHS SHOULD give selection of all files',
             '',
             file_matchers.FileMatcherConstant(True),
             ),
            ('name pattern in RHS SHOULD give selection of name pattern',
             file_matcher_arguments(name_pattern=name_pattern),
             file_matchers.FileMatcherNameGlobPattern(name_pattern),
             ),
            ('file type in RHS SHOULD give selection of name pattern',
             file_matcher_arguments(type_match=FileType.REGULAR),
             file_matchers.FileMatcherType(FileType.REGULAR),
             ),
        ]
        # ARRANGE #
        defined_name = 'defined_name'
        for name, rhs_source, expected_file_matcher in cases:
            with self.subTest(name=name):
                source = single_line_source(
                    src('{file_matcher_type} {defined_name} = {selector_argument}',
                        defined_name=defined_name,
                        selector_argument=rhs_source),
                )

                expected_container = matches_container(
                    resolved_value_equals_file_matcher(expected_file_matcher)
                )

                expectation = Expectation(
                    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 #
                self._check(source, ArrangementWithSds(), expectation)
Example #15
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):
Example #16
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,
        )
    )
Example #17
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,
        )
    )
Example #18
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)
Example #19
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)
Example #20
0
    def test_successful_parse_of_regex(self):
        # ARRANGE #

        regex_str = 'the_regex'
        models_for_equivalence_check = [
            asrt_matcher.ModelInfo((1, regex_str)),
            asrt_matcher.ModelInfo((2, 'before' + regex_str + 'after')),
            asrt_matcher.ModelInfo((1, 'no match')),
        ]

        symbol = LineMatcherSymbolContext.of_sdv('the_symbol_name',
                                                 CONSTANT_TRUE_MATCHER_SDV)

        regex_matcher_syntax = argument_syntax.syntax_for_regex_matcher(
            regex_str)

        matcher_argument = argument_syntax.syntax_for_and([
            symbol.name,
            regex_matcher_syntax,
        ])

        defined_name = 'defined_name'

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

        for argument_case in argument_cases:
            with self.subTest(argument_case.name):
                source = remaining_source(
                    src2(ValueType.LINE_MATCHER,
                         defined_name,
                         argument_case.value,
                         matcher_argument=matcher_argument),
                    following_lines=['following line'],
                )

                # EXPECTATION #

                symbol_table = symbol.symbol_table

                expected_matcher_sdv = parse_line_matcher.parsers().full.parse(
                    remaining_source(matcher_argument))

                expected_matcher = resolving_helper(
                    symbol_table).resolve_matcher(expected_matcher_sdv)

                expected_container = matches_container(
                    asrt.equals(ValueType.LINE_MATCHER),
                    sdv=type_sdv_assertions.matches_sdv_of_line_matcher(
                        references=asrt.matches_sequence([
                            is_reference_to_line_matcher(symbol.name),
                        ]),
                        primitive_value=asrt_matcher.is_equivalent_to(
                            expected_matcher, models_for_equivalence_check),
                        symbols=symbol_table,
                    ))

                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)
Example #21
0
    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)
Example #22
0
 def assert_matches_definition_of_sdv(self) -> Assertion[SymbolDefinition]:
     return matches_definition(
         name=asrt.equals(self.name),
         container=self.value.assert_matches_container_of_sdv)
Example #23
0
    def test_successful_parse_of_sequence(self):
        # ARRANGE #

        regex_str = 'the_regex'
        replacement_str = 'the_replacement'

        symbol = NameAndValue('the_symbol_name',
                              transformers.CustomStringTransformerTestImpl())

        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(
                           src('{lines_trans_type} {defined_name} = {transformer_argument}',
                               defined_name=defined_name,
                               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(2)
                       ),
            SourceCase('1st expr on first line followed by operator, 2nd expr on next line',
                       source=
                       remaining_source(
                           src('{lines_trans_type} {defined_name} = {symbol_name} {sequence_operator}',
                               defined_name=defined_name,
                               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(
                           src('{lines_trans_type} {defined_name} = {symbol_name} {sequence_operator}',
                               defined_name=defined_name,
                               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 #

        the_sequence_transformer = SequenceStringTransformer([
            symbol.value,
            transformers.replace_transformer(regex_str,
                                             replacement_str),
        ])

        expected_container = matches_container(
            assertion_on_resolver=
            resolved_value_equals_string_transformer(
                the_sequence_transformer,
                references=asrt.matches_sequence([
                    is_reference_to_string_transformer(symbol.name),
                ]),
                symbols=SymbolTable({
                    symbol.name: container(StringTransformerConstant(symbol.value)),
                }),
            )
        )

        for source_case in cases:
            with self.subTest(source_case.name):
                expectation = Expectation(
                    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 #

                self._check(source_case.source, ArrangementWithSds(), expectation)