class TestEqualsSourceLocation(unittest.TestCase):
    path_a = pathlib.Path('a')
    path_b = pathlib.Path('b')

    line_sequence_1 = LineSequence(1, ['line'])
    line_sequence_2 = LineSequence(2, ['line'])

    def test_not_equals(self):
        cases = [
            NEA(
                'different path',
                SourceLocation(self.line_sequence_1, self.path_a),
                SourceLocation(self.line_sequence_1, self.path_b),
            ),
            NEA(
                'different line sequence',
                SourceLocation(self.line_sequence_1, self.path_a),
                SourceLocation(self.line_sequence_2, self.path_a),
            ),
        ]
        for nea in cases:
            with self.subTest(nea.name):
                assertion = sut.equals_source_location(nea.expected)
                # ACT & ASSERT #
                assert_that_assertion_fails(assertion, nea.actual)

    def test_equals(self):
        assertion = sut.equals_source_location(
            SourceLocation(self.line_sequence_1, self.path_a))
        # ACT & ASSERT #
        assertion.apply_without_message(
            self, SourceLocation(self.line_sequence_1, self.path_a))
class TestEqualsSourceLocationPath(unittest.TestCase):
    path_a = pathlib.Path('a')
    path_b = pathlib.Path('b')

    line_sequence_1 = LineSequence(1, ['line'])
    line_sequence_2 = LineSequence(2, ['line'])

    def test_equals(self):
        location1 = SourceLocation(self.line_sequence_1, self.path_a)
        location2 = SourceLocation(self.line_sequence_2, self.path_b)
        cases = [
            NEA(
                'without file inclusion chain',
                expected=source_location_path_without_inclusions(location1),
                actual=source_location_path_without_inclusions(location1),
            ),
            NEA(
                'with file inclusion chain',
                expected=SourceLocationPath(location2, [location1]),
                actual=SourceLocationPath(location2, [location1]),
            ),
        ]
        for nea in cases:
            with self.subTest(nea.name):
                assertion = sut.equals_source_location_path(nea.expected)
                # ACT & ASSERT #
                assertion.apply_without_message(self, nea.actual)

    def test_not_equals(self):
        location1 = SourceLocation(self.line_sequence_1, self.path_a)
        location2 = SourceLocation(self.line_sequence_2, self.path_b)

        cases = [
            NEA(
                'without inclusion chain/different location',
                expected=source_location_path_without_inclusions(location1),
                actual=source_location_path_without_inclusions(location2),
            ),
            NEA(
                'with inclusion chain/different location',
                expected=SourceLocationPath(location1, [location1]),
                actual=SourceLocationPath(location2, [location1]),
            ),
            NEA(
                'different inclusion chain / different size',
                expected=SourceLocationPath(location1, [location1]),
                actual=SourceLocationPath(location1, []),
            ),
            NEA(
                'different inclusion chain / different contents',
                expected=SourceLocationPath(location1, [location1]),
                actual=SourceLocationPath(location1, [location2]),
            ),
        ]
        for nea in cases:
            with self.subTest(nea.name):
                assertion = sut.equals_source_location_path(nea.expected)
                # ACT & ASSERT #
                assert_that_assertion_fails(assertion, nea.actual)
