def test_references(self): reference_1 = TypeWithStrRenderingSymbolReference('symbol_1_name', is_any_type_w_str_rendering()) reference_2 = TypeWithStrRenderingSymbolReference('symbol_2_name', OrReferenceRestrictions([])) cases = [ ( 'no elements', sut.ListSdv([]), asrt.is_empty_sequence, ), ( 'single string constant element', sut.ListSdv([list_sdvs.str_element('string value')]), asrt.is_empty_sequence, ), ( 'multiple elements with multiple references', sut.ListSdv([ list_sdvs.symbol_element(reference_1.reference), list_sdvs.str_element('constant value'), list_sdvs.symbol_element(reference_2.reference), ]), asrt.matches_sequence([ reference_1.reference_assertion, reference_2.reference_assertion, ]), ), ] for test_name, list_sdv, expected_references_assertion in cases: with self.subTest(test_name=test_name): actual = list_sdv.references expected_references_assertion.apply_without_message(self, actual)
def test_string_not_equals_string_with_different_value(self): # ARRANGE # expected = list_sdvs.str_element('expected value') actual = list_sdvs.str_element('actual value') assertion = sut.equals_list_sdv_element(expected) # ACT & ASSERT # assert_that_assertion_fails(assertion, actual)
def test_symbol_ref_not_equals_string(self): # ARRANGE # string_element = list_sdvs.str_element('value') symbol_reference_element = list_sdvs.symbol_element(reference_to__on_direct_and_indirect('symbol_name')) assertion = sut.equals_list_sdv_element(symbol_reference_element) # ACT & ASSERT # assert_that_assertion_fails(assertion, string_element)
def test_equals(self): test_cases = [ list_sdvs.str_element('value'), list_sdvs.symbol_element(reference_to__on_direct_and_indirect('symbol_name')), ] for element in test_cases: with self.subTest(msg=str(element)): sut.equals_list_sdv_element(element).apply_without_message(self, element)
def test_resolve_without_symbol_references(self): string_constant_1 = 'string constant 1' string_constant_2 = 'string constant 2' string_symbol = StringConstantSymbolContext('string_symbol_name', 'string symbol value') cases = [ Case( 'no elements', sdv_to_check= sut.ListSdv([]), symbols= empty_symbol_table(), expected_resolved_value= ListDdv([]), ), Case( 'single constant element', sdv_to_check= sut.ListSdv([list_sdvs.str_element(string_constant_1)]), symbols= empty_symbol_table(), expected_resolved_value= ListDdv([string_ddv_of_single_string(string_constant_1)]), ), Case( 'multiple constant elements', sdv_to_check= sut.ListSdv([list_sdvs.str_element(string_constant_1), list_sdvs.str_element(string_constant_2)]), symbols= empty_symbol_table(), expected_resolved_value= ListDdv([string_ddv_of_single_string(string_constant_1), string_ddv_of_single_string(string_constant_2)]), ), Case( 'single string symbol reference element', sdv_to_check= sut.ListSdv([list_sdvs.symbol_element(string_symbol.reference__w_str_rendering)]), symbols= string_symbol.symbol_table, expected_resolved_value= ListDdv([string_ddv_of_single_string(string_symbol.str_value)]), ), ] self._check('resolve without symbol references', cases)
def test_string_token(self): # ARRANGE # plain_string = 'plain' symbol_name = 'symbol_name' string_symbol = StringSymbolContext.of_arbitrary_value(symbol_name) cases = [ Case.of( 'plain string', ArgumentOfRichStringAbsStx.of_str(plain_string), ARRANGEMENT__NEUTRAL, Expectation( elements=[list_sdvs.str_element(plain_string)], validators=asrt.is_empty_sequence, references=asrt.is_empty_sequence, )), Case.of( 'continuation token followed by plain string on next line', ContinuationTokenFollowedByArgumentAbsStx( ArgumentOfRichStringAbsStx.of_str(plain_string)), ARRANGEMENT__NEUTRAL, Expectation( elements=[list_sdvs.str_element(plain_string)], validators=asrt.is_empty_sequence, references=asrt.is_empty_sequence, )), Case.of( 'symbol reference', ArgumentOfSymbolReferenceAbsStx(symbol_name), Arrangement(string_symbol.symbol_table), Expectation( elements=[ list_sdvs.symbol_element( reference_to__on_direct_and_indirect(symbol_name)) ], validators=asrt.is_empty_sequence, references=asrt.matches_sequence( [string_symbol.reference_assertion__w_str_rendering]), )), ] # ACT & ASSERT # _test_cases_w_source_variants(self, cases)
def test_elements(self): # ARRANGE # element_1 = list_sdvs.str_element('constant value') element_2 = list_sdvs.symbol_element( data_references.reference_to__on_direct_and_indirect('symbol_name')) sdv = sut.ListSdv([element_1, element_2]) # ACT # actual = sdv.elements # ASSERT # assertion = asrt.matches_sequence([equals_list_sdv_element(element_1), equals_list_sdv_element(element_2)]) assertion.apply_without_message(self, actual)
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)))
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)
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)
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)
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)