class TestErrorMessage(unittest.TestCase):
    string_sym_def_1 = StringConstantSymbolContext('symbol1')
    string_sym_def_2 = StringConstantSymbolContext('symbol2')

    symbol_table = SymbolContext.symbol_table_of_contexts(
        [string_sym_def_1, string_sym_def_2])

    def test_invalid_type(self):
        # ACT #
        actual = sut.ErrorMessage(self.string_sym_def_1.name,
                                  self.symbol_table,
                                  InvalidValueTypeFailure([ValueType.PATH]))
        # ASSERT #
        asrt_text_doc.assert_is_valid_text_renderer(self, actual)

    def test_direct_reference(self):
        # ACT #
        actual = sut.ErrorMessage(
            self.string_sym_def_1.name, self.symbol_table,
            FailureOfDirectReference(_new_em('the message', 'the how to fix')))
        # ASSERT #
        asrt_text_doc.assert_is_valid_text_renderer(self, actual)

    def test_indirect_reference(self):
        # ACT #
        actual = sut.ErrorMessage(
            self.string_sym_def_1.name, self.symbol_table,
            FailureOfIndirectReference(
                self.string_sym_def_1.name, [self.string_sym_def_2.name],
                _new_em('the message', 'the how to fix')))
        # ASSERT #
        asrt_text_doc.assert_is_valid_text_renderer(self, actual)
 def test_here_document_with_symbol_references(self):
     symbol1 = StringConstantSymbolContext('symbol_1_name',
                                           'symbol 1 value')
     symbol2 = StringConstantSymbolContext('symbol_2_name',
                                           'symbol 2 value')
     symbol3 = StringConstantSymbolContext('symbol_3_name',
                                           'symbol 3 value')
     line_with_sym_ref_template = 'before symbol {symbol} after symbol'
     line_with_two_sym_refs_template = '{first_symbol} between symbols {second_symbol}'
     cases = [
         SuccessfulCase(
             source_lines=[
                 '<<eof',
                 line_with_sym_ref_template.format(
                     symbol=symbol1.name__sym_ref_syntax),
                 'eof',
                 'following line',
             ],
             expected_document_contents=hd.matches_resolved_value(
                 [
                     line_with_sym_ref_template.format(
                         symbol=symbol1.str_value),
                 ],
                 symbol_references=[
                     symbol1.reference__w_str_rendering,
                 ],
                 symbols=symbol1.symbol_table),
             source_after_parse=asrt_source.is_at_end_of_line(3),
         ),
         SuccessfulCase(
             source_lines=[
                 '<<eof',
                 line_with_sym_ref_template.format(
                     symbol=symbol1.name__sym_ref_syntax),
                 line_with_two_sym_refs_template.format(
                     first_symbol=symbol2.name__sym_ref_syntax,
                     second_symbol=symbol3.name__sym_ref_syntax),
                 'eof',
                 'following line',
             ],
             expected_document_contents=hd.matches_resolved_value(
                 [
                     line_with_sym_ref_template.format(
                         symbol=symbol1.str_value),
                     line_with_two_sym_refs_template.format(
                         first_symbol=symbol2.str_value,
                         second_symbol=symbol3.str_value),
                 ],
                 symbol_references=[
                     symbol1.reference__w_str_rendering,
                     symbol2.reference__w_str_rendering,
                     symbol3.reference__w_str_rendering,
                 ],
                 symbols=SymbolContext.symbol_table_of_contexts(
                     [symbol1, symbol2, symbol3], )),
             source_after_parse=asrt_source.is_at_end_of_line(4),
         ),
     ]
     for case in cases:
         self._check_case(case)
Example #3
0
class ValueWSymRefsAndVarRefs:
    REFERENCED_VAR_1 = NameAndValue('existing_var_1', '<val of existing 1>')
    REFERENCED_VAR_2 = NameAndValue('existing_var_2', '<val of existing 2>')
    REFERENCED_VAR__ALL = [REFERENCED_VAR_1, REFERENCED_VAR_2]
    VALUE_PATTERN = '{}between{}'
    VALUE_W_VAR_REFS = VALUE_PATTERN.format(
        env_var_ref_syntax(REFERENCED_VAR_1.name),
        env_var_ref_syntax(REFERENCED_VAR_2.name),
    )
    VALUE_WO_VAR_REFS = VALUE_PATTERN.format(
        REFERENCED_VAR_1.value,
        REFERENCED_VAR_2.value,
    )
    POS_OF_END_OF_VAR_REF_1 = end_of_1st_var_ref(VALUE_W_VAR_REFS)
    SYM_REF_PART_1 = StringConstantSymbolContext(
        'VAL_SYM_1',
        VALUE_W_VAR_REFS[:4],
        default_restrictions=asrt_w_str_rend_rest.is__w_str_rendering(),
    )
    CONST_STR_PART_2 = VALUE_W_VAR_REFS[4:(POS_OF_END_OF_VAR_REF_1 + 5)]

    SYM_REF_PART_3 = StringConstantSymbolContext(
        'VAL_SYM_3',
        VALUE_W_VAR_REFS[(POS_OF_END_OF_VAR_REF_1 + 5):],
        default_restrictions=asrt_w_str_rend_rest.is__w_str_rendering(),
    )
    SYMBOL_CONTEXTS = (SYM_REF_PART_1, SYM_REF_PART_3)

    STRING_ABS_STX = str_abs_stx.StringConcatAbsStx([
        SYM_REF_PART_1.abstract_syntax,
        str_abs_stx.StringLiteralAbsStx(CONST_STR_PART_2),
        SYM_REF_PART_3.abstract_syntax,
    ])
