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 test_transformer_component(self): checker = integration_check.checker__w_arbitrary_file_relativities() for validation_case in failing_validation_cases(): transformer_symbol = validation_case.value.symbol_context program_syntax = program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command'), transformation=transformer_symbol.abstract_syntax, ) string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_syntax, ignore_exit_code=False, ) with self.subTest(validation_case.name): checker.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_expr_parse__s__nsc, OptionallyOnNewLine(string_source_syntax), arrangement_wo_tcds( symbols=transformer_symbol.symbol_table), MultiSourceExpectation( symbol_references=transformer_symbol. references_assertion, execution=ExecutionExpectation( validation=validation_case.value.expectation)))
def test_without_transformer(self): # ARRANGE # src_file = fs.File('source-file.txt', 'contents of source file') src_rel_opt_conf = conf_rel_any(RelOptionType.REL_TMP) checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_rel_opt_conf.relativity)) file_contents_abs_stx = string_source_abs_stx.StringSourceOfFileAbsStx( src_rel_opt_conf.path_abs_stx_of_name(src_file.name) ) # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(file_contents_abs_stx), arrangement_w_tcds( tcds_contents=src_rel_opt_conf.populator_for_relativity_option_root( DirContents([src_file])) ), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( src_file.contents, may_depend_on_external_resources=True, ), symbol_references=asrt.is_empty_sequence, ) )
def test_fail_when_missing_end_end_paren(self): # ARRANGE # valid_string = str_abs_stx.StringLiteralAbsStx('contents') missing_end_paren = CustomAbsStx( TokenSequence.concat([ TokenSequence.singleton('('), valid_string.tokenization(), ])) # ACT & ASSERT # parse_check.checker().check_invalid_syntax__abs_stx( self, OptionallyOnNewLine(missing_end_paren))
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 test_fail_when_missing_end_end_paren(self): # ARRANGE # valid_program = ProgramOfSymbolReferenceAbsStx('PROGRAM_SYMBOL') missing_end_paren = CustomAbsStx( TokenSequence.concat([ TokenSequence.singleton('('), valid_program.tokenization(), ]) ) # ACT & ASSERT # PARSE_CHECKER.check_invalid_syntax__abs_stx( self, OptionallyOnNewLine(missing_end_paren) )
def check_invalid_syntax( self, put: unittest.TestCase, mk_source_variants: SourceStr2SourceVariants, source: AbstractSyntax, sub_test_identifiers: Mapping[str, Any] = MappingProxyType({}), ): for formatting_case in abs_stx_utils.formatting_cases( OptionallyOnNewLine(source)): for source_case in mk_source_variants(formatting_case.value): with put.subTest(zz_source_formatting=formatting_case.name, zz_source_variant=source_case.name, **sub_test_identifiers): with put.assertRaises( SingleInstructionInvalidArgumentException): self._parser.parse(source_case.source)
def test_here_doc(self): # ARRANGE # string_value = rich_str_abs_stx.HereDocAbsStx( 'single line in here doc\n') string_source_syntax = string_source_abs_stx.StringSourceOfStringAbsStx( string_value) CHECKER.check__abs_stx__layouts__source_variants__wo_input( self, equivalent_source_variants__for_full_line_expr_parse__s__nsc, OptionallyOnNewLine( WithinParensAbsStx(string_source_syntax, end_paren_on_separate_line=True)), arrangement_w_tcds(), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( string_value.value, may_depend_on_external_resources=False, )))
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 test_string_symbol(self): contents_symbol = StringSourceSymbolContextOfPrimitiveConstant( 'contents_symbol_name', 'symbol value contents', ) CHECKER.check__abs_stx( self, OptionallyOnNewLine( WithinParensAbsStx(contents_symbol.abstract_syntax)), None, arrangement_w_tcds(symbols=contents_symbol.symbol_table, ), Expectation.of_prim__const( primitive=asrt_string_source. pre_post_freeze__matches_str__const( contents_symbol.contents_str, may_depend_on_external_resources=False), parse=ParseExpectation( symbol_references=contents_symbol.references_assertion, ), ))
def runTest(self): # ARRANGE # text_printed_by_program = StringConstantSymbolContext( 'STRING_TO_PRINT_SYMBOL', 'hello world') to_upper_transformer = TO_UPPER_TRANSFORMER_SYMBOL expected_output = text_printed_by_program.str_value.upper() transformed_program_output_contents_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.name__sym_ref_syntax)), transformation=to_upper_transformer.abstract_syntax, )) symbols = SymbolContext.symbol_table_of_contexts([ text_printed_by_program, to_upper_transformer, ]) checker = integration_check.checker__w_arbitrary_file_relativities() # ACT & ASSERT # checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(transformed_program_output_contents_syntax), arrangement_w_tcds(symbols=symbols, ), MultiSourceExpectation. of_prim__const(symbol_references=asrt.matches_sequence([ text_printed_by_program.reference_assertion__w_str_rendering, to_upper_transformer.reference_assertion, ]), primitive=asrt_string_source. pre_post_freeze__matches_str__const_2( expected_output, may_depend_on_external_resources=True, frozen_may_depend_on_external_resources=asrt. anything_goes(), )))
def check( self, put: unittest.TestCase, mk_source_variants: SourceStr2SourceVariants, source: AbstractSyntax, arrangement: Arrangement, expectation: Expectation, sub_test_identifiers: Mapping[str, Any] = MappingProxyType({}), ): for formatting_case in abs_stx_utils.formatting_cases( OptionallyOnNewLine(source)): for source_case in mk_source_variants(formatting_case.value): with put.subTest(zz_source_formatting=formatting_case.name, zz_source_variant=source_case.name, **sub_test_identifiers): self._act_and_assert( put, source_case, arrangement, expectation, )
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 test_transformer_SHOULD_be_validated(self): src_file_rel_conf = conf_rel_any(RelOptionType.REL_HDS_CASE) src_file = fs.File.empty('src-file.txt') checker = integration_check.checker(parse_check.rel_opts_conf_of_single(src_file_rel_conf.relativity)) file_string_source_syntax = string_source_abs_stx.StringSourceOfFileAbsStx( src_file_rel_conf.path_abs_stx_of_name(self.src_file_name) ) str_trans_syntax = StringTransformerSymbolReferenceAbsStx( 'INVALID_STRING_TRANSFORMER' ) transformed_string_source_syntax = string_source_abs_stx.TransformedStringSourceAbsStx( file_string_source_syntax, str_trans_syntax, ) for validation_case in str_trans_validation_cases.failing_validation_cases(str_trans_syntax.symbol_name): with self.subTest(validation_case.name): v_case = validation_case.value checker.check__abs_stx__layouts__std_source_variants__wo_input( self, OptionallyOnNewLine(transformed_string_source_syntax), arrangement_w_tcds( tcds_contents=src_file_rel_conf.populator_for_relativity_option_root( fs.DirContents([src_file]) ), symbols=v_case.symbol_context.symbol_table, ), MultiSourceExpectation( symbol_references=v_case.symbol_context.references_assertion, execution=ExecutionExpectation( validation=v_case.expectation ) ), )
def _test(self, text_printed_by_program: str, expected_file_contents: str, make_arguments: Callable[[TransformableProgramAbsStxBuilder], ProgramAbsStx], additional_symbols: Dict[str, SymbolContainer], additional_symbol_references: List[Assertion[SymbolReference]], ): for proc_output_file in ProcOutputFile: python_source = py_programs.single_line_pgm_that_prints_to(proc_output_file, text_printed_by_program) program_that_executes_py_source_symbol = ProgramSymbolContext.of_sdv( 'PROGRAM_THAT_EXECUTES_PY_SOURCE', program_sdvs.for_py_source_on_command_line(python_source) ) program_cases = [ ProgramCase( 'python interpreter', program_abs_stx.ProgramOfPythonInterpreterAbsStx.of_execute_python_src_string(python_source), [] ), ProgramCase( 'symbol reference program', ProgramOfSymbolReferenceAbsStx(program_that_executes_py_source_symbol.name), [program_that_executes_py_source_symbol.reference_assertion], ), ] symbols_dict = { program_that_executes_py_source_symbol.name: program_that_executes_py_source_symbol.symbol_table_container, } symbols_dict.update(additional_symbols) symbols = SymbolTable(symbols_dict) checker = integration_check.checker__w_arbitrary_file_relativities() for program_case in program_cases: program_syntax_builder = TransformableProgramAbsStxBuilder(program_case.source) program_syntax = make_arguments(program_syntax_builder) expected_symbol_references = program_case.expected_references + additional_symbol_references syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( proc_output_file, program_syntax, ignore_exit_code=False, ) with self.subTest(program=program_case.name, output_channel=proc_output_file): 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=symbols, ), MultiSourceExpectation.of_prim__const( symbol_references=asrt.matches_sequence(expected_symbol_references), primitive=asrt_string_source.pre_post_freeze__matches_str__const_2( expected_file_contents, may_depend_on_external_resources=True, frozen_may_depend_on_external_resources=asrt.anything_goes(), ), ) )
def test_WHEN_program_is_non_non_existing_system_command_THEN_result_SHOULD_be_hard_error(self): failing_program_builder = program_abs_stx.TransformableProgramAbsStxBuilder( program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str(NON_EXISTING_SYSTEM_PROGRAM) ) transformer = TO_UPPER_TRANSFORMER_SYMBOL symbols = transformer.symbol_table cases = failing_program_builder.with_and_without_transformer_cases(transformer.abstract_syntax) for ignore_exit_code in [False, True]: for transformation_case in cases: syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, transformation_case.value, ignore_exit_code=ignore_exit_code, ) checker = integration_check.checker__w_arbitrary_file_relativities() with self.subTest(transformation=transformation_case.name, ignore_exit_code=ignore_exit_code): # ACT & ASSERT # 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=symbols, ), MultiSourceExpectation.of_const( symbol_references=asrt.anything_goes(), primitive=asrt_string_source.pre_post_freeze( asrt_str_src_contents.contents_raises_hard_error( may_depend_on_external_resources=asrt.equals(True) ), asrt_str_src_contents.contents_raises_hard_error__including_ext_deps(), ), ) )
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(), ), ), ),
def test_non_transformer_components(self): # ARRANGE # relativity_cases = [ NArrEx( 'pre SDS validation failure', RelOptionType.REL_HDS_CASE, ValidationAssertions.pre_sds_fails__w_any_msg(), ), NArrEx( 'post SDS validation failure', RelOptionType.REL_ACT, ValidationAssertions.post_sds_fails__w_any_msg(), ), ] def make_pgm_w_ref_to_executable_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.ProgramOfExecutableFileCommandLineAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file')) def make_pgm_w_ref_to_stdin_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.FullProgramAbsStx( program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command'), stdin=string_source_abs_stx.StringSourceOfFileAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file'))) def make_pgm_w_ref_to_argument_file( relativity: RelOptionType) -> ProgramAbsStx: return program_abs_stx.ProgramOfSystemCommandLineAbsStx.of_str( 'a-system-command', [ ArgumentOfExistingPathAbsStx( path_abs_stx.RelOptPathAbsStx(relativity, 'non-existing-file')) ]) program_cases = [ NameAndValue( 'missing executable file', make_pgm_w_ref_to_executable_file, ), NameAndValue( 'missing argument file', make_pgm_w_ref_to_argument_file, ), NameAndValue( 'missing stdin file', make_pgm_w_ref_to_stdin_file, ), ] for relativity_case in relativity_cases: for program_case in program_cases: string_source_syntax = string_source_abs_stx.StringSourceOfProgramAbsStx( ProcOutputFile.STDOUT, program_case.value(relativity_case.arrangement), ignore_exit_code=False, ) # ACT & ASSERT # checker = integration_check.checker__w_arbitrary_file_relativities( ) with self.subTest(step=relativity_case.name, 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(string_source_syntax), arrangement_w_tcds(), MultiSourceExpectation(execution=ExecutionExpectation( validation=relativity_case.expectation)))
class Case(NamedTuple): name: str syntax: RichStringAbsStx expected: str def _check_w_constant_contents(put: unittest.TestCase, case: Case): ss_syntax = string_source_abs_stx.StringSourceOfStringAbsStx(case.syntax) source_variants = ( equivalent_source_variants__for_full_line_expr_parse__s__nsc if case.syntax.spans_whole_line else equivalent_source_variants__for_expr_parse__s__nsc) CHECKER.check__abs_stx__layouts__source_variants__wo_input( put, source_variants, OptionallyOnNewLine(ss_syntax), arrangement_w_tcds(), MultiSourceExpectation.of_prim__const( asrt_string_source.pre_post_freeze__matches_str__const( case.expected, may_depend_on_external_resources=False, )), sub_test_identifiers={'name': case.name}) def _check_transformed_w_constant_contents(put: unittest.TestCase, case: Case): str_added_by_transformer = '<string added by transformer>' transformer_symbol = StringTransformerSymbolContext.of_primitive( 'TRANSFORMER', string_transformers.add(str_added_by_transformer), )