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)
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)
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')
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_inclusion_of_non_exiting_file_SHOULD_cause_file_access_error( self): # ARRANGE # name_of_non_existing_file = 'non-existing.src' configuration = configuration_with_no_instructions_and_no_preprocessor( ) 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 phase_identifier.ALL_WITH_INSTRUCTIONS: for proc_case in proc_cases: with self.subTest(phase.section_name, proc=proc_case.name): test_case_file = fs.file_with_lines( 'test.case', [ section_header(phase.section_name), directive_for_inclusion_of_file( name_of_non_existing_file), ]) cwd_contents = fs.DirContents([test_case_file]) 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 self.assertEqual(Status.ACCESS_ERROR, result.status) self.assertEqual(AccessErrorType.FILE_ACCESS_ERROR, result.access_error_type) source_location_path_expectation = equals_source_location_path( source_location_path_of( pathlib.Path(test_case_file.name), Line( 2, directive_for_inclusion_of_file( name_of_non_existing_file)))) source_location_path_expectation.apply_with_message( self, result.error_info.source_location_path, 'source location path')
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.resolver_container.source_location)
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 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 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 test_inclusion_of_non_exiting_file_SHOULD_cause_file_access_error(self): # ARRANGE # name_of_non_existing_file = 'non-existing.src' configuration = configuration_with_no_instructions_and_no_preprocessor() 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 phase_identifier.ALL_WITH_INSTRUCTIONS: for proc_case in proc_cases: with self.subTest(phase.section_name, proc=proc_case.name): test_case_file = fs.file_with_lines('test.case', [ section_header(phase.section_name), directive_for_inclusion_of_file(name_of_non_existing_file), ]) cwd_contents = fs.DirContents([test_case_file]) 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 self.assertEqual(Status.ACCESS_ERROR, result.status) self.assertEqual(AccessErrorType.FILE_ACCESS_ERROR, result.access_error_type) source_location_path_expectation = equals_source_location_path( source_location_path_of(pathlib.Path(test_case_file.name), Line(2, directive_for_inclusion_of_file(name_of_non_existing_file)))) source_location_path_expectation.apply_with_message(self, result.error_info.source_location_path, 'source location path')
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')