Example #4
0
    def runTest(self):
        # ARRANGE #

        symbol_in_regex = StringConstantSymbolContext(
            'symbol_in_regex',
            'plain string pattern',
            default_restrictions=asrt_regex.is_reference_restrictions__regex())
        symbol_in_replacement = StringConstantSymbolContext(
            'symbol_in_replacement',
            'the replacement',
            default_restrictions=asrt_ref_rest.is__w_str_rendering(),
        )

        input_lines = [
            symbol_in_regex.str_value,
        ]
        expected_lines = [
            symbol_in_replacement.str_value,
        ]
        quoting_cases = [
            None,
            QuoteType.SOFT,
        ]
        for line_filter_case in LINE_FILTER_CASES:
            all_symbols = line_filter_case.value.symbols + [
                symbol_in_regex,
                symbol_in_replacement,
            ]
            for preserve_new_lines in [False, True]:
                for quoting_variant in quoting_cases:
                    source = ReplaceRegexAbsStx(
                        StringLiteralAbsStx(
                            symbol_in_regex.name__sym_ref_syntax,
                            quoting_variant),
                        symbol_in_replacement.abstract_syntax,
                        preserve_new_lines=preserve_new_lines,
                        lines_filter=line_filter_case.value.syntax,
                    )
                    # ACT & ASSERT #

                    integration_check.CHECKER__PARSE_FULL.check__abs_stx__layout__std_source_variants(
                        self,
                        source,
                        model_constructor.of_lines(self, input_lines),
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                all_symbols), ),
                        expectation_of_successful_replace_execution__multi(
                            symbol_references=SymbolContext.
                            references_assertion_of_contexts(all_symbols),
                            may_depend_on_external_resources=line_filter_case.
                            value.may_depend_on_external_resources,
                            output_lines=expected_lines,
                        ),
                        sub_test_identifiers={
                            'preserve_new_lines': preserve_new_lines,
                            'quoting': quoting_variant,
                            'line_filtering': line_filter_case.name,
                        })
    def _check(
        self,
        pattern: str,
        replacement_str: str,
        lines_selector: LineMatcherSymbolContext,
        input_lines__wo_nl: List[str],
        output_lines__wo_nl: List[str],
    ):
        # ARRANGE #
        input_lines = with_appended_new_lines(input_lines__wo_nl)
        output_lines = with_appended_new_lines(output_lines__wo_nl)

        pattern_symbol = StringConstantSymbolContext(
            'PATTERN_SYMBOL',
            pattern,
            default_restrictions=asrt_regex.is_reference_restrictions__regex(),
        )
        replacement_symbol = StringConstantSymbolContext(
            'REPLACEMENT_SYMBOL',
            replacement_str,
            default_restrictions=asrt_ref_rest.is__w_str_rendering(),
        )

        all_symbol = [lines_selector, pattern_symbol, replacement_symbol]

        for preserve_new_lines in [False, True]:
            syntax = ReplaceRegexAbsStx(
                pattern_symbol.abstract_syntax,
                replacement_symbol.abstract_syntax,
                preserve_new_lines=preserve_new_lines,
                lines_filter=lines_selector.abstract_syntax,
            )
            # ACT & ASSERT #
            integration_check.CHECKER__PARSE_FULL.check__abs_stx__layout__std_source_variants(
                self,
                syntax,
                model_constructor.of_lines(self, input_lines),
                arrangement_w_tcds(
                    symbols=SymbolContext.symbol_table_of_contexts(
                        all_symbol), ),
                expectation_of_successful_replace_execution__multi(
                    symbol_references=SymbolContext.
                    references_assertion_of_contexts(all_symbol),
                    output_lines=output_lines,
                    may_depend_on_external_resources=True,
                ),
                sub_test_identifiers={
                    'preserve_new_lines': preserve_new_lines,
                })