Example #3
0
    def test_not_matches(self):
        # ARRANGE #
        expected_instruction = Instruction()
        expected_description = 'the description'
        instruction_info = InstructionInfo(expected_instruction, expected_description)
        expected_source = LineSequence(1, ('expected',))
        actual_element = ParsedInstruction(expected_source, instruction_info)

        cases = [
            NameAndValue('mismatch on source',
                         sut.matches_instruction(asrt.not_(equals_line_sequence(expected_source)),
                                                 matches_instruction_info(asrt.equals(expected_description),
                                                                          asrt.is_(expected_instruction)))
                         ),
            NameAndValue('mismatch on description',
                         sut.matches_instruction(equals_line_sequence(expected_source),
                                                 matches_instruction_info(asrt.not_(asrt.equals(expected_description)),
                                                                          asrt.is_(expected_instruction)))
                         ),
            NameAndValue('mismatch on instruction',
                         sut.matches_instruction(equals_line_sequence(expected_source),
                                                 matches_instruction_info(asrt.not_(asrt.equals(expected_description)),
                                                                          asrt.not_(asrt.is_(expected_instruction))))
                         ),
        ]
        for nav in cases:
            with self.subTest(nav.name):
                # ACT & ASSERT #
                assert_that_assertion_fails(nav.value, actual_element)
 def test_parse_single_line_instruction(self):
     parser = sut.standard_syntax_element_parser(
         _InstructionParserForInstructionLineThatStartsWith('I'))
     test_cases = [
         (['I arguments'], ''),
         (['I arguments', 'remaining'], 'remaining'),
         (['I line 1', 'I line 2',
           'not an instruction'], 'not an instruction'),
     ]
     for source_lines, remaining_source in test_cases:
         with self.subTest(source_lines=source_lines,
                           remaining_source=remaining_source):
             source = _source_for_lines(source_lines)
             # ACT #
             element = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             expected_instruction_source = LineSequence(
                 1, (source_lines[0], ))
             element_assertion = matches_instruction(
                 source=equals_line_sequence(expected_instruction_source),
                 instruction_info=matches_instruction_info(
                     assertion_on_description=asrt.is_none,
                     assertion_on_instruction=asrt.is_instance(
                         Instruction)))
             element_assertion.apply_with_message(self, element, 'element')
             self.assertEqual(remaining_source, source.remaining_source,
                              'Remaining source')
Example #5
0
    def test_SHOULD_be_equal(self):
        # ARRANGE #
        cases = [
            NEA('without lines',
                expected=LineSequence(1, ()),
                actual=LineSequence(1, ())),
            NEA('with lines',
                expected=LineSequence(2, ('first', 'second')),
                actual=LineSequence(2, ('first', 'second'))),
        ]

        for name_and_ea in cases:
            with self.subTest(name_and_ea.name):
                assertion = sut.equals_line_sequence(name_and_ea.expected)
                # ACT & ASSERT #
                assertion.apply_without_message(self, name_and_ea.actual)
