Exemple #1
0
    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
            ),
        )
Exemple #2
0
    def runTest(self):
        vars_before = [
            ValueWSymRefsAndVarRefs.REFERENCED_VAR_1,
            ValueWSymRefsAndVarRefs.REFERENCED_VAR_2
        ]
        var_to_set__resolved = NameAndValue(
            NameWSymRefs.RESOLVED_STR,
            ValueWSymRefsAndVarRefs.VALUE_WO_VAR_REFS)

        all_symbols = NameWSymRefs.SYMBOL_CONTEXTS + ValueWSymRefsAndVarRefs.SYMBOL_CONTEXTS

        execution_cases = _exe_cases_of_modification(
            before_modification=vars_before,
            after_modification=vars_before + [var_to_set__resolved],
            symbols=SymbolContext.symbol_table_of_contexts(all_symbols),
        )
        # ACT & ASSERT #
        for phase_spec__source in [None, Phase.NON_ACT]:
            CHECKER.check__abs_stx__multi__std_layouts_and_source_variants(
                self,
                SetVariableArgumentsAbsStx(
                    NameWSymRefs.STRING_ABS_STX,
                    StringSourceOfStringAbsStx(
                        ValueWSymRefsAndVarRefs.STRING_ABS_STX),
                    phase_spec=phase_spec__source,
                ),
                symbol_usages=SymbolContext.usages_assertion_of_contexts(
                    all_symbols),
                execution_cases=execution_cases,
                sub_test_identifiers={
                    'phase_spec__source': phase_spec__source,
                },
            )
Exemple #3
0
    def runTest(self):
        vars_before = [
            ValueWSymRefsAndVarRefs.REFERENCED_VAR_1,
            ValueWSymRefsAndVarRefs.REFERENCED_VAR_2
        ]
        setup_before = EnvironsSetup(vars_before, vars_before)

        var_to_set__resolved = NameAndValue(
            NameWSymRefs.RESOLVED_STR,
            ValueWSymRefsAndVarRefs.VALUE_WO_VAR_REFS)

        all_symbols = NameWSymRefs.SYMBOL_CONTEXTS + ValueWSymRefsAndVarRefs.SYMBOL_CONTEXTS

        execution_cases = _exe_cases_of_modification(
            before_modification=setup_before,
            after_modification=setup_before.new_with_added(
                var_to_set__resolved, var_to_set__resolved),
            symbols=SymbolContext.symbol_table_of_contexts(all_symbols),
        )
        # ACT & ASSERT #
        CHECKER.check__abs_stx__multi__std_layouts_and_source_variants(
            self,
            SetVariableArgumentsAbsStx(
                NameWSymRefs.STRING_ABS_STX,
                StringSourceOfStringAbsStx(
                    ValueWSymRefsAndVarRefs.STRING_ABS_STX),
                phase_spec=None,
            ),
            symbol_usages=SymbolContext.usages_assertion_of_contexts(
                all_symbols),
            execution_cases=execution_cases,
        )
 def test_fail_when_missing_end_quote(self):
     # ARRANGE #
     string_w_missing_end_quote = StringSourceOfStringAbsStx.of_plain(
         str_abs_stx.StringLiteralAbsStx(SOFT_QUOTE_CHAR + 'contents'))
     # ACT & ASSERT #
     parse_check.checker().check_invalid_syntax__abs_stx(
         self, string_w_missing_end_quote)
Exemple #5
0
def stdin_cases() -> Sequence[NameAndValue[StringSourceAbsStx]]:
    return [
        NameAndValue(
            'string with missing end quote',
            StringSourceOfStringAbsStx.of_plain(
                str_abs_stx.StringLiteralAbsStx(
                    QUOTE_CHAR_FOR_TYPE[QuoteType.SOFT] + 'after quote'))),
    ]
 def of_str(
     var_name: str,
     value: str,
     phase_spec: Optional[Phase],
     quoting: Optional[QuoteType] = None,
 ) -> 'SetVariableArgumentsAbsStx':
     return SetVariableArgumentsAbsStx(
         StringLiteralAbsStx(var_name),
         StringSourceOfStringAbsStx.of_str(value, quoting), phase_spec)
Exemple #7
0
 def test_doc_without_symbol_references(self):
     content_line_of_here_doc = 'content line of here doc\n'
     syntax = InstructionAbsStx(
         StringSourceOfStringAbsStx(
             HereDocAbsStx(content_line_of_here_doc)))
     CHECKER.check_multi_source__abs_stx(
         self, syntax, Arrangement(),
         MultiSourceExpectation(
             settings_builder=_stdin_is_present_and_valid(
                 content_line_of_here_doc,
                 may_depend_on_external_resources=False,
             ), ))