Example #6
0
    def test_value_SHOULD_be_able_to_have_symbol_references(self):
        # ARRANGE #

        variable_name = 'variable_to_assign'

        my_symbol = StringConstantSymbolContext('my_symbol', 'my symbol value')
        your_symbol = StringConstantSymbolContext('your_symbol',
                                                  'your symbol value')

        value_template = 'pre {MY_SYMBOL} {YOUR_SYMBOL} post'

        expected_evaluated_value_string = value_template.format(
            MY_SYMBOL=my_symbol.str_value,
            YOUR_SYMBOL=your_symbol.str_value,
        )
        expected_environ_after_main = {
            variable_name: expected_evaluated_value_string,
        }
        value_source_string = value_template.format(
            MY_SYMBOL=symbol_reference_syntax_for_name(my_symbol.name),
            YOUR_SYMBOL=symbol_reference_syntax_for_name(your_symbol.name),
        )

        source_syntax = SetVariableArgumentsAbsStx.of_str(
            variable_name,
            value_source_string,
            phase_spec=None,
            quoting=QuoteType.SOFT)

        # ACT & ASSERT #

        CHECKER.check__abs_stx__std_layouts_and_source_variants(
            self,
            source_syntax,
            Arrangement.setup_phase_aware(
                symbols=SymbolContext.symbol_table_of_contexts(
                    [my_symbol, your_symbol]),
                process_execution_settings=ProcessExecutionSettings.
                with_environ({}),
            ),
            MultiSourceExpectation.setup_phase_aware(
                main_side_effect_on_environment_variables=asrt.equals(
                    expected_environ_after_main),
                symbol_usages=asrt.matches_sequence([
                    my_symbol.usage_assertion__w_str_rendering,
                    your_symbol.usage_assertion__w_str_rendering,
                ]),
            ),
        )
    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, ),
            ))
Example #8
0
    def test_symbol_reference_on_command_line_SHOULD_be_reported_and_used_in_execution(
            self):
        symbol = StringConstantSymbolContext('symbol_name', 'symbol value')

        string_to_print_template = 'constant and {symbol}'
        expected_output_template = string_to_print_template + '\n'

        shell_source_line = shell_commands.command_that_prints_to_stdout(
            string_to_print_template.format(
                symbol=symbol.name__sym_ref_syntax))
        act_phase_instructions = [
            instr([shell_command_source_line_for(shell_source_line)])
        ]

        check_execution(
            self,
            sut.actor(),
            act_phase_instructions,
            arrangement_w_tcds(symbol_table=symbol.symbol_table),
            Expectation(symbol_usages=asrt.matches_singleton_sequence(
                symbol.reference_assertion__w_str_rendering),
                        post_sds=PostSdsExpectation.constant(
                            sub_process_result_from_execute=pr.stdout(
                                asrt.equals(
                                    expected_output_template.format(
                                        symbol=symbol.str_value))))),
        )
Example #9
0
    def runTest(self):
        # ARRANGE #
        exit_code_from_program = 0
        exe_file_in_path = fs.python_executable_file(
            'the-program',
            lines_content(py_program.exit_with_code(exit_code_from_program)),
        )
        program_symbol = StringConstantSymbolContext(
            'PROGRAM_NAME_SYMBOL',
            exe_file_in_path.name,
        )
        program_line = args.system_program_command_line(
            program_symbol.name__sym_ref_syntax).as_str

        with tmp_dir_in_path_with_files(DirContents([exe_file_in_path
                                                     ])) as environ:
            for source_case in valid_source_variants(program_line):
                with self.subTest(source_case.name):
                    # ACT & ASSERT #
                    integration_check.check_execution(
                        self,
                        sut.actor(),
                        [instr([program_line])],
                        arrangement_w_tcds(
                            symbol_table=program_symbol.symbol_table,
                            act_exe_input=AtcExeInputArr(environ=environ),
                        ),
                        Expectation(
                            symbol_usages=asrt.matches_singleton_sequence(
                                program_symbol.
                                reference_assertion__string__w_all_indirect_refs_are_strings
                            ),
                            execute=asrt_eh.is_exit_code(
                                exit_code_from_program)),
                    )
Example #10
0
    def test_symbol_value_with_space_SHOULD_be_given_as_single_argument_to_program(
            self):
        symbol = StringConstantSymbolContext('symbol_name',
                                             'symbol value with space')

        expected_output = lines_content([symbol.str_value])

        executable_file_name = 'the-executable_file_name'

        command_line = '{executable_file_name} {symbol}'.format(
            executable_file_name=executable_file_name,
            symbol=symbol.name__sym_ref_syntax,
        )

        arrangement = arrangement_w_tcds(
            hds_contents=relativity_configurations.ATC_FILE.
            populator_for_relativity_option_root__hds(
                fs.DirContents([
                    fs.python_executable_file(
                        executable_file_name,
                        PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES
                    )
                ])),
            symbol_table=symbol.symbol_table)

        expectation = Expectation(
            symbol_usages=asrt.matches_sequence(
                [symbol.reference_assertion__w_str_rendering]),
            execute=eh_assertions.is_exit_code(0),
            post_sds=PostSdsExpectation.
            constant(sub_process_result_from_execute=pr.stdout(
                asrt.Equals(expected_output, 'CLI arguments, one per line'))),
        )
        check_execution(self, sut.actor(), [instr([command_line])],
                        arrangement, expectation)
