예제 #1
0
파일: stdin.py 프로젝트: emilkarlen/exactly
 def test_here_document(self):
     source = assignment_of_list_of_args(['<<MARKER'],
                                         ['single line',
                                          'MARKER',
                                          'following line'])
     sut.Parser().parse(ARBITRARY_FS_LOCATION_INFO, source)
     is_at_beginning_of_line(4).apply_with_message(self, source, 'source')
def _source_variant_test_cases__multi_line(num_source_lines: int
                                           ) -> List[Tuple[List[str], ValueAssertion[ParseSource]]]:
    return [
        ([], every_line_is_consumed),
        (['following line'], is_at_beginning_of_line(num_source_lines + 1)),
        (['  '], is_at_beginning_of_line(num_source_lines + 1)),
    ]
예제 #3
0
def _source_variant_test_cases__multi_line(
        num_source_lines: int
) -> List[Tuple[List[str], Assertion[ParseSource]]]:
    return [
        ([], asrt_source.source_is_at_end),
        (['following line'],
         asrt_source.is_at_beginning_of_line(num_source_lines + 1)),
        (['  '], asrt_source.is_at_beginning_of_line(num_source_lines + 1)),
    ]
예제 #4
0
 def test_positive(self):
     cases = [
         SourceCase('Dir on first line, DIR-CONTENTS-MATCHER on following line',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        EMPTINESS_CHECK_ARGUMENT,
                    ]),
                    source_assertion=asrt_source.source_is_at_end
                    ),
         SourceCase('Dir on first line, DIR-CONTENTS-MATCHER on following line, followed by non-instr line',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        EMPTINESS_CHECK_ARGUMENT,
                        'following line',
                    ]),
                    source_assertion=asrt_source.is_at_beginning_of_line(3)
                    ),
         SourceCase('Empty lines between arguments',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        '',
                        EMPTINESS_CHECK_ARGUMENT,
                        '',
                        'following line',
                    ]),
                    source_assertion=asrt_source.is_at_beginning_of_line(4)
                    ),
         SourceCase('Followed by empty line '
                    '(a bit strange behaviour - should probably be at beginning of last line)',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        EMPTINESS_CHECK_ARGUMENT,
                        '',
                    ]),
                    source_assertion=asrt_source.source_is_at_end
                    ),
         SourceCase('With selection on separate line',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        selection_arguments(name_pattern='*'),
                        EMPTINESS_CHECK_ARGUMENT,
                        '',
                        'following line',
                    ]),
                    source_assertion=asrt_source.is_at_beginning_of_line(4)
                    ),
     ]
     for case in cases:
         self._check_sub_test(case,
                              PassOrFailOrHardErrorEnum.PASS)
