def validation_corresponding_to__post_sds_as_hard_error(actual: ValidationActual) -> 'ExecutionExpectation': if actual.pre_sds is not None: return ExecutionExpectation( validation_pre_sds=svh_assertions.is_validation_error( asrt_text_doc.is_string_for_test_that_equals(actual.pre_sds), ) ) elif actual.post_sds is not None: return ExecutionExpectation( main_result=pfh_assertions.is_hard_error( asrt_text_doc.is_string_for_test_that_equals(actual.post_sds), ) ) else: return ExecutionExpectation()
def expect_failing_validation( self, actual: ValidationActual, symbol_usages: Assertion[ Sequence[SymbolUsage]] = asrt.is_empty_sequence, ): if actual.pre_sds is not None: return self.expect_failing_validation_pre_sds( asrt_text_doc.is_string_for_test_that_equals(actual.pre_sds), symbol_usages=symbol_usages, ) else: return self.expect_failing_validation_post_setup( asrt_text_doc.is_string_for_test_that_equals(actual.post_sds), symbol_usages=symbol_usages, )
def test_WHEN_a_validator_fails_pre_sds_THEN_validation_SHOULD_fail_pre_sds(self): # ARRANGE # the_error_message = 'the error message' assertion_part_with_successful_validation = PartForValidation(SdvValidatorThat()) assertion_part_with_unsuccessful_validation = PartForValidation(SdvValidatorThat( pre_sds_return_value=asrt_text_doc.new_single_string_text_for_test(the_error_message)) ) sequence_checker = sut.SequenceOfCooperativeAssertionParts([assertion_part_with_successful_validation, assertion_part_with_unsuccessful_validation]) validation_environment = self.environment.path_resolving_environment_pre_or_post_sds # ACT # actual = sequence_checker.validator.validate_pre_sds_if_applicable(validation_environment) # ASSERT # asrt_text_doc.is_string_for_test_that_equals(the_error_message).apply_without_message( self, actual, )
def expectation(self) -> Expectation: return Expectation( symbol_usages=asrt.matches_singleton_sequence( is_reference_to_files_matcher__usage( self.name_of_referenced_symbol)), main_result=asrt_pfh.is_hard_error( is_string_for_test_that_equals(self.error_message)), )
def test_unsatisfied_restriction(self): error_message = 'error message' expected_result = asrt_data_rest.is_failure__of_direct_reference( message=asrt_text_doc.is_string_for_test_that_equals(error_message) ) restriction_on_direct = ValueRestrictionWithConstantResult.of_err_msg_for_test(error_message) self._check_direct_with_satisfied_variants_for_restriction_on_every_node(restriction_on_direct, expected_result)
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_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 runTest(self): # ARRANGE # command_py_program = py_program.exit_with_code(0) py_file = fs.File( 'the-program.py', lines_content(command_py_program), ) program_wo_transformation = ProgramSymbolContext.of_sdv( 'PROGRAM_SYMBOL', program_sdvs.interpret_py_source_file_that_must_exist( path_sdvs.of_rel_option_with_const_file_name( RelOptionType.REL_HDS_CASE, py_file.name, ) ) ) error_message = 'error message from transformer' transformer = StringTransformerPrimitiveSymbolContext( 'HARD_ERROR_TRANSFORMER', string_transformers.model_access_raises_hard_error(error_message), ) source = args.program( args.symbol_ref_command_line(program_wo_transformation.name), transformation=transformer.name__sym_ref_syntax) symbols = [ program_wo_transformation, transformer, ] # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr(source.as_arguments.lines)], arrangement_w_tcds( symbol_table=SymbolContext.symbol_table_of_contexts(symbols), hds_contents=hds_populators.contents_in( RelHdsOptionType.REL_HDS_CASE, DirContents([py_file]), ) ), Expectation( symbol_usages=SymbolContext.references_assertion_of_contexts(symbols), execute=asrt_eh.matches_hard_error( asrt_failure_details.is_failure_message_matching__td( asrt_text_doc.is_string_for_test_that_equals(error_message) ) ), ), )
def failing_validation_cases__svh( ) -> Sequence[NEA[ValidationExpectationSvh, ValidationActual]]: err_msg_pre_sds = 'validation err msg/pre sds' err_msg_post_sds = 'validation err msg/post sds' return [ NEA( 'validation fails/pre sds', ValidationExpectationSvh.fails__pre_sds( asrt_text_doc.is_string_for_test_that_equals(err_msg_pre_sds)), ValidationActual.fails_pre_sds(err_msg_pre_sds), ), NEA( 'validation fails/post sds', ValidationExpectationSvh.fails__post_sds( asrt_text_doc.is_string_for_test_that_equals( err_msg_post_sds)), ValidationActual.fails_post_sds(err_msg_post_sds), ), ]
def test_validation_SHOULD_fail_WHEN_custom_validator_fails(self): the_instruction_environment = instruction_environment.fake_pre_sds_environment( ) resolved_value = 1 # ARRANGE # error_message_from_custom_validator = 'error message' sdv_to_check = sut.IntegerSdv( string_sdvs.str_constant(str(resolved_value)), CustomValidator( value_that_makes_the_validation_succeed=resolved_value + 1, error_message=validation.new_single_string_text_for_test( error_message_from_custom_validator))) with self.subTest(tested_obect='sdv'): # ACT & ASSERT # with self.assertRaises(SvhValidationException) as cm: sdv_to_check.validate_pre_sds( the_instruction_environment.path_resolving_environment) err_msg_expectation = asrt_text_doc.is_string_for_test_that_equals( error_message_from_custom_validator) err_msg_expectation.apply_without_message(self, cm.exception.err_msg) with self.subTest(tested_obect='value'): # ACT & ASSERT # value_to_check = sdv_to_check.resolve( the_instruction_environment.symbols) actual = value_to_check.validator().validate_pre_sds_if_applicable( the_instruction_environment.hds) err_msg_expectation = asrt_text_doc.is_string_for_test_that_equals( error_message_from_custom_validator) err_msg_expectation.apply_without_message(self, actual)
def execution_cases( self ) -> Sequence[NExArr[ExecutionExpectation, ArrangementPostAct2]]: return [ NExArr( 'pre sds validation', ExecutionExpectation( validation_pre_sds=asrt_svh.is_validation_error( asrt_text_doc.is_string_for_test_that_equals( self.err_msg_from_validator)), ), self._arrangement( DdvValidatorThat(pre_sds_return_value=asrt_text_doc. new_single_string_text_for_test( self.err_msg_from_validator)))), NExArr( 'post sds validation', ExecutionExpectation(main_result=asrt_pfh.is_hard_error( asrt_text_doc.is_string_for_test_that_equals( self.err_msg_from_validator)), ), self._arrangement( DdvValidatorThat(post_setup_return_value=asrt_text_doc. new_single_string_text_for_test( self.err_msg_from_validator)))), ]
def runTest(self): # ARRANGE # the_error_message = 'the error message' parts = instruction_parts.InstructionParts( SdvValidatorThat( pre_sds_return_value=asrt_text_doc. new_single_string_text_for_test(the_error_message)), MainStepExecutorThat()) 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_failing_validation_pre_sds( error_message=asrt_text_doc.is_string_for_test_that_equals( the_error_message)))
def runTest(self): # ARRANGE # program_symbol = ProgramSymbolContext.of_arbitrary_value('PROGRAM_SYMBOL') const_true_int_matcher_symbol = IntegerMatcherSymbolContext.of_primitive_constant( 'CONST_TRUE_INT_MATCHER', result=True, ) all_symbols = [program_symbol, const_true_int_matcher_symbol, ] hard_error_message = 'the err msg' # ACT & ASSERT # CHECKER.check__abs_stx( self, InstructionArguments( program_symbol.abstract_syntax, const_true_int_matcher_symbol.abstract_syntax, ), ArrangementPostAct2( symbols=SymbolContext.symbol_table_of_contexts(all_symbols), process_execution=ProcessExecutionArrangement( os_services_access.new_for_cmd_exe( CommandExecutorThatRaisesHardError( asrt_text_doc.new_single_string_text_for_test(hard_error_message) ), ), ), ), Expectation2( ParseExpectation( symbol_usages=SymbolContext.usages_assertion_of_contexts(all_symbols), ), ExecutionExpectation( main_result=asrt_pfh.is_hard_error( asrt_text_doc.is_string_for_test_that_equals(hard_error_message) ) ), ) )
def runTest(self): error_message = 'the error message' matcher_that_raises_hard_error = StringMatcherSymbolContext.of_primitive( 'STRING_MATCHER', matchers.MatcherThatReportsHardError(error_message) ) self.configuration.checker.check__abs_stx__source_variants( self, self.configuration.syntax_for_matcher(matcher_that_raises_hard_error.abstract_syntax), self.configuration.arrangement_for_contents( '', post_sds_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY, symbols=matcher_that_raises_hard_error.symbol_table, ).as_arrangement_2(), MultiSourceExpectation( symbol_usages=matcher_that_raises_hard_error.usages_assertion, execution=ExecutionExpectation( main_result=pfh_assertions.is_hard_error( asrt_text_doc.is_string_for_test_that_equals(error_message) ), ) ), )
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')
def _svh_from_actual_for_test_err_msg( actual: Optional[str] ) -> Assertion[svh.SuccessOrValidationErrorOrHardError]: return (asrt_svh.is_success() if actual is None else asrt_svh.is_validation_error( asrt_text_doc.is_string_for_test_that_equals(actual)))