Example #6
0
 def _test_case(self) -> test_case_doc.TestCase:
     return full_test_case_with_instructions(
         [configuration_phase_instruction_that(
             main_initial_action=_RecordCurrDir(self.recorder,
                                                phase_step.CONFIGURATION__MAIN).call)],
         [setup_phase_instruction_that(
             validate_post_setup_initial_action=_RecordCurrDir(self.recorder,
                                                               phase_step.SETUP__VALIDATE_POST_SETUP),
             main_initial_action=_RecordCurrDirThenMakeDirAndChangeToIt(self.recorder,
                                                                        phase_step.SETUP__MAIN))],
         [act_phase_instruction_with_source(LineSequence(1, ('not used',)))],
         [before_assert_phase_instruction_that(
             validate_post_setup_initial_action=_RecordCurrDir(self.recorder,
                                                               phase_step.BEFORE_ASSERT__VALIDATE_POST_SETUP),
             main_initial_action=_RecordCurrDirThenMakeDirAndChangeToIt(self.recorder,
                                                                        phase_step.BEFORE_ASSERT__MAIN))],
         [assert_phase_instruction_that(
             validate_post_setup_initial_action=_RecordCurrDir(self.recorder,
                                                               phase_step.ASSERT__VALIDATE_POST_SETUP),
             main_initial_action=_RecordCurrDirThenMakeDirAndChangeToIt(self.recorder,
                                                                        phase_step.ASSERT__MAIN))],
         [cleanup_phase_instruction_that(
             main_initial_action=_RecordCurrDirThenMakeDirAndChangeToIt(self.recorder,
                                                                        phase_step.CLEANUP__MAIN))],
     )
    def test_instruction_environment_specifies_correct_tmp_dir_space_for_each_instruction(
            self):
        # ARRANGE #
        recorder = {}

        setup_phase_instr_that_records = setup_phase_instruction_that(
            validate_post_setup_initial_action=
            RecordTmpDirForInstructionInPhase(PhaseEnum.SETUP, recorder),
            main_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.SETUP, recorder).call,
        )
        before_assert_phase_instr_that_records = before_assert_phase_instruction_that(
            validate_post_setup_initial_action=
            RecordTmpDirForInstructionInPhase(PhaseEnum.BEFORE_ASSERT,
                                              recorder),
            main_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.BEFORE_ASSERT, recorder).call)
        assert_phase_instr_that_records = assert_phase_instruction_that(
            validate_post_setup_initial_action=
            RecordTmpDirForInstructionInPhase(PhaseEnum.ASSERT, recorder),
            main_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.ASSERT, recorder).call)
        cleanup_phase_instr_that_records = cleanup_phase_instruction_that(
            main_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.CLEANUP, recorder).call)
        test_case = partial_test_case_with_instructions(
            [
                setup_phase_instr_that_records,
                setup_phase_instr_that_records,
            ],
            [act_phase_instruction_with_source(LineSequence(1, ('line', )))],
            [
                before_assert_phase_instr_that_records,
                before_assert_phase_instr_that_records,
            ],
            [
                assert_phase_instr_that_records,
                assert_phase_instr_that_records,
            ],
            [
                cleanup_phase_instr_that_records,
                cleanup_phase_instr_that_records,
            ],
        )
        actor_that_registers_tmp_dirs = ActorThatRunsConstantActions(
            validate_post_setup_initial_action=
            RecordTmpDirForInstructionInPhase(PhaseEnum.ACT, recorder),
            prepare_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.ACT, recorder),
            execute_initial_action=RecordTmpDirForInstructionInPhase(
                PhaseEnum.ACT, recorder),
        )
        # ACT & ASSERT #
        test(self,
             test_case,
             actor_that_registers_tmp_dirs,
             functools.partial(tmp_dir_is_correct_for_each_instruction,
                               recorder, 2),
             is_keep_sandbox=False)
    def ending_at(self, after_parse: ParseSource) -> LineSequence:
        num_chars = len(self._remaining__before) - len(
            after_parse.remaining_source)

        if num_chars < len(self._first_line.text):
            return line_sequence_from_line(self._first_line)

        source__consumed = self._remaining__before[:num_chars].rstrip()
        return LineSequence(self._first_line.line_number,
                            source__consumed.split('\n'))
def py_pgm_with_stdout_stderr_and_sleep_in_between(
        stdout_output_before_sleep: str, stderr_output_before_sleep: str,
        stdout_output_after_sleep: str, stderr_output_after_sleep: str,
        sleep_seconds: int, exit_code: int) -> LineSequence:
    py_src = py_programs.py_pgm_with_stdout_stderr_and_sleep_in_between(
        stdout_output_before_sleep,
        stderr_output_before_sleep,
        stdout_output_after_sleep,
        stderr_output_after_sleep,
        sleep_seconds,
        exit_code,
    )

    return LineSequence(69, py_src.splitlines())
Example #10
0
    def test_assignment_of_single_constant_word(self):
        # ARRANGE #

        source = single_line_source('{string_type} name1 = v1')
        source_string = source.source_string

        parser = sut.EmbryoParser()

        the_file_path_rel_referrer = pathlib.Path('the-path-rel-referrer')
        the_file_location_info = FileLocationInfo(
            pathlib.Path.cwd(),
            the_file_path_rel_referrer,
            [
                SourceLocation(LineSequence(10, ('line-in-inclusion-chain',)),
                               pathlib.Path('the-path-rel-referrer-of-first-file'))
            ])
        fs_location_info = FileSystemLocationInfo(the_file_location_info)

        # ACT #

        instruction = parser.parse(fs_location_info, source)

        # ASSERT #

        expected_source_location_path = SourceLocationPath(
            SourceLocation(single_line_sequence(1, source_string),
                           the_file_path_rel_referrer),
            the_file_location_info.file_inclusion_chain,
        )

        assertion = matches_source_location_info(
            abs_path_of_dir_containing_first_file_path=asrt.equals(
                the_file_location_info.abs_path_of_dir_containing_first_file_path),
            source_location_path=equals_source_location_path(expected_source_location_path)
        )

        # ASSERT SANITY #

        assert 1 == len(instruction.symbol_usages), 'A single symbol should have been defined'

        symbol_definition = instruction.symbol_usages[0]

        assert isinstance(symbol_definition, SymbolDefinition)

        assertion.apply_without_message(self, symbol_definition.symbol_container.source_location)
