def test_action(what_to_do: int, message: str):
    if what_to_do == DO_HARD_ERROR:
        raise sut.PfhHardErrorException(asrt_text_doc.new_single_string_text_for_test(message))
    if what_to_do == DO_FAIL:
        raise sut.PfhFailException(asrt_text_doc.new_single_string_text_for_test(message))
    if what_to_do != DO_SUCCESS:
        raise ValueError('unexpected what_to_do: ' + str(what_to_do))
Exemplo n.º 2
0
    def new_w_defaults_of_hard_error(
        may_depend_on_external_resources: bool,
        as_str: Callable[[], str] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        as_lines: Callable[[], Iterator[str]] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        as_file: Callable[[DirFileSpace], Path] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
        write_to: Callable[[TextIO], None] = do_raise(
            HardErrorException(
                asrt_text_doc.new_single_string_text_for_test(
                    'should not be called'))),
    ) -> 'StringSourceContentsData':
        def _may_depend_on_external_resources() -> bool:
            return may_depend_on_external_resources

        return StringSourceContentsData(
            may_depend_on_external_resources=_may_depend_on_external_resources,
            as_str=as_str,
            as_lines=as_lines,
            as_file=as_file,
            write_to=write_to,
        )
def _new_em(message: str,
            how_to_fix: Optional[str] = None) -> ErrorMessageWithFixTip:
    return ErrorMessageWithFixTip(
        asrt_text_doc.new_single_string_text_for_test(message),
        (
            None
            if how_to_fix is None
            else asrt_text_doc.new_single_string_text_for_test(how_to_fix)
        )
    )
Exemplo n.º 4
0
    def matches_w_trace(self, model: FileMatcherModel) -> MatchingResult:
        if not isinstance(model, FileMatcherModel):
            raise HardErrorException(
                text_doc_assertions.new_single_string_text_for_test(
                    'Model is not a {}: {}'.format(type(FileMatcherModel),
                                                   model)))

        if not model.file_type_access.is_type(FileType.DIRECTORY):
            raise HardErrorException(
                text_doc_assertions.new_single_string_text_for_test(
                    'Test failure: File is not a directory'))

        return matching_result.of(True)
Exemplo n.º 5
0
    def test_WHEN_given_validator_fails_post_setup_THEN_main_SHOULD_report_hard_error(self):
        # ARRANGE #
        environment = fake_post_sds_environment()

        the_error_message = 'the error message'
        assertion_part = PartForValidation(SdvValidatorThat(
            post_setup_return_value=asrt_text_doc.new_single_string_text_for_test(the_error_message))
        )
        instruction = sut.AssertionInstructionFromAssertionPart(assertion_part,
                                                                lambda env: 'argument to assertion_part')
        is_validation_success = asrt_svh.is_success()
        main_result_is_hard_error = asrt_pfh.is_hard_error(
            asrt_text_doc.is_single_pre_formatted_text_that_equals(the_error_message)
        )

        # ACT & ASSERT #

        pre_sds_result = instruction.validate_pre_sds(environment)
        is_validation_success.apply_with_message(self, pre_sds_result,
                                                 'pre sds validation should succeed')
        post_sds_result = instruction.validate_post_setup(environment)
        is_validation_success.apply_with_message(self, post_sds_result,
                                                 'post setup validation should succeed')
        main_result = instruction.main(environment,
                                       optionally_from_proc_exe_settings(None, environment.proc_exe_settings),
                                       self.the_os_services)

        main_result_is_hard_error.apply_with_message(self,
                                                     main_result,
                                                     'main should fail')
Exemplo n.º 6
0
 def test_middle_instruction_fails(self):
     recording_media = RecordingMedia()
     instruction_executor = InstructionExecutorThatRecordsInstructionNameAndFailsFor(
         instruction_with_name('Middle instruction'),
         recording_media.new_recorder_with_header('instruction executor'),
         PartialInstructionControlledFailureInfo(
             PartialControlledFailureEnum.FAIL,
             asrt_text_doc.new_single_string_text_for_test('fail message')))
     phase_contents = SectionContents((
         new_instruction_element(Line(1, '1'),
                                 TestInstruction('First instruction')),
         new_instruction_element(Line(2, '2'),
                                 TestInstruction('Middle instruction')),
         new_instruction_element(Line(3, '3'),
                                 TestInstruction('Last instruction')),
     ))
     self._standard_test(
         recording_media, phase_contents, instruction_executor,
         asrt_failure.is_present_with(
             ExecutionFailureStatus.FAIL,
             equals_single_line_source_location_path(Line(2, '2')),
             asrt_failure_details.is_failure_message_of('fail message')), [
                 'instruction header for source line number: 1',
                 'instruction executor: First instruction',
                 'instruction header for source line number: 2',
                 'instruction executor: Middle instruction',
             ])
