Exemple #1
0
 def cases_for_word(reserved: str) -> List[TC]:
     return [
         TC(
             _src('{hard_quote}{reserved}{hard_quote}',
                  hard_quote=HARD_QUOTE_CHAR,
                  reserved=reserved),
             Expectation(
                 fragments=[constant(reserved)],
                 token_stream=assert_token_stream(is_null=asrt.is_true),
             )),
         TC(
             _src('{hard_quote}{reserved}{hard_quote} plain-word2',
                  hard_quote=HARD_QUOTE_CHAR,
                  reserved=reserved),
             Expectation(
                 fragments=[constant(reserved)],
                 token_stream=assert_token_stream(
                     head_token=assert_token_string_is('plain-word2')),
             )),
         TC(
             _src('{soft_quote}{reserved}{soft_quote} plain-word2',
                  soft_quote=SOFT_QUOTE_CHAR,
                  reserved=reserved),
             Expectation(
                 fragments=[constant(reserved)],
                 token_stream=assert_token_stream(
                     head_token=assert_token_string_is('plain-word2')),
             )),
     ]
Exemple #2
0
    def test_assignment_of_single_symbol_reference(self):
        # ARRANGE #
        referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
        expected_sdv = string_sdv_from_fragments([symbol(referred_symbol.name),
                                                  constant('\n')])
        assigned_symbol = StringSymbolContext.of_sdv(
            'defined_symbol',
            expected_sdv,
        )
        sb = SB.new_with(referred_symbol=referred_symbol,
                         name_of_defined_symbol=assigned_symbol.name)

        source = sb.lines(
            here_doc_lines(first_line_start='{string_type} {name_of_defined_symbol} = ',
                           marker='EOF',
                           contents_lines=[str(referred_symbol)])
        )
        # EXPECTATION #
        expectation = Expectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence([
                assigned_symbol.assert_matches_definition_of_sdv,
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                assigned_symbol.name,
                assigned_symbol.value.assert_matches_container_of_sdv,
            )
        )
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #3
0
 def test_assignment_of_symbols_and_constant_text_within_soft_quotes(self):
     # ARRANGE #
     referred_symbol1 = SymbolWithReferenceSyntax('referred_symbol_1')
     referred_symbol2 = SymbolWithReferenceSyntax('referred_symbol_2')
     assigned_symbol = StringSymbolContext.of_sdv(
         'defined_symbol',
         string_sdv_from_fragments([
             symbol(referred_symbol1.name),
             constant(' between '),
             symbol(referred_symbol2.name),
         ]))
     source = single_line_source('{string_type} {name} = {soft_quote}{sym_ref1} between {sym_ref2}{soft_quote}',
                                 soft_quote=SOFT_QUOTE_CHAR,
                                 name=assigned_symbol.name,
                                 sym_ref1=referred_symbol1,
                                 sym_ref2=referred_symbol2)
     expectation = Expectation.phase_agnostic(
         symbol_usages=asrt.matches_sequence([
             assigned_symbol.assert_matches_definition_of_sdv,
         ]),
         symbols_after_main=assert_symbol_table_is_singleton(
             assigned_symbol.name,
             assigned_symbol.value.assert_matches_container_of_sdv,
         )
     )
     # ACT & ASSERT #
     INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #4
0
    def test_assignment_of_single_symbol_reference(self):
        # ARRANGE #
        referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
        name_of_defined_symbol = 'defined_symbol'
        sb = SB.new_with(referred_symbol=referred_symbol,
                         name_of_defined_symbol=name_of_defined_symbol)

        source = sb.lines(
            here_doc_lines(first_line_start='{string_type} {name_of_defined_symbol} = ',
                           marker='EOF',
                           contents_lines=[str(referred_symbol)])
        )
        # EXPECTATION #
        expected_resolver = string_resolver_from_fragments([symbol(referred_symbol.name),
                                                            constant('\n')])
        container_of_expected_resolver = container(expected_resolver)
        expected_definition = SymbolDefinition(name_of_defined_symbol,
                                               container_of_expected_resolver)
        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(
                name_of_defined_symbol,
                equals_container(container_of_expected_resolver),
            )
        )
        # ACT & ASSERT #
        self._check(source, ArrangementWithSds(), expectation)
