def runTest(self): expected_contents = 'expected-contents' resolving_env = PathResolvingEnvironmentPreOrPostSds(fake_tcds()) sdv = self._sdv_expecting_equals(expected_contents) adv = sdv.resolve(resolving_env.symbols).value_of_any_dependency(resolving_env.tcds) for actual_contents in [expected_contents, expected_contents + '-unexpected']: expected_result = actual_contents == expected_contents expected_matching_result = asrt_matching_result.matches_value(expected_result) with self.subTest(expected_result=expected_result): model_w_ext_deps = model_constructor.of_str( self, actual_contents, may_depend_on_external_resources=True, ) with tmp_dir() as dir_space_dir__matcher: matcher = adv.primitive(self._app_env(dir_space_dir__matcher, 'matcher-path')) with tmp_dir() as dir_space_dir__model: model = model_w_ext_deps(FullResolvingEnvironment( resolving_env.symbols, resolving_env.tcds, self._app_env(dir_space_dir__model, 'model-path') )) # ACT # matching_result_1st = matcher.matches_w_trace(model) matching_result_2nd = matcher.matches_w_trace(model) # ASSERT # expected_matching_result.apply_with_message(self, matching_result_1st, '1st') expected_matching_result.apply_with_message(self, matching_result_2nd, '2nd')
def runTest(self): # ARRANGE # test_cases = [ (PathPartDdvAsFixedPath('file.txt'), empty_symbol_table(), 'file.txt'), ] tcds = fake_tcds() for path_suffix, symbol_table, expected_path_suffix in test_cases: with self.subTest(): path = self.config.path_suffix_2_path(path_suffix) assert isinstance(path, PathDdv) # ACT # if self.config.resolving_dependency is None: tested_path_msg = 'value_when_no_dir_dependencies' actual_path = path.value_when_no_dir_dependencies() elif self.config.exists_pre_sds: tested_path_msg = 'file_path_pre_sds' actual_path = path.value_pre_sds(tcds.hds) else: tested_path_msg = 'file_path_post_sds' actual_path = path.value_post_sds(tcds.sds) actual_path_pre_or_post_sds = path.value_of_any_dependency( tcds) # ASSERT # expected_relativity_root = self.config.tcds_2_relativity_root( tcds) expected_path = expected_relativity_root / expected_path_suffix self.assertEqual(str(expected_path), str(actual_path), tested_path_msg) self.assertEqual(str(expected_path), str(actual_path_pre_or_post_sds), 'file_path_pre_or_post_sds')
def __init__( self, resolved_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ): self.resolved_value = resolved_value self.tcds = tcds
def _matches_string_matcher_sdv( primitive_value: Assertion[MatcherWTrace] = asrt.anything_goes(), references: Assertion[ Sequence[SymbolReference]] = asrt.is_empty_sequence, symbols: SymbolTable = None, tcds: TestCaseDs = fake_tcds(), ) -> Assertion[SymbolDependentValue]: return sut.matches_matcher_sdv(primitive_value, references, symbols, tcds)
def matches_sdv_of_files_matcher( references: Assertion[ Sequence[SymbolReference]] = asrt.anything_goes(), primitive_value: Assertion[FilesMatcher] = asrt.anything_goes(), symbols: Optional[SymbolTable] = None, tcds: TestCaseDs = fake_tcds(), ) -> Assertion[SymbolDependentValue]: return matches_matcher_sdv(primitive_value, references, symbols, tcds)
class _ParseAndExecutionChecker(Generic[PRIMITIVE, INPUT, OUTPUT]): FAKE_TCDS = fake_tcds() def __init__( self, put: unittest.TestCase, model_constructor: INPUT, parser: Parser[FullDepsSdv[PRIMITIVE]], arrangement: Arrangement, configuration: CommonPropertiesConfiguration[PRIMITIVE, INPUT, OUTPUT], check_application_result_with_tcds: bool, expectation: Expectation[PRIMITIVE, OUTPUT], ): self.put = put self.parser = parser self.configuration = configuration self.expectation = expectation self.source_expectation = expectation.parse.source self._execution_checker = ExecutionChecker( put, model_constructor, arrangement, expectation.adv, expectation.primitive, expectation.execution, configuration.applier(), configuration.new_execution_checker(), check_application_result_with_tcds, ) def check(self, source: ParseSource): matcher_sdv = self._parse(source) self._execution_checker.check(matcher_sdv) def _parse(self, source: ParseSource) -> FullDepsSdv[PRIMITIVE]: sdv = self.parser.parse(source) self.source_expectation.apply_with_message( self.put, source, 'source after parse', ) message_builder = asrt.MessageBuilder('parsed object') self.expectation.parse.symbol_references.apply( self.put, sdv.references, message_builder.for_sub_component('symbol references'), ) self.configuration.new_sdv_checker().check( self.put, sdv, message_builder, ) return sdv
def matches_logic_ddv(primitive_value: Callable[[TestCaseDs], Assertion], tcds: TestCaseDs = fake_tcds() ) -> Assertion[DirDependentValue]: return asrt.is_instance_with__many( FullDepsDdv, [ has_valid_description(), has_validator(), asrt_ddv.matches_dir_dependent_value__with_adv(primitive_value, tcds), ])
def __init__( self, resolving_dependency: Optional[DirectoryStructurePartition], resolved_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ): resolving_dependencies = asrt.is_empty if resolving_dependency is None else asrt.equals( {resolving_dependency}) super().__init__(resolving_dependencies, resolved_value, tcds) self._resolving_dependency = resolving_dependency
def new_tcds(tcds: TestCaseDs = fake_tcds(), symbols: SymbolTable = None, process_execution_settings: ProcessExecutionSettings = proc_exe_env_for_test(), ) -> 'InstructionEnvironmentPostSdsBuilder': return InstructionEnvironmentPostSdsBuilder( tcds.hds, tcds.sds, process_execution_settings.environ, process_execution_settings.timeout_in_seconds, symbols, )
def _apply( self, put: unittest.TestCase, value: PathSdv, message_builder: MessageBuilder, ): put.assertIsInstance(value, PathSdv) path = value.resolve(self._symbols).value_of_any_dependency( fake_tcds()) self._name.apply(put, path.name, message_builder.for_sub_component('name'))
def __init__( self, dir_dependencies: DirDependencies, resolved_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ): super().__init__( asrt.equals( resolving_dependencies_from_dir_dependencies( dir_dependencies)), resolved_value, tcds) self._dir_dependencies = dir_dependencies
def test_file_path(self): relativity_test_cases = [ (RelOptionType.REL_HDS_CASE, True), (RelOptionType.REL_ACT, False), ] path_suffix_str = 'path-suffix-file.txt' path_suffix_test_cases = [ (path_part_sdvs.from_constant_str(path_suffix_str), ()), (path_part_sdvs.from_string( string_sdv_of_single_symbol_reference( 'path_suffix_symbol', reference_restrictions.is_any_type_w_str_rendering())), (StringConstantSymbolContext('path_suffix_symbol', path_suffix_str).entry, )), ] for rel_option, exists_pre_sds in relativity_test_cases: # ARRANGE # path_component_from_referenced_path = 'path-component-from-referenced-file-ref' referenced_sym = ConstantSuffixPathDdvSymbolContext( 'path_symbol', rel_option, path_component_from_referenced_path) for path_suffix, symbol_table_entries in path_suffix_test_cases: fr_sdv_to_check = sut.PathSdvRelSymbol( path_suffix, _symbol_reference_of_path_with_accepted( referenced_sym.name, rel_option)) symbol_table = referenced_sym.symbol_table symbol_table.add_all(symbol_table_entries) tcds = fake_tcds() expected_root_path = _root_path_of_option(rel_option, tcds) expected_path = expected_root_path / path_component_from_referenced_path / path_suffix_str expected_path_str = str(expected_path) environment = PathResolvingEnvironmentPreOrPostSds( tcds, symbol_table) with self.subTest(msg=str(rel_option)): # ACT # path_to_check = fr_sdv_to_check.resolve( environment.symbols) if exists_pre_sds: tested_path_msg = 'value_pre_sds' actual_path = path_to_check.value_pre_sds( environment.hds) else: tested_path_msg = 'value_post_sds' actual_path = path_to_check.value_post_sds( environment.sds) actual_path_pre_or_post_sds = path_to_check.value_of_any_dependency( environment.tcds) # ASSERT # self.assertEqual(expected_path_str, str(actual_path), tested_path_msg) self.assertEqual(expected_path_str, str(actual_path_pre_or_post_sds), 'value_of_any_dependency')
def matches_matcher_ddv(primitive_value: Callable[[TestCaseDs], Assertion[MatcherWTrace]], tcds: TestCaseDs = fake_tcds() ) -> Assertion[DirDependentValue]: def get_primitive_value_assertion(tcds_: TestCaseDs) -> Assertion: return asrt.is_instance_with(MatcherWTrace, primitive_value(tcds_)) return asrt.is_instance_with__many( MatcherDdv, [ has_node_description(), has_validator(), asrt_ddv.matches_dir_dependent_value__with_adv(get_primitive_value_assertion, tcds), ])
def _apply(self, put: unittest.TestCase, value: StringSdv, message_builder: asrt.MessageBuilder): put.assertIsInstance(value, StringSdv) equals_symbol_references__w_str_rendering(self.symbol_references).apply_with_message(put, value.references, 'symbol references') environment = PathResolvingEnvironmentPreOrPostSds(fake_tcds(), self.symbols) actual_resolved_prim_val = value.resolve_value_of_any_dependency(environment) self.expected_resolved_primitive_value.apply_with_message(put, actual_resolved_prim_val, 'resolved primitive value')
def _apply(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder): self._check_type(put, value, message_builder) assert isinstance(value, DependenciesAwareDdv) self._check_common_dependencies(put, value, message_builder) self._check_existence(put, value, message_builder) tcds = fake_tcds() self._check_custom_dependencies(put, value, tcds, message_builder) self._check_value(put, value, tcds, message_builder) self._check_custom(put, value, tcds, message_builder)
def resolving_env_w_custom_dir_space( sds: SandboxDs, mem_buff_size: int = 2**10, ) -> FullResolvingEnvironment: return FullResolvingEnvironment( SymbolTable.empty(), fake_tcds(), ApplicationEnvironment( OsServicesThatRaises(), proc_exe_env_for_test(), tmp_file_spaces.tmp_dir_file_space_for_test( sds.internal_tmp_dir / 'string-model-dir-for-test'), mem_buff_size, ))
def matches_matcher_sdv( primitive_value: Assertion[MatcherWTrace] = asrt.anything_goes(), references: Assertion[ Sequence[SymbolReference]] = asrt.is_empty_sequence, symbols: SymbolTable = None, tcds: TestCaseDs = fake_tcds(), ) -> Assertion[SymbolDependentValue]: return matches_sdv( asrt.is_instance(MatcherSdv), references, asrt.is_instance_with( MatcherDdv, asrt_logic.matches_matcher_ddv(lambda tcds_: primitive_value, tcds)), asrt.anything_goes(), symbol_table_from_none_or_value(symbols))
def matches_dir_dependent_value__with_adv( primitive_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ) -> Assertion[DirDependentValue[ApplicationEnvironmentDependentValue[T]]]: def adv_assertion( tcds_: TestCaseDs ) -> Assertion[DirDependentValue[ApplicationEnvironmentDependentValue[T]]]: ae = application_environment_for_test() return asrt.is_instance_with( ApplicationEnvironmentDependentValue, asrt.sub_component('primitive', lambda adv: adv.primitive(ae), primitive_value(tcds_))) return _DirDependentValueAssertion(adv_assertion, tcds)
def resolving_helper( symbols: Optional[SymbolTable] = None, tcds: TestCaseDs = fake_tcds(), file_space: DirFileSpace = DirFileSpaceThatMustNoBeUsed(), os_services_: OsServices = os_services_access.new_for_current_os(), process_execution_settings: ProcessExecutionSettings = ProcessExecutionSettings.null(), mem_buff_size: int = 2**10, ) -> LogicTypeResolvingHelper: return LogicTypeResolvingHelper( (symbols if symbols is not None else empty_symbol_table()), tcds, ApplicationEnvironment(os_services_, process_execution_settings, file_space, mem_buff_size), )
class ExecutionAssertion(AssertionBase[FullDepsSdv[PRIMITIVE]]): FAKE_TCDS = fake_tcds() def __init__( self, model_constructor: INPUT, arrangement: Arrangement, adv: Callable[ [AssertionResolvingEnvironment], Assertion[ApplicationEnvironmentDependentValue[PRIMITIVE]]], primitive: Callable[[AssertionResolvingEnvironment], Assertion[PRIMITIVE]], execution: ExecutionExpectation[OUTPUT], applier: Applier[PRIMITIVE, INPUT, OUTPUT], common_properties: CommonExecutionPropertiesChecker[PRIMITIVE, OUTPUT], check_application_result_with_tcds: bool, ): self.model_constructor = model_constructor self.applier = applier self.arrangement = arrangement self.adv = adv self.primitive = primitive self.execution = execution self.common_properties = common_properties self.check_application_result_with_tcds = check_application_result_with_tcds def _apply( self, put: unittest.TestCase, value: FullDepsSdv[PRIMITIVE], message_builder: MessageBuilder, ): execution_checker = ExecutionChecker( put, self.model_constructor, self.arrangement, self.adv, self.primitive, self.execution, self.applier, self.common_properties, self.check_application_result_with_tcds, message_builder, ) execution_checker.check(value)
class _Assertion(AssertionBase[SetupSettingsHandler]): FAKE_TCDS = fake_tcds() ENVIRONMENT = instruction_environment.fake_post_sds_environment() OS_SERVICES = OsServicesThatRaises() def __init__(self, settings_builder: Assertion[SettingsBuilderAssertionModel], atc_exe_input: Assertion[AdvWvAssertionModel[AtcExecutionInput]], ): self._settings_builder = settings_builder self._atc_exe_input = atc_exe_input def _apply(self, put: unittest.TestCase, value: SetupSettingsHandler, message_builder: MessageBuilder, ): settings_builder_model = asrt_settings_builder.SettingsBuilderAssertionModel( value.builder, self.ENVIRONMENT, self.OS_SERVICES, ) self._settings_builder.apply( put, settings_builder_model, message_builder.for_sub_component('settings builder'), ) aei_model = AdvWvAssertionModel( ApplicationEnvironment(self.OS_SERVICES, self.ENVIRONMENT.proc_exe_settings, self.ENVIRONMENT.tmp_dir__path_access.paths_access, self.ENVIRONMENT.mem_buff_size, ), value.as_atc_execution_input(), ) self._atc_exe_input.apply( put, aei_model, message_builder.for_sub_component('act execution info'), )
def test_validation_SHOULD_succeed_WHEN_value_is_an_integer_and_custom_validator_succeeds( self): the_instruction_environment = instruction_environment.fake_pre_sds_environment( ) resolved_value = 16 resolved_string_value_cases = [ '16', '8 + 8', '2 * 2 * 2 * 2', ] custom_validator_cases = [ None, CustomValidator( value_that_makes_the_validation_succeed=resolved_value, error_message=validation.new_single_string_text_for_test( 'error message from custom validator')) ] for value_string in resolved_string_value_cases: for custom_validator in custom_validator_cases: sdv_to_check = sut.IntegerSdv( string_sdvs.str_constant(str(value_string)), custom_validator) with self.subTest( custom_validator_is_none=str(custom_validator is None), value_string=value_string): with self.subTest(tested_obect='sdv'): sdv_to_check.validate_pre_sds( the_instruction_environment. path_resolving_environment) with self.subTest(tested_obect='value'): value_to_check = sdv_to_check.resolve( the_instruction_environment.symbols) value_to_check.validator( ).validate_pre_sds_if_applicable( the_instruction_environment.hds) value_to_check.validator( ).validate_post_sds_if_applicable(fake_tcds())
def _check(put: unittest.TestCase, source: TokenParser, arrangement: Arrangement, expectation: Expectation): # ARRANGE # parser = sut.ParserOfRegex() tcds = fake_tcds() # ACT # actual_sdv = parser.parse_from_token_parser(source) # ASSERT # expectation.token_stream.apply_with_message(put, source.token_stream, 'token stream') sdv_assertion = expectation.matches_regex_sdv(arrangement.symbols, tcds) sdv_assertion.apply_with_message(put, actual_sdv, 'sdv')
def test_model_is_returned_WHEN_file_is_existing_regular_file(self): # ARRANGE # assertion_part = sut.IsExistingRegularFileAssertionPart() existing_regular_file = File.empty('regular.txt') with tmp_dir(DirContents([existing_regular_file])) as path_of_existing_directory: path_of_existing_regular_file = path_of_existing_directory / existing_regular_file.name path_ddv = path_ddvs.absolute_path(path_of_existing_regular_file) path = path_ddv.value_of_any_dependency__d(fake_tcds()) model = sut.ComparisonActualFile(path, True) # ACT # actual = assertion_part.check(self.environment, self.the_os_services, model) # ASSERT # self.assertIs(model, actual)
def test_model_SHOULD_be_evaluated_so_that_failures_from_evaluation_are_detected( self): # ARRANGE # sut = transformer_checker._Applier(force_evaluation_of_model=True) resolving_env = FullResolvingEnvironment( symbol_table.empty_symbol_table(), fake_tcds(), application_environment_for_test(), ) transformer_that_raises = string_transformers.model_access_raises_hard_error( ) # ACT & ASSERT # with self.assertRaises(HardErrorException): sut.apply( self, asrt.MessageBuilder.new_empty(), transformer_that_raises, resolving_env, model_constructor.must_not_be_used, )
def _apply(self, put: unittest.TestCase, value: StringFragmentSdv, message_builder: asrt.MessageBuilder): put.assertIsInstance(value, StringFragmentSdv) assert isinstance(value, StringFragmentSdv) # Type info for IDE symbols = symbol_table_with_values_matching_references(self.expected.references) tcds = fake_tcds() environment = PathResolvingEnvironmentPreOrPostSds(tcds, symbols) assertions = [ asrt.sub_component('is_string_constant', lambda sfr: sfr.is_string_constant, asrt.equals(self.expected.is_string_constant) ), asrt.sub_component('resolve', lambda sfr: sfr.resolve(environment.symbols), equals_string_fragment_ddv(self.expected.resolve(environment.symbols)) ), asrt.sub_component('resolve_value_of_any_dependency', lambda sfr: sfr.resolve_value_of_any_dependency(environment), asrt.equals( self.expected.resolve_value_of_any_dependency(environment)) ), ] if self.expected.is_string_constant: assertions.append( asrt.sub_component('string_constant', lambda sfr: sfr.string_constant, asrt.equals(self.expected.string_constant) ) ) assertion = asrt.and_(assertions) assertion.apply(put, value, message_builder)
class MatcherDdvFromPartsTestImpl(Generic[MODEL], MatcherDdv[MODEL]): FAKE_TCDS = fake_tcds() APPLICATION_ENVIRONMENT = application_environment_for_test() def __init__( self, make_primitive: Callable[[TestCaseDs], MatcherWTrace[MODEL]], validator: DdvValidator = ddv_validation.ConstantDdvValidator. new_success(), ): self._make_primitive = make_primitive self._validator = validator def structure(self) -> StructureRenderer: return (self.value_of_any_dependency(self.FAKE_TCDS).primitive( self.APPLICATION_ENVIRONMENT).structure()) @property def validator(self) -> DdvValidator: return self._validator def value_of_any_dependency(self, tcds: TestCaseDs) -> MatcherAdv[MODEL]: return advs.ConstantMatcherAdv(self._make_primitive(tcds))
def matches_multi_dir_dependent_value( dir_dependencies: DirDependencies, resolved_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ) -> Assertion[DependenciesAwareDdv[T]]: return MultiDirDependentAssertion(dir_dependencies, resolved_value, tcds)
class TestWithoutExecution(unittest.TestCase): TCDS = fake_tcds() PARSER = sut.parser() def test_without_arguments(self): # ARRANGE # command_cases = _single_line_command_cases() for command_case in command_cases: expected_command_sdv = command_case.sdv_assertion( self.TCDS, arguments=asrt.is_empty_sequence) for source_case in equivalent_source_variants_for_consume_until_end_of_last_line2( command_case.source.as_arguments): with self.subTest(command=command_case.name, following_source_variant=source_case.name): # ACT # actual = self.PARSER.parse( command_case.source.as_remaining_source) # ASSERT # expected_command_sdv.apply_without_message(self, actual) def test_with_arguments(self): arg_wo_space = 'arg_wo_space' arg_w_space = 'an arg w space' arg_w_space__src = surrounded_by_soft_quotes(arg_w_space) string_symbol = StringConstantSymbolContext( 'STRING_SYMBOL', 'the string value', default_restrictions=asrt_rest.is__w_str_rendering(), ) list_symbol = ListConstantSymbolContext( 'LIST_SYMBOL', ['1st value', '2nd value'], ) string_with_invalid_quoting = '"string without ending soft quote' arguments_cases = [ ArgumentsCase( 'one', source=ArgumentElements([arg_w_space__src]), expected_arguments=[arg_w_space], ), ArgumentsCase( 'two', source=ArgumentElements([arg_wo_space, arg_w_space__src]), expected_arguments=[arg_wo_space, arg_w_space], ), ArgumentsCase( 'string symbol reference', source=ArgumentElements([string_symbol.name__sym_ref_syntax]), expected_arguments=[string_symbol.str_value], symbols=[string_symbol], ), ArgumentsCase( 'list symbol reference', source=ArgumentElements([list_symbol.name__sym_ref_syntax]), expected_arguments=list_symbol.constant_list, symbols=[list_symbol], ), ArgumentsCase( 'list and string symbol reference', source=ArgumentElements([ list_symbol.name__sym_ref_syntax, string_symbol.name__sym_ref_syntax ]), expected_arguments=list_symbol.constant_list + [string_symbol.str_value], symbols=[list_symbol, string_symbol], ), ArgumentsCase( 'special program argument', source=program_arguments. remaining_part_of_current_line_as_literal( string_with_invalid_quoting).as_argument_elements, expected_arguments=[string_with_invalid_quoting], symbols=[], ), ] # ARRANGE # command_cases = _single_line_command_cases() for command_case in command_cases: for arguments_case in arguments_cases: source_w_arguments = ( command_case.source.as_argument_elements.followed_by( arguments_case.source).as_arguments) expected_arguments = asrt.matches_sequence([ asrt.equals(arg) for arg in arguments_case.expected_arguments ]) expected_command_sdv = command_case.sdv_assertion( self.TCDS, arguments=expected_arguments, arguments_symbols=arguments_case.symbols) for source_case in equivalent_source_variants_for_consume_until_end_of_last_line2( source_w_arguments): with self.subTest( command=command_case.name, arguments=arguments_case.name, following_source_variant=source_case.name): # ACT # actual = self.PARSER.parse(source_case.input_value) # ASSERT # source_case.expected_value.apply_with_message( self, source_case.input_value, 'source') expected_command_sdv.apply_with_message( self, actual, 'command')
def matches_dir_dependent_value( resolved_value: Callable[[TestCaseDs], Assertion[T]], tcds: TestCaseDs = fake_tcds(), ) -> Assertion[DirDependentValue[T]]: return _DirDependentValueAssertion(resolved_value, tcds)