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)
class ReferencedInclusionDirectiveFileInIncludedFileDoesNotExist(
        check_exception.Setup):
    file_1_invalid_inclusion_line = ' '.join(
        [INCLUDING_DIRECTIVE_INFO.singular_name, 'does-not-exist.txt'])

    file_1 = File('1.xly', lines_content([file_1_invalid_inclusion_line]))

    root_suite_inclusion_line = ' '.join(
        [INCLUDING_DIRECTIVE_INFO.singular_name, file_1.name])
    inclusion_line_in_file_1 = single_line_sequence(
        1, file_1_invalid_inclusion_line)

    root_suite_file = File(
        '0.suite',
        lines_content([
            phase_names.CLEANUP.syntax,
            root_suite_inclusion_line,
        ]))
    inclusion_line_in_root_file = single_line_sequence(
        2, root_suite_inclusion_line)

    expected_source_location_path = SourceLocationPath(
        SourceLocation(
            inclusion_line_in_file_1,
            file_1.name_as_path,
        ), [
            SourceLocation(
                inclusion_line_in_root_file,
                root_suite_file.name_as_path,
            )
        ])

    def root_suite_based_at(self, root_path: pathlib.Path) -> pathlib.Path:
        return self.root_suite_file.name_as_path

    def file_structure_to_read(self) -> DirContents:
        return DirContents([self.root_suite_file, self.file_1])

    def check_exception(self, root_path: pathlib.Path, actual: Exception,
                        put: unittest.TestCase):
        put.assertIsInstance(actual, SuiteParseError)

        expectation = matches_suite_parse_error(
            suite_file=asrt.equals(self.root_suite_based_at(root_path)),
            maybe_section_name=asrt.equals(phase_names.CLEANUP.plain),
            source=equals_line_sequence(self.inclusion_line_in_file_1),
            source_location=equals_source_location_path(
                self.expected_source_location_path),
            document_parser_exception=asrt.is_instance(
                sec_doc_exceptions.FileAccessError),
        )

        expectation.apply(put, 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 #4
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)
    def check_exception(self, root_path: pathlib.Path, actual: Exception,
                        put: unittest.TestCase):
        put.assertIsInstance(actual, SuiteParseError)

        expected_source = single_line_sequence(1, '[invalid-section]')

        expectation = matches_suite_parse_error(
            suite_file=asrt.equals(self.root_suite_based_at(root_path)),
            maybe_section_name=asrt.is_none,
            source=equals_line_sequence(expected_source),
            source_location=equals_source_location_path(
                SourceLocationPath(
                    SourceLocation(
                        expected_source,
                        Path('main.suite'),
                    ), [])),
            document_parser_exception=asrt.is_instance(
                sec_doc_exceptions.FileSourceError),
        )

        expectation.apply(put, actual)
    def check_exception(self, root_path: pathlib.Path, actual: Exception,
                        put: unittest.TestCase):
        put.assertIsInstance(actual, SuiteParseError)

        expected_source = single_line_sequence(2, self.file_inclusion_line)

        expectation = matches_suite_parse_error(
            suite_file=asrt.equals(self.root_suite_based_at(root_path)),
            maybe_section_name=asrt.equals(phase_names.SETUP.plain),
            source=equals_line_sequence(expected_source),
            source_location=equals_source_location_path(
                SourceLocationPath(
                    SourceLocation(
                        expected_source,
                        self.root_suite_file,
                    ), [])),
            document_parser_exception=asrt.is_instance(
                sec_doc_exceptions.FileAccessError),
        )

        expectation.apply(put, actual)
Example #7
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)
from exactly_lib_test.section_document.test_resources import source_location_assertions as sut
from exactly_lib_test.test_resources.test_of_test_resources_util import assert_that_assertion_fails
from exactly_lib_test.test_resources.test_utils import NEA
from exactly_lib_test.util.test_resources.line_source_assertions import ARBITRARY_LINE_SEQUENCE


def suite() -> unittest.TestSuite:
    return unittest.TestSuite([
        unittest.makeSuite(TestEqualsSourceLocation),
        unittest.makeSuite(TestEqualsSourceLocationPath),
    ])