Exemplo n.º 7
0
 def check(self,
           environment: InstructionEnvironmentForPostSdsStep,
           os_services: OsServices,
           arg: int):
     if arg == 1:
         raise pfh_exception.PfhFailException(asrt_text_doc.new_single_string_text_for_test(self.ERROR_MESSAGE))
     else:
         return arg + 1
Exemplo n.º 8
0
 def test_fail_due_to_unexpected_hard_error_exception(self):
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(instruction_embryo_that__phase_agnostic(
             main=do_raise(HardErrorException(new_single_string_text_for_test('hard error message'))))
         ),
         Arrangement.phase_agnostic(),
         MultiSourceExpectation.phase_agnostic(
             main_result=asrt.anything_goes()
         ),
     )
Exemplo n.º 9
0
 def test_succeed_due_to_expected_hard_error_exception(self):
     self._check_source_and_exe_variants(
         ParserThatGives(instruction_embryo_that__phase_agnostic(
             main=do_raise(HardErrorException(new_single_string_text_for_test('hard error message'))))
         ),
         Arrangement.phase_agnostic(),
         MultiSourceExpectation.phase_agnostic(
             main_raises_hard_error=True
         ),
     )
Exemplo n.º 10
0
 def is_satisfied_by(self,
                     symbol_table: SymbolTable,
                     symbol_name: str,
                     value: SymbolContainer) -> Optional[ErrorMessageWithFixTip]:
     self.visited.update([symbol_name])
     error_message = self.symbol_container_2_result(value)
     return (
         ErrorMessageWithFixTip(asrt_text_doc.new_single_string_text_for_test(error_message))
         if error_message
         else None)