Exemple #5
0
 def test_assignment_of_symbols_and_constant_text_within_soft_quotes(self):
     # ARRANGE #
     referred_symbol1 = SymbolWithReferenceSyntax('referred_symbol_1')
     referred_symbol2 = SymbolWithReferenceSyntax('referred_symbol_2')
     name_of_defined_symbol = 'defined_symbol'
     source = single_line_source('{string_type} {name} = {soft_quote}{sym_ref1} between {sym_ref2}{soft_quote}',
                                 soft_quote=SOFT_QUOTE_CHAR,
                                 name=name_of_defined_symbol,
                                 sym_ref1=referred_symbol1,
                                 sym_ref2=referred_symbol2)
     expected_resolver = string_resolver_from_fragments([
         symbol(referred_symbol1.name),
         constant(' between '),
         symbol(referred_symbol2.name),
     ])
     container_of_expected_resolver = container(expected_resolver)
     expected_definition = SymbolDefinition(name_of_defined_symbol,
                                            container_of_expected_resolver)
     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(
             name_of_defined_symbol,
             equals_container(container_of_expected_resolver),
         )
     )
     # ACT & ASSERT #
     self._check(source, ArrangementWithSds(), expectation)
Exemple #6
0
 def runTest(self):
     cases = [
         ('', []),
         ('abc', [constant('abc')]),
         (sut.symbol_reference_syntax_for_name('sym_name'),
          [symbol('sym_name')]),
         (sut.symbol_reference_syntax_for_name('sym_NAME_1'),
          [symbol('sym_NAME_1')]),
         (sut.symbol_reference_syntax_for_name('not a symbol name'), [
             constant(
                 sut.symbol_reference_syntax_for_name('not a symbol name'))
         ]),
         (sut.symbol_reference_syntax_for_name(''),
          [constant(sut.symbol_reference_syntax_for_name(''))]),
         (sut.symbol_reference_syntax_for_name('  '),
          [constant(sut.symbol_reference_syntax_for_name('  '))]),
         (sut.symbol_reference_syntax_for_name('1isAValidSymbolName'),
          [symbol('1isAValidSymbolName')]),
         ('const{sym_ref}'.format(
             sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
          [constant('const'), symbol('sym_name')]),
         ('{sym_ref}const'.format(
             sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
          [symbol('sym_name'), constant('const')]),
         ('{sym_ref1}{sym_ref2}'.format(
             sym_ref1=sut.symbol_reference_syntax_for_name('sym_name1'),
             sym_ref2=sut.symbol_reference_syntax_for_name('sym_name2')),
          [symbol('sym_name1'), symbol('sym_name2')]),
         ('{sym_begin}{sym_ref}'.format(
             sym_begin=sut.SYMBOL_REFERENCE_BEGIN,
             sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
          [constant(sut.SYMBOL_REFERENCE_BEGIN),
           symbol('sym_name')]),
         ('{sym_ref1}const 1{not_a_symbol_name1}{not_a_symbol_name2}const 2{sym_ref2}'
          .format(
              sym_ref1=sut.symbol_reference_syntax_for_name('sym_name1'),
              not_a_symbol_name1=sut.symbol_reference_syntax_for_name(
                  'not a sym1'),
              not_a_symbol_name2=sut.symbol_reference_syntax_for_name(
                  'not a sym2'),
              sym_ref2=sut.symbol_reference_syntax_for_name('sym_name2')),
          [
              symbol('sym_name1'),
              constant('const 1' +
                       sut.symbol_reference_syntax_for_name('not a sym1') +
                       sut.symbol_reference_syntax_for_name('not a sym2') +
                       'const 2'),
              symbol('sym_name2')
          ]),
     ]
     for source, expected in cases:
         with self.subTest(source=source):
             actual = sut.split(source)
             self.assertEqual(expected, actual)
Exemple #7
0
def successful_parse_of_constant() -> List[TC]:
    return [
        TC(
            _src('plain-word1 plain-word2'),
            Expectation(
                fragments=[constant('plain-word1')],
                token_stream=assert_token_stream(
                    head_token=assert_token_string_is('plain-word2')),
            )),
        TC(
            _src('word'),
            Expectation(
                fragments=[constant('word')],
                token_stream=assert_token_stream(is_null=asrt.is_true),
            )),
        TC(
            _src('{soft_quote}double quoted word{soft_quote} plain-word2',
                 soft_quote=SOFT_QUOTE_CHAR),
            Expectation(
                fragments=[constant('double quoted word')],
                token_stream=assert_token_stream(
                    head_token=assert_token_string_is('plain-word2')),
            )),
        TC(
            _src('{hard_quote}single quoted word{hard_quote} plain-word2',
                 hard_quote=HARD_QUOTE_CHAR),
            Expectation(
                fragments=[constant('single quoted word')],
                token_stream=assert_token_stream(
                    head_token=assert_token_string_is('plain-word2')),
            )),
        TC(
            _src('{hard_quote}{symbol_reference}{hard_quote} plain-word2',
                 symbol_reference=symbol_reference_syntax_for_name('sym_name'),
                 hard_quote=HARD_QUOTE_CHAR),
            Expectation(
                fragments=[
                    constant(symbol_reference_syntax_for_name('sym_name'))
                ],
                token_stream=assert_token_stream(
                    head_token=assert_token_string_is('plain-word2')),
            )),
    ]
Exemple #8
0
def parse_sym_ref_or_fragments_from_token(token: Token) -> Either[SymbolName, List[symbol_syntax.Fragment]]:
    if token.is_quoted and token.is_hard_quote_type:
        return Either.of_right([symbol_syntax.constant(token.string)])
    fragments = symbol_syntax.split(token.string)
    mb_just_symbol_name = _is_single_sym_ref(fragments)
    return (
        Either.of_left(mb_just_symbol_name)
        if token.is_plain and mb_just_symbol_name is not None
        else
        Either.of_right(fragments)
    )
Exemple #9
0
 def test_assignment_of_single_symbol_reference_syntax_within_hard_quotes(self):
     # ARRANGE #
     referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
     assigned_symbol = StringSymbolContext.of_sdv('defined_symbol',
                                                  string_sdv_from_fragments([constant(str(referred_symbol))]))
     source = single_line_source('{string_type} {name} = {hard_quote}{symbol_reference}{hard_quote}',
                                 name=assigned_symbol.name,
                                 hard_quote=HARD_QUOTE_CHAR,
                                 symbol_reference=referred_symbol)
     expectation = Expectation.phase_agnostic(
         symbol_usages=asrt.matches_sequence([
             assigned_symbol.assert_matches_definition_of_sdv,
         ]),
         symbols_after_main=assert_symbol_table_is_singleton(
             assigned_symbol.name,
             assigned_symbol.value.assert_matches_container_of_sdv,
         )
     )
     # ACT & ASSERT #
     INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Exemple #10
0
 def test_assignment_of_single_symbol_reference_syntax_within_hard_quotes(self):
     # ARRANGE #
     referred_symbol = SymbolWithReferenceSyntax('referred_symbol')
     name_of_defined_symbol = 'defined_symbol'
     source = single_line_source('{string_type} {name} = {hard_quote}{symbol_reference}{hard_quote}',
                                 name=name_of_defined_symbol,
                                 hard_quote=HARD_QUOTE_CHAR,
                                 symbol_reference=referred_symbol)
     expected_resolver = string_resolver_from_fragments([constant(str(referred_symbol))])
     container_of_expected_resolver = container(expected_resolver)
     expected_definition = SymbolDefinition(name_of_defined_symbol,
                                            container_of_expected_resolver)
     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(
             name_of_defined_symbol,
             equals_container(container_of_expected_resolver),
         )
     )
     # ACT & ASSERT #
     self._check(source, ArrangementWithSds(), expectation)
Exemple #11
0
def successful_parse_of_complex_structure() -> List[TC]:
    return [
        TC(
            _src(
                '  {soft_quote}{sym_ref1}const 1{not_a_symbol_name1}{not_a_symbol_name2}const 2{sym_ref2}{soft_quote}',
                soft_quote=SOFT_QUOTE_CHAR,
                sym_ref1=symbol_reference_syntax_for_name('sym_name1'),
                not_a_symbol_name1=symbol_reference_syntax_for_name(
                    'not a sym1'),
                not_a_symbol_name2=symbol_reference_syntax_for_name(
                    'not a sym2'),
                sym_ref2=symbol_reference_syntax_for_name('sym_name2')),
            Expectation(
                fragments=[
                    symbol('sym_name1'),
                    constant('const 1' +
                             symbol_reference_syntax_for_name('not a sym1') +
                             symbol_reference_syntax_for_name('not a sym2') +
                             'const 2'),
                    symbol('sym_name2')
                ],
                token_stream=assert_token_stream(is_null=asrt.is_true),
            ))
    ]
Exemple #12
0
 def runTest(self):
     cases = [
         ('', []),
         ('abc', [constant('abc')]),
         (
             sut.symbol_reference_syntax_for_name('sym_name'),
             [symbol('sym_name')]
         ),
         (
             sut.symbol_reference_syntax_for_name('sym_NAME_1'),
             [symbol('sym_NAME_1')]
         ),
         (
             sut.symbol_reference_syntax_for_name('not a symbol name'),
             [constant(sut.symbol_reference_syntax_for_name('not a symbol name'))]
         ),
         (
             sut.symbol_reference_syntax_for_name(''),
             [constant(sut.symbol_reference_syntax_for_name(''))]
         ),
         (
             sut.symbol_reference_syntax_for_name('  '),
             [constant(sut.symbol_reference_syntax_for_name('  '))]
         ),
         (
             sut.symbol_reference_syntax_for_name('1isAValidSymbolName'),
             [symbol('1isAValidSymbolName')]
         ),
         (
             'const{sym_ref}'.format(sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
             [constant('const'),
              symbol('sym_name')]
         ),
         (
             '{sym_ref}const'.format(sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
             [symbol('sym_name'),
              constant('const')]
         ),
         (
             '{sym_ref1}{sym_ref2}'.format(
                 sym_ref1=sut.symbol_reference_syntax_for_name('sym_name1'),
                 sym_ref2=sut.symbol_reference_syntax_for_name('sym_name2')),
             [symbol('sym_name1'),
              symbol('sym_name2')]
         ),
         (
             '{sym_begin}{sym_ref}'.format(
                 sym_begin=sut.SYMBOL_REFERENCE_BEGIN,
                 sym_ref=sut.symbol_reference_syntax_for_name('sym_name')),
             [constant(sut.SYMBOL_REFERENCE_BEGIN),
              symbol('sym_name')]
         ),
         (
             '{sym_ref1}const 1{not_a_symbol_name1}{not_a_symbol_name2}const 2{sym_ref2}'.format(
                 sym_ref1=sut.symbol_reference_syntax_for_name('sym_name1'),
                 not_a_symbol_name1=sut.symbol_reference_syntax_for_name('not a sym1'),
                 not_a_symbol_name2=sut.symbol_reference_syntax_for_name('not a sym2'),
                 sym_ref2=sut.symbol_reference_syntax_for_name('sym_name2')),
             [symbol('sym_name1'),
              constant('const 1' +
                       sut.symbol_reference_syntax_for_name('not a sym1') +
                       sut.symbol_reference_syntax_for_name('not a sym2') +
                       'const 2'),
              symbol('sym_name2')]
         ),
     ]
     for source, expected in cases:
         with self.subTest(source=source):
             actual = sut.split(source)
             self.assertEqual(expected,
                              actual)
Exemple #13
0
def parse_fragments_from_token(token: Token) -> List[symbol_syntax.Fragment]:
    if token.is_quoted and token.is_hard_quote_type:
        return [symbol_syntax.constant(token.string)]
    return symbol_syntax.split(token.string)
Exemple #14
0
 def test_equals_string(self):
     actual = 's' == sut.constant('s')
     self.assertFalse(actual)