ARBITRARY_SOURCE_LOCATION_INFO = sut.SourceLocationInfo(
    pathlib.Path('abs_path_of_dir_containing_first_file_path'),
    SourceLocationPath(SourceLocation(ARBITRARY_LINE_SEQUENCE, None), []))


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),
Example #9
0
    def test_with_file_inclusion_chain(self):
        # ARRANGE #

        final_loc_ls = single_line_sequence(0, 'line sequence 0')
        final_loc_dir = Path('final-loc-dir')
        final_loc_base_name = Path('final-loc-base-name')
        final_location = SourceLocation(final_loc_ls,
                                        final_loc_dir / final_loc_base_name)
        ls1 = single_line_sequence(1, 'line sequence 1')
        ls2 = single_line_sequence(2, 'line sequence 2')

        referrer_location_sub_dir = Path('referrer-location-sub-dir')
        link_sub_dir = Path('link-sub-dir')
        link_sub_dir2 = Path('link-sub-dir-2')
        base_name = Path('base-name')
        base_name2 = Path('base-name-2')

        cases = [
            NIE('single link. referrer location is HERE, file_path_rel_referrer/inclusion=None',
                expected_value=[
                    (
                            [sut.line_number(ls1.first_line_number)]
                            +
                            expected_source_line_lines(ls1.lines)
                            +
                            expected_path_lines(
                                final_loc_dir / final_loc_base_name,
                                final_loc_ls.first_line_number)
                    ),
                    expected_source_line_lines(final_loc_ls.lines)
                ],
                input_value=SourceLocationPathInput(
                    referrer_location=Path('.'),
                    source_location_path=SourceLocationPath(
                        final_location,
                        [
                            SourceLocation(
                                source=ls1,
                                file_path_rel_referrer=None
                            )
                        ])
                ),
                ),
            NIE('multiple links. referrer location is sub-dir, file_path_rel_referrer in sub dir',
                expected_value=[
                    (expected_path_lines(
                        referrer_location_sub_dir / link_sub_dir / base_name,
                        ls1.first_line_number)
                     +
                     expected_source_line_lines(ls1.lines)
                     +
                     expected_path_lines(
                         referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / base_name2,
                         ls2.first_line_number)
                     +
                     expected_source_line_lines(ls2.lines)
                     +
                     expected_path_lines(
                         referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / final_loc_dir / final_loc_base_name,
                         final_loc_ls.first_line_number)
                     ),
                    expected_source_line_lines(final_loc_ls.lines)
                ],
                input_value=SourceLocationPathInput(
                    referrer_location=referrer_location_sub_dir,
                    source_location_path=SourceLocationPath(
                        final_location,
                        [
                            SourceLocation(
                                source=ls1,
                                file_path_rel_referrer=link_sub_dir / base_name,
                            ),
                            SourceLocation(
                                source=ls2,
                                file_path_rel_referrer=link_sub_dir2 / base_name2,
                            ),
                        ])
                ),
                ),
        ]

        formatter = sut.default_formatter()

        for case in cases:
            with self.subTest(case.name):
                # ACT #
                blocks = formatter.source_location_path(case.input_value.referrer_location,
                                                        case.input_value.source_location_path)

                # ASSERT #

                self.assertEqual(case.expected_value, blocks)
Example #10
0
 def source_location(self) -> SourceLocationPath:
     return SourceLocationPath(SourceLocation(self.source, self.suite_file),
                               [])
Example #11
0
from pathlib import Path

from exactly_lib.section_document.source_location import SourceLocationPath, SourceLocation
from exactly_lib.util.line_source import single_line_sequence

SOURCE_LOCATION_PATH_WITH_INCLUSIONS_AND_FILE_NAMES = SourceLocationPath(
    SourceLocation(single_line_sequence(1, 'the line'), Path('src-file')), [
        SourceLocation(single_line_sequence(2, 'the other line'),
                       Path('other src-file')),
    ])

ARBITRARY_SOURCE_LOCATION_PATH = SourceLocationPath(
    SourceLocation(single_line_sequence(1, 'the line'), Path('src-file')), [])
Example #12
0
    def _check_failing_line(
            self, configuration: sut.Configuration, phases: Sequence[Phase],
            invalid_line_cases: Sequence[NameAndValue[SourceAndStatus]]):
        proc_cases = [
            NameAndValue(
                'not allowed to pollute current process',
                sut.new_processor_that_should_not_pollute_current_process(
                    configuration)),
            NameAndValue(
                'allowed to pollute current process',
                sut.new_processor_that_is_allowed_to_pollute_current_process(
                    configuration)),
        ]
        for phase in phases:
            for invalid_line_case in invalid_line_cases:
                source_and_status = invalid_line_case.value
                assert isinstance(source_and_status,
                                  SourceAndStatus)  # Type info for IDE

                file_with_error = fs.file_with_lines('file-with-error.src', [
                    source_and_status.failing_source_line,
                ])
                erroneous_line = single_line_sequence(
                    1, source_and_status.failing_source_line)

                test_case_file = fs.file_with_lines('test.case', [
                    section_header(phase.section_name),
                    directive_for_inclusion_of_file(file_with_error.name),
                ])
                line_that_includes_erroneous_file = single_line_sequence(
                    2, directive_for_inclusion_of_file(file_with_error.name))

                cwd_contents = fs.DirContents(
                    [test_case_file, file_with_error])

                expected_source_location_path = SourceLocationPath(
                    location=SourceLocation(erroneous_line,
                                            pathlib.Path(
                                                file_with_error.name)),
                    file_inclusion_chain=[
                        SourceLocation(line_that_includes_erroneous_file,
                                       pathlib.Path(test_case_file.name))
                    ])

                for proc_case in proc_cases:
                    with self.subTest(phase=phase.section_name,
                                      proc=proc_case.name,
                                      line=erroneous_line.first_line):
                        processor = proc_case.value
                        assert isinstance(processor, Processor)
                        with tmp_dir_as_cwd(cwd_contents):
                            test_case_reference = test_case_reference_of_source_file(
                                pathlib.Path(test_case_file.name))
                            # ACT #
                            result = processor.apply(test_case_reference)
                            # ASSERT #
                            assert isinstance(result,
                                              Result)  # Type info for IDE

                            source_and_status.expected_result_statuses.apply_with_message(
                                self, result, 'result statuses')

                            source_location_path_expectation = equals_source_location_path(
                                expected_source_location_path)
                            source_location_path_expectation.apply_with_message(
                                self, result.source_location_path,
                                'source location path')