Exemple #1
0
 def test_not_equals__same_length__different_values(self):
     # ARRANGE #
     expected = (string_sdvs.str_fragment('expected value'), )
     actual = (string_sdvs.str_fragment('actual value'), )
     assertion = sut.equals_string_fragments(expected)
     # ACT & ASSERT #
     assert_that_assertion_fails(assertion, actual)
Exemple #2
0
    def test_not_equals(self):
        expected_string = 'expected value'
        cases = [
            NEA(
                'differs__resolved_value',
                string_sdvs.str_constant(expected_string),
                string_sdvs.str_constant('actual value'),
            ),
            NEA(
                'differs__number_of_references',
                string_sdvs.str_constant(expected_string),
                sdv_with_references([
                    SymbolReference(
                        'symbol_name',
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))
                ]),
            ),
            NEA(
                'different_number_of_references',
                StringSdvTestImpl(expected_string, [
                    SymbolReference(
                        'expected_symbol_name',
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))
                ]),
                StringSdvTestImpl(expected_string, [
                    SymbolReference(
                        'actual_symbol_name',
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))
                ]),
            ),
            NEA(
                'different_number_of_fragments',
                StringSdvTestImpl(expected_string, [],
                                  (string_sdvs.str_fragment('value'), )),
                StringSdvTestImpl(expected_string, [], (())),
            ),
            NEA(
                'different_fragments',
                StringSdvTestImpl(expected_string, [],
                                  (string_sdvs.str_fragment('value 1'), )),
                StringSdvTestImpl(expected_string, [],
                                  (string_sdvs.str_fragment('value 2'), )),
            ),
        ]

        for case in cases:
            with self.subTest(case.name):
                assertion = sut.equals_string_sdv(case.expected)
                # ACT & ASSERT #
                assert_that_assertion_fails(assertion, case.actual)
Exemple #3
0
 def test_equals(self):
     test_cases = [
         (
             (),
             (),
         ),
         (
             (string_sdvs.str_fragment('abc'), ),
             (string_sdvs.str_fragment('abc'), ),
         ),
         (
             (string_sdvs.symbol_fragment(
                 SymbolReference(
                     'symbol_name',
                     ReferenceRestrictionsOnDirectAndIndirect(
                         ArbitraryValueWStrRenderingRestriction.of_any()))),
              ),
             (string_sdvs.symbol_fragment(
                 SymbolReference(
                     'symbol_name',
                     ReferenceRestrictionsOnDirectAndIndirect(
                         ArbitraryValueWStrRenderingRestriction.of_any()))),
              ),
         ),
         (
             (
                 string_sdvs.str_fragment('abc'),
                 string_sdvs.symbol_fragment(
                     SymbolReference(
                         'symbol_name',
                         ReferenceRestrictionsOnDirectAndIndirect(
                             ArbitraryValueWStrRenderingRestriction.of_any(
                             )))),
             ),
             (
                 string_sdvs.str_fragment('abc'),
                 string_sdvs.symbol_fragment(
                     SymbolReference(
                         'symbol_name',
                         ReferenceRestrictionsOnDirectAndIndirect(
                             ArbitraryValueWStrRenderingRestriction.of_any(
                             )))),
             ),
         ),
     ]
     for fragments1, fragments2 in test_cases:
         with self.subTest(msg=str(fragments1) + ' ' + str(fragments2)):
             sut.equals_string_fragments(fragments1).apply_without_message(
                 self, fragments2)
             sut.equals_string_fragments(fragments2).apply_without_message(
                 self, fragments1)
Exemple #4
0
    def test_not_equals(self):
        # ARRANGE #
        value = 'a_value'
        cases = [
            NEA(
                'different constants',
                string_sdvs.str_fragment('some value'),
                string_sdvs.str_fragment('some other value'),
            ),
            NEA(
                'constant and reference',
                string_sdvs.str_fragment(value),
                string_sdvs.symbol_fragment(
                    SymbolReference(
                        value,
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))),
            ),
            NEA(
                'references with different names',
                string_sdvs.symbol_fragment(
                    SymbolReference(
                        'a name',
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))),
                string_sdvs.symbol_fragment(
                    SymbolReference(
                        'a different name',
                        ReferenceRestrictionsOnDirectAndIndirect(
                            ArbitraryValueWStrRenderingRestriction.of_any()))),
            ),
            # NEA(
            #     'references with different restrictions',
            #     string_sdvs.symbol_fragment(
            #         SymbolReference(
            #             'common name',
            #             ReferenceRestrictionsOnDirectAndIndirect(AnyDataTypeRestriction()))
            #     ),
            #     string_sdvs.symbol_fragment(
            #         SymbolReference(
            #             'common name',
            #             TypeCategoryRestriction(TypeCategory.LOGIC))
            #     ),
            # ),
        ]

        # ACT & ASSERT #

        for case in cases:
            with self.subTest(case.name, order='expected == actual'):
