def test_transformation_SHOULD_apply_only_to_matcher_argument(self): to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'the_to_upper_transformer', string_transformers.to_uppercase()) model = contents_transformation.TransformedContentsSetup( original='text', transformed='TEXT', ) integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, args2.conjunction([ args2.Parentheses( args2.Transformed( to_upper_transformer.name, args2.Equals.eq_string(model.transformed)), ), args2.Equals.eq_string(model.original), ]).as_arguments, model_constructor.of_str(self, model.original), arrangement_w_tcds(symbols=to_upper_transformer.symbol_table), Expectation( ParseExpectation( symbol_references=asrt.matches_singleton_sequence( is_reference_to_string_transformer( to_upper_transformer.name))), ExecutionExpectation( main_result=asrt_matching_result.matches_value(True))))
def test_transformation_only_as_source_argument(self): # ARRANGE # transformer = StringTransformerPrimitiveSymbolContext( 'STRING_TRANSFORMER', string_transformers.to_uppercase()) for pgm_and_args_case in pgm_and_args_cases.cases_w_and_wo_argument_list__including_program_reference( ): program_w_transformer = FullProgramAbsStx( pgm_and_args_case.pgm_and_args, transformation=transformer.abstract_syntax) symbols = list(pgm_and_args_case.symbols) + [transformer] with self.subTest(command=pgm_and_args_case.name): # ACT & ASSERT # CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, program_w_transformer, pgm_and_args_case.mk_arrangement( SymbolContext.symbol_table_of_contexts(symbols)), MultiSourceExpectation( symbol_references=SymbolContext. references_assertion_of_contexts(symbols), primitive=lambda env: (asrt_pgm_val.matches_program( asrt_command. matches_command(driver=pgm_and_args_case. expected_command_driver(env), arguments=asrt.is_empty_sequence), stdin=asrt_pgm_val.is_no_stdin(), transformer=asrt.matches_singleton_sequence( asrt.is_(transformer.primitive)), ))))
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 _file_contents_cases(self) -> InvalidDestinationFileTestCasesData: arbitrary_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) symbols = arbitrary_transformer.symbol_table relativity_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) src_file = relativity_conf.path_abs_stx_of_name('src-file.txt') contents_abs_stx_builder = string_source_abs_stx.TransformableAbsStxBuilder( string_source_abs_stx.StringSourceOfFileAbsStx(src_file)) src_file_in_hds_contents = relativity_conf.populator_for_relativity_option_root( DirContents([File.empty(src_file.name)])) file_contents_cases = [ NameAndValue( 'contents of existing file / without transformation', fs_abs_stx.FileContentsExplicitAbsStx( fs_abs_stx.ModificationType.CREATE, contents_abs_stx_builder.without_transformation())), NameAndValue( 'contents of existing file / with transformation', fs_abs_stx.FileContentsExplicitAbsStx( fs_abs_stx.ModificationType.CREATE, contents_abs_stx_builder.with_transformation( arbitrary_transformer.abstract_syntax))), ] return InvalidDestinationFileTestCasesData(file_contents_cases, symbols, src_file_in_hds_contents)
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_tree_of_transformations(self): # ARRANGE # to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'the_to_upper_transformer', string_transformers.to_uppercase()) keep_line_1 = StringTransformerSymbolContext.of_primitive( 'keep_line_1', string_transformers.keep_single_line(1)) keep_line_2 = StringTransformerSymbolContext.of_primitive( 'keep_line_2', string_transformers.keep_single_line(2)) equals_1st = StringMatcherSymbolContext.of_primitive( 'equals_1st', string_matchers.EqualsConstant('1ST\n')) equals_2nd = StringMatcherSymbolContext.of_primitive( 'equals_2nd', string_matchers.EqualsConstant('2ND\n')) model__original = '1st\n2nd\n' symbol_contexts = [ to_upper_transformer, keep_line_1, equals_1st, keep_line_2, equals_2nd, ] # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, args2.Transformed( to_upper_transformer.name__sym_ref_syntax, args2.Parentheses( args2.conjunction([ args2.Parentheses( args2.Transformed( keep_line_1.name__sym_ref_syntax, args2.SymbolReference(equals_1st.name), )), args2.Parentheses( args2.Transformed( keep_line_2.name__sym_ref_syntax, args2.SymbolReference(equals_2nd.name), )), ]))).as_arguments, model_constructor.of_str(self, model__original), arrangement_w_tcds(symbols=SymbolContext.symbol_table_of_contexts( symbol_contexts)), Expectation( ParseExpectation( symbol_references=SymbolContext. references_assertion_of_contexts(symbol_contexts)), ExecutionExpectation( main_result=asrt_matching_result.matches_value(True))))
def runTest(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') src_rel_opt_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) expected_file = fs.File('a-file-name.txt', src_file.contents.upper()) dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', to_uppercase(), ) symbols = to_upper_transformer.symbol_table transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name)), to_upper_transformer.abstract_syntax, ) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name), transformed_file_syntax, ) expected_non_hds_contents = dst_rel_opt_conf.assert_root_dir_contains_exactly( fs.DirContents([expected_file])) instruction_checker = self.conf.instruction_checker parser = self.conf.parser() for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx( instruction_syntax): with self.subTest(source_case.name): source = source_case.value.source # ACT # instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # source_case.value.expectation.apply_with_message( self, source, 'source-after-parse') # ACT & ASSERT # instruction_checker.check( self, instruction, self.conf.arrangement( pre_contents_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR, tcds_contents=src_rel_opt_conf. populator_for_relativity_option_root( DirContents([src_file])), symbols=symbols, ), self.conf.expect_success( symbol_usages=asrt.matches_sequence([ to_upper_transformer.reference_assertion, ]), main_side_effects_on_sds=expected_non_hds_contents, ))
def runTest(self): to_upper_transformer = NameAndValue( 'TRANSFORMER_SYMBOL', StringTransformerSdvConstantTestImpl(to_uppercase())) src_file = fs.File('src-file.txt', 'contents of source file') src_file_symbol = NameAndValue('SRC_FILE_SYMBOL', src_file.name) expected_dst_file = fs.File('dst-file-name.txt', src_file.contents.upper()) dst_file_symbol = NameAndValue('DST_FILE_SYMBOL', expected_dst_file.name) transformed_file_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( PathSymbolReferenceAbsStx(src_file_symbol.name)), str_trans_abs_stx.StringTransformerSymbolReferenceAbsStx( to_upper_transformer.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( PathSymbolReferenceAbsStx(dst_file_symbol.name), transformed_file_syntax, ) # ACT # for layout_case in tokens_layout.STANDARD_LAYOUT_SPECS: with self.subTest(layout_case.name): instruction = self.conf.parse_checker.parse__abs_stx( self, instruction_syntax, layout_case.value) assert isinstance( instruction, TestCaseInstructionWithSymbols) # Sanity check # ASSERT # expected_symbol_usages = [ asrt_sym_ref.matches_reference_2( dst_file_symbol.name, asrt_w_str_rend_rest.equals__w_str_rendering( path_or_string_reference_restrictions( new_file.REL_OPT_ARG_CONF.options. accepted_relativity_variants))), asrt_sym_ref.matches_reference_2( src_file_symbol.name, asrt_w_str_rend_rest.equals__w_str_rendering( path_or_string_reference_restrictions( src_rel_opt_arg_conf_for_phase( self.conf.phase_is_after_act()).options. accepted_relativity_variants))), is_reference_to_string_transformer__usage( to_upper_transformer.name), ] expected_symbol_references = asrt.matches_sequence( expected_symbol_usages) expected_symbol_references.apply_without_message( self, instruction.symbol_usages())
def runTest(self): # ARRANGE # arguments = ['arg'] str_src_contents = 'the_str_src_contents' stdin_syntax = str_src_abs_stx.StringSourceWithinParensAbsStx( str_src_abs_stx.StringSourceOfStringAbsStx.of_str( str_src_contents)) transformer_symbol = StringTransformerPrimitiveSymbolContext( 'TRANSFORMER', string_transformers.to_uppercase()) for pgm_and_args_case in pgm_and_args_cases.cases__w_argument_list__including_program_reference( ): program_w_stdin = FullProgramAbsStx( PgmAndArgsWArgumentsAbsStx( pgm_and_args_case.pgm_and_args, [ ArgumentOfRichStringAbsStx.of_str(arg) for arg in arguments ], ), stdin=stdin_syntax, transformation=transformer_symbol.abstract_syntax, ) symbols = list(pgm_and_args_case.symbols) + [transformer_symbol] def expected_program( env: AssertionResolvingEnvironment) -> Assertion[Program]: return asrt_pgm_val.matches_program( asrt_command.matches_command( driver=pgm_and_args_case.expected_command_driver(env), arguments=asrt.equals(arguments), ), stdin=asrt.matches_singleton_sequence( asrt_str_src.matches__str( asrt.equals(str_src_contents), )), transformer=asrt.matches_singleton_sequence( asrt.is_(transformer_symbol.primitive)), ) # ACT & ASSERT # CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, program_w_stdin, arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(symbols), tcds_contents=pgm_and_args_case.tcds, ), MultiSourceExpectation( symbol_references=SymbolContext. references_assertion_of_contexts(symbols), primitive=expected_program, ), sub_test_identifiers={'command': pgm_and_args_case.name})
def runTest(self): text_printed_by_program = 'single line of output' expected_file_contents = text_printed_by_program.upper() expected_file = fs.File('dst-file.txt', expected_file_contents) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TO_UPPER_CASE', to_uppercase(), ) symbols = to_upper_transformer.symbol_table dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP) program_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfPythonInterpreterAbsStx. of_execute_python_src_string( py_programs.single_line_pgm_that_prints_to( ProcOutputFile.STDOUT, text_printed_by_program)), transformation=to_upper_transformer.abstract_syntax, )) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.name), program_syntax, ) instruction_checker = self.conf.instruction_checker parser = self.conf.parser() for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx( instruction_syntax): with self.subTest(source_case.name): source = source_case.value.source # ACT # instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # source_case.value.expectation.apply_with_message( self, source, 'source-after-parse') # ACT & ASSERT # instruction_checker.check( self, instruction, self.conf.arrangement( pre_contents_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR, symbols=symbols), self.conf.expect_success( symbol_usages=asrt.matches_sequence([ to_upper_transformer.reference_assertion, ]), main_side_effects_on_sds=non_hds_dir_contains_exactly( dst_rel_opt_conf.root_dir__non_hds, fs.DirContents([expected_file])), ))
def test__with_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') expected_file = fs.File('a-file-name.txt', src_file.contents.upper()) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_rel_opt_conf = ALLOWED_SRC_FILE_RELATIVITIES__BEFORE_ACT[0] dst_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_ACT) expected_non_hds_contents = self._expected_non_hds_contents( dst_rel_opt_conf, expected_file, src_rel_opt_conf, src_file, ) transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name)), to_upper_transformer.abstract_syntax, ) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_rel_opt_conf.path_abs_stx_of_name(expected_file.file_name), transformed_file_contents_abs_stx, ) symbols = to_upper_transformer.symbol_table for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) with self.subTest(phase_is_after_act=phase_is_after_act): # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, tcds_contents=src_rel_opt_conf. populator_for_relativity_option_root( DirContents([src_file])), ), symbols=symbols, ), MultiSourceExpectation.phase_agnostic( main_result=IS_SUCCESS, main_side_effects_on_sds=expected_non_hds_contents, symbol_usages=to_upper_transformer.usages_assertion, ))
def _check_of_invalid_src_file( self, is_after_act_2_every_src_file_rel_conf: Callable[ [bool], Sequence[RelativityOptionConfiguration]], step_of_expected_failure: Step): # ARRANGE # transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) symbols = transformer.symbol_table dst_file_rel_conf = conf_rel_any(RelOptionType.REL_TMP) expectation_ = self._expect_failure_in(step_of_expected_failure) for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) for src_file_rel_conf in is_after_act_2_every_src_file_rel_conf( phase_is_after_act): contents_builder = string_source_abs_stx.TransformableAbsStxBuilder( string_source_abs_stx.StringSourceOfFileAbsStx( src_file_rel_conf.path_abs_stx_of_name( self.src_file_name))) for actual_src_file_variant in self.src_file_variants: for contents_arguments in contents_builder.with_and_without_transformer_cases( transformer.abstract_syntax): instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_file_rel_conf.path_abs_stx_of_name( 'dst-file.txt'), contents_arguments.value, ) # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, tcds_contents=src_file_rel_conf. populator_for_relativity_option_root( actual_src_file_variant.value), ), symbols=symbols, ), expectation_, sub_test_identifiers={ 'phase_is_after_act': phase_is_after_act, 'contents': contents_arguments.name, 'relativity_of_src_path': src_file_rel_conf.name, })
def runTest(self): # ARRANGE # to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_file = fs.File('src-file.txt', 'contents of source file') src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) expected_contents = src_file.contents.upper() path_relativity_variants = PathRelativityVariants({src_file_rel_conf.relativity}, True) checker = integration_check.checker(RelOptionsConfiguration(path_relativity_variants, src_file_rel_conf.relativity)) src_file_symbol = ConstantSuffixPathDdvSymbolContext( 'SRC_FILE_SYMBOL', src_file_rel_conf.relativity, src_file.name, path_relativity_variants, ) string_source_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx(src_file_symbol.abstract_syntax), to_upper_transformer.abstract_syntax, ) symbol_table = SymbolContext.symbol_table_of_contexts([ src_file_symbol, to_upper_transformer, ]) # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(string_source_syntax), arrangement_w_tcds( symbols=symbol_table, hds_contents=src_file_rel_conf.populator_for_relativity_option_root__hds( DirContents([src_file])) ), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=True, ), symbol_references=asrt.matches_sequence([ src_file_symbol.reference_assertion__path_or_string, to_upper_transformer.reference_assertion, ]), ), )
def runTest(self): # ARRANGE # transformer = StringTransformerPrimitiveSymbolContext( 'STRING_TRANSFORMER', string_transformers.to_uppercase()) after_bin_op = 'after bin op' after_bin_op_syntax = CustomStringTransformerAbsStx.of_str( after_bin_op) composition_string_transformer = StringTransformerCompositionAbsStx( [transformer.abstract_syntax, after_bin_op_syntax], within_parens=False, allow_elements_on_separate_lines=False, ) expected_source_after_parse = asrt_source.is_at_line( current_line_number=2, remaining_part_of_current_line=' '.join( [composition_string_transformer.operator_name(), after_bin_op]), ) for pgm_and_args_case in pgm_and_args_cases.cases_w_and_wo_argument_list__including_program_reference( ): command_followed_by_transformer = FullProgramAbsStx( pgm_and_args_case.pgm_and_args, transformation=composition_string_transformer, ) symbols = list(pgm_and_args_case.symbols) + [transformer] with self.subTest(command=pgm_and_args_case.name): source = remaining_source( command_followed_by_transformer.tokenization().layout( LayoutSpec.of_default())) # ACT & ASSERT # CHECKER_WO_EXECUTION.check( self, source, None, pgm_and_args_case.mk_arrangement( SymbolContext.symbol_table_of_contexts(symbols)), Expectation( ParseExpectation( source=expected_source_after_parse, symbol_references=SymbolContext. references_assertion_of_contexts(symbols), ), primitive=lambda env: (asrt_pgm_val.matches_program( asrt_command. matches_command(driver=pgm_and_args_case. expected_command_driver(env), arguments=asrt.is_empty_sequence), stdin=asrt_pgm_val.is_no_stdin(), transformer=asrt.matches_singleton_sequence( asrt.is_(transformer.primitive)), ))))
def runTest(self): # ARRANGE # named_transformer = StringTransformerSymbolContext.of_primitive( _TRANSFORMER_SYMBOL_NAME, string_transformers.to_uppercase() ) contents_generator = contents_transformation.TransformedContentsSetup( original='some\ntext', transformed='SOME\nTEXT', ) symbols = self.rel_opt.symbols.in_arrangement() symbols.put(named_transformer.name, named_transformer.symbol_table_container) expected_symbol_reference_to_transformer = is_reference_to_string_transformer(named_transformer.name) expected_symbol_references = asrt.matches_sequence( [expected_symbol_reference_to_transformer] + self.rel_opt.symbols.usage_expectation_assertions() ) self._check_with_source_variants( test_configuration.arguments_for( args( '{transform_option} {transformer} {maybe_not} {equals} ' '{file_option} {relativity_option} expected.txt', transformer=named_transformer.name, maybe_not=self.not_opt.nothing__if_positive__not_option__if_negative, relativity_option=self.rel_opt.option_argument)), model_constructor.of_str(self, contents_generator.original), arrangement_w_tcds( tcds_contents=self.rel_opt.populator_for_relativity_option_root(DirContents([ File('expected.txt', contents_generator.transformed) ])), post_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY, symbols=symbols, ), Expectation( ParseExpectation( symbol_references=expected_symbol_references, ), ExecutionExpectation( main_result=self.not_opt.pass__if_positive__fail__if_negative, ), ), )
def _check_of_invalid_src_file( self, src_file_relativity: RelOptionType, validation: ValidationAssertions, ): # ARRANGE # checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_file_relativity)) expectation_ = MultiSourceExpectation( symbol_references=asrt.anything_goes(), execution=ExecutionExpectation( validation=validation ) ) src_file_rel_conf = conf_rel_any(src_file_relativity) transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) symbols = transformer.symbol_table contents_builder = string_source_abs_stx.TransformableAbsStxBuilder( string_source_abs_stx.StringSourceOfFileAbsStx( src_file_rel_conf.path_abs_stx_of_name(self.src_file_name) ) ) for actual_src_file_variant in self.src_file_variants: for contents_arguments in contents_builder.with_and_without_transformer_cases( transformer.abstract_syntax): with self.subTest(src_file_variant=actual_src_file_variant.name, contents=contents_arguments.name, relativity_of_src_path=src_file_rel_conf.option_argument): # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(contents_arguments.value), arrangement_w_tcds( tcds_contents=src_file_rel_conf.populator_for_relativity_option_root( actual_src_file_variant.value), symbols=symbols, ), expectation_, )
def runTest(self): # ARRANGE # original_model_contents = 'some text' expected_model_contents = original_model_contents.upper() cases = [ NEA( 'transformation that makes matcher match', True, string_transformers.to_uppercase(), ), NEA( 'transformation that makes matcher NOT match', False, delete_everything(), ), ] # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check_multi__w_source_variants( self, args2.Transformed( _TRANSFORMER_SYMBOL_NAME, args2.Equals.eq_string( surrounded_by_hard_quotes_str( expected_model_contents))).as_arguments, asrt.matches_singleton_sequence( is_reference_to_string_transformer(_TRANSFORMER_SYMBOL_NAME)), model_constructor.of_str(self, original_model_contents), [ NExArr( case.name, PrimAndExeExpectation.of_exe( main_result=asrt_matching_result.matches_value( case.expected), ), arrangement_w_tcds( symbols=StringTransformerSymbolContext.of_primitive( _TRANSFORMER_SYMBOL_NAME, case.actual).symbol_table, )) for case in cases ], )
def test__with_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') expected_contents = src_file.contents.upper() to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP) transformed_file_contents_abs_stx = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name) ), to_upper_transformer.abstract_syntax, ) symbols = to_upper_transformer.symbol_table checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity)) # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(transformed_file_contents_abs_stx), arrangement_w_tcds( tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root( DirContents([src_file])), symbols=symbols, ), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=True, ), symbol_references=to_upper_transformer.references_assertion, ) )
def runTest(self): model__original = 'the model text' the_model_constructor = model_constructor.of_str(self, model__original) string_transformer = StringTransformerSymbolContext.of_primitive( 'THE_STRING_TRANSFORMER', string_transformers.to_uppercase()) sm_equals = StringMatcherSymbolContext.of_primitive( 'STRING_MATCHER_1', string_matchers.EqualsConstant(model__original.upper())) symbol = [ string_transformer, sm_equals, ] after_bin_op = 'after bin op' sm_conjunction = args2.conjunction([ args2.SymbolReference(sm_equals.name), args2.Custom(after_bin_op), ]) arguments = args2.Transformed(string_transformer.name__sym_ref_syntax, sm_conjunction) integration_check.CHECKER__PARSE_SIMPLE.check( self, source=arguments.as_remaining_source, input_=the_model_constructor, arrangement=arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(symbol), ), expectation=Expectation( ParseExpectation(source=asrt_source.is_at_line( current_line_number=1, remaining_part_of_current_line=' '.join( [sm_conjunction.operator, after_bin_op])), symbol_references=SymbolContext. references_assertion_of_contexts(symbol)), ExecutionExpectation( main_result=asrt_matching_result.matches_value(True))), )
from exactly_lib_test.symbol.test_resources.symbol_context import SymbolContext from exactly_lib_test.type_val_deps.types.string_transformer.test_resources.symbol_context import \ StringTransformerPrimitiveSymbolContext from exactly_lib_test.type_val_prims.string_transformer.test_resources import string_transformers DELETE_EVERYTHING_TRANSFORMER = StringTransformerPrimitiveSymbolContext( 'DELETE_EVERYTHING_TRANSFORMER', string_transformers.delete_everything()) DUPLICATE_WORDS_TRANSFORMER = StringTransformerPrimitiveSymbolContext( 'DUPLICATE_WORDS_TRANSFORMER', string_transformers.duplicate_words()) DELETE_INITIAL_WORD_TRANSFORMER = StringTransformerPrimitiveSymbolContext( 'DELETE_INITIAL_WORD_TRANSFORMER', string_transformers.delete_initial_word()) TO_UPPER_CASE_TRANSFORMER = StringTransformerPrimitiveSymbolContext( 'TO_UPPER_CASE_TRANSFORMER', string_transformers.to_uppercase()) SYMBOL_TABLE = SymbolContext.symbol_table_of_contexts([ DELETE_EVERYTHING_TRANSFORMER, DUPLICATE_WORDS_TRANSFORMER, DELETE_INITIAL_WORD_TRANSFORMER, TO_UPPER_CASE_TRANSFORMER, ])
def test_transformation_in_referenced_program_and_as_source_argument(self): # ARRANGE # transformer__in_referenced_program = StringTransformerPrimitiveSymbolContext( 'TRANSFORMER_IN_REFERENCED_PROGRAM', string_transformers.delete_everything()) referenced_program__system_program = 'the-system-program' referenced_program__sdv = program_sdvs.system_program( string_sdvs.str_constant(referenced_program__system_program), transformations=[transformer__in_referenced_program.reference_sdv]) referenced_program = ProgramSymbolContext.of_sdv( 'REFERENCED_PROGRAM', referenced_program__sdv) transformer__in_source = StringTransformerPrimitiveSymbolContext( 'TRANSFORMER_IN_SOURCE', string_transformers.to_uppercase()) symbols__symbol_table = [ referenced_program, transformer__in_referenced_program, transformer__in_source ] symbols__expected_references = [ referenced_program, transformer__in_source ] arguments_cases = [ NameAndValue( 'no arguments', [], ), NameAndValue( 'single argument', ['arg1'], ) ] for arguments_case in arguments_cases: with self.subTest(arguments=arguments_case.name): program_w_transformer = FullProgramAbsStx( ProgramOfSymbolReferenceAbsStx(referenced_program.name, [ ArgumentOfRichStringAbsStx.of_str(arg) for arg in arguments_case.value ]), transformation=transformer__in_source.abstract_syntax) expected_primitive = asrt_pgm_val.matches_program( asrt_command.matches_command( driver=asrt_command. matches_system_program_command_driver( asrt.equals(referenced_program__system_program)), arguments=asrt.equals(arguments_case.value), ), stdin=asrt_pgm_val.is_no_stdin(), transformer=asrt.matches_sequence([ asrt.is_(transformer__in_referenced_program.primitive), asrt.is_(transformer__in_source.primitive), ]), ) # ACT & ASSERT # CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, program_w_transformer, arrangement_wo_tcds( symbols=SymbolContext.symbol_table_of_contexts( symbols__symbol_table), ), MultiSourceExpectation.of_const( symbol_references=SymbolContext. references_assertion_of_contexts( symbols__expected_references), primitive=expected_primitive, ))
def test_string_transformer_should_be_parsed_as_simple_expression(self): # ARRANGE # the_layout = LayoutSpec.of_default() src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP) src_file = fs.File('source-file.txt', 'contents of source file') expected_contents = src_file.contents.upper() to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) str_trans__unused = StringTransformerSymbolReferenceAbsStx('UNUSED_TRANSFORMER') transformation_w_infix_op = str_trans_abs_stx.StringTransformerCompositionAbsStx( [ to_upper_transformer.abstract_syntax, str_trans__unused, ], within_parens=False, allow_elements_on_separate_lines=False, ) expected_remaining_tokens = TokenSequence.concat([ TokenSequence.singleton(str_trans_abs_stx.names.SEQUENCE_OPERATOR_NAME), str_trans__unused.tokenization(), ]) expected_remaining_source = expected_remaining_tokens.layout(the_layout) file_contents_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name) ), transformation_w_infix_op ) checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity)) # ACT & ASSERT # checker.check__abs_stx( self, file_contents_syntax, None, arrangement_w_tcds( tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root( fs.DirContents([src_file]) ), symbols=to_upper_transformer.symbol_table ), Expectation.of_prim__const( parse=ParseExpectation( source=asrt_source.source_is_not_at_end( remaining_source=asrt.equals(expected_remaining_source) ), symbol_references=to_upper_transformer.references_assertion, ), primitive=asrt_string_source.pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=True, ) ), the_layout )
program_w_complex_str_trans_wo_parentheses, ignore_exit_code=ignore_exit_code, ) with self.subTest(output_file=output_file, ignore_exit_code=ignore_exit_code): checker.check__abs_stx__wo_input( self, syntax, arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( symbols), tcds_contents=py_file_rel_conf. populator_for_relativity_option_root( DirContents([py_program_file]))), Expectation( ParseExpectation( source=asrt_source.source_is_not_at_end( remaining_source=asrt.equals( expected_remaining_source)), symbol_references=SymbolContext. references_assertion_of_contexts(symbols), )), the_layout, ) TO_UPPER_TRANSFORMER_SYMBOL = StringTransformerSymbolContext.of_primitive( 'TO_UPPER_TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), )
def _arbitrary_string_transformer() -> StringTransformerSdv: return StringTransformerSdvConstant(to_uppercase())
def runTest(self): non_zero_exit_code = 1 text_printed_by_program = 'the output from the program' py_file = File( 'exit-with-hard-coded-exit-code.py', py_programs.py_pgm_with_stdout_stderr_exit_code( stdout_output=text_printed_by_program, stderr_output='', exit_code=non_zero_exit_code, ), ) expected_file = fs.File( 'dst-file.txt', text_printed_by_program.upper(), ) to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TO_UPPER_CASE', to_uppercase(), ) symbols = to_upper_transformer.symbol_table py_src_file_rel_opt_conf = rel_opt.conf_rel_any( RelOptionType.REL_HDS_CASE) dst_file_rel_opt_conf = conf_rel_non_hds(RelNonHdsOptionType.REL_TMP) program_string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfPythonInterpreterAbsStx. of_execute_python_src_file( py_src_file_rel_opt_conf.path_abs_stx_of_name( py_file.name)), transformation=to_upper_transformer.abstract_syntax, ), ignore_exit_code=True, ) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_file_rel_opt_conf.path_abs_stx_of_name(expected_file.name), program_string_source_syntax, ) instruction_checker = self.conf.instruction_checker parser = self.conf.parser() for source_case in equivalent_source_variants__with_source_check__consume_last_line__abs_stx( instruction_syntax): with self.subTest(source_case.name): source = source_case.value.source # ACT # instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # source_case.value.expectation.apply_with_message( self, source, 'source-after-parse') # ACT & ASSERT # instruction_checker.check( self, instruction, self.conf.arrangement( pre_contents_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR, symbols=symbols, tcds_contents=py_src_file_rel_opt_conf. populator_for_relativity_option_root( DirContents([py_file]))), self.conf.expect_success( symbol_usages=asrt.matches_sequence([ to_upper_transformer.reference_assertion, ]), main_side_effects_on_sds=non_hds_dir_contains_exactly( dst_file_rel_opt_conf.root_dir__non_hds, fs.DirContents([expected_file])), ))
def test_symbol_usages(self): # ARRANGE # to_upper_transformer = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER_SYMBOL', string_transformers.to_uppercase(), ) src_file = fs.File('src-file.txt', 'contents of source file') src_file_rel_conf = conf_rel_hds(RelHdsOptionType.REL_HDS_CASE) expected_dst_file = fs.File('dst-file-name.txt', src_file.contents.upper()) dst_file_symbol = ConstantSuffixPathDdvSymbolContext( 'DST_FILE_SYMBOL', RelOptionType.REL_TMP, expected_dst_file.name, sut.REL_OPT_ARG_CONF.options.accepted_relativity_variants, ) for phase_is_after_act in [False, True]: checker = integration_check.checker(phase_is_after_act) src_file_rel_opt_conf = src_rel_opt_arg_conf_for_phase( phase_is_after_act) src_file_symbol = ConstantSuffixPathDdvSymbolContext( 'SRC_FILE_SYMBOL', src_file_rel_conf.relativity_option, src_file.name, src_file_rel_opt_conf.options.accepted_relativity_variants, ) transformed_file_contents = string_source_abs_stx.TransformedStringSourceAbsStx( string_source_abs_stx.StringSourceOfFileAbsStx( src_file_symbol.abstract_syntax), StringTransformerSymbolReferenceAbsStx( to_upper_transformer.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( dst_file_symbol.abstract_syntax, transformed_file_contents, ) symbols = SymbolContext.symbol_table_of_contexts([ dst_file_symbol, src_file_symbol, to_upper_transformer, ]) with self.subTest(phase_is_after_act=phase_is_after_act): # ACT & ASSERT # checker.check__abs_stx__std_layouts_and_source_variants( self, instruction_syntax, Arrangement.phase_agnostic( symbols=symbols, tcds=TcdsArrangement( hds_contents=src_file_rel_conf. populator_for_relativity_option_root__hds( DirContents([src_file]))), ), MultiSourceExpectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=asrt.matches_sequence([ dst_file_symbol. reference_assertion__path_or_string, src_file_symbol. reference_assertion__path_or_string, is_reference_to_string_transformer__usage( to_upper_transformer.name), ]), ), )