Example #11
0
    def parse(self, fs_location_info: FileSystemLocationInfo,
              source: ParseSource) -> ParsedInstruction:
        first_line_number = source.current_line_number
        current_line = source.current_line_text
        lines_read = [_un_escape(current_line)]
        source.consume_current_line()
        while not source.is_at_eof:
            current_line = source.current_line_text
            if syntax.is_section_header_line(current_line):
                break
            else:
                lines_read.append(_un_escape(current_line))
                source.consume_current_line()

        line_sequence = LineSequence(first_line_number, tuple(lines_read))
        return ParsedInstruction(
            line_sequence,
            InstructionInfo(SourceCodeInstruction(line_sequence), None))
 def test_parse_comment_line(self):
     parser = sut.standard_syntax_element_parser(
         _InstructionParserForInstructionLineThatStartsWith('I'))
     test_cases = [
         (['# comment'], 1, ''),
         (['  #  comment'], 1, ''),
         (['# A', '  # B'], 2, ''),
         (['# A', '  # B', ' '], 2, ' '),
         (['#   ', 'remaining'], 1, 'remaining'),
     ]
     for source_lines, num_comment_lines, remaining_source in test_cases:
         with self.subTest(source_lines=source_lines,
                           remaining_source=remaining_source):
             # ARRANGE #
             source = _source_for_lines(source_lines)
             # ACT #
             element = parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
             # ASSERT #
             element_assertion = equals_comment_element(
                 LineSequence(1, source_lines[:num_comment_lines]))
             element_assertion.apply_with_message(self, element, 'element')
             self.assertEqual(remaining_source, source.remaining_source,
                              'Remaining source')
Example #13
0
    def test_with_source(self):
        # ARRANGE #
        section_name = 'section-name'
        line_sequence_a = LineSequence(2, ['source a 1', 'source a 2'])
        file_path_a = pathlib.Path('file-a.src')

        minor_blocks_expectation = asrt.matches_sequence([
            _matches_plain_minor_block_w_single_plain_line(
                section_line(section_name)),
            _matches_plain_minor_block_w_single_plain_line(
                file_and_line_num_line(file_path_a, line_sequence_a)),
            matches_source_code_minor_block(line_sequence_a.lines),
        ])
        input_value = SourceLocationPath(location=SourceLocation(
            line_sequence_a, file_path_a),
                                         file_inclusion_chain=[])

        with self.subTest(blocks_rendering='minor blocks'):
            # ACT #
            actual_renderer = source_location.location_minor_blocks_renderer(
                input_value, section_name, None)
            actual = actual_renderer.render_sequence()
            # ASSERT #
            minor_blocks_expectation.apply_without_message(self, actual)

        with self.subTest(blocks_rendering='major blocks'):
            expected_major_blocks = asrt.matches_sequence([
                asrt_struct.matches_major_block__w_plain_properties(
                    minor_blocks=minor_blocks_expectation)
            ])
            # ACT #
            actual_renderer = source_location.location_blocks_renderer(
                input_value, section_name, None)
            actual = actual_renderer.render_sequence()
            # ASSERT #
            expected_major_blocks.apply_without_message(self, actual)
Example #14
0
    def test_SHOULD_not_be_equal(self):
        # ARRANGE #
        cases = [
            NEA('unequal first line',
                expected=LineSequence(1, ()),
                actual=LineSequence(2, ())),
            NEA('unequal single line',
                expected=LineSequence(1, ('expected', )),
                actual=LineSequence(1, ('actual', ))),
            NEA('unequal number of lines',
                expected=LineSequence(1, ('line', 'line')),
                actual=LineSequence(1, ('line', ))),
        ]

        for name_and_ea in cases:
            with self.subTest(name_and_ea.name):
                assertion = sut.equals_line_sequence(name_and_ea.expected)
                # ACT & ASSERT #
                assert_that_assertion_fails(assertion, name_and_ea.actual)