Exemple #5
0
 def test_not_equals__different_number_of_fragments__non_empty__empty(self):
     # ARRANGE #
     expected = (string_sdvs.str_fragment('value'), )
     actual = ()
     assertion = sut.equals_string_fragments(expected)
     # ACT & ASSERT #
     assert_that_assertion_fails(assertion, actual)
Exemple #6
0
def fragment_sdv_from_fragment(fragment: symbol_syntax.Fragment,
                               reference_restrictions: ReferenceRestrictions) -> StringFragmentSdv:
    if fragment.is_constant:
        return string_sdvs.str_fragment(fragment.value)
    else:
        sr = SymbolReference(fragment.value, reference_restrictions)
        return string_sdvs.symbol_fragment(sr)
Exemple #7
0
def fragment_sdv_from_fragment(fragment: Fragment) -> StringFragmentSdv:
    if fragment.is_constant:
        return string_sdvs.str_fragment(fragment.value)
    else:
        sr = SymbolReference(
            fragment.value,
            ReferenceRestrictionsOnDirectAndIndirect(
                direct=ArbitraryValueWStrRenderingRestriction.of_any(),
                indirect=None))
        return string_sdvs.symbol_fragment(sr)
Exemple #8
0
 def test_not_equals__same_length__different_types(self):
     # ARRANGE #
     expected = (string_sdvs.str_fragment('value'), )
     actual = (string_sdvs.symbol_fragment(
         SymbolReference(
             'value',
             ReferenceRestrictionsOnDirectAndIndirect(
                 ArbitraryValueWStrRenderingRestriction.of_any()))), )
     assertion = sut.equals_string_fragments(expected)
     # ACT & ASSERT #
     assert_that_assertion_fails(assertion, actual)
Exemple #9
0
 def test_equals(self):
     a_string = 'a string'
     an_identical_string = 'a string'
     test_cases = [
         NEA(
             'constant strings',
             string_sdvs.str_fragment(a_string),
             string_sdvs.str_fragment(an_identical_string),
         ),
         NEA(
             'symbol references',
             string_sdvs.symbol_fragment(
                 SymbolReference(
                     a_string,
                     ReferenceRestrictionsOnDirectAndIndirect(
                         ArbitraryValueWStrRenderingRestriction.of_any()))),
             string_sdvs.symbol_fragment(
                 SymbolReference(
                     an_identical_string,
                     ReferenceRestrictionsOnDirectAndIndirect(
                         ArbitraryValueWStrRenderingRestriction.of_any()))),
         ),
         NEA(
             'symbol references with different restrictions (surprisingly)',
             string_sdvs.symbol_fragment(
                 SymbolReference(
                     a_string,
                     ReferenceRestrictionsOnDirectAndIndirect(
                         ArbitraryValueWStrRenderingRestriction.of_any()))),
             string_sdvs.symbol_fragment(
                 SymbolReference(an_identical_string,
                                 ValueTypeRestriction([ValueType.PROGRAM
                                                       ]))),
         ),
     ]
     for case in test_cases:
         with self.subTest(case.name):
             sut.equals_string_fragment_sdv_with_exact_type(
                 case.expected).apply_without_message(self, case.actual)
             sut.equals_string_fragment_sdv_with_exact_type(
                 case.actual).apply_without_message(self, case.expected)
