def runTest(self): # ARRANGE # model_contents = 'the contents of stdin of program' stdin_string_source_syntax = string_source_abs_stx.StringSourceOfStringAbsStx.of_str_hard( model_contents, ) for output_file in ProcOutputFile: for ignore_exit_code in [False, True]: with self.subTest(output_file=output_file, ignore_exit_code=ignore_exit_code): test_setup = StdinCheckViaCopyToOutputFileTestSetup( output_file) # ACT & ASSERT # CHECKER.check__abs_stx__wo_input( self, StringSourceOfProgramAbsStx( output_file, FullProgramAbsStx( test_setup.program_that_copies_stdin_syntax(), stdin=stdin_string_source_syntax), ignore_exit_code, ), arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts( test_setup.symbols), ), Expectation.of_prim__const( parse=ParseExpectation( symbol_references=SymbolContext. references_assertion_of_contexts( test_setup.symbols)), primitive=asrt_string_source. pre_post_freeze__identical( asrt_contents.matches__str( asrt.equals(model_contents))), ), )
def test_stdin_only_as_source_argument(self): # ARRANGE # for pgm_and_args_case in pgm_and_args_cases.cases_w_and_wo_argument_list__including_program_reference( ): for validation_case in validation_cases.failing_validation_cases(): program_w_stdin = FullProgramAbsStx( pgm_and_args_case.pgm_and_args, stdin=validation_case.value.syntax, ) symbols = list(pgm_and_args_case.symbols) + [ validation_case.value.symbol_context ] # 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, pgm_and_args_case.mk_arrangement( SymbolContext.symbol_table_of_contexts(symbols)), MultiSourceExpectation.of_const( symbol_references=SymbolContext. references_assertion_of_contexts(symbols), execution=ExecutionExpectation( validation=validation_case.value.assertion, ), primitive=asrt.anything_goes(), ), sub_test_identifiers={ 'command': pgm_and_args_case.name, 'validation': validation_case.name }, )
def runTest(self): # ARRANGE # contents_of_stdin = 'the contents of stdin' string_matcher_that_checks_model = StringMatcherSymbolContext.of_primitive( 'SM_THAT_CHECKS_MODEL', EqualsConstant(contents_of_stdin), ) checker = instruction_check.Checker(self.configuration.parser()) program_that_copies_stdin_to_output = ProgramOfPythonInterpreterAbsStx.of_execute_python_src_string( py_programs.copy_stdin_to__single_line( self.configuration.output_file())) copy_stdin_and_check_via_matcher = StdoutErrFromProgramAbsStx( FullProgramAbsStx(program_that_copies_stdin_to_output, stdin=StringSourceOfStringAbsStx.of_str( contents_of_stdin, QuoteType.HARD)), string_matcher_that_checks_model.abstract_syntax, ) # ACT & ASSERT # checker.check__abs_stx__source_variants( self, copy_stdin_and_check_via_matcher, ArrangementPostAct2( symbols=string_matcher_that_checks_model.symbol_table, ), MultiSourceExpectation( symbol_usages=string_matcher_that_checks_model.usages_assertion ), )
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 _program_w_stdin_syntax( pgm_and_args: PgmAndArgsAbsStx, stdin_contents: str, ) -> FullProgramAbsStx: return FullProgramAbsStx( pgm_and_args, stdin=str_src_abs_stx.StringSourceOfStringAbsStx.of_str_hard( stdin_contents), )
def runTest(self): # ARRANGE # for program_case in pgm_and_args_cases.cases__w_argument_list__including_program_reference(): for transformer_case in invalid_syntax.stdin_cases(): with self.subTest(program=program_case.name, transformer=transformer_case.name): syntax = FullProgramAbsStx(program_case.pgm_and_args, stdin=transformer_case.value) # ACT & ASSERT # PARSE_CHECKER.check_invalid_syntax__abs_stx(self, syntax)
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): # 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 program_that_checks_stdin__syntax( self, program_stdin: str, additional_expected_stdin: str = '', ) -> FullProgramAbsStx: expected_contents_arg_syntax = ArgumentOfRichStringAbsStx.of_str( program_stdin + additional_expected_stdin, QuoteType.HARD) checker_pgm_syntax = ProgramOfPythonInterpreterAbsStx.of_execute_python_src_file( self._SRC_FILE_REL_CONF.path_abs_stx_of_name( self._CHECKER_PROGRAM_FILE_NAME), [expected_contents_arg_syntax], ) return FullProgramAbsStx( checker_pgm_syntax, stdin=StringSourceOfStringAbsStx.of_str_hard(program_stdin))
def runTest(self): # ARRANGE # program_w_superfluous_stx = FullProgramAbsStx( ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL_NAME'), transformation= symbol_reference_followed_by_superfluous_string_on_same_line( 'TRANSFORMER_SYMBOL_NAME')) define_symbol_syntax = DefineSymbolWMandatoryValue( 'the_symbol', ValueType.PROGRAM, program_w_superfluous_stx, ) PARSE_CHECKER.check_invalid_syntax__src_var_consume_last_line_abs_stx( self, define_symbol_syntax)
def test_stdin_only_as_source_argument(self): # ARRANGE # str_src_contents = 'the_str_src_contents' stdin_syntax = str_src_abs_stx.StringSourceOfStringAbsStx.of_str( str_src_contents) for pgm_and_args_case in pgm_and_args_cases.cases_w_and_wo_argument_list__including_program_reference( ): program_w_stdin = FullProgramAbsStx( pgm_and_args_case.pgm_and_args, stdin=stdin_syntax, ) symbols = list(pgm_and_args_case.symbols) 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.is_empty_sequence), stdin=asrt.matches_singleton_sequence( asrt_str_src.matches__str( asrt.equals(str_src_contents), )), transformer=asrt_pgm_val.is_no_transformation(), ) 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_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, ))
def runTest(self): # ARRANGE # program_symbol = ProgramSymbolContext.of_arbitrary_value( 'PROGRAM_SYMBOL') const_true_int_matcher_symbol = IntegerMatcherSymbolContext.of_primitive_constant( 'CONST_TRUE_INT_MATCHER', result=True, ) string_transformer_that_reports_failure_if_applied = StringTransformerSymbolContext.of_primitive( 'STRING_TRANSFORMER_THAT_MUST_NOT_BE_USED', StringTransformerThatFailsTestIfApplied(self)) all_symbols = [ program_symbol, string_transformer_that_reports_failure_if_applied, const_true_int_matcher_symbol, ] # ACT & ASSERT # CHECKER.check__abs_stx( self, InstructionArguments( FullProgramAbsStx( program_symbol.abstract_syntax, transformation= string_transformer_that_reports_failure_if_applied. abstract_syntax), const_true_int_matcher_symbol.abstract_syntax, ), ArrangementPostAct2( symbols=SymbolContext.symbol_table_of_contexts(all_symbols), process_execution=ProcessExecutionArrangement( os_services_access.new_for_cmd_exe( CommandExecutorThatJustReturnsConstant(1), ), ), ), Expectation2( ParseExpectation( symbol_usages=SymbolContext.usages_assertion_of_contexts( all_symbols), ), ExecutionExpectation(main_result=asrt_pfh.is_pass()), ))
def test_stdin_in_referenced_program_and_as_source_argument(self): # ARRANGE # str_src_contents__of_argument = 'the_str_src_contents__of_argument' str_src_contents__of_referenced_pgm = 'the_str_src_contents__of_program' stdin_argument_syntax = str_src_abs_stx.StringSourceOfStringAbsStx.of_str( str_src_contents__of_argument) stdin_sdv__of_referenced = str_src_sdvs.ConstantStringStringSourceSdv( string_sdvs.str_constant(str_src_contents__of_referenced_pgm)) referenced_program__system_program = 'the-system-program' referenced_program__sdv = program_sdvs.system_program( string_sdvs.str_constant(referenced_program__system_program), stdin=[stdin_sdv__of_referenced], ) referenced_program = ProgramSymbolContext.of_sdv( 'REFERENCED_PROGRAM', referenced_program__sdv) symbols__symbol_table = [referenced_program] symbols__expected_references = [referenced_program] arguments_cases = [ NameAndValue( 'no arguments', [], ), NameAndValue( 'single argument', ['arg1'], ) ] for arguments_case in arguments_cases: with self.subTest(arguments=arguments_case.name): program_w_stdin = FullProgramAbsStx( ProgramOfSymbolReferenceAbsStx(referenced_program.name, [ ArgumentOfRichStringAbsStx.of_str(arg) for arg in arguments_case.value ]), stdin=stdin_argument_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.matches_sequence([ asrt_str_src.matches__str( asrt.equals(str_src_contents__of_referenced_pgm)), asrt_str_src.matches__str( asrt.equals(str_src_contents__of_argument)), ]), 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, program_w_stdin, arrangement_w_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_stdin_in_referenced_program_and_as_source_argument(self): # ARRANGE # stdin_contents = 'str_src_contents' valid_stdin_sdv = sdvs.ConstantStringStringSourceSdv( string_sdvs.str_constant(stdin_contents)) valid_stdin_syntax = str_src_abs_stx.StringSourceOfStringAbsStx.of_str( stdin_contents) referenced_program__system_program = 'the-system-program' for validation_case in validation_cases.failing_validation_cases(): invalid_stdin_location_cases = [ ValidationCaseWAccumulation( 'in referenced symbol', sdv_of_referenced_program=validation_case.value. symbol_context.sdv, syntax_accumulated=valid_stdin_syntax, symbols=[], ), ValidationCaseWAccumulation( 'as source argument', sdv_of_referenced_program=valid_stdin_sdv, syntax_accumulated=validation_case.value.syntax, symbols=[validation_case.value.symbol_context], ), ] for invalid_stdin_location_case in invalid_stdin_location_cases: referenced_program__sdv = program_sdvs.system_program( string_sdvs.str_constant( referenced_program__system_program), stdin=[ invalid_stdin_location_case.sdv_of_referenced_program ]) referenced_program = ProgramSymbolContext.of_sdv( 'REFERENCED_PROGRAM', referenced_program__sdv) program_w_stdin = FullProgramAbsStx( ProgramOfSymbolReferenceAbsStx(referenced_program.name, ), stdin=invalid_stdin_location_case.syntax_of_accumulated, ) symbols__all = [ validation_case.value.symbol_context, referenced_program ] symbols__expected_references = [ referenced_program ] + invalid_stdin_location_case.symbols # 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__all), ), MultiSourceExpectation.of_const( symbol_references=SymbolContext. references_assertion_of_contexts( symbols__expected_references), execution=ExecutionExpectation( validation=validation_case.value.assertion, ), primitive=asrt.anything_goes(), ), sub_test_identifiers={ 'validation': validation_case.name, 'invalid_stdin_location': invalid_stdin_location_case.name, })
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_transformation_in_referenced_program_and_as_source_argument(self): # ARRANGE # valid_transformer = StringTransformerSymbolContext.of_arbitrary_value( 'VALID_TRANSFORMER', ) referenced_program__system_program = 'the-system-program' for validation_case in validation_cases.failing_validation_cases( 'INVALID_TRANSFORMER'): invalid_transformer_location_cases = [ ValidationCaseWAccumulation( 'in referenced program', of_referenced_program=validation_case.value.symbol_context, accumulated=valid_transformer, ), ValidationCaseWAccumulation( 'in referenced program', of_referenced_program=valid_transformer, accumulated=validation_case.value.symbol_context, ), ] for invalid_transformer_location_case in invalid_transformer_location_cases: referenced_program__sdv = program_sdvs.system_program( string_sdvs.str_constant( referenced_program__system_program), transformations=[ invalid_transformer_location_case. of_referenced_program.reference_sdv ]) referenced_program = ProgramSymbolContext.of_sdv( 'REFERENCED_PROGRAM', referenced_program__sdv) program_w_transformer = FullProgramAbsStx( ProgramOfSymbolReferenceAbsStx(referenced_program.name, ), transformation=invalid_transformer_location_case. accumulated.abstract_syntax, ) symbols__all = [ referenced_program, valid_transformer, validation_case.value.symbol_context, ] symbols__expected_references = [ referenced_program, invalid_transformer_location_case.accumulated ] with self.subTest(validation=validation_case.name, invalid_transformer_location= invalid_transformer_location_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, arrangement_wo_tcds( symbols=SymbolContext.symbol_table_of_contexts( symbols__all), ), MultiSourceExpectation.of_const( symbol_references=SymbolContext. references_assertion_of_contexts( symbols__expected_references), execution=ExecutionExpectation( validation=validation_case.value.expectation, ), primitive=asrt.anything_goes(), ))