def test_WHEN_multiple_transformers_THEN_transformers_SHOULD_be_chained( self): # ARRANGE # to_upper_t = to_uppercase() count_num_upper = string_transformers.count_num_uppercase_characters() transformer = SequenceStringTransformer([to_upper_t, count_num_upper]) input_lines = with_appended_new_lines([ 'this is', 'the', 'input', ]) model = string_sources.of_lines__w_check_for_validity( self, input_lines) # ACT # actual = transformer.transform(model) # ASSERT # expected_output_lines = with_appended_new_lines([ '6', '3', '5', ]) actual_lines = string_sources.as_lines_list__w_lines_validation( self, actual) self.assertEqual(expected_output_lines, actual_lines)
def test_WHEN_single_transformer_THEN_sequence_SHOULD_be_identical_to_the_single_transformer( self): # ARRANGE # to_upper_t = string_transformers.to_uppercase() transformer = SequenceStringTransformer([to_upper_t]) input_lines = with_appended_new_lines([ 'first', 'second', 'third', ]) model = string_sources.of_lines__w_check_for_validity( self, input_lines) # ACT # actual = transformer.transform(model) # ASSERT # expected_output_lines = with_appended_new_lines([ 'FIRST', 'SECOND', 'THIRD', ]) actual_lines = string_sources.as_lines_list__w_lines_validation( self, actual) self.assertEqual(expected_output_lines, actual_lines)
def test_every_line_SHOULD_be_filtered(self): matcher = LineMatcherSymbolContext.of_primitive( 'line_matcher_symbol', sub_string_line_matcher('MATCH'), ) line_matcher_arg = lm_args.SymbolReference(matcher.name) cases = [ NEA('no lines', [], [], ), NEA('single line that matches', ['a MATCH'], ['a MATCH'], ), NEA('single line that matches, ended by new-line', ['a MATCH\n'], ['a MATCH\n'], ), NEA('single line that does not match', actual=['not a match'], expected=[], ), NEA('some lines matches', actual=with_appended_new_lines([ 'first line is a MATCH', 'second line is not a match', 'third line MATCH:es', 'fourth line not', ]), expected=with_appended_new_lines([ 'first line is a MATCH', 'third line MATCH:es', ]), ), ] for case in cases: with self.subTest(case_name=case.name): arguments = st_args.syntax_for_filter_transformer(str(line_matcher_arg)) integration_check.CHECKER__PARSE_SIMPLE.check__w_source_variants( self, Arguments(arguments), model_constructor.of_lines(self, case.actual), arrangement_w_tcds( symbols=matcher.symbol_table ), expectation_of_successful_filter_execution( output_lines=case.expected, symbol_references=asrt.matches_singleton_sequence( references.is_reference_to_line_matcher(matcher.name) ), ) )
def _check( self, pattern: str, replacement_str: str, lines_selector: LineMatcherSymbolContext, input_lines__wo_nl: List[str], output_lines__wo_nl: List[str], ): # ARRANGE # input_lines = with_appended_new_lines(input_lines__wo_nl) output_lines = with_appended_new_lines(output_lines__wo_nl) pattern_symbol = StringConstantSymbolContext( 'PATTERN_SYMBOL', pattern, default_restrictions=asrt_regex.is_reference_restrictions__regex(), ) replacement_symbol = StringConstantSymbolContext( 'REPLACEMENT_SYMBOL', replacement_str, default_restrictions=asrt_ref_rest.is__w_str_rendering(), ) all_symbol = [lines_selector, pattern_symbol, replacement_symbol] for preserve_new_lines in [False, True]: syntax = ReplaceRegexAbsStx( pattern_symbol.abstract_syntax, replacement_symbol.abstract_syntax, preserve_new_lines=preserve_new_lines, lines_filter=lines_selector.abstract_syntax, ) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__abs_stx__layout__std_source_variants( self, syntax, model_constructor.of_lines(self, input_lines), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( all_symbol), ), expectation_of_successful_replace_execution__multi( symbol_references=SymbolContext. references_assertion_of_contexts(all_symbol), output_lines=output_lines, may_depend_on_external_resources=True, ), sub_test_identifiers={ 'preserve_new_lines': preserve_new_lines, })
def of_lines_wo_nl( put: unittest.TestCase, lines: List[str], may_depend_on_external_resources: bool = False, ) -> ModelConstructor: return _ModelOfLines(put, with_appended_new_lines(lines), may_depend_on_external_resources).construct
def runTest(self): def model( environment: FullResolvingEnvironment ) -> string_sources.StringSource: return string_sources.of_lines__w_check_for_validity( self, with_appended_new_lines([ str(environment.tcds.hds.case_dir), str(environment.tcds.hds.act_dir), str(environment.tcds.sds.act_dir), str(environment.tcds.sds.user_tmp_dir), ]), environment.application_environment.tmp_files_space. sub_dir_space()) expected = with_appended_new_lines([ path.EXACTLY_DIR__REL_HDS_CASE, path.EXACTLY_DIR__REL_HDS_ACT, path.EXACTLY_DIR__REL_ACT, path.EXACTLY_DIR__REL_TMP, ]) integration_check.CHECKER__PARSE_SIMPLE.check__w_source_variants( self, Arguments(args.tcds_path_replacement()), model, arrangement_w_tcds(), expectation_of_successful_execution_2( symbol_references=asrt.is_empty_sequence, output_lines=expected, may_depend_on_external_resources=False, is_identity_transformer=False, adv=freeze_check. first_invoked_method_of_source_model__is_freeze, ))
def model( environment: FullResolvingEnvironment ) -> string_sources.StringSource: return string_sources.of_lines__w_check_for_validity( self, with_appended_new_lines([ str(environment.tcds.hds.case_dir), str(environment.tcds.hds.act_dir), str(environment.tcds.sds.act_dir), str(environment.tcds.sds.user_tmp_dir), ]), environment.application_environment.tmp_files_space. sub_dir_space())
def test_WHEN_sequence_of_transformers_is_empty_THEN_output_SHOULD_be_equal_to_input( self): # ARRANGE # transformer = SequenceStringTransformer([]) input_lines = with_appended_new_lines([ 'first', 'second', 'third', ]) model = string_sources.of_lines__w_check_for_validity( self, input_lines) # ACT # actual = transformer.transform(model) # ASSERT # actual_lines = string_sources.as_lines_list__w_lines_validation( self, actual) self.assertEqual(input_lines, actual_lines)
def test_non_empty_list_of_lines(self): # ARRANGE # input_lines = with_appended_new_lines([ 'we are here and they are here too', 'here I am', 'I am here', ]) model = string_sources.of_lines__w_check_for_validity( self, input_lines) transformer = sut.IdentityStringTransformer() # ACT # actual = transformer.transform(model) # ASSERT # actual_lines = string_sources.as_lines_list__w_lines_validation( self, actual) self.assertEqual(input_lines, actual_lines)
def test_every_line_SHOULD_be_transformed(self): # ARRANGE # trans_fun = self.config.str_transformer input_lines = [ 'I object!', 'Object Oriented', 'Unidentified FLYING Object', ] expected_lines = [trans_fun(line) for line in input_lines] # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, Arguments(self.config.arguments), model_constructor.of_lines_wo_nl(self, input_lines), arrangement_w_tcds(), expectation_of_successful_execution_2( symbol_references=asrt.is_empty_sequence, output_lines=with_appended_new_lines(expected_lines), may_depend_on_external_resources=False, is_identity_transformer=False, adv=freeze_check. first_invoked_method_of_source_model__is_freeze, ))
def test_other_scenarios(self): cases = [ NameAndValue( 'trailing new lines should be removed from line matcher model, but not from transformer output', (line_matcher_from_predicates(line_contents_predicate=lambda x: x == 'X'), ['X\n'], ['X\n']) ), NameAndValue( 'line numbers should be paired with lines in order of iterator (1)', (is_identical_to(1, 'i'), with_appended_new_lines([ 'i', 'ii', ]), with_appended_new_lines(['i']) ) ), NameAndValue( 'line numbers should be paired with lines in order of iterator (2)', (is_identical_to(2, 'ii'), with_appended_new_lines([ 'i', 'ii', ]), with_appended_new_lines(['ii']) ) ), NameAndValue( 'line numbers should be propagated to line matcher', (line_matcher_from_predicates(line_num_predicate=lambda x: x in {1, 3}), with_appended_new_lines([ 'i', 'ii', 'iii', 'iv', ]), with_appended_new_lines([ 'i', 'iii', ]) ) ), ] line_matcher_name = 'the_line_matcher_symbol_name' line_matcher_arg = lm_args.SymbolReference(line_matcher_name) arguments = st_args.syntax_for_filter_transformer(str(line_matcher_arg)) for case in cases: matcher, input_lines, expected_output_lines = case.value with self.subTest(case_name=case.name): # ACT & ASSERT # integration_check.CHECKER__PARSE_SIMPLE.check__w_source_variants( self, Arguments(arguments), model_constructor.of_lines(self, input_lines), arrangement_w_tcds( symbols=LineMatcherSymbolContext.of_primitive( line_matcher_name, matcher, ).symbol_table, ), expectation_of_successful_filter_execution( output_lines=expected_output_lines, symbol_references=asrt.matches_singleton_sequence( references.is_reference_to_line_matcher(line_matcher_name) ) ) )
def lines(pattern_matching_string: str) -> List[str]: return with_appended_new_lines([ 'unidentified flying {}'.format(pattern_matching_string), '{} oriented'.format(pattern_matching_string), 'I {}!'.format(pattern_matching_string), ])