Exemple #10
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)
Exemple #11
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)
Exemple #12
0
    def test(self):
        # ARRANGE #
        plain_string1 = 'plain_1'
        plain_string2 = 'plain_2'
        symbol_name_1 = 'symbol_name_1'
        symbol_name_2 = 'symbol_name_2'
        remaining_part_of_current_line_with_sym_ref = ''.join([
            'before',
            symbol_reference_syntax_for_name(symbol_name_2), 'after'
        ])

        cases = [
            Case.of_multi(
                'plain strings', [
                    ArgumentOfRichStringAbsStx.of_str(plain_string1),
                    ArgumentOfRichStringAbsStx.of_str(plain_string2)
                ], ARRANGEMENT__NEUTRAL,
                Expectation(
                    elements=[
                        list_sdvs.str_element(plain_string1),
                        list_sdvs.str_element(plain_string2)
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.is_empty_sequence,
                )),
            Case.of_multi(
                'plain strings on several lines (separated by continuation token)',
                [
                    ArgumentOfRichStringAbsStx.of_str(plain_string1),
                    ContinuationTokenFollowedByArgumentAbsStx(
                        ArgumentOfRichStringAbsStx.of_str(plain_string2)),
                ], ARRANGEMENT__NEUTRAL,
                Expectation(
                    elements=[
                        list_sdvs.str_element(plain_string1),
                        list_sdvs.str_element(plain_string2)
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.is_empty_sequence,
                )),
            Case.of_multi(
                'plain strings on several lines (separated by continuation token) / first line empty',
                [
                    ContinuationTokenFollowedByArgumentAbsStx(
                        ArgumentOfRichStringAbsStx.of_str(plain_string1)),
                    ArgumentOfRichStringAbsStx.of_str(plain_string2),
                ], ARRANGEMENT__NEUTRAL,
                Expectation(
                    elements=[
                        list_sdvs.str_element(plain_string1),
                        list_sdvs.str_element(plain_string2)
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.is_empty_sequence,
                )),
            Case.of_multi(
                'symbol reference + plain string + until-end-of-line', [
                    ArgumentOfSymbolReferenceAbsStx(symbol_name_1),
                    ArgumentOfRichStringAbsStx.of_str(plain_string1),
                    ArgumentOfRichStringAbsStx.of_str_until_eol(
                        remaining_part_of_current_line_with_sym_ref),
                ],
                Arrangement(
                    SymbolContext.symbol_table_of_contexts([
                        StringSymbolContext.of_arbitrary_value(symbol_name_1),
                        StringSymbolContext.of_arbitrary_value(symbol_name_2),
                    ])),
                Expectation(
                    elements=[
                        list_sdvs.symbol_element(
                            reference_to__on_direct_and_indirect(
                                symbol_name_1)),
                        list_sdvs.str_element(plain_string1),
                        list_sdvs.string_element(
                            string_sdvs.from_fragments([
                                string_sdvs.str_fragment('before'),
                                string_sdvs.symbol_fragment(
                                    reference_to__on_direct_and_indirect(
                                        symbol_name_2)),
                                string_sdvs.str_fragment('after'),
                            ]))
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.matches_sequence([
                        asrt_sym_ref.matches_reference_2(
                            symbol_name_1,
                            asrt_data_rest.is__w_str_rendering()),
                        asrt_sym_ref.matches_reference_2(
                            symbol_name_2,
                            asrt_data_rest.is__w_str_rendering()),
                    ]),
                )),
        ]
        # ACT & ASSERT #
        _test_cases_w_source_variants(self, cases)
Exemple #13
0
    def test_remaining_part_of_current_line_as_literal(self):
        # ARRANGE #
        symbol_name = 'symbol_name'
        str_with_space_and_invalid_token_syntax = 'before and after space, ' + SOFT_QUOTE_CHAR + 'after quote'

        cases = [
            Case.of(
                'string with one space after marker, and no space at EOL',
                ArgumentOfRichStringAbsStx.of_str_until_eol(
                    str_with_space_and_invalid_token_syntax),
                ARRANGEMENT__NEUTRAL,
                Expectation(
                    elements=[
                        list_sdvs.str_element(
                            str_with_space_and_invalid_token_syntax)
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.is_empty_sequence,
                )),
            Case.of(
                'with surrounding space',
                ArgumentOfRichStringAbsStx.of_str_until_eol(
                    '   ' + str_with_space_and_invalid_token_syntax + '  \t '),
                ARRANGEMENT__NEUTRAL,
                Expectation(
                    elements=[
                        list_sdvs.str_element(
                            str_with_space_and_invalid_token_syntax)
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.is_empty_sequence,
                )),
            Case.of(
                'with symbol reference',
                ArgumentOfRichStringAbsStx.of_str_until_eol(''.join([
                    'before',
                    symbol_reference_syntax_for_name(symbol_name), 'after'
                ])),
                Arrangement(
                    StringSymbolContext.of_arbitrary_value(
                        symbol_name).symbol_table),
                Expectation(
                    elements=[
                        list_sdvs.string_element(
                            string_sdvs.from_fragments([
                                string_sdvs.str_fragment('before'),
                                string_sdvs.symbol_fragment(
                                    reference_to__on_direct_and_indirect(
                                        symbol_name)),
                                string_sdvs.str_fragment('after'),
                            ]))
                    ],
                    validators=asrt.is_empty_sequence,
                    references=asrt.matches_sequence([
                        asrt_sym_ref.matches_reference_2(
                            symbol_name, asrt_data_rest.is__w_str_rendering())
                    ]),
                )),
        ]
        # ACT & ASSERT #
        _test_cases_w_source_variants(self, cases)