Example #11
0
 def test_valid_syntax_with_symbol_references(self):
     symbol = StringConstantSymbolContext('symbol_name', 'symbol value')
     before_symbol = 'text before symbol'
     after_symbol = 'text after symbol'
     cases = [
         Variant(
             'single unquoted symbol reference',
             symbol.abstract_syntax,
             symbol.str_value,
         ),
         Variant(
             'reference embedded in quoted string',
             StringLiteralAbsStx(
                 '{before_sym_ref}{sym_ref}{after_sym_ref}'.format(
                     sym_ref=symbol.name__sym_ref_syntax,
                     before_sym_ref=before_symbol,
                     after_sym_ref=after_symbol,
                 ),
                 QuoteType.SOFT,
             ),
             before_symbol + symbol.str_value + after_symbol,
         ),
     ]
     for variant in cases:
         # ACT & ASSERT #
         CHECKER.check(
             self, equivalent_source_variants__for_expr_parse__s__nsc,
             variant.syntax, Arrangement(symbol.symbol_table),
             Expectation(
                 [symbol.reference__w_str_rendering],
                 variant.resolved_str,
             ))
Example #12
0
class NameWSymRefs:
    RESOLVED_STR = 'the_var_name'
    REF_1 = StringConstantSymbolContext(
        'NAME_SYM_1',
        RESOLVED_STR[:4],
        default_restrictions=asrt_w_str_rend_rest.is__string__w_all_indirect_refs_are_strings(),
    )
    REF_2 = StringConstantSymbolContext(
        'NAME_SYM_2',
        RESOLVED_STR[4:],
        default_restrictions=asrt_w_str_rend_rest.is__string__w_all_indirect_refs_are_strings(),
    )
    SYMBOL_CONTEXTS = (REF_1, REF_2)

    STRING_ABS_STX = str_abs_stx.StringConcatAbsStx([REF_1.abstract_syntax,
                                                     REF_2.abstract_syntax])
Example #13
0
    def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess):
        expected_content_line_template = 'expected content line, with {symbol} ref'

        def expected_content(symbol_content: str) -> str:
            return expected_content_line_template.format(symbol=symbol_content)

        symbol = StringConstantSymbolContext('symbol_name', 'the symbol value')
        self._check(
            test_configuration.source_for(
                args('{maybe_not} {equals} <<EOF',
                     maybe_not=maybe_not.nothing__if_positive__not_option__if_negative),
                [expected_content(symbol.name__sym_ref_syntax),
                 'EOF',
                 'following line']),
            model_constructor.of_str(self, lines_content([expected_content(symbol.str_value)])),
            arrangement_w_tcds(
                post_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY,
                symbols=symbol.symbol_table),
            Expectation(
                ParseExpectation(
                    source=asrt_source.is_at_end_of_line(3),
                    symbol_references=asrt.matches_sequence([
                        symbol.reference_assertion__w_str_rendering
                    ]),
                ),
                ExecutionExpectation(
                    main_result=maybe_not.pass__if_positive__fail__if_negative,
                ),
            ),
        )
Example #14
0
 def _check_direct_with_satisfied_variants_for_restriction_on_every_node(
         self,
         restriction_on_direct_node: ValueRestriction,
         expected_result: Assertion[Optional[Failure]],
 ):
     symbol_to_check = StringConstantSymbolContext('symbol_name')
     restriction_on_every_cases = [
         NameAndValue(
             'restriction on every is None',
             None,
         ),
         NameAndValue(
             'restriction on every is unconditionally satisfied',
             RestrictionThatIsAlwaysSatisfied(),
         ),
     ]
     for restriction_on_every_case in restriction_on_every_cases:
         with self.subTest(restriction_on_every_case.name):
             restrictions = sut.ReferenceRestrictionsOnDirectAndIndirect(
                 direct=restriction_on_direct_node,
                 indirect=restriction_on_every_case.value
             )
             actual_result = restrictions.is_satisfied_by(symbol_to_check.symbol_table,
                                                          symbol_to_check.name,
                                                          symbol_to_check.symbol_table_container)
             expected_result.apply_with_message(self, actual_result, 'return value')
Example #15
0
    def runTest(self):
        # ARRANGE #
        program_symbol = StringConstantSymbolContext(
            'program_name_symbol',
            'the-program',
            default_restrictions=asrt_rest.is__string__w_all_indirect_refs_are_strings(),
        )
        argument_list_symbol = ListConstantSymbolContext(
            'arguments_symbol',
            ['1st', '2nd'],
        )
        symbols = [program_symbol, argument_list_symbol]

        expected_command = asrt_command.matches_command(
            driver=asrt_command.matches_system_program_command_driver(
                asrt.equals(program_symbol.str_value)
            ),
            arguments=asrt.equals(argument_list_symbol.constant),
        )

        # ACT & ASSERT #
        check_successful_execution(
            self,
            arguments=command_line(
                program_symbol.name__sym_ref_syntax,
                program_arguments.simple(argument_list_symbol.name__sym_ref_syntax),
            ),
            expected_command=expected_command,
            symbols=SymbolContext.symbol_table_of_contexts(symbols),
            symbol_usages=SymbolContext.usages_assertion_of_contexts(symbols)
        )
