Exemplo n.º 1
0
 def test_valid_token(self):
     test_cases = [
         ('token', assert_plain('token'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('    token_preceded_by_space',
          assert_plain('token_preceded_by_space'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('token_followed_by_space ',
          assert_plain('token_followed_by_space'),
          source_is_not_at_end(
              remaining_part_of_current_line=asrt.equals(' '),
              current_line_number=asrt.equals(1))),
         ('token_followed_by_other_token other_token',
          assert_plain('token_followed_by_other_token'),
          source_is_not_at_end(
              remaining_part_of_current_line=asrt.equals(' other_token'),
              current_line_number=asrt.equals(1))),
         ('<<->> other_token', assert_plain('<<->>'),
          source_is_not_at_end(
              remaining_part_of_current_line=asrt.equals(' other_token'),
              current_line_number=asrt.equals(1))),
         ('\'single quoted\'',
          assert_quoted('single quoted', '\'single quoted\''),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('\"double quoted\"',
          assert_quoted('double quoted', '\"double quoted\"'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         (' \'quoted preceded by space\'',
          assert_quoted('quoted preceded by space',
                        '\'quoted preceded by space\''),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         (' \'quoted followed by space\' ',
          assert_quoted('quoted followed by space',
                        '\'quoted followed by space\''),
          source_is_not_at_end(
              remaining_part_of_current_line=asrt.equals(' '),
              current_line_number=asrt.equals(1))),
         (' \'quoted token followed by other token\' \'other_token\'',
          assert_quoted('quoted token followed by other token',
                        '\'quoted token followed by other token\''),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(
              ' \'other_token\''),
                               current_line_number=asrt.equals(1))),
     ]
     for first_line, token_assertion, source_assertion in test_cases:
         with self.subTest(msg=repr(first_line)):
             source = remaining_source(first_line)
             actual = sut.parse_token_on_current_line(source)
             token_assertion.apply_with_message(self, actual, 'token')
             source_assertion.apply_with_message(self, source, 'source')
Exemplo n.º 2
0
 def test_valid_token(self):
     test_cases = [
         ('token',
          assert_plain('token'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('    token_preceded_by_space',
          assert_plain('token_preceded_by_space'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('token_followed_by_space ',
          assert_plain('token_followed_by_space'),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' '),
                               current_line_number=asrt.equals(1))),
         ('token_followed_by_other_token other_token',
          assert_plain('token_followed_by_other_token'),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' other_token'),
                               current_line_number=asrt.equals(1))),
         ('<<->> other_token',
          assert_plain('<<->>'),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' other_token'),
                               current_line_number=asrt.equals(1))),
         ('\'single quoted\'',
          assert_quoted('single quoted', '\'single quoted\''),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('\"double quoted\"',
          assert_quoted('double quoted', '\"double quoted\"'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         (' \'quoted preceded by space\'',
          assert_quoted('quoted preceded by space', '\'quoted preceded by space\''),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         (' \'quoted followed by space\' ',
          assert_quoted('quoted followed by space', '\'quoted followed by space\''),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' '),
                               current_line_number=asrt.equals(1))),
         (' \'quoted token followed by other token\' \'other_token\'',
          assert_quoted('quoted token followed by other token',
                        '\'quoted token followed by other token\''),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' \'other_token\''),
                               current_line_number=asrt.equals(1))),
     ]
     for first_line, token_assertion, source_assertion in test_cases:
         with self.subTest(msg=repr(first_line)):
             source = remaining_source(first_line)
             actual = sut.parse_token_on_current_line(source)
             token_assertion.apply_with_message(self, actual, 'token')
             source_assertion.apply_with_message(self, source, 'source')
Exemplo n.º 3
0
    def test_successful_parse(self):
        symbol_name = 'the_symbol_name'
        space_after = '           '
        token_after = str(surrounded_by_hard_quotes('not an expression'))
        for grammar_description, grammar in self.grammars:
            cases = [
                SourceCase(
                    'first line is only simple expr',
                    source=
                    remaining_source('{symbol_name}'.format(
                        symbol_name=symbol_name,
                    )),
                    source_assertion=
                    asrt_source.is_at_end_of_line(1)
                ),
                SourceCase(
                    'first line is simple expr with space around',
                    source=
                    remaining_source('  {symbol_name}{space_after}'.format(
                        symbol_name=symbol_name,
                        space_after=space_after)),
                    source_assertion=
                    asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                     remaining_part_of_current_line=asrt.equals(space_after[1:]))
                ),
                SourceCase(
                    'expression is followed by non-expression',
                    source=
                    remaining_source('{symbol_name} {token_after}'.format(
                        symbol_name=symbol_name,
                        token_after=token_after)),
                    source_assertion=
                    asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                     remaining_part_of_current_line=asrt.equals(token_after))
                ),
            ]

            for case in cases:
                with self.subTest(grammar=grammar_description,
                                  name=case.name):
                    self._check(
                        Arrangement(
                            grammar=grammar,
                            source=case.source),
                        Expectation(
                            expression=ast.RefExpr(symbol_name),
                            source=case.source_assertion,
                        )
                    )
Exemplo n.º 4
0
def has_remaining_part_of_first_line__re(
        remaining_part_reg_ex: str) -> Assertion[ParseSource]:
    return asrt_source.source_is_not_at_end(
        current_line_number=asrt.equals(1),
        remaining_part_of_current_line=asrt_str.matches_reg_ex(
            remaining_part_reg_ex),
    )
Exemplo n.º 5
0
 def test(self):
     cases = [
         Case('empty line with no following lines',
              source=
              remaining_source(''),
              expectation=
              Expectation(elements=[],
                          source=asrt_source.is_at_end_of_line(1)),
              ),
         Case('only white space on current line, with no following lines',
              source=
              remaining_source('   '),
              expectation=
              Expectation(elements=[],
                          source=asrt_source.source_is_not_at_end(
                              current_line_number=asrt.equals(1),
                              remaining_part_of_current_line=asrt.equals('   '))),
              ),
         Case('empty line, with following lines',
              source=
              remaining_source('', ['contents of following line']),
              expectation=
              Expectation(elements=[],
                          source=asrt_source.is_at_end_of_line(1),
                          )
              ),
     ]
     # ACT & ASSERT #
     _test_cases(self, cases)
Exemplo n.º 6
0
 def test_all_fail_None_SHOULD_be_returned_WHEN_all_parsers_return_None(self):
     # ARRANGE #
     source_text = 'first line'
     cases = [
         NameAndValue('no parsers',
                      [])
         ,
         NameAndValue('one parser',
                      [SectionElementParserThatReturnsNone()]
                      ),
         NameAndValue('more than one parser',
                      [SectionElementParserThatReturnsNone(),
                       SectionElementParserThatReturnsNone()]
                      ),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = ParseSource(source_text)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             self.assertIsNone(actual, 'return value from parser')
             expected_source = asrt_source.source_is_not_at_end(remaining_source=asrt.equals(source_text))
             expected_source.apply_with_message(self, source, 'source')
Exemplo n.º 7
0
 def assertion(self) -> Assertion[ParseSource]:
     return (asrt_source.is_at_end_of_line(self.current_line_number)
             if self.remaining_part_of_current_line is None else
             asrt_source.source_is_not_at_end(
                 current_line_number=asrt.equals(self.current_line_number),
                 remaining_part_of_current_line=asrt.equals(
                     self.remaining_part_of_current_line)))
Exemplo n.º 8
0
 def test_paren_end(self):
     _test_case(
         self, ARRANGEMENT__NEUTRAL,
         Expectation(
             elements=[],
             validators=asrt.is_empty_sequence,
             references=asrt.is_empty_sequence,
         ), remaining_source(reserved_words.PAREN_END),
         asrt_source.source_is_not_at_end(
             current_line_number=asrt.equals(1),
             remaining_source=asrt.equals(reserved_words.PAREN_END)))
Exemplo n.º 9
0
 def test_string_token_followed_by_paren_end(self):
     plain_string = 'plain'
     source = ' '.join((plain_string, reserved_words.PAREN_END))
     _test_case(
         self, ARRANGEMENT__NEUTRAL,
         Expectation(
             elements=[list_sdvs.str_element(plain_string)],
             validators=asrt.is_empty_sequence,
             references=asrt.is_empty_sequence,
         ), remaining_source(source),
         asrt_source.source_is_not_at_end(
             current_line_number=asrt.equals(1),
             remaining_source=asrt.equals(reserved_words.PAREN_END)))
Exemplo n.º 10
0
 def test(self):
     cases = [
         Case(
             'empty line with no following lines',
             source=remaining_source(''),
             expectation=Expectation(
                 elements=[], source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'only white space on current line, with no following lines',
             source=remaining_source('   '),
             expectation=Expectation(
                 elements=[], source=asrt_source.is_at_end_of_line(1)),
         ),
         Case('empty line, with following lines',
              source=remaining_source('', ['contents of following line']),
              expectation=Expectation(
                  elements=[],
                  source=asrt_source.is_at_end_of_line(1),
              )),
         Case('line with r-paren',
              source=remaining_source(reserved_words.PAREN_END,
                                      ['contents of following line']),
              expectation=Expectation(
                  elements=[],
                  source=asrt_source.source_is_not_at_end(
                      current_line_number=asrt.equals(1),
                      remaining_part_of_current_line=asrt.equals(
                          reserved_words.PAREN_END)),
              )),
         Case('line with continuation-token, followed by empty line',
              source=remaining_source(defs.CONTINUATION_TOKEN, ['']),
              expectation=Expectation(
                  elements=[],
                  source=asrt_source.is_at_end_of_line(2),
              )),
         Case(
             'line with continuation-token (followed by space), followed by empty line',
             source=remaining_source(defs.CONTINUATION_TOKEN + '  ', ['']),
             expectation=Expectation(
                 elements=[],
                 source=asrt_source.is_at_end_of_line(2),
             )),
     ]
     # ACT & ASSERT #
     _test_cases(self, cases)
Exemplo n.º 11
0
 def test_valid_token(self):
     test_cases = [
         ('token', assert_plain('token'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('<<->> other_token', assert_plain('<<->>'),
          source_is_not_at_end(
              remaining_part_of_current_line=asrt.equals(' other_token'),
              current_line_number=asrt.equals(1))),
     ]
     for first_line, token_assertion, source_assertion in test_cases:
         with self.subTest(msg=repr(first_line)):
             source = remaining_source(first_line)
             actual = sut.parse_token_or_none_on_current_line(source)
             token_assertion.apply_with_message(self, actual, 'token')
             source_assertion.apply_with_message(self, source, 'source')
Exemplo n.º 12
0
 def test_valid_token(self):
     test_cases = [
         ('token',
          assert_plain('token'),
          assert_source(is_at_eol=asrt.is_true,
                        has_current_line=asrt.is_true,
                        current_line_number=asrt.equals(1))),
         ('<<->> other_token',
          assert_plain('<<->>'),
          source_is_not_at_end(remaining_part_of_current_line=asrt.equals(' other_token'),
                               current_line_number=asrt.equals(1))),
     ]
     for first_line, token_assertion, source_assertion in test_cases:
         with self.subTest(msg=repr(first_line)):
             source = remaining_source(first_line)
             actual = sut.parse_token_or_none_on_current_line(source)
             token_assertion.apply_with_message(self, actual, 'token')
             source_assertion.apply_with_message(self, source, 'source')
Exemplo n.º 13
0
 def test_all_fail_None_SHOULD_be_returned_WHEN_all_parsers_return_None(
         self):
     # ARRANGE #
     source_text = 'first line'
     cases = [
         NameAndValue('no parsers', []),
         NameAndValue('one parser',
                      [SectionElementParserThatReturnsNone()]),
         NameAndValue('more than one parser', [
             SectionElementParserThatReturnsNone(),
             SectionElementParserThatReturnsNone()
         ]),
     ]
     for case in cases:
         with self.subTest(case.name):
             source = ParseSource(source_text)
             parser = sut.ParserFromSequenceOfParsers(case.value)
             # ACT #
             actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             self.assertIsNone(actual, 'return value from parser')
             expected_source = asrt_source.source_is_not_at_end(
                 remaining_source=asrt.equals(source_text))
             expected_source.apply_with_message(self, source, 'source')
Exemplo n.º 14
0
    def test_string_transformer_should_be_parsed_as_simple_expression(self):
        # ARRANGE #
        the_layout = LayoutSpec.of_default()

        src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP)

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

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

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

        file_contents_syntax = string_source_abs_stx.TransformedStringSourceAbsStx(
            string_source_abs_stx.StringSourceOfFileAbsStx(
                src_rel_opt_conf.path_abs_stx_of_name(src_file.name)
            ),
            transformation_w_infix_op
        )
        checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity))
        # ACT & ASSERT #
        checker.check__abs_stx(
            self,
            file_contents_syntax,
            None,
            arrangement_w_tcds(
                tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root(
                    fs.DirContents([src_file])
                ),
                symbols=to_upper_transformer.symbol_table
            ),
            Expectation.of_prim__const(
                parse=ParseExpectation(
                    source=asrt_source.source_is_not_at_end(
                        remaining_source=asrt.equals(expected_remaining_source)
                    ),
                    symbol_references=to_upper_transformer.references_assertion,
                ),
                primitive=asrt_string_source.pre_post_freeze__matches_str__const(
                    expected_contents,
                    may_depend_on_external_resources=True,
                )
            ),
            the_layout
        )
def has_remaining_part_of_first_line(remaining_part: str) -> ValueAssertion[ParseSource]:
    return asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                            remaining_part_of_current_line=asrt.equals(
                                                remaining_part))
Exemplo n.º 16
0
 def test(self):
     single_token_value = 'single_token_value'
     single_token_value_1 = 'single_token_value_1'
     single_token_value_2 = 'single_token_value_2'
     symbol_name = 'a_symbol_name'
     cases = [
         Case(
             'multiple string constants on line that is the last line',
             source=remaining_source(single_token_value_1 + ' ' +
                                     single_token_value_2),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element(single_token_value_1),
                     list_sdvs.str_element(single_token_value_2)
                 ],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'multiple string constants on line that is followed by an empty line',
             source=remaining_source(
                 single_token_value_1 + ' ' + single_token_value_2, ['']),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element(single_token_value_1),
                     list_sdvs.str_element(single_token_value_2)
                 ],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'multiple string constants on line that is followed by a non-empty line',
             source=remaining_source(
                 single_token_value_1 + ' ' + single_token_value_2, ['  ']),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element(single_token_value_1),
                     list_sdvs.str_element(single_token_value_2)
                 ],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'symbol-reference and string constant on first line',
             source=remaining_source('{sym_ref} {string_constant}'.format(
                 sym_ref=symbol_reference_syntax_for_name(symbol_name),
                 string_constant=single_token_value)),
             expectation=Expectation(
                 elements=[
                     list_sdvs.symbol_element(
                         reference_to__on_direct_and_indirect(symbol_name)),
                     list_sdvs.str_element(single_token_value)
                 ],
                 references=asrt.matches_sequence([
                     asrt_sym_ref.matches_reference_2(
                         symbol_name, asrt_data_rest.is__w_str_rendering())
                 ]),
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'complex element (sym-ref and str const) and string constant, '
             'followed by space, '
             'followed by non-empty line',
             source=remaining_source(
                 symbol_reference_syntax_for_name(symbol_name) +
                 single_token_value + ' ' + single_token_value_1, ['   ']),
             expectation=Expectation(
                 elements=[
                     list_sdvs.string_element(
                         string_sdvs.from_fragments(
                             [
                                 string_sdvs.symbol_fragment(
                                     SymbolReference(
                                         symbol_name,
                                         reference_restrictions.
                                         is_any_type_w_str_rendering()), ),
                                 string_sdvs.str_fragment(
                                     single_token_value),
                             ])),
                     list_sdvs.str_element(single_token_value_1),
                 ],
                 references=asrt.matches_sequence([
                     asrt_sym_ref.matches_reference_2(
                         symbol_name, asrt_data_rest.is__w_str_rendering())
                 ]),
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'multiple string constants on line followed by r-paren',
             source=remaining_source(
                 ' '.join((single_token_value_1, single_token_value_2,
                           reserved_words.PAREN_END, 'constant')), ['  ']),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element(single_token_value_1),
                     list_sdvs.str_element(single_token_value_2)
                 ],
                 source=asrt_source.source_is_not_at_end(
                     current_line_number=asrt.equals(1),
                     remaining_part_of_current_line=asrt.equals(' '.join(
                         (reserved_words.PAREN_END, 'constant'))))),
         ),
         Case(
             '1st string on first line, followed by continuation-token, followed by line with 2nd string',
             source=remaining_source(
                 single_token_value_1 + ' ' + defs.CONTINUATION_TOKEN,
                 [single_token_value_2]),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element(single_token_value_1),
                     list_sdvs.str_element(single_token_value_2)
                 ],
                 source=asrt_source.is_at_end_of_line(2)),
         ),
         Case(
             'multiple elements on two lines, separated by continuation token',
             source=remaining_source('a b ' + defs.CONTINUATION_TOKEN,
                                     ['  c d  ']),
             expectation=Expectation(
                 elements=[
                     list_sdvs.str_element('a'),
                     list_sdvs.str_element('b'),
                     list_sdvs.str_element('c'),
                     list_sdvs.str_element('d'),
                 ],
                 source=asrt_source.is_at_end_of_line(2)),
         ),
     ]
     # ACT & ASSERT #
     _test_cases(self, cases)
