示例#1
0
    def test_assignment_of_list_with_multiple_constant_elements(self):
        # ARRANGE #
        value_without_space = 'value_without_space'
        value_with_space = 'value with space'

        symbol_to_assign = ListSymbolContext.of_sdv(
            'the_symbol_name',
            sdvs.from_str_constants([value_without_space, value_with_space]))

        syntax = _syntax(
            symbol_to_assign,
            NonEmptyListAbsStx([
                ListElementStringAbsStx.of_str(value_without_space),
                ListElementStringAbsStx.of_str(value_with_space,
                                               QuoteType.SOFT),
            ]))

        expectation = MultiSourceExpectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence(
                [symbol_to_assign.assert_matches_definition_of_sdv]),
            symbols_after_main=assert_symbol_table_is_singleton(
                symbol_to_assign.name,
                symbol_to_assign.value.assert_matches_container_of_sdv,
            ),
        )
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check__abs_stx__layout_and_source_variants(
            self,
            syntax,
            Arrangement.phase_agnostic(),
            expectation,
        )
示例#2
0
    def test_assignment_of_list_with_symbol_references(self):
        # ARRANGE #
        referred_symbol_name = 'referred_symbol'
        expected_symbol_reference = references.reference_to__w_str_rendering(
            referred_symbol_name)
        symbol_to_assign = ListSymbolContext.of_sdv(
            'the_symbol_name',
            sdvs.from_elements(
                [sdvs.symbol_element(expected_symbol_reference)]))

        syntax = _syntax(symbol_to_assign,
                         ListSymbolReferenceAbsStx(referred_symbol_name))
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check__abs_stx__layout_and_source_variants(
            self,
            syntax,
            Arrangement.phase_agnostic(),
            MultiSourceExpectation.phase_agnostic(
                symbol_usages=asrt.matches_sequence(
                    [symbol_to_assign.assert_matches_definition_of_sdv]),
                symbols_after_main=assert_symbol_table_is_singleton(
                    symbol_to_assign.name,
                    symbol_to_assign.value.assert_matches_container_of_sdv,
                ),
            ),
        )
示例#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)))
示例#4
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)
示例#5
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)
示例#6
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)
示例#7
0
    def test_reference_to_string_source(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME

        referenced_symbol = StringSourceSymbolContextOfPrimitiveConstant('referenced_name',
                                                                         'contents of string source')
        syntax = _syntax_of(
            referenced_symbol.abstract_syntax,
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            referenced_symbol.symbol_table,
            referenced_symbol.references_assertion,
            referenced_symbol.contents_str,
            may_depend_on_external_resources=False
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            syntax,
            arrangement,
            expectation,
        )
示例#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)))
示例#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)))
示例#10
0
    def test_reference(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME
        create_file = fs.File('created-file.txt',
                              'contents of created file')

        referenced_symbol = FilesSourceSymbolContext.of_primitive(
            'referenced_name',
            file_list.Primitive([
                file_list.FileSpecification(
                    create_file.name,
                    RegularFileMaker(
                        ModificationType.CREATE,
                        string_sources.of_string(create_file.contents),
                        None,
                    ),
                ),
            ]),
        )
        instruction_syntax = _syntax_of(
            referenced_symbol.abstract_syntax,
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            referenced_symbol.symbol_table,
            (),
            referenced_symbol.references_assertion,
            asrt_fs.dir_contains_exactly_2([create_file])
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            instruction_syntax,
            arrangement,
            expectation,
        )
示例#11
0
    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 argument_case in argument_cases:
            with self.subTest(argument_case.name):
                source = single_line_source(
                    src2(
                        ValueType.STRING_MATCHER,
                        defined_name,
                        argument_case.value,
                        matcher_argument=arg_syntax.
                        arbitrary_single_line_value_that_must_not_be_quoted()),
                )

                # EXPECTATION #

                expected_container = matches_container(
                    asrt.equals(ValueType.STRING_MATCHER),
                    matches_sdv_of_string_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)
示例#12
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,
        )
示例#13
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)
示例#14
0
    def test_here_doc_with_embedded_references(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME

        here_doc_line_template = 'pre symbol {symbol} post symbol'

        referenced_symbol = StringConstantSymbolContext(
            'REFERENCED_STRING_SYMBOL',
            'contents of string symbol',
            default_restrictions=data_restrictions_assertions.is__w_str_rendering(),
        )
        expected_contents = here_doc_line_template.format(symbol=referenced_symbol.str_value) + '\n'

        syntax = _syntax_of(
            StringSourceOfStringAbsStx(
                HereDocAbsStx.of_lines__wo_new_lines([
                    here_doc_line_template.format(symbol=referenced_symbol.name__sym_ref_syntax)
                ])
            ),
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            referenced_symbol.symbol_table,
            referenced_symbol.references_assertion,
            expected_contents,
            may_depend_on_external_resources=False
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            syntax,
            arrangement,
            expectation,
        )
示例#15
0
    def test_assignment_of_empty_list(self):
        list_symbol = ListSymbolContext.of_empty('the_symbol_name')

        syntax = _syntax(list_symbol, EmptyListAbsStx())

        expectation = MultiSourceExpectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence(
                [list_symbol.assert_matches_definition_of_sdv]),
            symbols_after_main=assert_symbol_table_is_singleton(
                list_symbol.name,
                list_symbol.value.assert_matches_container_of_sdv,
            ),
        )

        INSTRUCTION_CHECKER.check__abs_stx__layout_and_source_variants(
            self,
            syntax,
            Arrangement.phase_agnostic(),
            expectation,
        )