Exemplo n.º 11
0
 def test_success_WHEN_actual_is_expected(self):
     # ARRANGE #
     expected_err_msg = 'expected error message'
     cases = [
         NEA(
             'fail',
             sut.failure_message_is(
                 asrt_text_doc.is_string_for_test(
                     asrt.equals(expected_err_msg))),
             pfh.new_pfh_fail(
                 asrt_text_doc.new_single_string_text_for_test(
                     expected_err_msg)),
         ),
         NEA(
             'fail/const msg',
             sut.failure_message_is(
                 asrt_text_doc.is_string_for_test(
                     asrt.equals(expected_err_msg))),
             pfh.new_pfh_fail__str(expected_err_msg),
         ),
         NEA(
             'hard error',
             sut.failure_message_is(
                 asrt_text_doc.is_string_for_test(
                     asrt.equals(expected_err_msg))),
             pfh.new_pfh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     expected_err_msg)),
         ),
         NEA(
             'hard error/const msg',
             sut.failure_message_is(
                 asrt_text_doc.is_string_for_test(
                     asrt.equals(expected_err_msg))),
             pfh.new_pfh_hard_error__str(expected_err_msg),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT & ASSERT #
             case.expected.apply_without_message(self, case.actual)
Exemplo n.º 12
0
 def test_success_WHEN_actual_is_expected(self):
     # ARRANGE #
     cases = [
         NEA(
             'SUCCESS',
             svh.SuccessOrValidationErrorOrHardErrorEnum.SUCCESS,
             svh.new_svh_success(),
         ),
         NEA(
             'VALIDATION_ERROR',
             svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR,
             svh.new_svh_validation_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'validation msg')),
         ),
         NEA(
             'VALIDATION_ERROR/const msg',
             svh.SuccessOrValidationErrorOrHardErrorEnum.VALIDATION_ERROR,
             svh.new_svh_validation_error__str('validation msg'),
         ),
         NEA(
             'HARD_ERROR',
             svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
             svh.new_svh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard err msg')),
         ),
         NEA(
             'HARD_ERROR/const msg',
             svh.SuccessOrValidationErrorOrHardErrorEnum.HARD_ERROR,
             svh.new_svh_hard_error__str('hard err msg'),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             assertion = sut.status_is(case.expected)
             # ACT & ASSERT #
             assertion.apply_without_message(self, case.actual)
Exemplo n.º 13
0
 def test_success_WHEN_actual_is_not_pass(self):
     # ARRANGE #
     cases = [
         NameAndValue(
             'fail',
             pfh.new_pfh_fail(
                 asrt_text_doc.new_single_string_text_for_test(
                     'failure msg'))),
         NameAndValue('fail/const msg',
                      pfh.new_pfh_fail__str('failure msg')),
         NameAndValue(
             'hard error',
             pfh.new_pfh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard error msg'))),
         NameAndValue('hard error/const msg',
                      pfh.new_pfh_hard_error__str('hard error msg')),
     ]
     assertion = sut.is_pass()
     for case in cases:
         with self.subTest(case.name):
             # ACT #
             assert_that_assertion_fails(assertion, case.value)
Exemplo n.º 14
0
 def test_success_WHEN_actual_is_not_success(self):
     # ARRANGE #
     cases = [
         NameAndValue(
             'VALIDATION_ERROR',
             svh.new_svh_validation_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'failure msg'))),
         NameAndValue('VALIDATION_ERROR/const msg',
                      svh.new_svh_validation_error__str('failure msg')),
         NameAndValue(
             'HARD_ERROR',
             svh.new_svh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard error msg'))),
         NameAndValue('HARD_ERROR/const msg',
                      svh.new_svh_hard_error__str('hard error msg')),
     ]
     assertion = sut.is_success()
     for case in cases:
         with self.subTest(case.name):
             # ACT #
             assert_that_assertion_fails(assertion, case.value)
Exemplo n.º 15
0
 def test_success_WHEN_actual_is_expected(self):
     # ARRANGE #
     cases = [
         NEA(
             'pass',
             sut.status_is(pfh.PassOrFailOrHardErrorEnum.PASS),
             pfh.new_pfh_pass(),
         ),
         NEA(
             'fail',
             sut.status_is(pfh.PassOrFailOrHardErrorEnum.FAIL),
             pfh.new_pfh_fail(
                 asrt_text_doc.new_single_string_text_for_test('fail msg')),
         ),
         NEA(
             'fail/const msg',
             sut.status_is(pfh.PassOrFailOrHardErrorEnum.FAIL),
             pfh.new_pfh_fail__str('fail msg'),
         ),
         NEA(
             'hard error',
             sut.status_is(pfh.PassOrFailOrHardErrorEnum.HARD_ERROR),
             pfh.new_pfh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     'hard err msg')),
         ),
         NEA(
             'hard error/const msg',
             sut.status_is(pfh.PassOrFailOrHardErrorEnum.HARD_ERROR),
             pfh.new_pfh_hard_error__str('hard err msg'),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT & ASSERT #
             case.expected.apply_without_message(self, case.actual)
Exemplo n.º 16
0
    def test_WHEN_given_validator_fails_pre_sds_THEN_validation_SHOULD_fail_pre_sds(self):
        # ARRANGE #
        environment = fake_post_sds_environment()

        the_error_message = 'the error message'
        assertion_part = PartForValidation(SdvValidatorThat(
            pre_sds_return_value=asrt_text_doc.new_single_string_text_for_test(the_error_message))
        )
        instruction = sut.AssertionInstructionFromAssertionPart(assertion_part,
                                                                lambda env: 'argument to assertion_part')
        # ACT #
        actual = instruction.validate_pre_sds(environment)
        # ASSERT #
        assertion = asrt_svh.is_validation_error(
            asrt_text_doc.is_single_pre_formatted_text_that_equals(the_error_message)
        )
        assertion.apply_without_message(self, actual)
Exemplo n.º 17
0
 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,
     )
Exemplo n.º 18
0
 def runTest(self):
     CHECKER.check__w_source_variants(
         self,
         command_line(
             'program',
         ).as_str,
         Arrangement.phase_agnostic(
             os_services=os_services_access.new_for_cmd_exe(
                 CommandExecutorThatRaisesHardError(
                     asrt_text_doc.new_single_string_text_for_test('the error message')
                 ),
             ),
             tcds=TcdsArrangement(),
         ),
         MultiSourceExpectation.phase_agnostic(
             main_raises_hard_error=True
         ),
     )
