def _test_symbol_reference_in_contents( self, symbol_ref_syntax_2_contents_arguments: Callable[ [str], StringSourceAbsStx], symbol_value_2_expected_contents: Callable[[str], str]): contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'contents symbol value', default_restrictions=asrt_rest.is__w_str_rendering(), ) expected_contents = symbol_value_2_expected_contents( contents_symbol.str_value) symbols = [contents_symbol] expected_symbol_references = SymbolContext.references_assertion_of_contexts( symbols) symbol_table = SymbolContext.symbol_table_of_contexts(symbols) string_source_syntax = symbol_ref_syntax_2_contents_arguments( symbol_reference_syntax_for_name(contents_symbol.name)) CHECKER.check__abs_stx( self, string_source_syntax, None, arrangement_w_tcds(symbols=symbol_table, ), Expectation.of_prim__const( primitive=asrt_string_source. pre_post_freeze__matches_str__const( expected_contents, may_depend_on_external_resources=False), parse=ParseExpectation( symbol_references=expected_symbol_references, ), ))
def runTest(self): # ARRANGE # program_ref_case = pgm_and_args_cases.program_reference__w_argument_list() def expected_program(env: AssertionResolvingEnvironment) -> Assertion[Program]: return asrt_pgm_val.matches_program( asrt_command.matches_command( driver=program_ref_case.expected_command_driver(env), arguments=asrt.is_empty_sequence, ), stdin=asrt_pgm_val.is_no_stdin(), transformer=asrt_pgm_val.is_no_transformation(), ) # ACT & ASSERT # CHECKER_WO_EXECUTION.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, OptionallyOnNewLine(WithinParensAbsStx(program_ref_case.pgm_and_args, end_paren_on_separate_line=False)), arrangement_wo_tcds( symbols=SymbolContext.symbol_table_of_contexts(program_ref_case.symbols), ), MultiSourceExpectation( symbol_references=SymbolContext.references_assertion_of_contexts(program_ref_case.symbols), primitive=expected_program, ) )
def _check_with_exit_code(self, exit_code: int): # ARRANGE # result = SubProcessResult( exitcode=exit_code, stdout='output on stdout', stderr='output on stderr', ) command_py_program = py_program.program_that_prints_and_exits_with_exit_code(result) py_file = fs.File( 'the-program.py', command_py_program, ) program_wo_transformation = ProgramSymbolContext.of_sdv( 'PROGRAM_SYMBOL', program_sdvs.interpret_py_source_file_that_must_exist( path_sdvs.of_rel_option_with_const_file_name( RelOptionType.REL_HDS_CASE, py_file.name, ) ) ) source = args.program( args.symbol_ref_command_line(program_wo_transformation.name), transformation=TO_UPPER_CASE_TRANSFORMER.name__sym_ref_syntax) symbols = [ program_wo_transformation, TO_UPPER_CASE_TRANSFORMER, ] # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr(source.as_arguments.lines)], arrangement_w_tcds( symbol_table=SymbolContext.symbol_table_of_contexts(symbols), hds_contents=hds_populators.contents_in( RelHdsOptionType.REL_HDS_CASE, DirContents([py_file]), ) ), Expectation( symbol_usages=SymbolContext.references_assertion_of_contexts(symbols), execute=asrt_eh.is_exit_code(result.exitcode), post_sds=PostSdsExpectation.constant( sub_process_result_from_execute=asrt_proc_result.matches_proc_result( exit_code=asrt.equals(result.exitcode), stdout=asrt.equals(result.stdout.upper()), stderr=asrt.equals(result.stderr), ) ) ), )
def runTest(self): # ARRANGE # command_py_program = py_program.exit_with_code(0) py_file = fs.File( 'the-program.py', lines_content(command_py_program), ) program_wo_transformation = ProgramSymbolContext.of_sdv( 'PROGRAM_SYMBOL', program_sdvs.interpret_py_source_file_that_must_exist( path_sdvs.of_rel_option_with_const_file_name( RelOptionType.REL_HDS_CASE, py_file.name, ) ) ) error_message = 'error message from transformer' transformer = StringTransformerPrimitiveSymbolContext( 'HARD_ERROR_TRANSFORMER', string_transformers.model_access_raises_hard_error(error_message), ) source = args.program( args.symbol_ref_command_line(program_wo_transformation.name), transformation=transformer.name__sym_ref_syntax) symbols = [ program_wo_transformation, transformer, ] # ACT & ASSERT # integration_check.check_execution( self, sut.actor(), [instr(source.as_arguments.lines)], arrangement_w_tcds( symbol_table=SymbolContext.symbol_table_of_contexts(symbols), hds_contents=hds_populators.contents_in( RelHdsOptionType.REL_HDS_CASE, DirContents([py_file]), ) ), Expectation( symbol_usages=SymbolContext.references_assertion_of_contexts(symbols), execute=asrt_eh.matches_hard_error( asrt_failure_details.is_failure_message_matching__td( asrt_text_doc.is_string_for_test_that_equals(error_message) ) ), ), )
def _test_symbol_reference_in_dst_file_and_contents( self, symbol_ref_syntax_2_contents_arguments: Callable[ [str], StringSourceAbsStx], symbol_value_2_expected_contents: Callable[[str], str]): sub_dir_symbol = ConstantSuffixPathDdvSymbolContext( 'sub_dir_symbol', RelOptionType.REL_ACT, 'sub-dir', ACCEPTED_DST_RELATIVITY_VARIANTS, ) contents_symbol = StringConstantSymbolContext( 'contents_symbol_name', 'contents symbol value', default_restrictions=asrt_rest.is__w_str_rendering(), ) expected_file_contents = symbol_value_2_expected_contents( contents_symbol.str_value) expected_file = fs.File('a-file-name.txt', expected_file_contents) symbols = [sub_dir_symbol, contents_symbol] expected_symbol_references = SymbolContext.references_assertion_of_contexts( symbols) symbol_table = SymbolContext.symbol_table_of_contexts(symbols) contents_arguments = symbol_ref_syntax_2_contents_arguments( symbol_reference_syntax_for_name(contents_symbol.name)) instruction_syntax = instr_abs_stx.create_w_explicit_contents( path_abs_stx.PathStringAbsStx.of_plain_components( [sub_dir_symbol.name__sym_ref_syntax, expected_file.name]), contents_arguments, ) integration_check.CHECKER__AFTER_ACT.check__abs_stx( self, instruction_syntax, Arrangement.phase_agnostic( tcds=TcdsArrangement( pre_population_action= SETUP_CWD_INSIDE_SDS_BUT_NOT_A_SDS_DIR__PLAIN, ), symbols=symbol_table, ), Expectation.phase_agnostic( main_result=IS_SUCCESS, symbol_usages=expected_symbol_references, main_side_effects_on_sds=dir_contains_exactly( sub_dir_symbol.rel_option_type, fs.DirContents( [fs.Dir(sub_dir_symbol.path_suffix, [expected_file])])), ))
def sdv_assertion( self, tcds: TestCaseDs, arguments: Assertion[Sequence[str]], arguments_symbols: Sequence[SymbolContext] = (), ) -> Assertion[SymbolDependentValue]: expected_command = asrt_command.matches_command( driver=self.expected_command_driver(tcds), arguments=arguments) symbols = tuple(self.symbols) + tuple(arguments_symbols) return matches_sdv( asrt.is_instance(CommandSdv), symbols=SymbolContext.symbol_table_of_contexts(symbols), references=SymbolContext.references_assertion_of_contexts(symbols), resolved_value=asrt.is_instance_with( CommandDdv, matches_dir_dependent_value(lambda _: expected_command)), )
def check_matcher_should_be_parsed_as_full_expression(put: unittest.TestCase, symbol_1: MatcherTypeSymbolContext, symbol_2: MatcherTypeSymbolContext, value_type: ValueType, ): # ARRANGE # symbols = [symbol_1, symbol_2] value_argument = matcher_argument.disjunction([symbol_1.argument, symbol_2.argument]) defined_name = 'the_defined_name' source = remaining_source( src3(value_type, defined_name, value_argument.as_str, ), ) # EXPECTATION # expected_symbol_references = SymbolContext.references_assertion_of_contexts(symbols) expected_container = matches_container( asrt.equals(value_type), type_sdv_assertions.matches_sdv_of_file_matcher( references=expected_symbol_references, primitive_value=asrt.anything_goes(), symbols=SymbolContext.symbol_table_of_contexts(symbols) ) ) expected_symbol_usages = asrt.matches_singleton_sequence( asrt_sym_usage.matches_definition(asrt.equals(defined_name), expected_container) ) expected_source = asrt_source.source_is_at_end # ACT # actual = sut.PARTS_PARSER.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # expected_source.apply_with_message(put, source, 'source') expected_symbol_usages.apply_with_message(put, actual.symbol_usages, 'symbol usages')
def runTest(self): # ARRANGE # for pgm_and_args_case in pgm_and_args_cases.cases_w_and_wo_argument_list__including_program_reference(): 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_consume_until_end_of_last_line__s__nsc, pgm_and_args_case.pgm_and_args, pgm_and_args_case.mk_arrangement(SymbolContext.symbol_table_of_contexts(pgm_and_args_case.symbols)), MultiSourceExpectation( symbol_references=SymbolContext.references_assertion_of_contexts(pgm_and_args_case.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_pgm_val.is_no_transformation(), ) ) ) )
def references_assertion(self) -> Assertion[Sequence[SymbolReference]]: return SymbolContext.references_assertion_of_contexts(self.symbols)
def runTest(self): fn_f_1 = _fn_symbol('file__name__1', 'file-1.txt') fn_f_2 = _fn_symbol('file__name__2', 'file-2.txt') fn_f_3 = _fn_symbol('file__name__3', fn_f_2.str_value) fn_d_1 = _fn_symbol('dir__name__1', 'dir-1') fn_d_2 = _fn_symbol('dir__name__2', 'dir-2') fn_d_3 = _fn_symbol('dir__name__3', fn_d_2.str_value) contents_f_2 = StringSourceSymbolContextOfPrimitiveConstant('file_contents_2', 'contents of file 2') contents_f_3 = StringSourceSymbolContextOfPrimitiveConstant('file_contents_3', 'contents of file 3') contents_d_2 = FilesSourceSymbolContext.of_primitive('dir_contents_2', primitives.FilesSourceThatDoesNothingTestImpl()) contents_d_3 = FilesSourceSymbolContext.of_primitive('dir_contents_3', primitives.FilesSourceThatDoesNothingTestImpl()) expected_created_files = [ fs.File.empty(fn_f_1.str_value), fs.File(fn_f_2.str_value, contents_f_2.contents_str + contents_f_3.contents_str), fs.Dir.empty(fn_d_1.str_value), fs.Dir.empty(fn_d_2.str_value), ] all_symbols = [ fn_f_1, fn_f_2, contents_f_2, fn_f_3, contents_f_3, fn_d_1, fn_d_2, contents_d_2, fn_d_3, contents_d_3, ] syntax = LiteralFilesSourceAbsStx([ _spec_regular(fn_f_1, abs_stx.FileContentsEmptyAbsStx()), _spec_regular(fn_f_2, abs_stx.FileContentsExplicitAbsStx( ModificationType.CREATE, contents_f_2.abstract_syntax, )), _spec_regular(fn_f_3, abs_stx.FileContentsExplicitAbsStx( ModificationType.APPEND, contents_f_3.abstract_syntax, )), _spec_dir(fn_d_1, abs_stx.DirContentsEmptyAbsStx()), _spec_dir(fn_d_2, abs_stx.DirContentsExplicitAbsStx( ModificationType.CREATE, contents_d_2.abstract_syntax, )), _spec_dir(fn_d_3, abs_stx.DirContentsExplicitAbsStx( ModificationType.APPEND, contents_d_3.abstract_syntax, )), ]) # ACT & ASSERT # integration_check.CHECKER.check__abs_stx__layout__std_source_variants( self, syntax, (), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(all_symbols) ), MultiSourceExpectation( symbol_references=SymbolContext.references_assertion_of_contexts(all_symbols), execution=ExecutionExpectation( main_result=asrt_fs.dir_contains_exactly_2( expected_created_files ) ) ) )
def _check_exit_codes(self, exit_code_cases: List[int], ignore_exit_code: bool, expected_primitive: Callable[[str], Assertion[StringSource]], ): # ARRANGE # program_output = { ProcOutputFile.STDOUT: 'output on stdout', ProcOutputFile.STDERR: 'output on stderr', } transformer = TO_UPPER_TRANSFORMER_SYMBOL sym_ref_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME') program_builder = program_abs_stx.TransformableProgramAbsStxBuilder( ProgramOfSymbolReferenceAbsStx(sym_ref_program.symbol_name) ) program_cases = [ ProgramAndSymbolsCase( 'without transformation', program_builder.without_transformation(), [], adapt_expected_program_output=lambda s: s ), ProgramAndSymbolsCase( 'with transformation', program_builder.with_transformation(transformer.abstract_syntax), [transformer], adapt_expected_program_output=str.upper ), ] program_builder.with_and_without_transformer_cases(transformer.abstract_syntax) py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE) for exit_code in exit_code_cases: py_file = File('exit-with-hard-coded-exit-code.py', py_programs.py_pgm_with_stdout_stderr_exit_code( exit_code=exit_code, stdout_output=program_output[ProcOutputFile.STDOUT], stderr_output=program_output[ProcOutputFile.STDERR], ), ) py_file_conf = py_file_rel_conf.named_file_conf(py_file.name) program_symbol = ProgramSymbolContext.of_sdv( sym_ref_program.symbol_name, program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv) ) for output_file in ProcOutputFile: for program_case in program_cases: syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( output_file, program_case.syntax, ignore_exit_code=ignore_exit_code, ) expected_program_output = program_case.adapt_expected_program_output(program_output[output_file]) symbol_contexts = [program_symbol] + program_case.additional_symbols # ACT && ASSERT # checker = integration_check.checker__w_arbitrary_file_relativities() with self.subTest(exit_code=exit_code, output_file=output_file, program=program_case.name): checker.check__abs_stx( self, syntax, None, arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(symbol_contexts), tcds_contents=py_file_rel_conf.populator_for_relativity_option_root( DirContents([py_file]) ) ), Expectation( ParseExpectation( symbol_references=SymbolContext.references_assertion_of_contexts(symbol_contexts), ), primitive=prim_asrt__constant( expected_primitive(expected_program_output) ), ), ),
def test_syntax_layout_variants(self): # ARRANGE # exit_code_from_program = 1 output_from_program = 'the output from the program' transformer = TO_UPPER_TRANSFORMER_SYMBOL sym_ref_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME') program_builder = program_abs_stx.TransformableProgramAbsStxBuilder( ProgramOfSymbolReferenceAbsStx(sym_ref_program.symbol_name) ) output_cases = [ OutputFileCase( ProcOutputFile.STDOUT, { ProcOutputFile.STDOUT: output_from_program, ProcOutputFile.STDERR: '', } ), OutputFileCase( ProcOutputFile.STDERR, { ProcOutputFile.STDOUT: '', ProcOutputFile.STDERR: output_from_program, } ), ] program_cases = [ ProgramAndSymbolsCase( 'without transformation', program_builder.without_transformation(), [], adapt_expected_program_output=lambda s: s ), ProgramAndSymbolsCase( 'with transformation', program_builder.with_transformation(transformer.abstract_syntax), [transformer], adapt_expected_program_output=str.upper ), ] py_file_rel_conf = rel_opt.conf_rel_any(RelOptionType.REL_HDS_CASE) for output_case in output_cases: py_file = File('exit-with-hard-coded-exit-code.py', py_programs.py_pgm_with_stdout_stderr_exit_code_2( exit_code=exit_code_from_program, output=output_case.program_output, ), ) py_file_conf = py_file_rel_conf.named_file_conf(py_file.name) program_symbol = ProgramSymbolContext.of_sdv( sym_ref_program.symbol_name, program_sdvs.interpret_py_source_file_that_must_exist(py_file_conf.path_sdv) ) for program_case in program_cases: syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( output_case.output_file, program_case.syntax, ignore_exit_code=True, ) expected_program_output = program_case.adapt_expected_program_output(output_from_program) symbol_contexts = [program_symbol] + program_case.additional_symbols # ACT && ASSERT # checker = integration_check.checker__w_arbitrary_file_relativities() with self.subTest(output_file=output_case.output_file, program=program_case.name): checker.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_full_line_expr_parse__s__nsc, OptionallyOnNewLine(syntax), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(symbol_contexts), tcds_contents=py_file_rel_conf.populator_for_relativity_option_root( DirContents([py_file]) ) ), MultiSourceExpectation.of_const( symbol_references=SymbolContext.references_assertion_of_contexts(symbol_contexts), primitive=asrt_string_source.pre_post_freeze__matches_str__const_2( expected_program_output, may_depend_on_external_resources=True, frozen_may_depend_on_external_resources=asrt.anything_goes(), ), ), ),