Exemplo n.º 17
0
    def test_string_transformer_should_be_parsed_as_simple_expression(self):
        the_layout = LayoutSpec.of_default()

        output_from_program = 'untransformed output from the program'

        sym_ref_program_syntax = ProgramOfSymbolReferenceAbsStx(
            'PROGRAM_THAT_EXECUTES_PY_FILE')

        str_trans__unused = StringTransformerSymbolReferenceAbsStx(
            'UNUSED_TRANSFORMER')

        program_w_complex_str_trans_wo_parentheses = program_abs_stx.FullProgramAbsStx(
            ProgramOfSymbolReferenceAbsStx(sym_ref_program_syntax.symbol_name),
            transformation=str_trans_abs_stx.
            StringTransformerCompositionAbsStx(
                [
                    TO_UPPER_TRANSFORMER_SYMBOL.abstract_syntax,
                    str_trans__unused,
                ],
                within_parens=False,
                allow_elements_on_separate_lines=False,
            ))
        expected_remaining_tokens = TokenSequence.concat([
            TokenSequence.singleton(
                str_trans_abs_stx.names.SEQUENCE_OPERATOR_NAME),
            str_trans__unused.tokenization(),
        ])
        expected_remaining_source = expected_remaining_tokens.layout(
            the_layout)

        checker = integration_check.checker__w_arbitrary_file_relativities()

        py_program_file = File(
            'program.py',
            py_programs.py_pgm_with_stdout_stderr_exit_code(
                exit_code=0,
                stdout_output=output_from_program,
                stderr_output=output_from_program,
            ),
        )
        py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE)
        py_file_conf = py_file_rel_conf.named_file_conf(py_program_file.name)

        program_symbol__that_executes_py_file = ProgramSymbolContext.of_sdv(
            sym_ref_program_syntax.symbol_name,
            program_sdvs.interpret_py_source_file_that_must_exist(
                py_file_conf.path_sdv))
        symbols = [
            program_symbol__that_executes_py_file,
            TO_UPPER_TRANSFORMER_SYMBOL,
        ]

        for output_file in ProcOutputFile:
            for ignore_exit_code in [False, True]:
                syntax = string_source_abs_stx.StringSourceOfProgramAbsStx(
                    output_file,
                    program_w_complex_str_trans_wo_parentheses,
                    ignore_exit_code=ignore_exit_code,
                )
                with self.subTest(output_file=output_file,
                                  ignore_exit_code=ignore_exit_code):
                    checker.check__abs_stx__wo_input(
                        self,
                        syntax,
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                symbols),
                            tcds_contents=py_file_rel_conf.
                            populator_for_relativity_option_root(
                                DirContents([py_program_file]))),
                        Expectation(
                            ParseExpectation(
                                source=asrt_source.source_is_not_at_end(
                                    remaining_source=asrt.equals(
                                        expected_remaining_source)),
                                symbol_references=SymbolContext.
                                references_assertion_of_contexts(symbols),
                            )),
                        the_layout,
                    )