import unittest
from typing import Any, Sequence

from exactly_lib.util.line_source import Line, LineSequence, line_sequence_from_line
from exactly_lib_test.test_resources.value_assertions import value_assertion as asrt
from exactly_lib_test.test_resources.value_assertions.value_assertion import Assertion

ARBITRARY_LINE_SEQUENCE = LineSequence(10, ('arbitrary line sequence', ))


def equals_line(expected: Line) -> Assertion[Any]:
    return asrt.is_instance_with(
        Line,
        asrt.And([
            asrt.sub_component('line_number', Line.line_number.fget,
                               asrt.equals(expected.line_number)),
            asrt.sub_component('text', Line.text.fget,
                               asrt.equals(expected.text))
        ]))


def is_line(description: str = '') -> Assertion[Any]:
    return asrt.is_instance_with(
        Line,
        asrt.And([
            asrt.sub_component('line_number', Line.line_number.fget,
                               asrt.is_instance(int)),
            asrt.sub_component('text', Line.text.fget, asrt.is_instance(str))
        ]), description)

Example #16
0
def instr(lines: List[str],
          first_line_number: int = 1) -> SourceCodeInstruction:
    return SourceCodeInstruction(LineSequence(first_line_number, tuple(lines)))
Example #17
0
def act_phase_instruction_with_source(source_code: LineSequence = LineSequence(
    72, ('Dummy source code from act_phase_instruction_with_source',
         ))) -> ActPhaseInstruction:
    return SourceCodeInstruction(source_code)
 def __init__(self,
              recorder: ListRecorder = None,
              act_phase_source: LineSequence = LineSequence(
                  99, ('act phase line', ))):
     super().__init__(recorder)
     self.act_phase_source = act_phase_source
Example #19
0
def new_ls_from_line(line: Line) -> LineSequence:
    return LineSequence(line.line_number, (line.text, ))
 def as_line_sequence(self) -> LineSequence:
     return LineSequence(72, self.py_source().splitlines())
Example #21
0
def test_case_that_does_nothing() -> TestCase:
    return partial_test_case_with_instructions(act_phase_instructions=[
        act_phase_instruction_with_source(LineSequence(1, ('line', )))
    ], )
Example #22
0
 def _instruction_for(command: str) -> ContextManager[TestCaseSourceSetup]:
     yield TestCaseSourceSetup(act_phase_instructions=[
         SourceCodeInstruction(
             LineSequence(1, [shell_command_source_line_for(command)]))
     ])
def act_phase_instructions_that_does_nothing():
    return [act_phase_instruction_with_source(LineSequence(1, ('line', )))]
Example #24
0
        files_to_include = [pathlib.Path('file name')]
        actual_element = ParsedFileInclusionDirective(LINE_SEQUENCE, files_to_include)

        assertion = sut.matches_file_inclusion_directive(equals_line_sequence(LINE_SEQUENCE),
                                                         asrt.equals(files_to_include))
        # ACT & ASSERT #
        assertion.apply_without_message(self, actual_element)

    def test_not_matches(self):
        # ARRANGE #
        files_to_include = [pathlib.Path('file name')]
        actual_element = ParsedFileInclusionDirective(LINE_SEQUENCE, files_to_include)

        cases = [
            NameAndValue('mismatch on source',
                         sut.matches_file_inclusion_directive(asrt.not_(equals_line_sequence(LINE_SEQUENCE)),
                                                              asrt.equals(files_to_include))
                         ),
            NameAndValue('mismatch on files to include',
                         sut.matches_file_inclusion_directive(equals_line_sequence(LINE_SEQUENCE),
                                                              asrt.not_(asrt.equals(files_to_include)))
                         ),
        ]
        for nav in cases:
            with self.subTest(nav.name):
                # ACT & ASSERT #
                assert_that_assertion_fails(nav.value, actual_element)


LINE_SEQUENCE = LineSequence(1, ('first line',))