class ValueWSymRefsAndVarRefs: REFERENCED_VAR_1 = NameAndValue('existing_var_1', '<val of existing 1>') REFERENCED_VAR_2 = NameAndValue('existing_var_2', '<val of existing 2>') REFERENCED_VAR__ALL = [REFERENCED_VAR_1, REFERENCED_VAR_2] VALUE_PATTERN = '{}between{}' VALUE_W_VAR_REFS = VALUE_PATTERN.format( env_var_ref_syntax(REFERENCED_VAR_1.name), env_var_ref_syntax(REFERENCED_VAR_2.name), ) VALUE_WO_VAR_REFS = VALUE_PATTERN.format( REFERENCED_VAR_1.value, REFERENCED_VAR_2.value, ) POS_OF_END_OF_VAR_REF_1 = end_of_1st_var_ref(VALUE_W_VAR_REFS) SYM_REF_PART_1 = StringConstantSymbolContext( 'VAL_SYM_1', VALUE_W_VAR_REFS[:4], default_restrictions=asrt_w_str_rend_rest.is__w_str_rendering(), ) CONST_STR_PART_2 = VALUE_W_VAR_REFS[4:(POS_OF_END_OF_VAR_REF_1 + 5)] SYM_REF_PART_3 = StringConstantSymbolContext( 'VAL_SYM_3', VALUE_W_VAR_REFS[(POS_OF_END_OF_VAR_REF_1 + 5):], default_restrictions=asrt_w_str_rend_rest.is__w_str_rendering(), ) SYMBOL_CONTEXTS = (SYM_REF_PART_1, SYM_REF_PART_3) STRING_ABS_STX = str_abs_stx.StringConcatAbsStx([ SYM_REF_PART_1.abstract_syntax, str_abs_stx.StringLiteralAbsStx(CONST_STR_PART_2), SYM_REF_PART_3.abstract_syntax, ])
def _test_symbol_reference_in_contents( self, symbol_ref_syntax_2_contents_arguments: Callable[ [str], StringSourceAbsStx], symbol_value_2_expected_contents: Callable[[str], str]): contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'contents symbol value', default_restrictions=asrt_rest.is__w_str_rendering(), ) expected_contents = symbol_value_2_expected_contents( contents_symbol.str_value) symbols = [contents_symbol] expected_symbol_references = SymbolContext.references_assertion_of_contexts( symbols) symbol_table = SymbolContext.symbol_table_of_contexts(symbols) string_source_syntax = symbol_ref_syntax_2_contents_arguments( symbol_reference_syntax_for_name(contents_symbol.name)) CHECKER.check__abs_stx( self, string_source_syntax, None, arrangement_w_tcds(symbols=symbol_table, ), Expectation.of_prim__const( primitive=asrt_string_source. pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=False), parse=ParseExpectation( symbol_references=expected_symbol_references, ), ))
def runTest(self): # ARRANGE # symbol_in_regex = StringConstantSymbolContext( 'symbol_in_regex', 'plain string pattern', default_restrictions=asrt_regex.is_reference_restrictions__regex()) symbol_in_replacement = StringConstantSymbolContext( 'symbol_in_replacement', 'the replacement', default_restrictions=asrt_ref_rest.is__w_str_rendering(), ) input_lines = [ symbol_in_regex.str_value, ] expected_lines = [ symbol_in_replacement.str_value, ] quoting_cases = [ None, QuoteType.SOFT, ] for line_filter_case in LINE_FILTER_CASES: all_symbols = line_filter_case.value.symbols + [ symbol_in_regex, symbol_in_replacement, ] for preserve_new_lines in [False, True]: for quoting_variant in quoting_cases: source = ReplaceRegexAbsStx( StringLiteralAbsStx( symbol_in_regex.name__sym_ref_syntax, quoting_variant), symbol_in_replacement.abstract_syntax, preserve_new_lines=preserve_new_lines, lines_filter=line_filter_case.value.syntax, ) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__abs_stx__layout__std_source_variants( self, source, model_constructor.of_lines(self, input_lines), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( all_symbols), ), expectation_of_successful_replace_execution__multi( symbol_references=SymbolContext. references_assertion_of_contexts(all_symbols), may_depend_on_external_resources=line_filter_case. value.may_depend_on_external_resources, output_lines=expected_lines, ), sub_test_identifiers={ 'preserve_new_lines': preserve_new_lines, 'quoting': quoting_variant, 'line_filtering': line_filter_case.name, })
def _test_symbol_reference_in_dst_file_and_contents( self, symbol_ref_syntax_2_contents_arguments: Callable[ [str], StringSourceAbsStx], symbol_value_2_expected_contents: Callable[[str], str]): sub_dir_symbol = ConstantSuffixPathDdvSymbolContext( 'sub_dir_symbol', RelOptionType.REL_ACT, 'sub-dir', ACCEPTED_DST_RELATIVITY_VARIANTS, ) contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'contents symbol value', default_restrictions=asrt_rest.is__w_str_rendering(), ) expected_file_contents = symbol_value_2_expected_contents( contents_symbol.str_value) expected_file = fs.File('a-file-name.txt', expected_file_contents) symbols = [sub_dir_symbol, contents_symbol] expected_symbol_references = SymbolContext.references_assertion_of_contexts( symbols) symbol_table = SymbolContext.symbol_table_of_contexts(symbols) contents_arguments = symbol_ref_syntax_2_contents_arguments( symbol_reference_syntax_for_name(contents_symbol.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( path_abs_stx.PathStringAbsStx.of_plain_components( [sub_dir_symbol.name__sym_ref_syntax, expected_file.name]), contents_arguments, ) integration_check.CHECKER__AFTER_ACT.check__abs_stx( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), symbols=symbol_table, ), Expectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=expected_symbol_references, main_side_effects_on_sds=dir_contains_exactly( sub_dir_symbol.rel_option_type, fs.DirContents( [fs.Dir(sub_dir_symbol.path_suffix, [expected_file])])), ))
def _check( self, pattern: str, replacement_str: str, lines_selector: LineMatcherSymbolContext, input_lines__wo_nl: List[str], output_lines__wo_nl: List[str], ): # ARRANGE # input_lines = with_appended_new_lines(input_lines__wo_nl) output_lines = with_appended_new_lines(output_lines__wo_nl) pattern_symbol = StringConstantSymbolContext( 'PATTERN_SYMBOL', pattern, default_restrictions=asrt_regex.is_reference_restrictions__regex(), ) replacement_symbol = StringConstantSymbolContext( 'REPLACEMENT_SYMBOL', replacement_str, default_restrictions=asrt_ref_rest.is__w_str_rendering(), ) all_symbol = [lines_selector, pattern_symbol, replacement_symbol] for preserve_new_lines in [False, True]: syntax = ReplaceRegexAbsStx( pattern_symbol.abstract_syntax, replacement_symbol.abstract_syntax, preserve_new_lines=preserve_new_lines, lines_filter=lines_selector.abstract_syntax, ) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__abs_stx__layout__std_source_variants( self, syntax, model_constructor.of_lines(self, input_lines), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( all_symbol), ), expectation_of_successful_replace_execution__multi( symbol_references=SymbolContext. references_assertion_of_contexts(all_symbol), output_lines=output_lines, may_depend_on_external_resources=True, ), sub_test_identifiers={ 'preserve_new_lines': preserve_new_lines, })
def runTest(self): # ARRANGE # symbol_in_program_source = NameAndValue('SYMBOL_IN_PROGRAM_SOURCE', data_restrictions_assertions.is__w_str_rendering()) symbol_in_matcher = NameAndValue('SYMBOL_IN_MATCHER', ss_references.IS_STRING_SOURCE_OR_STRING_REFERENCE_RESTRICTION) symbol_in_transformer_of_program = NameAndValue('SYMBOL_IN_TRANSFORMER_OF_PROGRAM', is_reference_restrictions__value_type( [ValueType.STRING_TRANSFORMER])) symbol_in_transformer_of_instruction = NameAndValue('SYMBOL_IN_TRANSFORMER_OF_INSTRUCTION', is_reference_restrictions__value_type( [ValueType.STRING_TRANSFORMER])) program_with_ref_to_symbols = pgm_args.program( pgm_args.interpret_py_source_line( self.configuration.py_source_for_print( symbol_reference_syntax_for_name(symbol_in_program_source.name))), transformation=symbol_in_transformer_of_program.name ) matcher_with_ref_to_symbol = matcher_arguments.equals_matcher(SymbolReferenceArgument(symbol_in_matcher.name)) arguments = po_ab.from_program(program_with_ref_to_symbols, matcher_with_ref_to_symbol, transformation=symbol_in_transformer_of_instruction.name) symbol_usages_assertion = asrt.matches_sequence([ matches_reference(symbol_in_program_source), matches_reference(symbol_in_transformer_of_program), matches_reference(symbol_in_transformer_of_instruction), matches_reference(symbol_in_matcher), ]) source = arguments.as_remaining_source # ACT # actual = self.configuration.parser().parse(ARBITRARY_FS_LOCATION_INFO, source) actual_symbol_usages = actual.symbol_usages() # ASSERT # symbol_usages_assertion.apply_without_message(self, actual_symbol_usages)
def _string_case(contents_transformation: Callable[[str], str]) -> Case: string_contents = 'the value of the string symbol' def get_assertion_on_primitive( env: AssertionResolvingEnvironment) -> Assertion[StringSource]: return asrt_string_source.pre_post_freeze__matches_str__const( contents_transformation(string_contents), may_depend_on_external_resources=False) symbol_context = StringConstantSymbolContext( 'STRING_SYMBOL', string_contents, default_restrictions=asrt_rest.is__w_str_rendering(), ) return Case( StringSourceSymbolReferenceAbsStx(symbol_context.name), symbol_context, get_assertion_on_primitive, )
def test_here_doc_with_embedded_references(self): # ARRANGE # defined_name = symbol_syntax.A_VALID_SYMBOL_NAME here_doc_line_template = 'pre symbol {symbol} post symbol' referenced_symbol = StringConstantSymbolContext( 'REFERENCED_STRING_SYMBOL', 'contents of string symbol', default_restrictions=data_restrictions_assertions.is__w_str_rendering(), ) expected_contents = here_doc_line_template.format(symbol=referenced_symbol.str_value) + '\n' syntax = _syntax_of( StringSourceOfStringAbsStx( HereDocAbsStx.of_lines__wo_new_lines([ here_doc_line_template.format(symbol=referenced_symbol.name__sym_ref_syntax) ]) ), defined_name, ) arrangement = Arrangement.phase_agnostic() # EXPECTATION # expectation = _expect_definition_of( defined_name, referenced_symbol.symbol_table, referenced_symbol.references_assertion, expected_contents, may_depend_on_external_resources=False ) # ACT & ASSERT # INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants( self, syntax, arrangement, expectation, )
def test_just_symbol_reference(self): contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'symbol value contents', default_restrictions=asrt_rest.is__w_str_rendering(), ) string_source_syntax = StringSourceOfStringAbsStx.of_plain( str_abs_stx.StringSymbolAbsStx(contents_symbol.name)) CHECKER.check__abs_stx( self, string_source_syntax, None, arrangement_w_tcds(symbols=contents_symbol.symbol_table, ), Expectation.of_prim__const( primitive=asrt_string_source. pre_post_freeze__matches_str__const( contents_symbol.str_value, may_depend_on_external_resources=False), parse=ParseExpectation( symbol_references=asrt.matches_singleton_sequence( references.is_reference_to__string_source_or_string( contents_symbol.name)), ), ))
def test_list_of_arguments_and_symbol_references(self): arg_wo_space = 'arg_wo_space' arg_w_space = 'an arg w space' string_symbol_1 = StringConstantSymbolContext( 'STRING_SYMBOL_1', 'value of string symbol 1', default_restrictions=asrt_data_rest.is__w_str_rendering(), ) string_symbol_2 = StringConstantSymbolContext( 'STRING_SYMBOL_2', 'value of string symbol 2', default_restrictions=asrt_data_rest.is__w_str_rendering(), ) arguments_cases = [ ArgumentsCase( 'one / wo symbol references', arguments=[ ArgumentOfRichStringAbsStx.of_str(arg_w_space, QuoteType.SOFT) ], expected_arguments=[arg_w_space], symbols=(), ), ArgumentsCase( 'two / wo symbol references', arguments=[ ArgumentOfRichStringAbsStx.of_str(arg_wo_space), ArgumentOfRichStringAbsStx.of_str(arg_w_space, QuoteType.HARD) ], expected_arguments=[arg_wo_space, arg_w_space], symbols=(), ), ArgumentsCase( 'three / w symbol references', arguments=[ ArgumentOfSymbolReferenceAbsStx(string_symbol_1.name), ArgumentOfRichStringAbsStx.of_str(arg_w_space, QuoteType.HARD), ArgumentOfSymbolReferenceAbsStx(string_symbol_2.name) ], expected_arguments=[ string_symbol_1.str_value, arg_w_space, string_symbol_2.str_value ], symbols=[string_symbol_1, string_symbol_2], ), ] # ARRANGE # for pgm_and_args_case in pgm_and_args_cases.cases__w_argument_list__excluding_program_reference( ): for arguments_case in arguments_cases: pgm_w_args = PgmAndArgsWArgumentsAbsStx( pgm_and_args_case.pgm_and_args, arguments_case.arguments) expected_arguments = asrt.equals( arguments_case.expected_arguments) symbols = list(pgm_and_args_case.symbols) + list( arguments_case.symbols) expectation = MultiSourceExpectation( symbol_references=SymbolContext. references_assertion_of_contexts(symbols), primitive=lambda env: (asrt_pgm_val.matches_program( asrt_command.matches_command( driver=pgm_and_args_case.expected_command_driver( env), arguments=expected_arguments, ), stdin=asrt_pgm_val.is_no_stdin(), transformer=asrt_pgm_val.is_no_transformation(), ))) with self.subTest(command=pgm_and_args_case.name, arguments=arguments_case.name): # ACT & ASSERT # CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_consume_until_end_of_last_line__s__nsc, pgm_w_args, pgm_and_args_case.mk_arrangement( SymbolContext.symbol_table_of_contexts(symbols)), expectation, )
def reference_assertion(self, symbol_name: str) -> Assertion[SymbolReference]: return asrt_sym_ref.matches_reference_2( symbol_name, asrt_data_rest.is__w_str_rendering())
def test_with_arguments(self): arg_wo_space = 'arg_wo_space' arg_w_space = 'an arg w space' arg_w_space__src = surrounded_by_soft_quotes(arg_w_space) string_symbol = StringConstantSymbolContext( 'STRING_SYMBOL', 'the string value', default_restrictions=asrt_rest.is__w_str_rendering(), ) list_symbol = ListConstantSymbolContext( 'LIST_SYMBOL', ['1st value', '2nd value'], ) string_with_invalid_quoting = '"string without ending soft quote' arguments_cases = [ ArgumentsCase( 'one', source=ArgumentElements([arg_w_space__src]), expected_arguments=[arg_w_space], ), ArgumentsCase( 'two', source=ArgumentElements([arg_wo_space, arg_w_space__src]), expected_arguments=[arg_wo_space, arg_w_space], ), ArgumentsCase( 'string symbol reference', source=ArgumentElements([string_symbol.name__sym_ref_syntax]), expected_arguments=[string_symbol.str_value], symbols=[string_symbol], ), ArgumentsCase( 'list symbol reference', source=ArgumentElements([list_symbol.name__sym_ref_syntax]), expected_arguments=list_symbol.constant_list, symbols=[list_symbol], ), ArgumentsCase( 'list and string symbol reference', source=ArgumentElements([ list_symbol.name__sym_ref_syntax, string_symbol.name__sym_ref_syntax ]), expected_arguments=list_symbol.constant_list + [string_symbol.str_value], symbols=[list_symbol, string_symbol], ), ArgumentsCase( 'special program argument', source=program_arguments. remaining_part_of_current_line_as_literal( string_with_invalid_quoting).as_argument_elements, expected_arguments=[string_with_invalid_quoting], symbols=[], ), ] # ARRANGE # command_cases = _single_line_command_cases() for command_case in command_cases: for arguments_case in arguments_cases: source_w_arguments = ( command_case.source.as_argument_elements.followed_by( arguments_case.source).as_arguments) expected_arguments = asrt.matches_sequence([ asrt.equals(arg) for arg in arguments_case.expected_arguments ]) expected_command_sdv = command_case.sdv_assertion( self.TCDS, arguments=expected_arguments, arguments_symbols=arguments_case.symbols) for source_case in equivalent_source_variants_for_consume_until_end_of_last_line2( source_w_arguments): with self.subTest( command=command_case.name, arguments=arguments_case.name, following_source_variant=source_case.name): # ACT # actual = self.PARSER.parse(source_case.input_value) # ASSERT # source_case.expected_value.apply_with_message( self, source_case.input_value, 'source') expected_command_sdv.apply_with_message( self, actual, 'command')
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 usage_assertion__w_str_rendering( symbol_name: str) -> Assertion[SymbolUsage]: return asrt_sym_usage.matches_reference_2( symbol_name, asrt_rest.is__w_str_rendering())
from exactly_lib.symbol.sdv_structure import SymbolReference from exactly_lib.symbol.value_type import ValueType from exactly_lib_test.symbol.test_resources import symbol_reference_assertions as asrt_sym_ref from exactly_lib_test.test_resources.value_assertions.value_assertion import Assertion from exactly_lib_test.type_val_deps.test_resources.any_.restrictions_assertions import \ is_reference_restrictions__value_type from exactly_lib_test.type_val_deps.test_resources.w_str_rend import data_restrictions_assertions as asrt_w_str_rend IS_STRING_SOURCE_OR_STRING_REFERENCE_RESTRICTION = is_reference_restrictions__value_type( (ValueType.STRING_SOURCE, ValueType.STRING)) IS_STRING_REFERENCE_RESTRICTION = asrt_w_str_rend.is__w_str_rendering() def is_reference_to__string_source_or_string( symbol_name: str) -> Assertion[SymbolReference]: return asrt_sym_ref.matches_reference_2( symbol_name, IS_STRING_SOURCE_OR_STRING_REFERENCE_RESTRICTION, ) def is_reference_to__string(symbol_name: str) -> Assertion[SymbolReference]: return asrt_sym_ref.matches_reference_2( symbol_name, IS_STRING_REFERENCE_RESTRICTION, )
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)
def is_reference_to__w_str_rendering(symbol_name: str ) -> Assertion[SymbolReference]: return matches_reference_2( symbol_name, asrt_data_ref_restriction.is__w_str_rendering(), )