Example #16
0
    def test_assignment_of_single_constant_line(self):
        value_str = 'value'
        symbol_name = 'name1'
        sb = SB.new_with(value_str=value_str,
                         symbol_name=symbol_name)

        source = sb.lines(
            here_doc_lines(first_line_start='{string_type} {symbol_name} = ',
                           marker='EOF',
                           contents_lines=['{value_str}']) +
            ['following line']
        )
        # EXPECTATION #
        expected_symbol = StringConstantSymbolContext(symbol_name, value_str + '\n')
        expectation = Expectation.phase_agnostic(
            symbol_usages=asrt.matches_sequence([
                expected_symbol.assert_matches_definition_of_sdv
            ]),
            symbols_after_main=assert_symbol_table_is_singleton(
                symbol_name,
                expected_symbol.value.assert_matches_container_of_sdv,
            ),
            source=asrt_source.is_at_beginning_of_line(4)
        )
        # ACT & ASSERT #
        INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
Example #17
0
 def test_invalid_arguments_with_symbol_references(self):
     symbol_name = SymbolWithReferenceSyntax('symbol_name')
     operand_arg_with_symbol_ref_list = [
         ' {op} {sym}'.format(op=comparators.EQ.name, sym=symbol_name),
     ]
     invalid_symbol_values = [
         'not_a_number',
         '1.5',
         '72 87',
     ]
     for invalid_symbol_value in invalid_symbol_values:
         symbol = StringConstantSymbolContext(symbol_name.name,
                                              invalid_symbol_value)
         for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list:
             arguments = self._conf().arguments_constructor.apply(
                 operand_arg_with_symbol_ref)
             with self.subTest(argument=arguments,
                               invalid_symbol_value=invalid_symbol_value):
                 for source in equivalent_source_variants__for_expression_parser(
                         self, Arguments(arguments)):
                     self._check(
                         source,
                         Arrangement(symbols=symbol.symbol_table),
                         Expectation(
                             ParseExpectation(
                                 symbol_references=asrt.matches_sequence([
                                     symbol.
                                     reference_assertion__string__w_all_indirect_refs_are_strings
                                 ]), ),
                             ExecutionExpectation(
                                 validation=asrt_validation.
                                 ValidationAssertions.
                                 pre_sds_fails__w_any_msg(), ),
                         ),
                     )
Example #18
0
 def test_not_equals(self):
     string_symbol = StringConstantSymbolContext('string_symbol_name',
                                                 'string symbol value')
     cases = [
         Case('different number of elements',
              expected=
              list_sdvs.empty(),
              actual=
              list_sdvs.from_str_constants(['value']),
              ),
         Case('different value of single string',
              expected=
              list_sdvs.from_str_constants(['expected value']),
              actual=
              list_sdvs.from_str_constants(['actual value']),
              ),
         Case('different element types, but same resolved value',
              expected=
              list_sdvs.from_str_constants([string_symbol.str_value]),
              actual=
              list_sdvs.from_elements([list_sdvs.symbol_element(string_symbol.reference__w_str_rendering)]),
              symbols=
              string_symbol.symbol_table,
              ),
     ]
     for case in cases:
         with self.subTest(msg=case.name):
             assertion = sut.equals_list_sdv(case.expected, case.symbols)
             assert_that_assertion_fails(assertion, case.actual)
Example #19
0
    def test_that_symbols_from_arrangement_exist_in_environment(self):
        symbol = StringConstantSymbolContext('symbol_name', 'the symbol value')
        symbol_table_of_arrangement = symbol.symbol_table
        expected_symbol_table = symbol.symbol_table
        assertion_for_validation = do_fail_if_symbol_table_does_not_equal(
            self,
            expected_symbol_table,
            get_symbol_table_from_path_resolving_environment_that_is_first_arg)

        assertion_for_main = do_fail_if_symbol_table_does_not_equal(
            self,
            expected_symbol_table,
            get_symbol_table_from_instruction_environment_that_is_first_arg)

        self._check(
            utils.ParserThatGives(
                before_assert_phase_instruction_that(
                    validate_pre_sds_initial_action=assertion_for_validation,
                    validate_post_setup_initial_action=assertion_for_validation,
                    main_initial_action=assertion_for_main,
                )),
            utils.single_line_source(),
            sut.arrangement(symbols=symbol_table_of_arrangement),
            sut.Expectation(),
        )
Example #20
0
    def test_that_symbols_from_arrangement_exist_in_environment(self):
        symbol = StringConstantSymbolContext('symbol_name', 'the symbol value')
        symbol_table_of_arrangement = symbol.symbol_table
        expected_symbol_table = symbol.symbol_table
        expectation = asrt_sym.equals_symbol_table(expected_symbol_table)

        sdv_that_checks_symbols = MatcherSdvThatAssertsThatSymbolsAreAsExpected(self, expectation)

        cases = [
            NameAndValue('arrangement without tcds',
                         arrangement_wo_tcds(
                             symbol_table_of_arrangement)
                         ),
            NameAndValue('arrangement with tcds',
                         arrangement_w_tcds(
                             symbols=symbol_table_of_arrangement)
                         ),
        ]
        for arrangement in cases:
            with self.subTest(arrangement.name):
                self._check___single_and_multi(
                    utils.single_line_arguments(),
                    ARBITRARY_MODEL,
                    _constant_line_matcher_type_parser_of_matcher_sdv(sdv_that_checks_symbols),
                    arrangement.value,
                    Expectation(),
                )