示例#16
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)))
示例#17
0
    def test_literal(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME
        create_file = fs.File('created-file.txt',
                              'contents of created file')

        literal_syntax = abs_stx.LiteralFilesSourceAbsStx([
            abs_stx.regular_file_spec(
                StringLiteralAbsStx(create_file.name, QuoteType.HARD),
                abs_stx.FileContentsExplicitAbsStx(
                    ModificationType.CREATE,
                    StringSourceOfStringAbsStx.of_str(create_file.contents,
                                                      QuoteType.HARD),
                )
            )
        ])
        syntax = _syntax_of(
            literal_syntax,
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            SymbolTable.empty(),
            (),
            asrt.is_empty_sequence,
            asrt_fs.dir_contains_exactly_2([create_file])
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            syntax,
            arrangement,
            expectation,
        )
示例#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)
示例#19
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)
示例#20
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)
示例#21
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)
示例#22
0
                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),
            )

            with self.subTest(case.name):
                # ACT & ASSERT #
                INSTRUCTION_CHECKER.check(
                    self,
                    source,
                    Arrangement.phase_agnostic(
                        tcds=TcdsArrangement()
                    ),
                    expectation,
                )

    @staticmethod
    def _not_num_files_beginning_with_a_eq_1_arg() -> str:
        file_matcher_arg__begins_with_a = fm_args.file_matcher_arguments(
            name_pattern='a*'
        )

        files_matcher_args__num_files_eq_1 = fsm_args.NumFilesAssertionVariant(
            int_args.int_condition(comparators.EQ, 1)
        )

        args = fsm_args.SelectionAndMatcherArgumentsConstructor(
示例#23
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)
示例#24
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)
示例#25
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 argument_case in argument_cases:
            with self.subTest(argument_case.name):
                source = single_line_source(
                    src2(
                        ValueType.STRING_MATCHER,
                        defined_name,
                        argument_case.value,
                        matcher_argument=arg_syntax.
                        arbitrary_single_line_value_that_must_not_be_quoted()),
                )

                # EXPECTATION #

                expected_container = matches_container(
                    asrt.equals(ValueType.STRING_MATCHER),
                    matches_sdv_of_string_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)

    def test_successful_parse_of_reference(self):
        defined_name = 'defined_name'

        referenced_symbol = StringMatcherSymbolContext.of_arbitrary_value(
            'referenced_name')

        symbols = referenced_symbol.symbol_table

        # ARRANGE #

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

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expected_container = matches_container(
            asrt.equals(ValueType.STRING_MATCHER),
            matches_sdv_of_string_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)

    def test_matcher_SHOULD_be_parsed_as_full_expression(self):
        matcher_helpers.check_matcher_should_be_parsed_as_full_expression(
            self,
            StringMatcherSymbolContext.of_arbitrary_value('symbol_1'),
            StringMatcherSymbolContext.of_arbitrary_value('symbol_2'),
            ValueType.STRING_MATCHER,
        )

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