Exemplo n.º 18
0
 def test_success_of_expression_within_parentheses_spanning_several_lines(self):
     s = ast.PrimitiveSansArg()
     cases = [
         NArrEx(
             'primitive expr and ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         ['{s} )'.format(s=ast.PRIMITIVE_SANS_ARG)]),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         NArrEx(
             'primitive expr and ) on following line, followed by non-expr',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         ['{s} ) non-expr'.format(s=ast.PRIMITIVE_SANS_ARG)]),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.source_is_not_at_end(current_line_number=asrt.equals(2),
                                                         remaining_part_of_current_line=asrt.equals('non-expr')),
             ),
         ),
         NArrEx(
             'primitive expr with ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s}'.format(s=ast.PRIMITIVE_SANS_ARG),
                                         [' )']),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         NArrEx(
             'primitive expr with ) on following line, and non-expr on line after that',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s}'.format(s=ast.PRIMITIVE_SANS_ARG),
                                         [' )',
                                          'non-expr']),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         NArrEx(
             'binary op with only ( on first line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         [' {s} {op} {s} )'.format(s=ast.PRIMITIVE_SANS_ARG,
                                                                   op=ast.INFIX_OP_A)
                                          ]),
             ),
             Expectation(
                 expression=InfixOpA([s, s]),
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         NArrEx(
             'binary op with ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s} {op} {s}'.format(s=ast.PRIMITIVE_SANS_ARG,
                                                                 op=ast.INFIX_OP_A),
                                         [' ) ']),
             ),
             Expectation(
                 expression=InfixOpA([s, s]),
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
     ]
     for case in cases:
         with self.subTest(name=case.name):
             check(self,
                   PARSER_MAKER_OF_FULL_EXPR_PARSER,
                   case.arrangement,
                   case.expectation
                   )
