def test_satisfied(self): cases = [ NameAndValue( 'single satisfied string-selector restriction, unconditionally satisfied part', sut.OrReferenceRestrictions([ sut.OrRestrictionPart( WithStrRenderingType.STRING, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied())) ])), NameAndValue( 'multiple unconditionally satisfied restrictions', sut.OrReferenceRestrictions([ sut.OrRestrictionPart( WithStrRenderingType.STRING, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied())), sut.OrRestrictionPart( WithStrRenderingType.STRING, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied())) ])), ] for case in cases: with self.subTest(msg=case.name): actual = case.value.is_satisfied_by( *self._symbol_setup_with_indirectly_referenced_symbol()) self.assertIsNone(actual)
def test_that_only_direct_symbol_is_processed_when_restriction_on_indirect_ref_is_absent(self): # ARRANGE # restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) references = [] level_2_symbol = TestDataSymbolContext.of('level_2_symbol', references, WithStrRenderingType.STRING) references1 = [reference_to(level_2_symbol, restrictions_that_should_not_be_used)] level_1a_symbol = TestDataSymbolContext.of('level_1a_symbol', references1, WithStrRenderingType.STRING) references2 = [] level_1b_symbol = TestDataSymbolContext.of('level_1b_symbol', references2, WithStrRenderingType.STRING) references3 = [reference_to(level_1a_symbol, restrictions_that_should_not_be_used), reference_to(level_1b_symbol, restrictions_that_should_not_be_used)] level_0_symbol = TestDataSymbolContext.of('level_0_symbol', references3, WithStrRenderingType.STRING) symbol_table_entries = [level_0_symbol, level_1a_symbol, level_1b_symbol, level_2_symbol] symbol_table = SymbolContext.symbol_table_of_contexts(symbol_table_entries) restriction_that_registers_processed_symbols = RestrictionThatRegistersProcessedSymbols( symbol_container_2_result=unconditional_satisfaction) restrictions_to_test = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=restriction_that_registers_processed_symbols, ) # ACT # restrictions_to_test.is_satisfied_by(symbol_table, level_0_symbol.name, level_0_symbol.symbol_table_container) # ASSERT # actual_processed_symbols = dict(restriction_that_registers_processed_symbols.visited.items()) expected_processed_symbol = {level_0_symbol.name: 1} self.assertEqual(expected_processed_symbol, actual_processed_symbols)
def test_long_path_to_symbol_that_fails(self): # ARRANGE # restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) satisfied_value_type = WithStrRenderingType.STRING dissatisfied_value_type = WithStrRenderingType.PATH references = [] level_3_symbol = TestDataSymbolContext.of('level_3_symbol', references, dissatisfied_value_type) references1 = [reference_to(level_3_symbol, restrictions_that_should_not_be_used)] level_2_symbol = TestDataSymbolContext.of('level_2_symbol', references1, satisfied_value_type) references2 = [] level_1a_symbol = TestDataSymbolContext.of('level_1a_symbol', references2, satisfied_value_type) references3 = [reference_to(level_2_symbol, restrictions_that_should_not_be_used)] level_1b_symbol = TestDataSymbolContext.of('level_1b_symbol', references3, satisfied_value_type) references4 = [reference_to(level_1a_symbol, restrictions_that_should_not_be_used), reference_to(level_1b_symbol, restrictions_that_should_not_be_used)] level_0_symbol = TestDataSymbolContext.of('level_0_symbol', references4, satisfied_value_type) symbol_table_entries = [level_0_symbol, level_1a_symbol, level_1b_symbol, level_2_symbol, level_3_symbol] symbol_table = SymbolContext.symbol_table_of_contexts(symbol_table_entries) restriction_on_every_indirect = RestrictionThatRegistersProcessedSymbols( symbol_container_2_result=dissatisfaction_if_value_type_is( W_STR_RENDERING_TYPE_2_VALUE_TYPE[dissatisfied_value_type])) restrictions_to_test = sut.ReferenceRestrictionsOnDirectAndIndirect( indirect=restriction_on_every_indirect, direct=ValueRestrictionWithConstantResult.of_unconditionally_satisfied(), meaning_of_failure_of_indirect_reference= asrt_text_doc.new_single_string_text_for_test__optional('meaning of failure'), ) # ACT # actual_result = restrictions_to_test.is_satisfied_by(symbol_table, level_0_symbol.name, level_0_symbol.symbol_table_container) # ASSERT # expected_result = asrt_data_rest.is_failure__of_indirect_reference( failing_symbol=asrt.equals(level_3_symbol.name), path_to_failing_symbol=asrt.equals([level_1b_symbol.name, level_2_symbol.name]), meaning_of_failure=asrt_text_doc.is_string_for_test_that_equals('meaning of failure'), ) expected_result.apply_with_message(self, actual_result, 'result of processing') actual_processed_symbols = dict(restriction_on_every_indirect.visited.items()) expected_processed_symbol = { level_1a_symbol.name: 1, level_1b_symbol.name: 1, level_2_symbol.name: 1, level_3_symbol.name: 1, } self.assertEqual(expected_processed_symbol, actual_processed_symbols)
def _check_direct_with_satisfied_variants_for_restriction_on_every_node( self, restriction_on_direct_node: ValueRestriction, expected_result: Assertion[Optional[Failure]], ): symbol_to_check = StringConstantSymbolContext('symbol_name') restriction_on_every_cases = [ NameAndValue( 'restriction on every is None', None, ), NameAndValue( 'restriction on every is unconditionally satisfied', RestrictionThatIsAlwaysSatisfied(), ), ] for restriction_on_every_case in restriction_on_every_cases: with self.subTest(restriction_on_every_case.name): restrictions = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=restriction_on_direct_node, indirect=restriction_on_every_case.value ) actual_result = restrictions.is_satisfied_by(symbol_to_check.symbol_table, symbol_to_check.name, symbol_to_check.symbol_table_container) expected_result.apply_with_message(self, actual_result, 'return value')
def test_unconditionally_failing_restriction_on_indirect_referenced_symbol(self): # ARRANGE # restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) references = [] level_1a_symbol = TestDataSymbolContext.of('level_1a_symbol', references, WithStrRenderingType.STRING) references1 = [] level_1b_symbol = TestDataSymbolContext.of('level_1b_symbol', references1, WithStrRenderingType.STRING) references2 = [reference_to(level_1a_symbol, restrictions_that_should_not_be_used), reference_to(level_1b_symbol, restrictions_that_should_not_be_used)] level_0_symbol = TestDataSymbolContext.of('level_0_symbol', references2, WithStrRenderingType.STRING) symbol_table_entries = [level_0_symbol, level_1a_symbol, level_1b_symbol] symbol_table = SymbolContext.symbol_table_of_contexts(symbol_table_entries) result_that_indicates_error = 'result that indicates error' function_that_reports_error = unconditional_dissatisfaction(result_that_indicates_error) restriction_that_registers_processed_symbols = RestrictionThatRegistersProcessedSymbols( symbol_container_2_result=function_that_reports_error) restrictions_to_test = sut.ReferenceRestrictionsOnDirectAndIndirect( indirect=restriction_that_registers_processed_symbols, direct=ValueRestrictionWithConstantResult.of_unconditionally_satisfied(), meaning_of_failure_of_indirect_reference= asrt_text_doc.new_single_string_text_for_test__optional('meaning of failure'), ) # ACT # actual_result = restrictions_to_test.is_satisfied_by(symbol_table, level_0_symbol.name, level_0_symbol.symbol_table_container) # ASSERT # result_assertion = asrt_data_rest.is_failure__of_indirect_reference( failing_symbol=asrt.equals(level_1a_symbol.name), path_to_failing_symbol=asrt.equals([]), error_message=asrt_text_doc.is_string_for_test_that_equals(result_that_indicates_error), meaning_of_failure=asrt_text_doc.is_string_for_test_that_equals('meaning of failure') ) result_assertion.apply_with_message(self, actual_result, 'result of processing') actual_processed_symbols = dict(restriction_that_registers_processed_symbols.visited.items()) expected_processed_symbol = { level_1a_symbol.name: 1, } self.assertEqual(expected_processed_symbol, actual_processed_symbols)
def test_fail__different_name(self): # ARRANGE # actual = SymbolReference('actual value name', r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = symbol_usage_assertions.matches_reference( asrt.equals('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_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( assertion_on_restrictions=asrt.is_instance(r.OrReferenceRestrictions)) assert_that_assertion_fails(assertion, actual)
def test_fail__different_name(self): # ARRANGE # actual = SymbolReference('actual value name', r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = sut.matches_symbol_reference_with_restriction_on_direct_target('expected value name', asrt.is_instance( vr.ArbitraryValueWStrRenderingRestriction)) assert_that_assertion_fails(assertion, actual)
def test_fail__failing_assertion_on_value_restriction(self): # ARRANGE # common_name = 'actual value name' actual = SymbolReference(common_name, r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = sut.matches_symbol_reference_with_restriction_on_direct_target( common_name, asrt.is_instance(vr.PathAndRelativityRestriction)) assert_that_assertion_fails(assertion, actual)
def test_that_every_referenced_symbol_is_processed_once(self): # ARRANGE # references = [] level_2_symbol = TestDataSymbolContext.of('level_2_symbol', references, WithStrRenderingType.STRING) restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) references1 = [reference_to(level_2_symbol, restrictions_that_should_not_be_used)] level_1a_symbol = TestDataSymbolContext.of('level_1a_symbol', references1, WithStrRenderingType.STRING) references2 = [] level_1b_symbol = TestDataSymbolContext.of('level_1b_symbol', references2, WithStrRenderingType.STRING) references3 = [reference_to(level_1a_symbol, restrictions_that_should_not_be_used), reference_to(level_1b_symbol, restrictions_that_should_not_be_used)] level_0_symbol = TestDataSymbolContext.of('level_0_symbol', references3, WithStrRenderingType.STRING) symbol_table_entries = [level_0_symbol, level_1a_symbol, level_1b_symbol, level_2_symbol] symbol_table = SymbolContext.symbol_table_of_contexts(symbol_table_entries) restriction_that_registers_processed_symbols = RestrictionThatRegistersProcessedSymbols( symbol_container_2_result=unconditional_satisfaction) restrictions_to_test = sut.ReferenceRestrictionsOnDirectAndIndirect( indirect=restriction_that_registers_processed_symbols, direct=ValueRestrictionWithConstantResult.of_unconditionally_satisfied(), ) # ACT # container = level_0_symbol.symbol_table_container assert isinstance(container, SymbolContainer), 'Expects a SymbolContainer' actual_result = restrictions_to_test.is_satisfied_by(symbol_table, level_0_symbol.name, container) # ASSERT # result_that_indicates_success = None self.assertEqual(result_that_indicates_success, actual_result, 'result of processing') actual_processed_symbols = dict(restriction_that_registers_processed_symbols.visited.items()) expected_processed_symbol = { level_1a_symbol.name: 1, level_1b_symbol.name: 1, level_2_symbol.name: 1 } self.assertEqual(expected_processed_symbol, actual_processed_symbols)
def test_pass(self): # ARRANGE # symbol_name = 'value name' symbol_reference = SymbolReference(symbol_name, r.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) assertion = sut.matches_symbol_reference_with_restriction_on_direct_target(symbol_name, asrt.is_instance( vr.ArbitraryValueWStrRenderingRestriction)) # ACT & ASSERT # assertion.apply_without_message(self, symbol_reference)
def test_direct_and_indirect(self): # ARRANGE # expected_return_value = 72 visitor = _ReferenceRestrictionsVisitorThatRegisterClassOfVisitMethod( expected_return_value) # ACT # actual_return_value = visitor.visit( sut.ReferenceRestrictionsOnDirectAndIndirect( vr.ArbitraryValueWStrRenderingRestriction.of_any())) # ASSERT # self.assertEqual([sut.ReferenceRestrictionsOnDirectAndIndirect], visitor.visited_classes, 'visited classes') self.assertEqual(expected_return_value, actual_return_value, 'return value')
def _symbol_setup_with_indirectly_referenced_symbol(): references = [] referenced_symbol = TestDataSymbolContext.of( 'referenced_symbol', references, WithStrRenderingType.STRING) restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) references1 = [ reference_to(referenced_symbol, restrictions_that_should_not_be_used) ] referencing_symbol = TestDataSymbolContext.of( 'referencing_symbol', references1, WithStrRenderingType.STRING) symbol_table_entries = [referencing_symbol, referenced_symbol] symbol_table = SymbolContext.symbol_table_of_contexts( symbol_table_entries) return symbol_table, referencing_symbol.name, referencing_symbol.symbol_table_container,
def test_unsatisfied(self): def mk_err_msg(symbol_name: str, value_type: ValueType) -> str: return symbol_name + ': ' + 'Value type of tested symbol is ' + str( value_type) def value_type_error_message_function( symbol_name: str, container: SymbolContainer) -> TextRenderer: sdv = container.sdv assert isinstance(sdv, SymbolDependentValue) # Type info for IDE return asrt_text_doc.new_single_string_text_for_test( mk_err_msg(symbol_name, container.value_type)) references = [] referenced_symbol_cases = [ NameAndValue( 'data symbol', TestDataSymbolContext.of('referenced_data_symbol', references, WithStrRenderingType.STRING)), NameAndValue( 'logic symbol', StringTransformerSymbolContext.of_sdv( 'referenced_logic_symbol', StringTransformerSdvConstantTestImpl( string_transformers.arbitrary(), references=[]))), ] for referenced_symbol_case in referenced_symbol_cases: restrictions_that_should_not_be_used = sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionThatRaisesErrorIfApplied(), indirect=ValueRestrictionThatRaisesErrorIfApplied(), ) value_type_of_referencing_symbol = WithStrRenderingType.STRING value_type_other_than_referencing_symbol = WithStrRenderingType.PATH references1 = [ reference_to(referenced_symbol_case.value, restrictions_that_should_not_be_used) ] referencing_symbol = TestDataSymbolContext.of( 'referencing_symbol', references1, value_type_of_referencing_symbol) symbol_table_entries = [ referencing_symbol, referenced_symbol_case.value ] symbol_table = SymbolContext.symbol_table_of_contexts( symbol_table_entries) cases = [ NEA( 'no restriction parts / default error message generator', asrt_data_rest.is_failure__of_direct_reference(), sut.OrReferenceRestrictions([]), ), NEA( 'no restriction parts / custom error message generator', asrt_data_rest.is_failure__of_direct_reference( message=asrt_text_doc.is_string_for_test_that_equals( mk_err_msg( referencing_symbol.name, W_STR_RENDERING_TYPE_2_VALUE_TYPE[ value_type_of_referencing_symbol])), ), sut.OrReferenceRestrictions( [], value_type_error_message_function), ), NEA( 'single direct: unsatisfied selector', asrt_data_rest.is_failure__of_direct_reference(), sut.OrReferenceRestrictions([ sut.OrRestrictionPart( value_type_other_than_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied())), ]), ), NEA( 'single direct: satisfied selector, unsatisfied part-restriction', asrt_data_rest.is_failure__of_direct_reference(), sut.OrReferenceRestrictions([ sut.OrRestrictionPart( value_type_of_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))), ]), ), NEA( 'multiple direct: unconditionally unsatisfied selectors', asrt_data_rest.is_failure__of_direct_reference(), sut.OrReferenceRestrictions([ sut.OrRestrictionPart( value_type_other_than_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))), sut.OrRestrictionPart( value_type_other_than_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))) ]), ), NEA( 'multiple direct: unconditionally satisfied selectors, unconditionally satisfied restrictions', asrt_data_rest.is_failure__of_direct_reference(), sut.OrReferenceRestrictions([ sut.OrRestrictionPart( value_type_of_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))), sut.OrRestrictionPart( value_type_of_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))) ]), ), NEA( 'first: selector=satisfied, direct=satisfied, indirect=unsatisfied. second:satisfied ', asrt_data_rest.is_failure__of_indirect_reference( failing_symbol=asrt.equals( referenced_symbol_case.value.name), path_to_failing_symbol=asrt.equals([])), sut.OrReferenceRestrictions([ sut.OrRestrictionPart( value_type_of_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied(), indirect=ValueRestrictionWithConstantResult. of_err_msg_for_test('error message'))), sut.OrRestrictionPart( value_type_of_referencing_symbol, sut.ReferenceRestrictionsOnDirectAndIndirect( direct=ValueRestrictionWithConstantResult. of_unconditionally_satisfied())), ]), ), ] for case in cases: with self.subTest( referenced_symbol_case_name=referenced_symbol_case. name, msg=case.name): actual = case.actual.is_satisfied_by( symbol_table, referencing_symbol.name, referencing_symbol.symbol_table_container) case.expected.apply_with_message(self, actual, 'return value')