예제 #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_container = string_constant_container(value_str + '\n')
        expected_definition = SymbolDefinition(symbol_name, expected_container)
        expectation = Expectation(
            symbol_usages=asrt.matches_sequence([
                vs_asrt.equals_symbol(expected_definition, ignore_source_line=True)
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                symbol_name,
                equals_container(expected_container),
            ),
            source=asrt_source.is_at_beginning_of_line(4)
        )
        # ACT & ASSERT #
        self._check(source, ArrangementWithSds(), expectation)
예제 #6
0
    def test_path_argument_SHOULD_use_posix_syntax(self):
        # ARRANGE #
        inclusion_directive_name = 'incl-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)

        path_arg = pathlib.PurePosixPath('first') / 'second'
        inclusion_directive_line = inclusion_directive_name + ' ' + str(
            path_arg)
        source = source_of_lines([
            inclusion_directive_line,
            'second line',
        ])
        # ACT #
        actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
        # ASSERT #
        expected_directive = is_file_inclusion_directive(
            matches_file_inclusion_directive(
                files_to_include=asrt.matches_sequence([
                    asrt.equals(pathlib.Path(path_arg)),
                ]),
                source=matches_line_sequence(
                    first_line_number=asrt.equals(1),
                    lines=asrt.matches_sequence(
                        [asrt.equals(inclusion_directive_line)]))))
        expected_directive.apply_with_message(self, actual, 'parsed directive')

        expected_source = is_at_beginning_of_line(2)
        expected_source.apply_with_message(self, source, 'source')
예제 #7
0
파일: stdin.py 프로젝트: emilkarlen/exactly
 def test_file__rel_home(self):
     accepted_relativity_options = [
         rel_opt_conf.conf_rel_any(RelOptionType.REL_HOME_CASE),
         rel_opt_conf.default_conf_rel_any(RelOptionType.REL_HOME_CASE),
         rel_opt_conf.symbol_conf_rel_any(
             RelOptionType.REL_HOME_CASE,
             'SYMBOL',
             sut.RELATIVITY_OPTIONS_CONFIGURATION.options.accepted_relativity_variants),
     ]
     for rel_opt in accepted_relativity_options:
         with self.subTest(option_string=rel_opt.option_argument):
             self._run(assignment_of('{file_option} {relativity_option} file.txt'.format(
                 file_option=option_syntax(parse_here_doc_or_file_ref.FILE_ARGUMENT_OPTION),
                 relativity_option=rel_opt.option_argument),
                 ['following line']),
                 Arrangement(
                     hds_contents=case_home_dir_contents(
                         DirContents([empty_file('file.txt')])),
                     symbols=rel_opt.symbols.in_arrangement(),
                 ),
                 Expectation(
                     settings_builder=AssertStdinFileIsSetToFile(
                         file_refs.of_rel_option(RelOptionType.REL_HOME_CASE,
                                                 file_refs.constant_path_part('file.txt'))),
                     symbol_usages=rel_opt.symbols.usages_expectation(),
                     source=is_at_beginning_of_line(2)),
             )
    def test_with_symbol_references(self):
        symbol1 = NameAndValue('symbol_1_name', 'symbol 1 value')
        line_with_sym_ref_template = 'before symbol {symbol} after symbol'
        source = remaining_source_lines(['<<MARKER',
                                         line_with_sym_ref_template.format(
                                             symbol=symbol_reference_syntax_for_name(symbol1.name)),
                                         'MARKER',
                                         'Line 4',
                                         ]
                                        )
        expectation = ExpectedHereDoc(
            resolved_here_doc_lines=[
                line_with_sym_ref_template.format(
                    symbol=symbol1.value)
            ],
            common=CommonExpectation(
                symbol_references=[
                    references.reference_to_any_data_type_value(symbol1.name),
                ],
                symbol_table=symbol_table_with_string_values_from_name_and_value([
                    symbol1,
                ]),
                source=asrt_source.is_at_beginning_of_line(4),
            )

        )
        _expect_here_doc(self, source, expectation)
예제 #9
0
    def test_assignment_of_list_with_symbol_references(self):
        symbol_name = 'the_symbol_name'
        referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
        source = remaining_source(src(
            '{list_type} {symbol_name} = {symbol_reference} ',
            symbol_name=symbol_name,
            symbol_reference=referred_symbol,
        ),
            ['following line'],
        )
        expected_symbol_reference = references.reference_to_any_data_type_value(referred_symbol.name)
        expected_resolver = lrs.from_elements([lrs.symbol_element(expected_symbol_reference)])

        expected_resolver_container = container(expected_resolver)

        expectation = Expectation(
            symbol_usages=asrt.matches_sequence([
                vs_asrt.equals_symbol(SymbolDefinition(symbol_name, expected_resolver_container),
                                      ignore_source_line=True)
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                symbol_name,
                equals_container(expected_resolver_container),
            ),
            source=asrt_source.is_at_beginning_of_line(2),
        )
        self._check(source, ArrangementWithSds(), expectation)
예제 #10
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)
예제 #11
0
    def test_assignment_of_empty_list(self):
        symbol_name = 'the_symbol_name'
        sb = SB.new_with(symbol_name=symbol_name)

        cases = [
            SourceCase('No following lines',
                       source=sb.single_line('{list_type} {symbol_name} = '),
                       source_assertion=asrt_source.source_is_at_end
                       ),
            SourceCase('Following lines',
                       source=sb.lines(['{list_type} {symbol_name} = ',
                                        'following line'],
                                       ),
                       source_assertion=asrt_source.is_at_beginning_of_line(2)
                       ),
        ]
        expected_resolver = lrs.empty()
        expected_resolver_container = container(expected_resolver)

        for case in cases:
            with self.subTest(case.name):
                expectation = Expectation(
                    symbol_usages=asrt.matches_sequence([
                        vs_asrt.equals_symbol(SymbolDefinition(symbol_name, expected_resolver_container),
                                              ignore_source_line=True)
                    ]),
                    symbols_after_main=assert_symbol_table_is_singleton(
                        symbol_name,
                        equals_container(expected_resolver_container),
                    ),
                    source=case.source_assertion
                )
                self._check(case.source, ArrangementWithSds(), expectation)
예제 #12
0
    def runTest(self):
        expected_content_line_template = 'expected content line, with {symbol} ref'

        def expected_content(symbol_content: str) -> str:
            return expected_content_line_template.format(symbol=symbol_content)

        symbol = NameAndValue('symbol_name', 'the symbol value')
        self._check(
            self.configuration.source_for(
                args('{maybe_not} {equals} <<EOF',
                     maybe_not=self.maybe_not.nothing__if_positive__not_option__if_negative),
                [expected_content(symbol_reference_syntax_for_name(symbol.name)),
                 'EOF',
                 'following line']),
            self.configuration.arrangement_for_contents(
                lines_content([expected_content(symbol.value)]),
                post_sds_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY,
                symbols=SymbolTable({
                    symbol.name: data_symbol_utils.string_constant_container(symbol.value),
                })),
            Expectation(main_result=self.maybe_not.pass__if_positive__fail__if_negative,
                        symbol_usages=equals_symbol_references([
                            SymbolReference(symbol.name, is_any_data_type())
                        ]),
                        source=is_at_beginning_of_line(4)),
        )
    def test_path_argument_SHOULD_use_posix_syntax(self):
        # ARRANGE #
        inclusion_directive_name = 'incl-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)

        path_arg = pathlib.PurePosixPath('first') / 'second'
        inclusion_directive_line = inclusion_directive_name + ' ' + str(path_arg)
        source = source_of_lines([
            inclusion_directive_line,
            'second line',
        ])
        # ACT #
        actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
        # ASSERT #
        expected_directive = is_file_inclusion_directive(
            matches_file_inclusion_directive(
                files_to_include=asrt.matches_sequence([
                    asrt.equals(pathlib.Path(path_arg)),
                ]),
                source=matches_line_sequence(
                    first_line_number=asrt.equals(1),
                    lines=asrt.matches_sequence([
                        asrt.equals(inclusion_directive_line)
                    ]))
            ))
        expected_directive.apply_with_message(self,
                                              actual,
                                              'parsed directive')

        expected_source = is_at_beginning_of_line(2)
        expected_source.apply_with_message(self, source, 'source')
예제 #14
0
파일: stdin.py 프로젝트: emilkarlen/exactly
 def test_doc_without_symbol_references(self):
     content_line_of_here_doc = 'content line of here doc'
     self._run(assignment_of(' <<MARKER  ',
                             [content_line_of_here_doc,
                              'MARKER',
                              'following line']),
               Arrangement(),
               Expectation(
                   settings_builder=AssertStdinIsSetToContents(
                       string_resolvers.str_constant(hd.contents_str_from_lines([content_line_of_here_doc])),
                   ),
                   source=is_at_beginning_of_line(4)),
               )
예제 #15
0
 def runTest(self):
     self._check(
         self.configuration.source_for(
             args('{maybe_not} {equals} <<EOF',
                  maybe_not=self.maybe_not.nothing__if_positive__not_option__if_negative),
             ['expected content line',
              'EOF',
              'the following line']),
         self.configuration.arrangement_for_contents(
             lines_content(['actual contents that is not equal to expected contents']),
             post_sds_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY),
         Expectation(main_result=self.maybe_not.fail__if_positive__pass_if_negative,
                     source=is_at_beginning_of_line(4)),
     )
예제 #16
0
 def test_not_last_line(self):
     expected_file = fs.empty_file('a-file-name.txt')
     self._check(
         remaining_source(
             '{file_name}'.format(file_name=expected_file.file_name),
             ['following line']),
         ArrangementWithSds(
             pre_contents_population_action=SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
         ),
         Expectation(
             main_result=IS_SUCCESS,
             source=is_at_beginning_of_line(2),
         ),
     )
    def test_without_symbol_references(self):
        expected_contents_line = 'contents'
        source = remaining_source_lines(['<<MARKER',
                                         expected_contents_line,
                                         'MARKER',
                                         'Line 4',
                                         ])
        expectation = ExpectedHereDoc(
            resolved_here_doc_lines=[expected_contents_line],
            common=CommonExpectation(
                symbol_references=[],
                source=asrt_source.is_at_beginning_of_line(4))

        )
        _expect_here_doc(self, source, expectation)
예제 #18
0
 def runTest(self):
     expected_contents = 'expected contents'
     self._check(
         self.configuration.source_for(
             args('{maybe_not} {equals} {expected_contents}  ',
                  expected_contents=surrounded_by_hard_quotes_str(expected_contents),
                  maybe_not=self.maybe_not.nothing__if_positive__not_option__if_negative),
             ['following line']
         ),
         self.configuration.arrangement_for_contents(
             expected_contents,
             post_sds_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY),
         Expectation(main_result=self.maybe_not.pass__if_positive__fail__if_negative,
                     source=asrt_source.is_at_beginning_of_line(2)),
     )
    def test_WHEN_source_does_not_start_with_directive_name_THEN_return_value_SHOULD_be_none(self):
        # ARRANGE #
        inclusion_directive_name = 'the-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)
        source = source_of_lines([
            inclusion_directive_name + 'non-space arg',
            'second line',
        ])
        # ACT #
        actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
        # ASSERT #
        self.assertIsNone(actual, 'return value')

        expected_source = is_at_beginning_of_line(1)
        expected_source.apply_with_message(self, source, 'source')
예제 #20
0
    def test_WHEN_source_does_not_start_with_directive_name_THEN_return_value_SHOULD_be_none(
            self):
        # ARRANGE #
        inclusion_directive_name = 'the-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)
        source = source_of_lines([
            inclusion_directive_name + 'non-space arg',
            'second line',
        ])
        # ACT #
        actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
        # ASSERT #
        self.assertIsNone(actual, 'return value')

        expected_source = is_at_beginning_of_line(1)
        expected_source.apply_with_message(self, source, 'source')
예제 #21
0
    def test_with_symbol_references(self):
        symbol_1_name = 'symbol_1_name'
        symbol_2_name = 'symbol_2_name'

        test_cases = [
            CaseWithSymbols(
                'two argument form with a symbol that is a single integer',
                argument=' {op} {sym_ref}'.format(op=comparators.EQ.name,
                                                  sym_ref=symbol_reference_syntax_for_name(symbol_1_name)),
                symbol_name_and_value_list=[NameAndValue(symbol_1_name, '72')],
                actual_value_for_pass=72,
                actual_value_for_fail=87,
            ),
            CaseWithSymbols(
                'two argument form with two symbols that makes up the expected value when concatenated',
                argument=' {op} {sym_ref1}{sym_ref2}'.format(
                    op=comparators.EQ.name,
                    sym_ref1=symbol_reference_syntax_for_name(symbol_1_name),
                    sym_ref2=symbol_reference_syntax_for_name(symbol_2_name)),
                symbol_name_and_value_list=[NameAndValue(symbol_1_name, '7'),
                                            NameAndValue(symbol_2_name, '2')],
                actual_value_for_pass=72,
                actual_value_for_fail=87,
            ),
        ]
        for case in test_cases:
            for sub_name, actual_value, result_expectation in [('pass',
                                                                case.actual_value_for_pass,
                                                                pfh_assertions.is_pass()),
                                                               ('fail',
                                                                case.actual_value_for_fail,
                                                                pfh_assertions.is_fail())]:
                with self.subTest(name=case.name, sub_name=sub_name):
                    self._run(
                        remaining_source(case.argument,
                                         ['following line']),
                        ArrangementPostAct(
                            act_result_producer=act_result_of(actual_value),
                            symbols=symbol_table_with_string_constant_symbols(case.symbol_name_and_value_list),
                        ),
                        Expectation(
                            source=is_at_beginning_of_line(2),
                            main_result=result_expectation,
                            symbol_usages=equals_symbol_references(
                                string_symbol_references_of(case.symbol_name_and_value_list))
                        ),
                    )
예제 #22
0
 def test_not_last_line__contents(self):
     hd_args = here_document_contents_arguments([]).as_arguments
     self._check(
         remaining_source(
             '{file_name} {hd_args}'.format(
                 file_name='a-file-name.txt',
                 hd_args=hd_args.first_line,
             ),
             hd_args.following_lines + ['following line']),
         ArrangementWithSds(
             pre_contents_population_action=SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
         ),
         Expectation(
             main_result=IS_SUCCESS,
             source=is_at_beginning_of_line(3),
         ),
     )
예제 #23
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)
예제 #24
0
 def test_element_from_first_parser_that_returns_an_element_SHOULD_be_returned(self):
     # ARRANGE #
     source_lines = ['first line',
                     'second line']
     expected_returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'expected'),
                                                             ElementType.EMPTY)
     unexpected_returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'unexpected'),
                                                               ElementType.EMPTY)
     cases = [
         NameAndValue('single successful parser',
                      [
                          SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element),
                      ])
         ,
         NameAndValue('more than one successful parser',
                      [
                          SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element),
                          SectionElementParserThatReturnsConstantAndConsumesCurrentLine(unexpected_returned_element),
                      ]
                      ),
         NameAndValue('first parser is unsuccessful - returns None',
                      [
                          SectionElementParserThatReturnsNone(),
                          SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element),
                      ]
                      ),
         NameAndValue('first parser is unsuccessful - throws unrecognized element',
                      [
                          SectionElementParserThatRaisesUnrecognizedSectionElementSourceError(),
                          SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element),
                      ]
                      ),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = source_of_lines(source_lines)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             self.assertIs(expected_returned_element,
                           actual, 'return value from parser')
             expected_source = asrt_source.is_at_beginning_of_line(2)
             expected_source.apply_with_message(self, source, 'source')