Example #21
0
 def test_invalid_arguments_with_symbol_references(self):
     symbol_name = SymbolWithReferenceSyntax('symbol_name')
     operand_arg_with_symbol_ref_list = [
         IntegerMatcherComparisonAbsStx.of_cmp_op(
             comparators.EQ,
             StringSymbolAbsStx(symbol_name.name),
         ),
     ]
     invalid_symbol_values = [
         'not_a_number',
         '1.5',
         '72 87',
     ]
     for invalid_symbol_value in invalid_symbol_values:
         symbol = StringConstantSymbolContext(symbol_name.name,
                                              invalid_symbol_value)
         for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list:
             full_syntax = self._conf().arguments_constructor.apply(
                 operand_arg_with_symbol_ref)
             with self.subTest(argument=full_syntax.as_str__default(),
                               invalid_symbol_value=invalid_symbol_value):
                 self._check(
                     full_syntax,
                     ArrangementPostAct2(symbols=symbol.symbol_table),
                     MultiSourceExpectation(
                         symbol_usages=asrt.matches_sequence([
                             symbol.
                             reference_assertion__string__w_all_indirect_refs_are_strings
                         ]),
                         execution=ExecutionExpectation(
                             validation_pre_sds=svh_asrt.
                             is_validation_error(), ),
                     ),
                 )
Example #22
0
 def test_invalid_arguments_with_symbol_references(self):
     symbol_name = SymbolWithReferenceSyntax('symbol_name')
     operand_arg_with_symbol_ref_list = [
         ' {op} {sym}'.format(op=comparators.EQ.name, sym=symbol_name),
     ]
     invalid_symbol_values = [
         'not_a_number',
         '1.5',
         '72 87',
     ]
     for invalid_symbol_value in invalid_symbol_values:
         symbol = StringConstantSymbolContext(symbol_name.name,
                                              invalid_symbol_value)
         for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list:
             arguments = self._conf().arguments_constructor.apply(
                 operand_arg_with_symbol_ref)
             with self.subTest(argument=arguments,
                               invalid_symbol_value=invalid_symbol_value):
                 for source in equivalent_source_variants__with_source_check__consume_last_line(
                         self, arguments):
                     self._check(
                         source,
                         ArrangementPostAct(symbols=symbol.symbol_table),
                         Expectation(
                             validation_pre_sds=svh_asrt.
                             is_validation_error(),
                             symbol_usages=asrt.matches_sequence([
                                 symbol.
                                 reference_assertion__string__w_all_indirect_refs_are_strings
                             ]),
                         ),
                     )
Example #23
0
    def test_symbol_references(self):
        python_interpreter_symbol = StringConstantSymbolContext(
            'python_interpreter_symbol', sys.executable)
        execute_program_option_symbol = StringConstantSymbolContext(
            'execute_program_option', '-c')
        exit_code_symbol = StringIntConstantSymbolContext(
            'exit_code_symbol', 87)

        argument = ' {python_interpreter} {execute_program_option} {source_option}   exit({exit_code})  '.format(
            python_interpreter=python_interpreter_symbol.name__sym_ref_syntax,
            execute_program_option=execute_program_option_symbol.
            name__sym_ref_syntax,
            source_option=syntax_elements.
            REMAINING_PART_OF_CURRENT_LINE_AS_LITERAL_MARKER,
            exit_code=exit_code_symbol.name__sym_ref_syntax,
        )

        arrangement = Arrangement.phase_agnostic(
            symbols=SymbolContext.symbol_table_of_contexts([
                python_interpreter_symbol,
                execute_program_option_symbol,
                exit_code_symbol,
            ]),
            tcds=TcdsArrangement(),
        )

        source = remaining_source(argument, ['following line'])

        expectation = Expectation.phase_agnostic(
            source=assert_source(current_line_number=asrt.equals(2),
                                 column_index=asrt.equals(0)),
            symbol_usages=asrt.matches_sequence([
                python_interpreter_symbol.usage_assertion__path_or_string(
                    syntax_elements.EXE_FILE_REL_OPTION_ARG_CONF.options.
                    accepted_relativity_variants),
                execute_program_option_symbol.usage_assertion__w_str_rendering,
                exit_code_symbol.usage_assertion__w_str_rendering,
            ]),
            main_result=result_assertions.equals(exit_code_symbol.int_value,
                                                 ''),
        )

        parser = sut.embryo_parser('instruction-name')
        embryo_check.check(self, parser, source, arrangement, expectation)