Exemple #8
0
 def runTest(self):
     # ARRANGE #
     for phase_spec in PHASE_SPECS:
         cases = [
             NameAndValue(
                 'missing arguments',
                 SetVariableArgumentsAbsStx(
                     StringLiteralAbsStx(''),
                     StringSourceOfStringAbsStx.of_str(''),
                     phase_spec=phase_spec,
                 ),
             ),
             NameAndValue(
                 'more than three arguments',
                 SetVariableArgumentsAbsStx(
                     StringLiteralAbsStx('arg1'),
                     StringSourceOfStringAbsStx.of_str('arg2 arg3'),
                     phase_spec=phase_spec,
                 ),
             ),
             NameAndValue(
                 'invalid quoting of value',
                 SetVariableArgumentsAbsStx(
                     StringLiteralAbsStx('name'),
                     StringSourceOfStringAbsStx(MISSING_END_QUOTE__SOFT),
                     phase_spec=phase_spec,
                 ),
             ),
         ]
         for case in cases:
             # ACT & ASSERT #
             PARSE_CHECKER.check_invalid_syntax__abs_stx(
                 self,
                 case.value,
                 {
                     'phase_spec': phase_spec,
                     'variant': case.name,
                 },
             )
Exemple #9
0
 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))
Exemple #10
0
 def test_failing_parse(self):
     cases = [
         NameAndValue(
             'missing argument',
             CustomAbsStx.empty(),
         ),
         NameAndValue(
             'missing end quote',
             StringSourceOfStringAbsStx(MISSING_END_QUOTE__SOFT),
         ),
         NameAndValue(
             'superfluous arguments',
             SequenceAbsStx.followed_by_superfluous(
                 StringSourceOfStringAbsStx.of_str_hard('valid string')
             ),
         ),
     ]
     # ARRANGE #
     for case in cases:
         with self.subTest(case.name):
             PARSE_CHECKER.check_invalid_syntax__abs_stx(
                 self,
                 _syntax_of(case.value)
             )
def _list_case(contents_transformation: Callable[[str], str]) -> Case:
    elements = ['1st element', '2nd element']

    def get_assertion_on_primitive(
            env: AssertionResolvingEnvironment) -> Assertion[StringSource]:
        list_as_str = list_formatting.format_elements(elements)
        return asrt_string_source.pre_post_freeze__matches_str__const(
            contents_transformation(list_as_str),
            may_depend_on_external_resources=False)

    symbol_context = ListSymbolContext.of_constants('LIST_SYMBOL', elements)

    return Case(
        StringSourceOfStringAbsStx.of_plain(
            StringLiteralAbsStx(symbol_context.name__sym_ref_syntax,
                                QuoteType.SOFT)),
        symbol_context,
        get_assertion_on_primitive,
    )
Exemple #12
0
    def test_here_doc_with_embedded_references(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME

        here_doc_line_template = 'pre symbol {symbol} post symbol'

        referenced_symbol = StringConstantSymbolContext(
            'REFERENCED_STRING_SYMBOL',
            'contents of string symbol',
            default_restrictions=data_restrictions_assertions.is__w_str_rendering(),
        )
        expected_contents = here_doc_line_template.format(symbol=referenced_symbol.str_value) + '\n'

        syntax = _syntax_of(
            StringSourceOfStringAbsStx(
                HereDocAbsStx.of_lines__wo_new_lines([
                    here_doc_line_template.format(symbol=referenced_symbol.name__sym_ref_syntax)
                ])
            ),
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            referenced_symbol.symbol_table,
            referenced_symbol.references_assertion,
            expected_contents,
            may_depend_on_external_resources=False
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            syntax,
            arrangement,
            expectation,
        )
Exemple #13
0
    def test_literal(self):
        # ARRANGE #
        defined_name = symbol_syntax.A_VALID_SYMBOL_NAME
        create_file = fs.File('created-file.txt',
                              'contents of created file')

        literal_syntax = abs_stx.LiteralFilesSourceAbsStx([
            abs_stx.regular_file_spec(
                StringLiteralAbsStx(create_file.name, QuoteType.HARD),
                abs_stx.FileContentsExplicitAbsStx(
                    ModificationType.CREATE,
                    StringSourceOfStringAbsStx.of_str(create_file.contents,
                                                      QuoteType.HARD),
                )
            )
        ])
        syntax = _syntax_of(
            literal_syntax,
            defined_name,
        )

        arrangement = Arrangement.phase_agnostic()

        # EXPECTATION #

        expectation = _expect_definition_of(
            defined_name,
            SymbolTable.empty(),
            (),
            asrt.is_empty_sequence,
            asrt_fs.dir_contains_exactly_2([create_file])
        )

        # ACT & ASSERT #

        INSTRUCTION_CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            syntax,
            arrangement,
            expectation,
        )