예제 #25
0
    def test_assignment_of_list_with_multiple_constant_elements(self):
        symbol_name = 'the_symbol_name'
        value_without_space = 'value_without_space'
        value_with_space = 'value with space'
        expected_resolver = lrs.from_str_constants([value_without_space,
                                                    value_with_space])
        expected_resolver_container = container(expected_resolver)

        sb = SB.new_with(symbol_name=symbol_name,
                         value_without_space=value_without_space,
                         value_with_space=value_with_space)
        cases = [
            SourceCase('No following lines',
                       source=sb.single_line(
                           '{list_type} {symbol_name} = '
                           '{value_without_space} {soft_quote}{value_with_space}{soft_quote} '),
                       source_assertion=asrt_source.source_is_at_end
                       ),
            SourceCase('Following lines',
                       source=sb.lines(
                           [
                               '{list_type} {symbol_name} = '
                               '{value_without_space} {soft_quote}{value_with_space}{soft_quote}',
                               'following line',
                           ]),
                       source_assertion=asrt_source.is_at_beginning_of_line(2)
                       ),
        ]

        for case in cases:
            with self.subTest(case.name):
                expectation = Expectation(
                    symbol_usages=asrt.matches_sequence([
                        vs_asrt.equals_symbol(SymbolDefinition(symbol_name, expected_resolver_container),
                                              ignore_source_line=True)
                    ]),
                    symbols_after_main=assert_symbol_table_is_singleton(
                        symbol_name,
                        equals_container(expected_resolver_container),
                    ),
                    source=case.source_assertion,
                )
                self._check(case.source, ArrangementWithSds(), expectation)