Example #24
0
    def test_removal_of_new_lines_SHOULD_join_lines(self):
        # ARRANGE #
        cases = [
            NIE('final line not ended by new-line',
                input_value=[
                    '1\n',
                    '2\n',
                    '3',
                ],
                expected_value=[
                    '123',
                ]),
            NIE('final line ended by new-line',
                input_value=[
                    '1\n',
                    '2\n',
                    '3\n',
                ],
                expected_value=[
                    '123',
                ]),
        ]
        for line_filter_case in LINE_FILTER_CASES:
            for case in cases:
                with self.subTest(model=case.name,
                                  line_filtering=line_filter_case.name):
                    nl_string_symbol = StringConstantSymbolContext(
                        'NL',
                        '\n',
                        default_restrictions=asrt_regex.
                        is_reference_restrictions__regex(),
                    )
                    all_symbols = line_filter_case.value.symbols + [
                        nl_string_symbol
                    ]
                    source = ReplaceRegexAbsStx(
                        nl_string_symbol.abstract_syntax,
                        StringLiteralAbsStx.empty_string(),
                        preserve_new_lines=False,
                        lines_filter=line_filter_case.value.syntax,
                    )

                    # ACT & ASSERT #
                    integration_check.CHECKER__PARSE_SIMPLE.check__abs_stx(
                        self, source,
                        model_constructor.of_lines(self, case.input_value),
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                all_symbols), ),
                        expectation_of_successful_replace_execution(
                            symbol_references=SymbolContext.
                            references_assertion_of_contexts(all_symbols),
                            may_depend_on_external_resources=line_filter_case.
                            value.may_depend_on_external_resources,
                            output_lines=case.expected_value,
                        ))
 def test_all_referenced_symbols_have_definition_source(self):
     for path_to_failing_symbol in [[], ['symbol_on_path_to_failing_symbol']]:
         for how_to_fix in ['', 'how_to_fix']:
             with self.subTest(path_to_failing_symbol=path_to_failing_symbol,
                               how_to_fix=how_to_fix):
                 error = _new_em('error message',
                                 how_to_fix=how_to_fix)
                 failure = FailureOfIndirectReference(failing_symbol='name_of_failing_symbol',
                                                      path_to_failing_symbol=path_to_failing_symbol,
                                                      error=error)
                 # ACT #
                 checked_symbol = StringConstantSymbolContext('checked_symbol')
                 symbol_table = SymbolContext.symbol_table_of_contexts(
                     [checked_symbol] +
                     [StringConstantSymbolContext(failing_symbol)
                      for failing_symbol in path_to_failing_symbol]
                 )
                 actual = sut.ErrorMessage(checked_symbol.name, symbol_table, failure)
                 # ASSERT #
                 asrt_text_doc.assert_is_valid_text_renderer(self, actual)
Example #26
0
    def test_file_path(self):
        relativity_test_cases = [
            (RelOptionType.REL_HDS_CASE, True),
            (RelOptionType.REL_ACT, False),
        ]
        path_suffix_str = 'path-suffix-file.txt'
        path_suffix_test_cases = [
            (path_part_sdvs.from_constant_str(path_suffix_str), ()),
            (path_part_sdvs.from_string(
                string_sdv_of_single_symbol_reference(
                    'path_suffix_symbol',
                    reference_restrictions.is_any_type_w_str_rendering())),
             (StringConstantSymbolContext('path_suffix_symbol',
                                          path_suffix_str).entry, )),
        ]
        for rel_option, exists_pre_sds in relativity_test_cases:
            # ARRANGE #
            path_component_from_referenced_path = 'path-component-from-referenced-file-ref'
            referenced_sym = ConstantSuffixPathDdvSymbolContext(
                'path_symbol', rel_option, path_component_from_referenced_path)

            for path_suffix, symbol_table_entries in path_suffix_test_cases:
                fr_sdv_to_check = sut.PathSdvRelSymbol(
                    path_suffix,
                    _symbol_reference_of_path_with_accepted(
                        referenced_sym.name, rel_option))
                symbol_table = referenced_sym.symbol_table
                symbol_table.add_all(symbol_table_entries)
                tcds = fake_tcds()
                expected_root_path = _root_path_of_option(rel_option, tcds)
                expected_path = expected_root_path / path_component_from_referenced_path / path_suffix_str
                expected_path_str = str(expected_path)
                environment = PathResolvingEnvironmentPreOrPostSds(
                    tcds, symbol_table)
                with self.subTest(msg=str(rel_option)):
                    # ACT #
                    path_to_check = fr_sdv_to_check.resolve(
                        environment.symbols)
                    if exists_pre_sds:
                        tested_path_msg = 'value_pre_sds'
                        actual_path = path_to_check.value_pre_sds(
                            environment.hds)
                    else:
                        tested_path_msg = 'value_post_sds'
                        actual_path = path_to_check.value_post_sds(
                            environment.sds)
                    actual_path_pre_or_post_sds = path_to_check.value_of_any_dependency(
                        environment.tcds)
                    # ASSERT #
                    self.assertEqual(expected_path_str, str(actual_path),
                                     tested_path_msg)
                    self.assertEqual(expected_path_str,
                                     str(actual_path_pre_or_post_sds),
                                     'value_of_any_dependency')