Exemple #14
0
 def test_doc_with_symbol_references(self):
     # ARRANGE #
     content_line_of_here_doc_template = 'content line of here doc with {symbol}\n'
     symbol = StringConstantSymbolContext('symbol_name', 'the symbol value')
     expected_stdin_contents = content_line_of_here_doc_template.format(
         symbol=symbol.str_value)
     expected_symbol_references = asrt.matches_singleton_sequence(
         symbol.reference_assertion__w_str_rendering)
     syntax = InstructionAbsStx(
         StringSourceOfStringAbsStx(
             HereDocAbsStx(
                 content_line_of_here_doc_template.format(
                     symbol=symbol.name__sym_ref_syntax))))
     # ACT & ASSERT #
     CHECKER.check_multi_source__abs_stx(
         self, syntax, Arrangement(symbols=symbol.symbol_table),
         MultiSourceExpectation(
             settings_builder=_stdin_is_present_and_valid(
                 expected_stdin_contents,
                 may_depend_on_external_resources=False),
             symbols_after_parse=expected_symbol_references,
         ))
    def test_just_symbol_reference(self):
        contents_symbol = StringConstantSymbolContext(
            'contents_symbol_name',
            'symbol value contents',
            default_restrictions=asrt_rest.is__w_str_rendering(),
        )

        string_source_syntax = StringSourceOfStringAbsStx.of_plain(
            str_abs_stx.StringSymbolAbsStx(contents_symbol.name))

        CHECKER.check__abs_stx(
            self, string_source_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.str_value,
                    may_depend_on_external_resources=False),
                parse=ParseExpectation(
                    symbol_references=asrt.matches_singleton_sequence(
                        references.is_reference_to__string_source_or_string(
                            contents_symbol.name)), ),
            ))
def _path_case(contents_transformation: Callable[[str], str]) -> Case:
    relativity = RelOptionType.REL_TMP
    path_suffix = 'the path suffix'

    def get_assertion_on_primitive(
            env: AssertionResolvingEnvironment) -> Assertion[StringSource]:
        path_as_str = str(env.tcds.sds.user_tmp_dir / path_suffix)
        return asrt_string_source.pre_post_freeze__matches_str__const(
            contents_transformation(path_as_str),
            may_depend_on_external_resources=False)

    symbol_context = ConstantSuffixPathDdvSymbolContext(
        'PATH_SYMBOL',
        relativity,
        path_suffix,
        ARBITRARY_FILE_RELATIVITIES.accepted_relativity_variants,
    )
    return Case(
        StringSourceOfStringAbsStx.of_plain(
            StringLiteralAbsStx(symbol_context.name__sym_ref_syntax,
                                QuoteType.SOFT)),
        symbol_context,
        get_assertion_on_primitive,
    )
Exemple #17
0

def file_type_and_contents_variants(file_name: StringAbsStx) -> Sequence[NameAndValue[FileSpecAbsStx]]:
    return [
        NameAndValue(
            'file_type={}, contents={}'.format(file_type, contents_variant.name),
            abs_stx.FileSpecAbsStx.of_file_type(file_type,
                                                file_name,
                                                contents_variant.value)
        )
        for file_type in defs.FileType
        for contents_variant in _CONTENTS_CASES[file_type]
    ]


_VALID_STRING_SOURCE = StringSourceOfStringAbsStx.of_plain(StringLiteralAbsStx.empty_string())
_VALID_FILES_SOURCE = abs_stx.LiteralFilesSourceAbsStx(())

_CONTENTS_CASES: Mapping[FileType, Sequence[NameAndValue[ContentsAbsStx]]] = {
    defs.FileType.REGULAR: (
            [
                NameAndValue('empty', abs_stx.FileContentsEmptyAbsStx()),
            ] +
            [
                NameAndValue('explicit/{}'.format(mod),
                             abs_stx.FileContentsExplicitAbsStx(mod, _VALID_STRING_SOURCE))
                for mod in defs.ModificationType
            ]),
    defs.FileType.DIR: (
            [
                NameAndValue('empty', abs_stx.DirContentsEmptyAbsStx()),
 def symbol_ref_syntax_2_contents_arguments(
         syntax: str) -> StringSourceAbsStx:
     string_value = string_value_template.format(symbol=syntax)
     return StringSourceOfStringAbsStx.of_plain(
         str_abs_stx.StringLiteralAbsStx(string_value, QuoteType.SOFT))
 def symbol_ref_syntax_2_contents_arguments(
         syntax: str) -> StringSourceAbsStx:
     return StringSourceOfStringAbsStx(
         rich_str_abs_stx.HereDocAbsStx.of_lines__wo_new_lines(
             [here_doc_line_template.format(symbol=syntax)]))