Beispiel #1
0
    def test_successful_step_sequence(self):
        validate_pre_sds = 'validate_pre_sds'
        validate_post_sds = 'validate_post_sds'
        main = 'main'

        expected_recordings = [
            validate_pre_sds,
            validate_post_sds,
            main,
        ]
        recorder = []

        def recording_of(s: str) -> Callable[[InstructionEnvironmentForPostSdsStep,
                                              InstructionSettings,
                                              OsServices],
                                             None]:
            def ret_val(*args):
                recorder.append(s)

            return ret_val

        instruction_that_records_steps = instruction_embryo_that__phase_agnostic(
            validate_pre_sds_initial_action=recording_of(validate_pre_sds),
            validate_post_sds_initial_action=recording_of(validate_post_sds),
            main_initial_action=recording_of(main))
        self._check(
            ParserThatGives(instruction_that_records_steps),
            single_line_source(),
            Arrangement.phase_agnostic(),
            Expectation.phase_agnostic())

        self.assertEqual(expected_recordings,
                         recorder,
                         'step execution sequence')
Beispiel #2
0
    def test_main_method_arguments(self):
        # ARRANGE #
        the_environ = MappingProxyType({'the_env_var': 'the env var value'})
        the_timeout = 69
        the_os_services = os_services_access.new_for_current_os()

        def main_action_that_checks_arguments(environment: InstructionEnvironmentForPostSdsStep,
                                              instruction_settings: InstructionSettings,
                                              os_services: OsServices):
            self.assertIs(os_services, the_os_services, 'os_services')

            self.assertEqual(the_environ, environment.proc_exe_settings.environ,
                             'proc exe settings/environment')

            self.assertEqual(the_timeout, environment.proc_exe_settings.timeout_in_seconds,
                             'proc exe settings/timeout')

            self.assertEqual(the_environ, instruction_settings.environ(),
                             'instruction settings/environment')

        # ACT & ASSERT #
        self._check_source_and_exe_variants(
            ParserThatGives(instruction_embryo_that__phase_agnostic(
                main_initial_action=main_action_that_checks_arguments
            )),
            Arrangement.phase_agnostic(
                process_execution_settings=ProcessExecutionSettings(the_timeout, the_environ),
                os_services=the_os_services,
            ),
            MultiSourceExpectation.phase_agnostic(),
        )
Beispiel #3
0
 def test_fail_if_instruction_is_not_setup_phase_aware(self):
     with self.assertRaises(utils.TestError):
         self._check(
             ParserThatGives(instruction_embryo_that__phase_agnostic()),
             single_line_source(),
             Arrangement.setup_phase_aware(),
             Expectation.setup_phase_aware(),
         )
Beispiel #4
0
 def test_fail_due_to_unexpected_result__from_main(self):
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(instruction_embryo_that__phase_agnostic(main=do_return('actual'))),
         Arrangement.phase_agnostic(),
         MultiSourceExpectation.phase_agnostic(
             main_result=asrt.equals('different-from-actual'),
         ),
     )
Beispiel #5
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
         ),
     )
Beispiel #6
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()
         ),
     )
Beispiel #7
0
 def test_that_default_expectation_assumes_no_symbol_usages(self):
     unexpected_symbol_usages = [
         data_references.reference_to__on_direct_and_indirect('symbol_name')]
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(
             instruction_embryo_that__phase_agnostic(
                 symbol_usages=do_return(unexpected_symbol_usages))),
         Arrangement.phase_agnostic(),
         Expectation.phase_agnostic(),
     )
Beispiel #8
0
 def test_that_cwd_for_main__and__validate_post_setup_is_act_dir(self):
     instruction_that_raises_exception_if_unexpected_state = instruction_embryo_that__phase_agnostic(
         main_initial_action=utils.raise_test_error_if_cwd_is_not_act_root__env,
         validate_post_sds_initial_action=utils.raise_test_error_if_cwd_is_not_act_root__env,
     )
     self._check_source_and_exe_variants(
         ParserThatGives(instruction_that_raises_exception_if_unexpected_state),
         Arrangement.phase_agnostic(
             tcds=TcdsArrangement(),
         ),
         MultiSourceExpectation.phase_agnostic(),
     )