Example #27
0
    def test_string_symbol_reference_in_executable_and_argument(self):
        symbol_for_executable = StringConstantSymbolContext(
            'executable_symbol_name', 'the-executable')

        argument_symbol = StringConstantSymbolContext('argument_symbol_name',
                                                      'string-constant')

        expected_output = lines_content([argument_symbol.str_value])

        command_line = '{executable} {argument} '.format(
            executable=symbol_for_executable.name__sym_ref_syntax,
            argument=argument_symbol.name__sym_ref_syntax,
        )

        arrangement = arrangement_w_tcds(
            hds_contents=relativity_configurations.ATC_FILE.
            populator_for_relativity_option_root__hds(
                fs.DirContents([
                    fs.python_executable_file(
                        symbol_for_executable.str_value,
                        PYTHON_PROGRAM_THAT_PRINTS_COMMAND_LINE_ARGUMENTS_ON_SEPARATE_LINES
                    )
                ])),
            symbol_table=SymbolContext.symbol_table_of_contexts([
                symbol_for_executable,
                argument_symbol,
            ]))

        expectation = Expectation(
            execute=eh_assertions.is_exit_code(0),
            symbol_usages=asrt.matches_sequence([
                symbol_for_executable.reference_assertion__path_or_string(
                    PATH_RELATIVITY_VARIANTS_FOR_FILE_TO_RUN),
                argument_symbol.reference_assertion__w_str_rendering,
            ]),
            post_sds=PostSdsExpectation.
            constant(sub_process_result_from_execute=pr.stdout(
                asrt.Equals(expected_output, 'CLI arguments, one per line'))),
        )
        check_execution(self, sut.actor(), [instr([command_line])],
                        arrangement, expectation)
Example #28
0
 def test_with_symbol_references(self):
     symbol = StringConstantSymbolContext('symbol_1_name', 'symbol 1 value')
     line_with_sym_ref_template = 'before symbol {symbol} after symbol\n'
     syntax = HereDocAbsStx(
         line_with_sym_ref_template.format(
             symbol=symbol.name__sym_ref_syntax))
     CHECKER.check(
         self, equivalent_source_variants__for_full_line_expr_parse__s__nsc,
         syntax, Arrangement(symbol.symbol_table),
         Expectation(
             [symbol.reference__w_str_rendering],
             line_with_sym_ref_template.format(symbol=symbol.str_value)))
 def test_directly_referenced_symbol_is_builtin(self):
     referenced_symbol = StringConstantSymbolContext('referenced symbol',
                                                     'referenced symbol value',
                                                     definition_source=None)
     for how_to_fix in ['', 'how_to_fix']:
         with self.subTest(how_to_fix=how_to_fix):
             error = _new_em('error message',
                             how_to_fix=how_to_fix)
             failure = FailureOfIndirectReference(failing_symbol='name_of_failing_symbol',
                                                  path_to_failing_symbol=[referenced_symbol.name],
                                                  error=error)
             # ACT #
             checked_symbol = StringConstantSymbolContext('checked symbol name',
                                                          'checked symbol value')
             symbol_table = SymbolContext.symbol_table_of_contexts([
                 checked_symbol,
                 referenced_symbol,
             ])
             actual = sut.ErrorMessage(checked_symbol.name, symbol_table, failure)
             # ASSERT #
             asrt_text_doc.assert_is_valid_text_renderer(self, actual)
Example #30
0
    def test_symbol_references(self):
        expected_exit_status = 72
        file_to_interpret = fs.File('python-logic_symbol_utils.py',
                                    py_script_that_exists_with_status(expected_exit_status))
        file_to_interpret_symbol = StringConstantSymbolContext('file_to_interpret_symbol',
                                                               file_to_interpret.file_name)
        python_interpreter_symbol = StringConstantSymbolContext('python_interpreter_symbol', sys.executable)

        argument = ' "{python_interpreter}" {file_to_interpret}'.format(
            python_interpreter=python_interpreter_symbol.name__sym_ref_syntax,
            file_to_interpret=file_to_interpret_symbol.name__sym_ref_syntax,
        )

        following_line = 'following line'
        source = remaining_source(argument, [following_line])

        arrangement = Arrangement.phase_agnostic(
            tcds=TcdsArrangement(
                tcds_contents=TcdsPopulatorForRelOptionType(
                    RelOptionType.REL_ACT,
                    fs.DirContents([file_to_interpret])),
            ),
            symbols=SymbolContext.symbol_table_of_contexts([
                python_interpreter_symbol,
                file_to_interpret_symbol,
            ]),
        )

        expectation = Expectation.phase_agnostic(
            source=assert_source(current_line_number=asrt.equals(2),
                                 column_index=asrt.equals(0)),
            symbol_usages=asrt.matches_sequence([
                python_interpreter_symbol.usage_assertion__w_str_rendering,
                file_to_interpret_symbol.usage_assertion__w_str_rendering,
            ]),
            main_result=result_assertions.equals(expected_exit_status, ''),
        )

        parser = sut.embryo_parser('instruction-name')
        embryo_check.check(self, parser, source, arrangement, expectation)