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)
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)
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'):
def test_value_ref__differs__symbol_name(self): # ARRANGE # path = arbitrary_path() expected = sdv_from_constants(path, [SymbolReference('expected_symbol_name', ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any()))]) actual = sdv_from_constants(path, [SymbolReference('actual_symbol_name', ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any()))]) assertion = sut.equals_path_sdv(expected) # ACT & ASSERT # assert_that_assertion_fails(assertion, actual)
def test(self): instruction_argument = src2( ValueType.PATH, 'ASSIGNED_NAME', '{rel_symbol} REFERENCED_SYMBOL component') for source in equivalent_source_variants__with_source_check__consume_last_line( self, instruction_argument): expected_path_sdv = path_sdvs.rel_symbol( SymbolReference( 'REFERENCED_SYMBOL', ReferenceRestrictionsOnDirectAndIndirect( PathAndRelativityRestriction( type_parser.REL_OPTIONS_CONFIGURATION. accepted_relativity_variants))), path_part_sdvs.from_constant_str('component')) expected_symbol_value = PathSymbolValueContext.of_sdv( expected_path_sdv) expected_symbol = PathSymbolContext('ASSIGNED_NAME', expected_symbol_value) INSTRUCTION_CHECKER.check( self, source, Arrangement.phase_agnostic(), Expectation.phase_agnostic( symbol_usages=asrt.matches_sequence( [expected_symbol.assert_matches_definition_of_sdv]), symbols_after_main=assert_symbol_table_is_singleton( expected_symbol.name, expected_symbol_value. assert_matches_container_of_sdv)))
def test_equals(self): test_cases = [ ( 'Plain string', string_sdvs.str_constant('string value'), empty_symbol_table(), ), ( 'String with reference', sdv_with_references([ SymbolReference( 'symbol_name', ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any())) ]), empty_symbol_table(), ), ] for test_case_name, string_value, symbol_table in test_cases: assert isinstance(string_value, StringSdv), 'Type info for IDE' with self.subTest(msg='{}::with checked references::{}'.format( sut.equals_string_sdv.__name__, test_case_name)): assertion = sut.equals_string_sdv(string_value) assertion.apply_with_message(self, string_value, test_case_name)
def test_value_ref__invalid_type_of_value_restriction(self): # ARRANGE # path = arbitrary_path() expected = sdv_from_constants(path, [SymbolReference('reffed-name', ReferenceRestrictionsOnDirectAndIndirect( _relativity_restriction( {RelOptionType.REL_ACT}, False)))]) actual = sdv_from_constants(path, [SymbolReference('reffed-name', ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any()))]) assertion = sut.equals_path_sdv(expected) # ACT & ASSERT # assert_that_assertion_fails(assertion, actual)
def _symbol_reference_of_path_with_accepted( value_name: str, accepted: RelOptionType) -> SymbolReference: return SymbolReference( value_name, ReferenceRestrictionsOnDirectAndIndirect( PathAndRelativityRestriction( _path_relativity_variants_with(accepted))))
def test_WHEN_referenced_symbol_is_in_symbol_table_and_satisfies_value_restriction_THEN_no_error(self): # ARRANGE # symbol_table = StringSymbolContext.of_constant('val_name', 'value string').symbol_table symbol_reference = SymbolReference('val_name', ReferenceRestrictionsOnDirectAndIndirect( RestrictionThatIsAlwaysSatisfied())) # ACT # actual = sut.validate_symbol_usage(symbol_reference, symbol_table) self.assertIsNone(actual, 'result should indicate success')
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 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)
def path_or_string_reference_restrictions(accepted_relativities: PathRelativityVariants ) -> WithStrRenderingTypeRestrictions: return OrReferenceRestrictions([ OrRestrictionPart( WithStrRenderingType.PATH, ReferenceRestrictionsOnDirectAndIndirect(PathAndRelativityRestriction(accepted_relativities))), OrRestrictionPart( WithStrRenderingType.STRING, reference_restrictions__string__w_all_indirect_refs_are_strings()), ])
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)
def test_value_ref__differs__restriction_relativity_variants(self): # ARRANGE # def restriction_with_relativity(relativity: RelOptionType) -> ValueRestriction: return _relativity_restriction({relativity}, False) path = arbitrary_path() expected = sdv_from_constants( path, [SymbolReference('reffed-name', ReferenceRestrictionsOnDirectAndIndirect( restriction_with_relativity(RelOptionType.REL_ACT)))]) actual = sdv_from_constants( path, [SymbolReference('reffed-name', ReferenceRestrictionsOnDirectAndIndirect( restriction_with_relativity(RelOptionType.REL_HDS_CASE)))]) assertion = sut.equals_path_sdv(expected) # ACT & ASSERT # assert_that_assertion_fails(assertion, actual)
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)
def test_WHEN_defined_symbol_not_in_symbol_table_but_referenced_symbols_not_in_table_THEN_validation_error(self): # ARRANGE # symbol_table = StringSymbolContext.of_constant('OTHER', 'value').symbol_table symbol = PathSymbolContext.of_sdv( 'UNDEFINED', path_sdvs.rel_symbol(SymbolReference('REFERENCED', ReferenceRestrictionsOnDirectAndIndirect( RestrictionThatIsAlwaysSatisfied())), path_part_sdvs.from_constant_str('file-name'))) # ACT # actual = sut.validate_symbol_usage(symbol.definition, symbol_table) self.assertIsNotNone(actual, 'return value for indicating error')
def test_differs__symbol_references(self): # ARRANGE # path = PathDdvTestImpl(RelOptionType.REL_ACT, path_ddvs.constant_path_part('file-name')) actual = PathSdvTestImplWithConstantPathAndSymbolReferences( path, [SymbolReference('symbol_name', ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any()))]) assertion = sut.matches_path_sdv(path, asrt.matches_sequence([]), empty_symbol_table()) # ACT & ASSERT # assert_that_assertion_fails(assertion, actual)
def test_WHEN_defined_symbol_not_in_symbol_table_and_referenced_symbol_is_in_table_and_satisfies_restriction_THEN_ok( self): # ARRANGE # referenced_symbol = StringSymbolContext.of_constant('REFERENCED', 'value') symbol_table = referenced_symbol.symbol_table symbol = PathSymbolContext.of_sdv('UNDEFINED', path_sdvs.rel_symbol( SymbolReference('REFERENCED', ReferenceRestrictionsOnDirectAndIndirect( RestrictionThatIsAlwaysSatisfied())), path_part_sdvs.from_constant_str('file-name'))) # ACT # actual = sut.validate_symbol_usage(symbol.definition, symbol_table) # ASSERT # self.assertIsNone(actual, 'return value for indicating success') self.assertTrue(symbol_table.contains('UNDEFINED'), 'definition should have been added')
def test(self): file_symbol = ConstantSuffixPathDdvSymbolContext( 'file_symbol', RelOptionType.REL_TMP, 'first_path_component') string_symbol = StringConstantSymbolContext('string_symbol', 'string symbol value') reference_of_relativity_symbol = SymbolReference( file_symbol.name, path_relativity_restriction( syntax_elements.EXE_FILE_REL_OPTION_ARG_CONF.options. accepted_relativity_variants)) reference_of_path_string_symbol_as_path_component = SymbolReference( string_symbol.name, ReferenceRestrictionsOnDirectAndIndirect( direct=value_restrictions.is_string(), indirect=value_restrictions.is_string()), ) symbols = SymbolContext.symbol_table_of_contexts([ file_symbol, string_symbol, ]) cases = [ Case.of( 'symbol references in file', RelSymbolPathAbsStx(file_symbol.name, string_symbol.name__sym_ref_syntax), arguments=(), expectation=ExpectationOnExeFile( path_ddv=path_ddvs.stacked( file_symbol.ddv, path_ddvs.constant_path_part(string_symbol.str_value)), expected_symbol_references=[ reference_of_relativity_symbol, reference_of_path_string_symbol_as_path_component ], symbol_for_value_checks=symbols, ), source_after_parse=asrt_source.is_at_end_of_line(1), ), ] for case in cases: with self.subTest(name=case.name): _parse_and_check(self, case)
def runTest(self): # ARRANGE # symbol_name = 'SYMBOL_NAME' string_restriction = value_restrictions.is_string() symbol_reference = SymbolReference( symbol_name, ReferenceRestrictionsOnDirectAndIndirect(string_restriction)) expected_symbol_usages = asrt.matches_sequence([ matches_symbol_reference_with_restriction_on_direct_target( symbol_name, asrt_val_rest.is__string()) ]) parts = instruction_parts.InstructionParts( SdvValidatorThat(), MainStepExecutorThat(), symbol_usages=(symbol_reference, )) parser = self.conf.instruction_parser_from_parts_parser( PartsParserThatGives(parts)) source = remaining_source('ignored') # ACT & ASSERT # self.conf.run_test_with_parser( self, parser, source, self.conf.arrangement(), self.conf.expect_success(symbol_usages=expected_symbol_usages))
def reference_restrictions__unconditionally_unsatisfied( ) -> ReferenceRestrictions: return ReferenceRestrictionsOnDirectAndIndirect( ValueRestrictionWithConstantResult.of_err_msg_for_test())
def reference_to__on_direct_and_indirect( name: str, value_restriction: ValueRestriction = ArbitraryValueWStrRenderingRestriction.of_any(), ) -> SymbolReference: return SymbolReference(name, ReferenceRestrictionsOnDirectAndIndirect(value_restriction))
def reference_restrictions_for_path_symbol( accepted_relativity_variants: PathRelativityVariants ) -> ReferenceRestrictions: return ReferenceRestrictionsOnDirectAndIndirect( PathAndRelativityRestriction(accepted_relativity_variants))
_NOT_EXISTS_PRE_SDS_RELATIVITY = RelOptionType.REL_ACT _PATH_SUFFIX_VARIANTS = [ path_ddvs.constant_path_part('file-name'), path_ddvs.empty_path_part(), ] _RELATIVITY_VARIANTS = [ _EXISTS_PRE_SDS_RELATIVITY, _NOT_EXISTS_PRE_SDS_RELATIVITY, ] _SYMBOL_REFERENCES = [ [], [SymbolReference('symbol_name', ReferenceRestrictionsOnDirectAndIndirect(ArbitraryValueWStrRenderingRestriction.of_any()))] ] class TestEqualsCommonToBothAssertionMethods(unittest.TestCase): def runTest(self): symbols = empty_symbol_table() for relativity in _RELATIVITY_VARIANTS: for path_suffix in _PATH_SUFFIX_VARIANTS: for symbol_references in _SYMBOL_REFERENCES: path = PathDdvTestImpl(relativity, path_suffix) path_sdv = PathSdvTestImplWithConstantPathAndSymbolReferences(path, symbol_references) test_case_descr = 'relativity:{}, path-suffix: {}'.format(relativity, type(path_suffix)) with self.subTest(msg=sut.equals_path_sdv.__name__ + ' :: ' + test_case_descr): assertion = sut.equals_path_sdv(path_sdv)
def reference_restrictions__string__w_all_indirect_refs_are_strings( ) -> ReferenceRestrictionsOnDirectAndIndirect: return ReferenceRestrictionsOnDirectAndIndirect( value_restrictions.is_string(), value_restrictions.is_string(), )
def reference_restrictions__unconditionally_satisfied( ) -> ReferenceRestrictions: return ReferenceRestrictionsOnDirectAndIndirect( ArbitraryValueWStrRenderingRestriction.of_any())
def no_restrictions() -> ReferenceRestrictions: return ReferenceRestrictionsOnDirectAndIndirect( direct=ArbitraryValueWStrRenderingRestriction.of_any(), indirect=None)
def path_reference_restrictions(accepted_relativities: PathRelativityVariants ) -> WithStrRenderingTypeRestrictions: return ReferenceRestrictionsOnDirectAndIndirect(PathAndRelativityRestriction(accepted_relativities))