Exemplo n.º 19
0
 def test(self):
     single_token_value = 'single_token_value'
     string_symbol = NameAndValue('string_symbol_name',
                                  'string symbol value')
     cases = [
         Case(
             'single string constant, at end of line, on the last line',
             source=remaining_source(single_token_value),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=assert_source(is_at_eof=asrt.is_true)),
         ),
         Case(
             'single symbol reference, at end of line, on the last line',
             source=remaining_source(
                 symbol_reference_syntax_for_name(string_symbol.name)),
             expectation=Expectation(
                 elements=[
                     list_sdvs.symbol_element(
                         reference_to__on_direct_and_indirect(
                             string_symbol.name))
                 ],
                 source=assert_source(is_at_eof=asrt.is_true),
                 references=asrt.matches_sequence([
                     asrt_sym_ref.matches_reference_2(
                         string_symbol.name,
                         asrt_data_rest.is__w_str_rendering())
                 ])),
         ),
         Case(
             'complex element (str const and sym-refs), at end of line, on the last line',
             source=remaining_source(
                 single_token_value +
                 symbol_reference_syntax_for_name(string_symbol.name)),
             expectation=Expectation(
                 elements=[
                     list_sdvs.string_element(
                         string_sdvs.from_fragments(
                             [
                                 string_sdvs.str_fragment(
                                     single_token_value),
                                 string_sdvs.symbol_fragment(
                                     SymbolReference(
                                         string_symbol.name,
                                         reference_restrictions.
                                         is_any_type_w_str_rendering(),
                                     )),
                             ]))
                 ],
                 references=asrt.matches_sequence([
                     asrt_sym_ref.matches_reference_2(
                         string_symbol.name,
                         asrt_data_rest.is__w_str_rendering())
                 ]),
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, followed by more than one space, on the last line',
             source=remaining_source(single_token_value + '  ', []),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, followed by single space, on the last line',
             source=remaining_source(single_token_value + ' ', []),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, followed by space, followed by empty line',
             source=remaining_source(single_token_value + '  ', ['']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, at end of line, followed by line with only space',
             source=remaining_source(single_token_value, ['   ']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, followed by space, followed by line with only space',
             source=remaining_source(single_token_value + '  ', ['   ']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'single element, at end of line, followed by line with invalid quoting',
             source=remaining_source(single_token_value, ['"   ']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(1)),
         ),
         Case(
             'continuation token, followed by line with single element',
             source=remaining_source(defs.CONTINUATION_TOKEN,
                                     [single_token_value]),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(2)),
         ),
         Case(
             'single element, followed by continuation token, followed by empty line',
             source=remaining_source(
                 single_token_value + ' ' + defs.CONTINUATION_TOKEN, ['']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.is_at_end_of_line(2)),
         ),
         Case(
             'single element, followed by r-paren and more',
             source=remaining_source(
                 ' '.join((single_token_value, reserved_words.PAREN_END,
                           'const_str')), ['"   ']),
             expectation=Expectation(
                 elements=[list_sdvs.str_element(single_token_value)],
                 source=asrt_source.source_is_not_at_end(
                     current_line_number=asrt.equals(1),
                     remaining_part_of_current_line=asrt.equals(' '.join(
                         (reserved_words.PAREN_END, 'const_str'))))),
         ),
     ]
     # ACT & ASSERT #
     _test_cases(self, cases)
Exemplo n.º 20
0
 def test_success_of_expression_within_parentheses_spanning_several_lines(self):
     s = ast.SimpleSansArg()
     cases = [
         (
             'simple expr and ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         ['{s} )'.format(s=ast.SIMPLE_SANS_ARG)]),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         (
             'simple expr and ) on following line, followed by non-expr',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         ['{s} ) non-expr'.format(s=ast.SIMPLE_SANS_ARG)]),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.source_is_not_at_end(current_line_number=asrt.equals(2),
                                                         remaining_part_of_current_line=asrt.equals('non-expr')),
             ),
         ),
         (
             'simple expr with ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s}'.format(s=ast.SIMPLE_SANS_ARG),
                                         [' )']),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         (
             'simple expr with ) on following line, and non-expr on line after that',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s}'.format(s=ast.SIMPLE_SANS_ARG),
                                         [' )',
                                          'non-expr']),
             ),
             Expectation(
                 expression=s,
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         (
             'binary op with only ( on first line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('(',
                                         [' {s} {op} {s} )'.format(s=ast.SIMPLE_SANS_ARG,
                                                                   op=ast.COMPLEX_A)
                                          ]),
             ),
             Expectation(
                 expression=ComplexA([s, s]),
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
         (
             'binary op with ) on following line',
             Arrangement(
                 grammar=ast.GRAMMAR_WITH_ALL_COMPONENTS,
                 source=remaining_source('( {s} {op} {s}'.format(s=ast.SIMPLE_SANS_ARG,
                                                                 op=ast.COMPLEX_A),
                                         [' ) ']),
             ),
             Expectation(
                 expression=ComplexA([s, s]),
                 source=asrt_source.is_at_end_of_line(2),
             ),
         ),
     ]
     for case_name, arrangement, expectation in cases:
         with self.subTest(name=case_name):
             _check(self,
                    arrangement,
                    expectation
                    )
Exemplo n.º 21
0
    def test_success_of_single_operator(self):
        space_after = '           '
        quoted_string = str(surrounded_by_hard_quotes('quoted string'))

        valid_simple_expressions = [
            (
                '{simple_expression}'.format(simple_expression=ast.SIMPLE_SANS_ARG),
                ast.SimpleSansArg()
            ),
            (
                '{simple_expression_name} {argument}'.format(
                    simple_expression_name=ast.SIMPLE_WITH_ARG,
                    argument='simple-expr-argument'),
                ast.SimpleWithArg('simple-expr-argument')
            ),

        ]
        operators = [
            (
                ast.COMPLEX_A,
                ast.ComplexA,
            ),
            (
                ast.COMPLEX_B_THAT_IS_NOT_A_VALID_SYMBOL_NAME,
                ast.ComplexB,
            ),
        ]

        for valid_simple_expr_source, expected_simple_expr in valid_simple_expressions:
            for operator_source, operator_constructor in operators:
                expected_expression = operator_constructor([expected_simple_expr,
                                                            expected_simple_expr])
                cases = [
                    SourceCase(
                        'first line is just complex expr',
                        source=
                        remaining_source('{simple_expr} {operator} {simple_expr}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                        )),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1)
                    ),
                    SourceCase(
                        'first line is complex expr, followed by space',
                        source=
                        remaining_source('{simple_expr} {operator} {simple_expr}{space_after}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                            space_after=space_after,
                        )),
                        source_assertion=
                        asrt_source.source_is_not_at_end(
                            current_line_number=asrt.equals(1),
                            remaining_part_of_current_line=asrt.equals(space_after[1:]))
                    ),
                    SourceCase(
                        'complex expr followed by non-operator',
                        source=
                        remaining_source('{simple_expr} {operator} {simple_expr} {quoted_string}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                            quoted_string=quoted_string,
                        )),
                        source_assertion=
                        asrt_source.source_is_not_at_end(
                            current_line_number=asrt.equals(1),
                            remaining_part_of_current_line=asrt.equals(quoted_string))

                    ),
                    SourceCase(
                        'complex expr followed by simple expression',
                        source=
                        remaining_source('{simple_expr} {operator} {simple_expr} {simple_expr}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                        )),
                        source_assertion=
                        asrt_source.source_is_not_at_end(
                            current_line_number=asrt.equals(1),
                            remaining_part_of_current_line=asrt.equals(valid_simple_expr_source))

                    ),
                    SourceCase(
                        'complex expr followed by quoted operator',
                        source=
                        remaining_source('{simple_expr} {operator} {simple_expr} {quoted_operator}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                            quoted_operator=surrounded_by_soft_quotes(operator_source),
                        )),
                        source_assertion=
                        asrt_source.source_is_not_at_end(
                            current_line_number=asrt.equals(1),
                            remaining_part_of_current_line=asrt.equals(str(surrounded_by_soft_quotes(operator_source))))

                    ),
                    SourceCase(
                        'first line is just complex expr: inside ()',
                        source=
                        remaining_source('( {simple_expr} {operator} {simple_expr} )'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                        )),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1)
                    ),
                    SourceCase(
                        'first simple expr inside ()',
                        source=
                        remaining_source('( {simple_expr} ) {operator} {simple_expr}'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                        )),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1)
                    ),
                    SourceCase(
                        'second simple expr inside ()',
                        source=
                        remaining_source('{simple_expr} {operator} ( {simple_expr} )'.format(
                            simple_expr=valid_simple_expr_source,
                            operator=operator_source,
                        )),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1)
                    ),
                    SourceCase(
                        'second expr on following line',
                        source=
                        remaining_source(valid_simple_expr_source + ' ' + operator_source,
                                         [valid_simple_expr_source]),
                        source_assertion=
                        asrt_source.is_at_end_of_line(2)
                    ),
                ]
                for case in cases:
                    with self.subTest(name=case.name,
                                      operator_source=operator_source,
                                      valid_simple_expr_source=valid_simple_expr_source):
                        _check(self,
                               Arrangement(
                                   grammar=
                                   ast.GRAMMAR_WITH_ALL_COMPONENTS,
                                   source=
                                   case.source),
                               Expectation(
                                   expression=
                                   expected_expression,
                                   source=
                                   case.source_assertion,
                               )
                               )
