def test_symbol_references(self): file_to_interpret = fs.File('python-program.py', python_program_that_exits_with_code_given_as_first_cl_arg) file_to_interpret_symbol = NameAndValue('file_to_interpret_symbol', file_to_interpret.file_name) python_interpreter_symbol = NameAndValue('python_interpreter_symbol', sys.executable) exit_code_symbol = NameAndValue('exit_code_symbol', 72) argument = ' {python_interpreter} {interpret_option} {file_to_interpret} "{exit_code}"'.format( python_interpreter=symbol_reference_syntax_for_name(python_interpreter_symbol.name), interpret_option=args.option(syntax_elements.EXISTING_FILE_OPTION_NAME).as_str, file_to_interpret=symbol_reference_syntax_for_name(file_to_interpret_symbol.name), exit_code=symbol_reference_syntax_for_name(str(exit_code_symbol.name)), ) following_line = 'following line' source = remaining_source(argument, [following_line]) arrangement = ArrangementWithSds( home_or_sds_contents=HomeOrSdsPopulatorForRelOptionType( parse_file_ref.ALL_REL_OPTIONS_CONFIG.options.default_option, fs.DirContents([file_to_interpret])), symbols=SymbolTable({ python_interpreter_symbol.name: su.string_constant_container(python_interpreter_symbol.value), file_to_interpret_symbol.name: su.string_constant_container(file_to_interpret_symbol.value), exit_code_symbol.name: su.string_constant_container(str(exit_code_symbol.value)), }), ) expectation = embryo_check.Expectation( source=assert_source(current_line_number=asrt.equals(2), column_index=asrt.equals(0)), symbol_usages=asrt.matches_sequence([ matches_reference_2( python_interpreter_symbol.name, equals_data_type_reference_restrictions( parse_file_ref.path_or_string_reference_restrictions( syntax_elements.REL_OPTION_ARG_CONF.options.accepted_relativity_variants ))), matches_reference_2( file_to_interpret_symbol.name, equals_data_type_reference_restrictions( parse_file_ref.path_or_string_reference_restrictions( parse_file_ref.ALL_REL_OPTIONS_CONFIG.options.accepted_relativity_variants ))), matches_reference_2( exit_code_symbol.name, equals_data_type_reference_restrictions(is_any_data_type() )), ]), main_result=spr_check.is_success_result(exit_code_symbol.value, ''), ) parser = sut.embryo_parser('instruction-name') embryo_check.check(self, parser, source, arrangement, expectation)
def test_set_value_SHOULD_be_able_to_have_symbol_references_in_the_right_hand_side(self): variable_name = 'variable_to_assign' my_symbol = NameAndValue('my_symbol', 'my symbol value') your_symbol = NameAndValue('your_symbol', 'your symbol value') value_template = 'pre {MY_SYMBOL} {YOUR_SYMBOL} post' expected_evaluated_value_string = value_template.format( MY_SYMBOL=my_symbol.value, YOUR_SYMBOL=your_symbol.value, ) expected_environ_after_main = { variable_name: expected_evaluated_value_string, } value_source_string = value_template.format( MY_SYMBOL=symbol_reference_syntax_for_name(my_symbol.name), YOUR_SYMBOL=symbol_reference_syntax_for_name(your_symbol.name), ) source_line = ' {variable_name} = {soft_quote}{source_value_string}{soft_quote}'.format( variable_name=variable_name, source_value_string=value_source_string, soft_quote=SOFT_QUOTE_CHAR, ) following_line = 'following line' source = remaining_source(source_line, [following_line]) arrangement = ArrangementWithSds( symbols=SymbolTable({ my_symbol.name: su.string_constant_container(my_symbol.value), your_symbol.name: su.string_constant_container(your_symbol.value), }), ) expectation = embryo_check.Expectation( main_side_effect_on_environment_variables=asrt.equals(expected_environ_after_main), symbol_usages=asrt.matches_sequence([ matches_reference_2( my_symbol.name, is_any_data_type_reference_restrictions()), matches_reference_2( your_symbol.name, is_any_data_type_reference_restrictions()), ]), source=assert_source(current_line_number=asrt.equals(2), column_index=asrt.equals(0)), ) parser = sut.EmbryoParser() embryo_check.check(self, parser, source, arrangement, expectation)
def test_symbol_references(self): python_interpreter_symbol = NameAndValue('python_interpreter_symbol', sys.executable) execute_program_option_symbol = NameAndValue('execute_program_option', '-c') exit_code_symbol = NameAndValue('exit_code_symbol', 87) argument = ' {python_interpreter} {execute_program_option} {source_option} exit({exit_code}) '.format( python_interpreter=symbol_reference_syntax_for_name(python_interpreter_symbol.name), execute_program_option=symbol_reference_syntax_for_name(execute_program_option_symbol.name), source_option=syntax_elements.REMAINING_PART_OF_CURRENT_LINE_AS_LITERAL_MARKER, exit_code=symbol_reference_syntax_for_name(str(exit_code_symbol.name)), ) arrangement = ArrangementWithSds( symbols=SymbolTable({ python_interpreter_symbol.name: su.string_constant_container(python_interpreter_symbol.value), execute_program_option_symbol.name: su.string_constant_container( execute_program_option_symbol.value), exit_code_symbol.name: su.string_constant_container(str(exit_code_symbol.value)), }), ) source = remaining_source(argument, ['following line']) expectation = embryo_check.Expectation( source=assert_source(current_line_number=asrt.equals(2), column_index=asrt.equals(0)), symbol_usages=asrt.matches_sequence([ matches_reference_2( python_interpreter_symbol.name, equals_data_type_reference_restrictions( parse_file_ref.path_or_string_reference_restrictions( syntax_elements.REL_OPTION_ARG_CONF.options.accepted_relativity_variants ))), matches_reference_2( execute_program_option_symbol.name, equals_data_type_reference_restrictions( is_any_data_type() )), matches_reference_2( exit_code_symbol.name, equals_data_type_reference_restrictions( is_any_data_type() )), ]), main_result=spr_check.is_success_result(exit_code_symbol.value, ''), ) parser = sut.embryo_parser('instruction-name') embryo_check.check(self, parser, source, arrangement, expectation)
def test_symbol_references(self): # ARRANGE # expected_restriction = FileRefRelativityRestriction( PathRelativityVariants({RelOptionType.REL_ACT, RelOptionType.REL_HOME_CASE}, True)) symbol_name_of_rel_path = 'symbol_name_of_rel_path' symbol_name_of_path_suffix = 'symbol_name_of_path_suffix' restrictions_on_path_suffix_symbol = restrictions.ReferenceRestrictionsOnDirectAndIndirect( AnyDataTypeRestriction()) expected_mandatory_references = [ vr_tr.equals_symbol_reference_with_restriction_on_direct_target( symbol_name_of_rel_path, restrictions.equals_file_ref_relativity_restriction(expected_restriction)) ] symbol_ref_of_path = SymbolReference(symbol_name_of_rel_path, ReferenceRestrictionsOnDirectAndIndirect(expected_restriction)) path_suffix_test_cases = [ (path_part_resolvers.from_constant_str('file.txt'), [], ), (path_part_resolvers.from_string( string_resolver_of_single_symbol_reference(symbol_name_of_path_suffix, restrictions_on_path_suffix_symbol)), [asrt_sym_usage.matches_reference_2( symbol_name_of_path_suffix, vr_tr.matches_restrictions_on_direct_and_indirect())], ), ] for path_suffix_resolver, additional_expected_references in path_suffix_test_cases: file_ref_resolver = sut.FileRefResolverRelSymbol(path_suffix_resolver, symbol_ref_of_path) # ACT # actual = file_ref_resolver.references # ASSERT # expected_references = expected_mandatory_references + additional_expected_references assertion = asrt.matches_sequence(expected_references) assertion.apply_with_message(self, actual, 'symbol references')
def test_fail__different_name(self): # ARRANGE # actual = SymbolReference('actual value name', r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = symbol_usage_assertions.matches_reference_2( 'expected value name', asrt.anything_goes()) assert_that_assertion_fails(assertion, actual)
def test_pass_with_default_assertion_on_restrictions(self): # ARRANGE # symbol_name = 'symbol name' symbol_reference = SymbolReference(symbol_name, r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = symbol_usage_assertions.matches_reference_2(symbol_name) # ACT & ASSERT # assertion.apply_without_message(self, symbol_reference)
def test_symbol_references(self): # ARRANGE # symbol1 = NameAndValue('symbol_1_name', 'symbol 1 value') symbol1_ref = self._symbol_reference(symbol1.name) symbol2 = NameAndValue('symbol_2_name', 'symbol 2 value') symbol2_ref = self._symbol_reference(symbol2.name) fragments = [string_resolvers.symbol_fragment(symbol1_ref), string_resolvers.symbol_fragment(symbol2_ref)] resolver = sut.StringResolver(tuple(fragments)) path_part = sut.PathPartResolverAsStringResolver(resolver) # ACT # actual = path_part.references # ASSERT # assertion = asrt.matches_sequence([ matches_reference_2(symbol1.name), matches_reference_2(symbol2.name), ]) assertion.apply_with_message(self, actual, 'symbol references')
def test_fail__failing_assertion_on_value_restriction(self): # ARRANGE # actual_symbol_name = 'actual value name' actual = SymbolReference(actual_symbol_name, r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = symbol_usage_assertions.matches_reference_2( actual_symbol_name, asrt.is_instance(r.OrReferenceRestrictions)) assert_that_assertion_fails(assertion, actual)
def test_symbol_references(self): # ARRANGE # expected_restriction = PathAndRelativityRestriction( PathRelativityVariants( {RelOptionType.REL_ACT, RelOptionType.REL_HDS_CASE}, True)) symbol_name_of_rel_path = 'symbol_name_of_rel_path' symbol_name_of_path_suffix = 'symbol_name_of_path_suffix' restrictions_on_path_suffix_symbol = ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any()) expected_mandatory_references = [ asrt_sym_ref. matches_symbol_reference_with_restriction_on_direct_target( symbol_name_of_rel_path, asrt_val_rest.equals__path_w_relativity(expected_restriction)) ] symbol_ref_of_path = SymbolReference( symbol_name_of_rel_path, ReferenceRestrictionsOnDirectAndIndirect(expected_restriction)) path_suffix_test_cases = [ ( path_part_sdvs.from_constant_str('file.txt'), [], ), ( path_part_sdvs.from_string( string_sdv_of_single_symbol_reference( symbol_name_of_path_suffix, restrictions_on_path_suffix_symbol)), [ asrt_sym_usage.matches_reference_2( symbol_name_of_path_suffix, asrt_w_str_rend_rest.matches__on_direct_and_indirect()) ], ), ] for path_suffix_sdv, additional_expected_references in path_suffix_test_cases: path_sdv = sut.PathSdvRelSymbol(path_suffix_sdv, symbol_ref_of_path) # ACT # actual = path_sdv.references # ASSERT # expected_references = expected_mandatory_references + additional_expected_references assertion = asrt.matches_sequence(expected_references) assertion.apply_with_message(self, actual, 'symbol references')
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())
def matches_reference( expected: NameAndValue[Assertion[ReferenceRestrictions]] ) -> Assertion[SymbolUsage]: return matches_reference_2(expected.name, expected.value)
def matches_reference(expected: NameAndValue[ValueAssertion[ReferenceRestrictions]] ) -> ValueAssertion[SymbolUsage]: return matches_reference_2(expected.name, expected.value)