예제 #26
0
 def test_element_from_first_parser_that_returns_an_element_SHOULD_be_returned(
         self):
     # ARRANGE #
     source_lines = ['first line', 'second line']
     expected_returned_element = ParsedNonInstructionElement(
         single_line_sequence(1, 'expected'), ElementType.EMPTY)
     unexpected_returned_element = ParsedNonInstructionElement(
         single_line_sequence(1, 'unexpected'), ElementType.EMPTY)
     cases = [
         NameAndValue('single successful parser', [
             SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                 expected_returned_element),
         ]),
         NameAndValue('more than one successful parser', [
             SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                 expected_returned_element),
             SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                 unexpected_returned_element),
         ]),
         NameAndValue('first parser is unsuccessful - returns None', [
             SectionElementParserThatReturnsNone(),
             SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                 expected_returned_element),
         ]),
         NameAndValue(
             'first parser is unsuccessful - throws unrecognized element', [
                 SectionElementParserThatRaisesUnrecognizedSectionElementSourceError(
                 ),
                 SectionElementParserThatReturnsConstantAndConsumesCurrentLine(
                     expected_returned_element),
             ]),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = source_of_lines(source_lines)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             self.assertIs(expected_returned_element, actual,
                           'return value from parser')
             expected_source = asrt_source.is_at_beginning_of_line(2)
             expected_source.apply_with_message(self, source, 'source')
    def test_valid_syntax_of_inclusion_directive(self):
        # ARRANGE #
        inclusion_directive_name = 'incl-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)

        file_name = 'the-file-name'
        cases = [
            NameAndValue('simple inclusion',
                         inclusion_directive_name + ' ' + file_name,
                         ),
            NameAndValue('some superfluous space around path argument',
                         inclusion_directive_name + '   ' + file_name + '     ',
                         ),
        ]
        for case in cases:
            with self.subTest(case.name):
                source = source_of_lines([
                    case.value,
                    'second line',
                ])
                # ACT #
                actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                expected_directive = is_file_inclusion_directive(
                    matches_file_inclusion_directive(
                        files_to_include=asrt.matches_sequence([
                            asrt.equals(pathlib.Path(file_name)),
                        ]),
                        source=matches_line_sequence(
                            first_line_number=asrt.equals(1),
                            lines=asrt.matches_sequence([
                                asrt.equals(case.value)
                            ]))
                    ))
                expected_directive.apply_with_message(self,
                                                      actual,
                                                      'parsed directive')

                expected_source = is_at_beginning_of_line(2)
                expected_source.apply_with_message(self, source, 'source')