Exemplo n.º 22
0
    def test_no_new_line_variants(self):
        # ARRANGE #

        identity_transformer = NameAndValue('TRANSFORMER_SYMBOL',
                                            StringTransformerResolverConstantTestImpl(IdentityStringTransformer()))

        symbols = SymbolTable({
            identity_transformer.name: container(identity_transformer.value),
        })

        src_file = fs.File('src-file.txt', 'source file contents')
        src_file_rel_opt_conf = conf_rel_home(RelHomeOptionType.REL_HOME_CASE)

        expected_dst_file = fs.File('dst-file.txt', src_file.contents)
        dst_file_rel_opt_conf = conf_rel_non_home(RelNonHomeOptionType.REL_ACT)

        file_arguments_constructor = TransformableContentsConstructor(
            file_with_rel_opt_conf(src_file.name,
                                   src_file_rel_opt_conf,
                                   with_new_line_after_source_type_option=True,
                                   ),
        )

        text_on_line_after_instruction = ' text on line after instruction'

        for with_file_maker_on_separate_line in [False, True]:
            file_contents_cases = [
                NameAndValue(
                    'without transformation',
                    file_arguments_constructor.without_transformation(
                        with_file_maker_on_separate_line=with_file_maker_on_separate_line)
                ),
                NameAndValue(
                    'with transformation',
                    file_arguments_constructor.with_transformation(
                        identity_transformer.name,
                        with_file_maker_on_separate_line=with_file_maker_on_separate_line)
                ),
            ]
            for file_contents_case in file_contents_cases:
                optional_arguments_elements = file_contents_case.value
                assert isinstance(optional_arguments_elements, ArgumentElements)  # Type info for IDE
                optional_arguments = optional_arguments_elements.as_arguments

                with self.subTest(file_contents_variant=file_contents_case.name,
                                  first_line_argments=optional_arguments.first_line,
                                  with_file_maker_on_separate_line=with_file_maker_on_separate_line):
                    source = remaining_source(
                        '{rel_opt} {dst_file_name} {optional_arguments}'.format(
                            rel_opt=dst_file_rel_opt_conf.option_argument,
                            dst_file_name=expected_dst_file.name,
                            optional_arguments=optional_arguments.first_line,
                        ),
                        optional_arguments.following_lines +
                        [text_on_line_after_instruction]
                    )

                    # ACT & ASSERT #

                    self._check(source,
                                ArrangementWithSds(
                                    pre_contents_population_action=SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR,
                                    home_or_sds_contents=src_file_rel_opt_conf.populator_for_relativity_option_root(
                                        DirContents([src_file])),
                                    symbols=symbols,
                                ),
                                Expectation(
                                    main_result=IS_SUCCESS,
                                    symbol_usages=asrt.anything_goes(),
                                    main_side_effects_on_sds=dst_file_rel_opt_conf.assert_root_dir_contains_exactly(
                                        fs.DirContents([expected_dst_file])),
                                    source=source_is_not_at_end(
                                        remaining_part_of_current_line=asrt.equals(text_on_line_after_instruction)
                                    )
                                )
                                )