Beispiel #9
0
 def test_fail_if_instruction_is_not_phase_agnostic__multi(self):
     with self.assertRaises(utils.TestError):
         checker = sut.Checker(ParserThatGives(instruction_embryo_that__phase_agnostic()))
         checker.check__abs_stx__multi__std_layouts_and_source_variants(
             self.tc,
             CustomAbsStx.empty(),
             symbol_usages=asrt.is_empty_sequence,
             execution_cases=[
                 NArrEx(
                     'the one and only case',
                     Arrangement.setup_phase_aware(),
                     ExecutionExpectation.setup_phase_aware(),
                 )
             ]
         )
Beispiel #10
0
 def test_that_fails_due_to_missing_symbol_reference(self):
     symbol_usages_of_instruction = []
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(
             instruction_embryo_that__phase_agnostic(
                 symbol_usages=do_return(symbol_usages_of_instruction))),
         Arrangement.phase_agnostic(),
         Expectation.phase_agnostic(
             symbol_usages=asrt.matches_singleton_sequence(
                 matches_data_type_symbol_reference(
                     'symbol_name',
                     reference_restrictions.is_any_type_w_str_rendering()
                 )
             )),
     )
Beispiel #11
0
    def test_symbols_populated_by_main_SHOULD_appear_in_symbol_table_given_to_symbols_after_main(self):
        symbol = StringConstantSymbolContext('symbol_name', 'const string')

        def add_symbol_to_symbol_table(environment: InstructionEnvironmentForPostSdsStep, *args):
            environment.symbols.put(symbol.name,
                                    symbol.symbol_table_container)

        self._check_source_and_exe_variants(
            ParserThatGives(
                instruction_embryo_that__phase_agnostic(
                    main_initial_action=add_symbol_to_symbol_table)),
            Arrangement.phase_agnostic(),
            MultiSourceExpectation.phase_agnostic(
                symbols_after_main=asrt.sub_component('names_set',
                                                      SymbolTable.names_set.fget,
                                                      asrt.equals({symbol.name}))),
        )
Beispiel #12
0
    def test_that_symbols_from_arrangement_exist_in_environment(self):
        symbol = StringConstantSymbolContext('symbol_name', 'the symbol value')
        symbol_table_of_arrangement = symbol.symbol_table
        expected_symbol_table = symbol.symbol_table
        assertion_for_validation = do_fail_if_symbol_table_does_not_equal(
            self,
            expected_symbol_table,
            get_symbol_table_from_path_resolving_environment_that_is_first_arg)

        assertion_for_main = do_fail_if_symbol_table_does_not_equal(
            self,
            expected_symbol_table,
            get_symbol_table_from_instruction_environment_that_is_first_arg)

        self._check_source_and_exe_variants(
            ParserThatGives(
                instruction_embryo_that__phase_agnostic(
                    validate_pre_sds_initial_action=assertion_for_validation,
                    validate_post_sds_initial_action=assertion_for_validation,
                    main_initial_action=assertion_for_main,
                )),
            Arrangement.phase_agnostic(symbols=symbol_table_of_arrangement),
            MultiSourceExpectation.phase_agnostic(),
        )
Beispiel #13
0
 def test_fail_if_instruction_is_not_setup_phase_aware__source_variant(self):
     self._check_source_and_exe_variants__failing_assertions(
         ParserThatGives(instruction_embryo_that__phase_agnostic()),
         Arrangement.setup_phase_aware(),
         MultiSourceExpectation.setup_phase_aware(),
     )
Beispiel #14
0
        return self.instruction


class ParserThatConsumesCurrentLine(Generic[T], embryo.InstructionEmbryoParser[T]):
    def __init__(self, parser_that_do_not_consume_any_source: embryo.InstructionEmbryoParser[T]):
        self.parser_that_do_not_consume_any_source = parser_that_do_not_consume_any_source

    def parse(self,
              fs_location_info: FileSystemLocationInfo,
              source: ParseSource) -> embryo.InstructionEmbryo[T]:
        ret_val = self.parser_that_do_not_consume_any_source.parse(fs_location_info, source)
        source.consume_current_line()
        return ret_val


PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION = ParserThatGives(instruction_embryo_that__phase_agnostic())


class InstructionThatSetsEnvironmentVariable(embryo.PhaseAgnosticInstructionEmbryo[None]):
    def __init__(self, variable: NameAndValue):
        self.variable = variable

    def main(self,
             environment: InstructionEnvironmentForPostSdsStep,
             settings: InstructionSettings,
             os_services: OsServices,
             ):
        variable = self.variable
        settings.environ()[variable.name] = variable.value