Exemplo n.º 19
0
 def test_pass__value_from_td(self):
     message_str = 'error message'
     the_error_message = asrt_text_doc.new_single_string_text_for_test(message_str)
     cases = [
         NEA('no assertion on error message',
             asrt.anything_goes(),
             sh.new_sh_hard_error(the_error_message),
             ),
         NEA('assertion on error message',
             asrt_text_doc.is_single_pre_formatted_text_that_equals(message_str),
             sh.new_sh_hard_error(the_error_message),
             ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ARRANGE #
             assertion = sut.is_hard_error(case.expected)
             # ACT #
             assertion.apply_without_message(self, case.actual)
Exemplo n.º 20
0
 def test_success_WHEN_actual_is_expected(self):
     # ARRANGE #
     assertion = sut.is_hard_error__with_arbitrary_message()
     expected_err_msg = 'expected error message'
     cases = [
         NameAndValue(
             'hard error',
             pfh.new_pfh_hard_error(
                 asrt_text_doc.new_single_string_text_for_test(
                     expected_err_msg)),
         ),
         NameAndValue(
             'hard error/const msg',
             pfh.new_pfh_hard_error__str(expected_err_msg),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT & ASSERT #
             assertion.apply_without_message(self, case.value)
Exemplo n.º 21
0
 def test_fail__value_from_td(self):
     message_str = 'error message'
     the_error_message = asrt_text_doc.new_single_string_text_for_test(message_str)
     cases = [
         NEA('is success',
             asrt.anything_goes(),
             sh.new_sh_success(),
             ),
         NEA('assertion on error message fails',
             asrt_text_doc.is_single_pre_formatted_text_that_equals(
                 message_str + ' - part of message not in actual'
             ),
             sh.new_sh_hard_error(the_error_message),
             ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ARRANGE #
             assertion = sut.is_hard_error(case.expected)
             # ACT #
             assert_that_assertion_fails(assertion, case.actual)
Exemplo n.º 22
0
 def runTest(self):
     hard_error_message = 'the err msg'
     with self.config.program_that_exits_with_code(0) as setup:
         self._check(
             setup.act_phase_instructions,
             arrangement_w_tcds(
                 symbol_table=setup.symbols,
                 hds_contents=_hds_pop_of(setup),
                 process_execution=ProcessExecutionArrangement(
                     os_services=os_services_access.new_for_cmd_exe(
                         CommandExecutorThatRaisesHardError(
                             asrt_text_doc.new_single_string_text_for_test(
                                 hard_error_message))),
                     process_execution_settings=proc_exe_env_for_test(
                         environ=setup.environ),
                 )),
             expectation=Expectation.hard_error_from_execute(
                 symbol_usages=setup.symbol_usages,
                 error_message=asrt_text_doc.
                 is_single_pre_formatted_text_that_equals(
                     hard_error_message),
             ))
Exemplo n.º 23
0
 def test_single_failing_instruction_executor__status_hard_error(self):
     recording_media = RecordingMedia()
     instruction_executor = InstructionExecutorThatRecordsInstructionNameAndFailsFor(
         any_instruction,
         recording_media.new_recorder_with_header('instruction executor'),
         PartialInstructionControlledFailureInfo(
             PartialControlledFailureEnum.HARD_ERROR,
             asrt_text_doc.new_single_string_text_for_test(
                 'hard error message')))
     phase_contents = SectionContents(
         (new_instruction_element(Line(1, '1'),
                                  TestInstruction('The instruction')), ))
     self._standard_test(
         recording_media, phase_contents, instruction_executor,
         asrt_failure.is_present_with(
             ExecutionFailureStatus.HARD_ERROR,
             equals_single_line_source_location_path(Line(1, '1')),
             asrt_failure_details.is_failure_message_of(
                 'hard error message')), [
                     'instruction header for source line number: 1',
                     'instruction executor: The instruction'
                 ])
 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)
                 )
             ),
         )
     )
Exemplo n.º 25
0
    def test_hard_error_exception_from_executor_execute_SHOULD_be_handled(
            self):
        # ARRANGE #
        act_phase_instructions = []
        hard_error_message = 'the err msg'

        actor = sut.ActorFromParts(
            ParserWithConstantResult(SymbolUserWithConstantSymbolReferences(
                ())),
            sut.UnconditionallySuccessfulValidatorConstructor(),
            _ExecutorConstructorForConstant(
                ExecutorThat(execute_initial_action=do_raise(
                    HardErrorException(
                        asrt_text_doc.new_single_string_text_for_test(
                            hard_error_message))))),
        )

        arrangement = arrangement_w_tcds()
        expectation = Expectation.hard_error_from_execute(
            error_message=asrt_text_doc.
            is_single_pre_formatted_text_that_equals(hard_error_message))
        # ACT & ASSERT #
        check_execution(self, actor, act_phase_instructions, arrangement,
                        expectation)
Exemplo n.º 26
0
 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))
Exemplo n.º 27
0
 def freeze(self):
     raise HardErrorException(
         asrt_text_doc.new_single_string_text_for_test(
             'freeze not allowed here'))
Exemplo n.º 28
0
 def may_depend_on_external_resources() -> bool:
     raise HardErrorException(
         asrt_text_doc.new_single_string_text_for_test('hard err msg'))
Exemplo n.º 29
0
 def of_err_msg_for_test(
         error_message: str = 'unconditional error') -> ValueRestriction:
     return ValueRestrictionWithConstantResult(
         ErrorMessageWithFixTip(
             asrt_text_doc.new_single_string_text_for_test(error_message)))
Exemplo n.º 30
0
def unconditionally_unsuccessful_validator() -> Optional[TextRenderer]:
    return asrt_text_doc.new_single_string_text_for_test('the err msg')