Exemplo n.º 23
0
    def test_successful_parse_with_simple_expr(self):

        space_after = '           '
        token_after = str(surrounded_by_hard_quotes('not an expression'))

        simple_expr = ast.SimpleSansArg()
        simple_expr_src = ast.SIMPLE_SANS_ARG

        for grammar_description, grammar in self.grammars:
            for prefix_operator, mk_prefix_expr in self.prefix_operators:
                cases = [
                    SourceCase(
                        'first line is only simple expr',
                        source=
                        remaining_source('{op} {simple_expr}'.format(
                            op=prefix_operator,
                            simple_expr=simple_expr_src,
                        )),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1)
                    ),
                    SourceCase(
                        'first line is simple expr with space around',
                        source=
                        remaining_source(' {op}  {simple_expr}{space_after}'.format(
                            op=prefix_operator,
                            simple_expr=simple_expr_src,
                            space_after=space_after)),
                        source_assertion=
                        asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                         remaining_part_of_current_line=asrt.equals(space_after[1:]))
                    ),
                    SourceCase(
                        'expression is followed by non-expression',
                        source=
                        remaining_source('{op} {simple_expr} {token_after}'.format(
                            op=prefix_operator,
                            simple_expr=simple_expr_src,
                            token_after=token_after)),
                        source_assertion=
                        asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                         remaining_part_of_current_line=asrt.equals(token_after))
                    ),
                    SourceCase(
                        '( op simple )',
                        source=
                        remaining_source('( {op} {simple_expr} )'.format(
                            op=prefix_operator,
                            simple_expr=simple_expr_src,
                            token_after=token_after)),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1),
                    ),
                    SourceCase(
                        'op ( simple )',
                        source=
                        remaining_source('{op} ( {simple_expr} )'.format(
                            op=prefix_operator,
                            simple_expr=simple_expr_src,
                            token_after=token_after)),
                        source_assertion=
                        asrt_source.is_at_end_of_line(1),
                    ),
                ]

                for case in cases:
                    with self.subTest(grammar=grammar_description,
                                      prefix_operator=prefix_operator,
                                      name=case.name):
                        self._check(
                            Arrangement(
                                grammar=grammar,
                                source=case.source),
                            Expectation(
                                expression=mk_prefix_expr(simple_expr),
                                source=case.source_assertion,
                            )
                        )