예제 #28
0
    def test_valid_syntax_of_inclusion_directive(self):
        # ARRANGE #
        inclusion_directive_name = 'incl-directive'
        parser = sut.FileInclusionDirectiveParser(inclusion_directive_name)

        file_name = 'the-file-name'
        cases = [
            NameAndValue(
                'simple inclusion',
                inclusion_directive_name + ' ' + file_name,
            ),
            NameAndValue(
                'some superfluous space around path argument',
                inclusion_directive_name + '   ' + file_name + '     ',
            ),
        ]
        for case in cases:
            with self.subTest(case.name):
                source = source_of_lines([
                    case.value,
                    'second line',
                ])
                # ACT #
                actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
                # ASSERT #
                expected_directive = is_file_inclusion_directive(
                    matches_file_inclusion_directive(
                        files_to_include=asrt.matches_sequence([
                            asrt.equals(pathlib.Path(file_name)),
                        ]),
                        source=matches_line_sequence(
                            first_line_number=asrt.equals(1),
                            lines=asrt.matches_sequence(
                                [asrt.equals(case.value)]))))
                expected_directive.apply_with_message(self, actual,
                                                      'parsed directive')

                expected_source = is_at_beginning_of_line(2)
                expected_source.apply_with_message(self, source, 'source')
예제 #29
0
 def test_negative(self):
     cases = [
         SourceCase('Dir on 1st line, Negation and DIR-CONTENTS-MATCHER on 2nd line',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        instruction_arguments.NEGATION_ARGUMENT_STR + ' ' + EMPTINESS_CHECK_ARGUMENT,
                    ]),
                    source_assertion=asrt_source.source_is_at_end
                    ),
         SourceCase('Dir on 1st line, Negation and DIR-CONTENTS-MATCHER on 2nd line, followed by non-instr lines',
                    source=remaining_source_lines([
                        DIR_TO_CHECK,
                        instruction_arguments.NEGATION_ARGUMENT_STR,
                        EMPTINESS_CHECK_ARGUMENT,
                        '',
                        'following line',
                    ]),
                    source_assertion=asrt_source.is_at_beginning_of_line(4)
                    ),
     ]
     for case in cases:
         self._check_sub_test(case,
                              PassOrFailOrHardErrorEnum.FAIL)