Exemplo n.º 24
0
 def test_successful_parse_of_expr_with_argument(self):
     the_argument = 'the-argument'
     space_after = '           '
     token_after = str(surrounded_by_hard_quotes('not an expression'))
     for grammar_description, grammar in self.grammars:
         cases = [
             SourceCase(
                 'first line is only simple expr',
                 source=
                 remaining_source('{simple_with_arg} {argument}'.format(
                     simple_with_arg=ast.SIMPLE_WITH_ARG,
                     argument=the_argument)),
                 source_assertion=
                 asrt_source.is_at_end_of_line(1)
             ),
             SourceCase(
                 'first line is simple expr with space around',
                 source=
                 remaining_source('  {simple_with_arg}    {argument}{space_after}'.format(
                     simple_with_arg=ast.SIMPLE_WITH_ARG,
                     argument=the_argument,
                     space_after=space_after)),
                 source_assertion=
                 asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                  remaining_part_of_current_line=asrt.equals(space_after[1:]))
             ),
             SourceCase(
                 'expression is followed by non-expression',
                 source=
                 remaining_source('  {simple_with_arg}    {argument} {token_after}'.format(
                     simple_with_arg=ast.SIMPLE_WITH_ARG,
                     argument=the_argument,
                     token_after=token_after)),
                 source_assertion=
                 asrt_source.source_is_not_at_end(current_line_number=asrt.equals(1),
                                                  remaining_part_of_current_line=asrt.equals(token_after))
             ),
             SourceCase(
                 '( simple )',
                 source=
                 remaining_source('( {simple_with_arg} {argument} )'.format(
                     simple_with_arg=ast.SIMPLE_WITH_ARG,
                     argument=the_argument,
                 )),
                 source_assertion=
                 asrt_source.is_at_end_of_line(1),
             ),
             SourceCase(
                 '( ( simple ) )',
                 source=
                 remaining_source('( ( {simple_with_arg} {argument} ) )'.format(
                     simple_with_arg=ast.SIMPLE_WITH_ARG,
                     argument=the_argument,
                 )),
                 source_assertion=
                 asrt_source.is_at_end_of_line(1),
             ),
         ]
         for case in cases:
             with self.subTest(grammar=grammar_description,
                               name=case.name):
                 self._check(
                     Arrangement(
                         grammar=grammar,
                         source=case.source),
                     Expectation(
                         expression=ast.SimpleWithArg(the_argument),
                         source=case.source_assertion,
                     )
                 )