예제 #30
0
파일: stdin.py 프로젝트: emilkarlen/exactly
 def test_doc_with_symbol_references(self):
     content_line_of_here_doc_template = 'content line of here doc with {symbol}'
     here_doc_contents_template = hd.contents_str_from_lines([content_line_of_here_doc_template])
     symbol_name = 'symbol_name'
     symbol = NameAndValue('symbol_name', 'the symbol value')
     expected_symbol_references = [
         SymbolReference(symbol.name,
                         is_any_data_type())
     ]
     cases = [
         ('string value container',
          data_symbol_utils.string_constant_container('string symbol value')),
         ('file ref value container',
          data_symbol_utils.file_ref_constant_container(
              file_refs.rel_act(file_refs.constant_path_part('file-name.txt')))),
     ]
     for case in cases:
         with self.subTest(case[0]):
             self._run(assignment_of(' <<MARKER  ',
                                     [content_line_of_here_doc_template.format(
                                         symbol=symbol_reference_syntax_for_name(symbol_name)),
                                         'MARKER',
                                         'following line']),
                       Arrangement(
                           symbols=SymbolTable({
                               symbol_name: case[1]
                           })
                       ),
                       Expectation(
                           settings_builder=AssertStdinIsSetToContents(
                               parse_string.string_resolver_from_string(
                                   here_doc_contents_template.format(
                                       symbol=symbol_reference_syntax_for_name(symbol_name)))),
                           symbol_usages=equals_symbol_references(expected_symbol_references),
                           source=is_at_beginning_of_line(4)),
                       )
예제 #31
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)
예제 #32
0
 def test_fail_due_to_unexpected_source_after_parse(self):
     with self.assertRaises(utils.TestError):
         self._check(
             utils.ParserThatGives(_SUCCESSFUL_INSTRUCTION),
             utils.single_line_source(), sut.ArrangementPostAct(),
             Expectation(source=asrt_source.is_at_beginning_of_line(10), ))
예제 #33
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)