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')
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_element_parser_SHOULD_be_able_to_report_syntax_error_by_returning_None(self): # ARRANGE # section_name = 'section-name' unknown_section_name = 'unknown-section-name' parser = new_parser_for( SectionsConfiguration([SectionConfiguration(section_name, SectionElementParserThatReturnsNone())], default_section_name=section_name)) unrecognized_line = 'unrecognized' cases = [ NEA('unrecognized source inside declared section', actual=[section_header(section_name), unrecognized_line, 'following line'], expected=matches_file_source_error( maybe_section_name=asrt.equals(section_name), location_path=[ SourceLocation(single_line_sequence(2, unrecognized_line), EXPECTED_SOURCE_FILE_PATH) ]) ), NEA('unrecognized source in default section', actual=[unrecognized_line, 'following line'], expected=matches_file_source_error( maybe_section_name=asrt.equals(section_name), location_path=[ SourceLocation(single_line_sequence(1, unrecognized_line), EXPECTED_SOURCE_FILE_PATH) ]) ), NEA('unrecognized section name', actual=[section_header(unknown_section_name), 'following line'], expected=matches_file_source_error( maybe_section_name=asrt.is_none, location_path=[ SourceLocation(single_line_sequence(1, section_header(unknown_section_name)), EXPECTED_SOURCE_FILE_PATH) ]) ), ] for nea in cases: with self.subTest(nea.name): with self.assertRaises(FileSourceError) as cm: # ACT & ASSERT # self._parse_lines(parser, nea.actual) nea.expected.apply_without_message(self, cm.exception)
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_all_element_types( self, builder: sut.SectionContentElementBuilder, assertion_on_file_path: Assertion[pathlib.Path], assertion_on_file_inclusion_chain: Assertion[ Sequence[SourceLocation]]): # ARRANGE # description = 'a description' line_sequence = single_line_sequence(10, 'the text') cases = [ NEA('empty', expected=matches_section_contents_element( ElementType.EMPTY, instruction_info=asrt.is_none, source_location_info=matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, )), actual=builder.new_empty(line_sequence)), NEA('comment', expected=matches_section_contents_element( ElementType.COMMENT, instruction_info=asrt.is_none, source_location_info=matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, )), actual=builder.new_comment(line_sequence)), NEA('instruction without description', expected=matches_section_contents_element( ElementType.INSTRUCTION, instruction_info=matches_instruction_info( asrt.is_none, equals_instruction_in_section(INSTRUCTION)), source_location_info=matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, )), actual=builder.new_instruction(line_sequence, INSTRUCTION, None)), NEA('instruction with description', expected=matches_section_contents_element( ElementType.INSTRUCTION, instruction_info=matches_instruction_info( asrt.equals(description), equals_instruction_in_section(INSTRUCTION)), source_location_info=matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, )), actual=builder.new_instruction(line_sequence, INSTRUCTION, description)), ] for nea in cases: with self.subTest(nea.name): # ASSERT # nea.expected.apply_without_message(self, nea.actual)
def test_all_fail_recognized_element_SHOULD_be_raised_WHEN_a_parser_raises_RE(self): # ARRANGE # returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'parsed by successful parser'), ElementType.EMPTY) source_text = 'first line' err_msg = 'error message in RE exception' cases = [ NameAndValue('single parser raises RE', [SectionElementParserThatRaisesRecognizedSectionElementSourceError(err_msg)] ), NameAndValue('1:st parser fails by returning None, 2nd=last parsers - last parser raises RE', [SectionElementParserThatReturnsNone(), SectionElementParserThatRaisesRecognizedSectionElementSourceError(err_msg)] ), NameAndValue('1:st parser fails by raising UnrecognizedError, 2nd=last parsers - last parser raises RE', [SectionElementParserThatRaisesUnrecognizedSectionElementSourceError('err msg of UE'), SectionElementParserThatRaisesRecognizedSectionElementSourceError(err_msg)] ), NameAndValue('first parser raises RE, following parser succeeds', [SectionElementParserThatRaisesRecognizedSectionElementSourceError(err_msg), SectionElementParserThatReturnsConstantAndConsumesCurrentLine(returned_element), ] ), ] for case in cases: with self.subTest(case.name): source = ParseSource(source_text) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT # with self.assertRaises(RecognizedSectionElementSourceError) as cm: parser.parse(ARBITRARY_FS_LOCATION_INFO, source) self.assertEqual(err_msg, cm.exception.message, 'error message in exception')
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 test_internal_error_in_act_execute(self): test_case = _single_successful_instruction_in_each_phase( single_line_sequence(72, 'ignored')) self._check( Arrangement( test_case, ActorThatRunsConstantActions( execute_action=execute_action_that_raises( test.ImplementationErrorTestException()))), Expectation( asrt_result.matches2( ExecutionFailureStatus.INTERNAL_ERROR, asrt_result.has_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForPhaseFailure.new_with_exception( phase_step.ACT__EXECUTE, test.ImplementationErrorTestException)), atc_stdout_output=asrt.equals(''), atc_stderr_output=asrt.equals(''), step_recordings=[phase_step.ACT__PARSE] + SYMBOL_VALIDATION_STEPS__ONCE + PRE_SDS_VALIDATION_STEPS__ONCE + [ phase_step.SETUP__MAIN, phase_step.SETUP__VALIDATE_POST_SETUP, phase_step.ACT__VALIDATE_POST_SETUP, phase_step.BEFORE_ASSERT__VALIDATE_POST_SETUP, phase_step.ASSERT__VALIDATE_POST_SETUP, phase_step.ACT__VALIDATE_EXE_INPUT, phase_step.ACT__PREPARE, phase_step.ACT__EXECUTE, (phase_step.CLEANUP__MAIN, PreviousPhase.ACT), ], ))
def test_implementation_error_in_act_execute(self): test_case = _single_successful_instruction_in_each_phase(single_line_sequence(72, 'ignored')) self._check( Arrangement(test_case, ActorThatRunsConstantActions( execute_action=execute_action_that_raises( test.ImplementationErrorTestException()))), Expectation( asrt_result.matches2(PartialExeResultStatus.IMPLEMENTATION_ERROR, asrt_result.has_sds(), asrt_result.has_no_action_to_check_outcome(), ExpectedFailureForPhaseFailure.new_with_exception( phase_step.ACT__EXECUTE, test.ImplementationErrorTestException) ), atc_stdout_output=asrt.equals(''), atc_stderr_output=asrt.equals(''), step_recordings= [phase_step.ACT__PARSE] + SYMBOL_VALIDATION_STEPS__ONCE + PRE_SDS_VALIDATION_STEPS__ONCE + [phase_step.SETUP__MAIN, phase_step.SETUP__VALIDATE_POST_SETUP, phase_step.ACT__VALIDATE_POST_SETUP, phase_step.BEFORE_ASSERT__VALIDATE_POST_SETUP, phase_step.ASSERT__VALIDATE_POST_SETUP, phase_step.ACT__PREPARE, phase_step.ACT__EXECUTE, (phase_step.CLEANUP__MAIN, PreviousPhase.ACT), ], ))
def test_exception_SHOULD_be_raised_WHEN_a_parser_to_try_raises_an_exception( self): # ARRANGE # source_lines = ['first line', 'second line'] returned_element = ParsedNonInstructionElement( single_line_sequence(1, 'expected'), ElementType.EMPTY) cases = [ NameAndValue('single failing parser', [ SectionElementParserThatRaisesRecognizedSectionElementSourceError( ), ]), NameAndValue('failing parser followed by successful parser', [ SectionElementParserThatReturnsNone(), SectionElementParserThatRaisesRecognizedSectionElementSourceError( ), SectionElementParserThatReturnsConstantAndConsumesCurrentLine( returned_element), ]), ] for case in cases: with self.subTest(case.name): source = source_of_lines(source_lines) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT & ASSERT # with self.assertRaises(SectionElementError): parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
def test_exception_SHOULD_be_raised_WHEN_a_parser_to_try_raises_an_exception(self): # ARRANGE # source_lines = ['first line', 'second line'] returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'expected'), ElementType.EMPTY) cases = [ NameAndValue('single failing parser', [ SectionElementParserThatRaisesRecognizedSectionElementSourceError(), ]) , NameAndValue('failing parser followed by successful parser', [ SectionElementParserThatReturnsNone(), SectionElementParserThatRaisesRecognizedSectionElementSourceError(), SectionElementParserThatReturnsConstantAndConsumesCurrentLine(returned_element), ] ), ] for case in cases: with self.subTest(case.name): source = source_of_lines(source_lines) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT & ASSERT # with self.assertRaises(SectionElementError): parser.parse(ARBITRARY_FS_LOCATION_INFO, source)
def test_not_matches(self): # ARRANGE # expected_line_num = 2 expected_line_text = 'line text' expected_section_name = 'section name' expected_file_path = pathlib.Path('a path') expected_file_inclusion_chain = [ SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path')) ] expected_abs_path_of_dir_containing_file = pathlib.Path( pathlib.Path.cwd().root) assertion = sut.equals_instruction_without_description( expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) cases = [ NameAndValue( 'unexpected line num', new_instruction(expected_line_num + 1, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain)), NameAndValue( 'unexpected line text', new_instruction(expected_line_num, expected_line_text + ' unexpected', expected_section_name, expected_file_path, expected_file_inclusion_chain)), NameAndValue( 'unexpected section name', new_instruction(expected_line_num, expected_line_text, expected_section_name + ' unexpected', expected_file_path, expected_file_inclusion_chain)), NameAndValue( 'unexpected file path', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path / 'unexpected', expected_file_inclusion_chain)), NameAndValue( 'unexpected abs path of dir containing file', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path / 'unexpected', expected_file_inclusion_chain)), NameAndValue( 'unexpected file inclusion chain', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path, [])), NameAndValue('unexpected element type - empty', new_empty(expected_line_num, expected_line_text)), NameAndValue('unexpected element type - comment', new_comment(expected_line_num, expected_line_text)), ] for nav in cases: with self.subTest(nav.name): # ACT & ASSERT # assert_that_assertion_fails(assertion, nav.value)
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteFileReferenceError) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence( put, single_line_sequence(2, 'does-not_exist.case'), actual.source)
def equals_comment_element( line_number: int, line_text: str) -> Assertion[model.SectionContentElement]: return matches_section_contents_element( ElementType.COMMENT, instruction_info=asrt.is_none, source_location_info=matches_source_location_info2( source=equals_line_sequence( single_line_sequence(line_number, line_text)), ))
def test_element_from_first_parser_that_returns_an_element_SHOULD_be_returned(self): # ARRANGE # source_lines = ['first line', 'second line'] expected_returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'expected'), ElementType.EMPTY) unexpected_returned_element = ParsedNonInstructionElement(single_line_sequence(1, 'unexpected'), ElementType.EMPTY) cases = [ NameAndValue('single successful parser', [ SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element), ]) , NameAndValue('more than one successful parser', [ SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element), SectionElementParserThatReturnsConstantAndConsumesCurrentLine(unexpected_returned_element), ] ), NameAndValue('first parser is unsuccessful - returns None', [ SectionElementParserThatReturnsNone(), SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element), ] ), NameAndValue('first parser is unsuccessful - throws unrecognized element', [ SectionElementParserThatRaisesUnrecognizedSectionElementSourceError(), SectionElementParserThatReturnsConstantAndConsumesCurrentLine(expected_returned_element), ] ), ] for case in cases: with self.subTest(case.name): source = source_of_lines(source_lines) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT # actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # self.assertIs(expected_returned_element, actual, 'return value from parser') expected_source = asrt_source.is_at_beginning_of_line(2) expected_source.apply_with_message(self, source, 'source')
def equals_comment_element(line_number: int, line_text: str) -> ValueAssertion[model.SectionContentElement]: return matches_section_contents_element(ElementType.COMMENT, instruction_info=asrt.is_none, source_location_info= matches_source_location_info2( source=equals_line_sequence( single_line_sequence(line_number, line_text)), ) )
def matches_single_line_source_location_info(line_number: int, line_text: str, file_path_rel_referrer: pathlib.Path, file_inclusion_chain: List[SourceLocation], ) -> ValueAssertion[SourceLocationInfo]: return matches_source_location_info2( source=equals_line_sequence(single_line_sequence(line_number, line_text)), file_path_rel_referrer=asrt.equals(file_path_rel_referrer), file_inclusion_chain=equals_file_inclusion_chain(file_inclusion_chain), )
def test_single_file_inclusion_chain_argument(self): # ARRANGE # file_inclusion_chain = [SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path'))] self._test_all_element_types( sut.SectionContentElementBuilder(FileLocationInfo(pathlib.Path.cwd(), file_inclusion_chain=file_inclusion_chain)), assertion_on_file_path=asrt.is_none, assertion_on_file_inclusion_chain=equals_file_inclusion_chain( file_inclusion_chain))
def test_element_from_first_parser_that_returns_an_element_SHOULD_be_returned( self): # ARRANGE # source_lines = ['first line', 'second line'] expected_returned_element = ParsedNonInstructionElement( single_line_sequence(1, 'expected'), ElementType.EMPTY) unexpected_returned_element = ParsedNonInstructionElement( single_line_sequence(1, 'unexpected'), ElementType.EMPTY) cases = [ NameAndValue('single successful parser', [ SectionElementParserThatReturnsConstantAndConsumesCurrentLine( expected_returned_element), ]), NameAndValue('more than one successful parser', [ SectionElementParserThatReturnsConstantAndConsumesCurrentLine( expected_returned_element), SectionElementParserThatReturnsConstantAndConsumesCurrentLine( unexpected_returned_element), ]), NameAndValue('first parser is unsuccessful - returns None', [ SectionElementParserThatReturnsNone(), SectionElementParserThatReturnsConstantAndConsumesCurrentLine( expected_returned_element), ]), NameAndValue( 'first parser is unsuccessful - throws unrecognized element', [ SectionElementParserThatRaisesUnrecognizedSectionElementSourceError( ), SectionElementParserThatReturnsConstantAndConsumesCurrentLine( expected_returned_element), ]), ] for case in cases: with self.subTest(case.name): source = source_of_lines(source_lines) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT # actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # self.assertIs(expected_returned_element, actual, 'return value from parser') expected_source = asrt_source.is_at_beginning_of_line(2) expected_source.apply_with_message(self, source, 'source')
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteParseError) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence( put, single_line_sequence(1, '"starting but no closing double quote'), actual.source)
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteParseError) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence(put, single_line_sequence(1, '"starting but no closing double quote'), actual.source)
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteFileReferenceError) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence(put, single_line_sequence(2, 'does-not_exist.suite'), actual.source)
def test_arbitrary_failure(self): # ARRANGE # actual_resolver = string_resolvers.str_constant('s') source_line = single_line_sequence(1, 'source line') actual_container = SymbolContainer(actual_resolver, su.source_info_for_line_sequence(source_line)) assertion_that_is_expected_to_fail = asrt.not_(equals_line_sequence(source_line)) assertion_to_check = sut.matches_container( assertion_on_resolver=asrt.anything_goes(), assertion_on_source=assertion_that_is_expected_to_fail) assert_that_assertion_fails(assertion_to_check, actual_container)
def test_source_line_SHOULD_be_given_as_argument_to_source_line_assertion(self): # ARRANGE # actual_resolver = string_resolvers.str_constant('s') source_line = single_line_sequence(1, 'source line') actual_container = SymbolContainer(actual_resolver, su.source_info_for_line_sequence(source_line)) assertion_that_is_expected_to_succeed = equals_line_sequence(source_line) assertion_to_check = sut.matches_container( assertion_on_resolver=asrt.anything_goes(), assertion_on_source=assertion_that_is_expected_to_succeed) # ACT & ASSERT # assertion_to_check.apply_without_message(self, actual_container)
def matches_single_line_source_location_info( line_number: int, line_text: str, file_path_rel_referrer: pathlib.Path, file_inclusion_chain: List[SourceLocation], ) -> Assertion[SourceLocationInfo]: return matches_source_location_info2( source=equals_line_sequence( single_line_sequence(line_number, line_text)), file_path_rel_referrer=asrt.equals(file_path_rel_referrer), file_inclusion_chain=equals_file_inclusion_chain(file_inclusion_chain), )
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteDoubleInclusion) put.assertEqual(str(Path('local-2.suite')), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence( put, single_line_sequence(3, 'subdir/in-subdir.suite'), actual.source) put.assertEqual( Path('subdir') / 'in-subdir.suite', actual.included_suite_file, 'File that is included twice') put.assertEqual(Path('local-1.suite'), actual.first_referenced_from, 'File that first included the suite file')
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteDoubleInclusion) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence(put, single_line_sequence(2, 'main.suite'), actual.source) put.assertEqual(self.root_suite_based_at(root_path), actual.included_suite_file, 'File that is included twice') put.assertEqual(None, actual.first_referenced_from, 'File that first included the suite file')
def test_single_file_inclusion_chain_argument(self): # ARRANGE # file_inclusion_chain = [ SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path')) ] self._test_all_element_types( sut.SectionContentElementBuilder( FileLocationInfo(pathlib.Path.cwd(), file_inclusion_chain=file_inclusion_chain)), assertion_on_file_path=asrt.is_none, assertion_on_file_inclusion_chain=equals_file_inclusion_chain( file_inclusion_chain))
def test_arbitrary_failure(self): # ARRANGE # actual_sdv = string_sdvs.str_constant('s') source_line = single_line_sequence(1, 'source line') actual_container = StringSymbolValueContext( actual_sdv, source_location.source_info_for_line_sequence( source_line)).container assertion_that_is_expected_to_fail = asrt.not_( equals_line_sequence(source_line)) assertion_to_check = sut.matches_container( value_type=asrt.anything_goes(), sdv=asrt.anything_goes(), definition_source=assertion_that_is_expected_to_fail) assert_that_assertion_fails(assertion_to_check, actual_container)
def test_instruction_in_default_section_SHOULD_not_be_allowed_when_there_is_no_default_section(self): # ARRANGE # parser = parser_for_sections(['section 1']) source_lines = ['instruction default', '[section 1]', 'instruction 1'] # ACT & ASSERT # with self.assertRaises(FileSourceError) as cm: self._parse_lines(parser, source_lines) # ASSERT # assert_equals_line_sequence(self, line_source.single_line_sequence(1, 'instruction default'), cm.exception.source) self.assertIsNone(cm.exception.maybe_section_name, 'Section name')
def test_source_line_SHOULD_be_given_as_argument_to_source_line_assertion( self): # ARRANGE # actual_sdv = string_sdvs.str_constant('s') source_line = single_line_sequence(1, 'source line') actual_container = StringSymbolValueContext( actual_sdv, source_location.source_info_for_line_sequence( source_line)).container assertion_that_is_expected_to_succeed = equals_line_sequence( source_line) assertion_to_check = sut.matches_container( value_type=asrt.anything_goes(), sdv=asrt.anything_goes(), definition_source=assertion_that_is_expected_to_succeed) # ACT & ASSERT # assertion_to_check.apply_without_message(self, actual_container)
def equals_instruction_without_description(line_number: int, line_text: str, section_name: str, file_path_rel_referrer: pathlib.Path, file_inclusion_chain: List[SourceLocation], ) -> ValueAssertion[model.SectionContentElement]: return matches_section_contents_element( ElementType.INSTRUCTION, instruction_info= matches_instruction_info_without_description(equals_instruction_in_section(InstructionInSection(section_name))), source_location_info= matches_source_location_info2( source=equals_line_sequence(single_line_sequence(line_number, line_text)), file_path_rel_referrer=asrt.equals(file_path_rel_referrer), file_inclusion_chain=equals_file_inclusion_chain(file_inclusion_chain), ) )
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteDoubleInclusion) put.assertEqual(str(self.root_suite_based_at(root_path)), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence(put, single_line_sequence(2, 'main.suite'), actual.source) put.assertEqual(self.root_suite_based_at(root_path), actual.included_suite_file, 'File that is included twice') put.assertEqual(None, actual.first_referenced_from, 'File that first included the suite file')
def check_exception(self, root_path: pathlib.Path, actual: Exception, put: unittest.TestCase): put.assertIsInstance(actual, SuiteDoubleInclusion) put.assertEqual(str(Path('local-2.suite')), str(actual.suite_file), 'Source file that contains the error') assert_equals_line_sequence(put, single_line_sequence(3, 'subdir/in-subdir.suite'), actual.source) put.assertEqual(Path('subdir') / 'in-subdir.suite', actual.included_suite_file, 'File that is included twice') put.assertEqual(Path('local-1.suite'), actual.first_referenced_from, 'File that first included the suite file')
def test_all_fail_recognized_element_SHOULD_be_raised_WHEN_a_parser_raises_RE( self): # ARRANGE # returned_element = ParsedNonInstructionElement( single_line_sequence(1, 'parsed by successful parser'), ElementType.EMPTY) source_text = 'first line' err_msg = 'error message in RE exception' cases = [ NameAndValue('single parser raises RE', [ SectionElementParserThatRaisesRecognizedSectionElementSourceError( err_msg) ]), NameAndValue( '1:st parser fails by returning None, 2nd=last parsers - last parser raises RE', [ SectionElementParserThatReturnsNone(), SectionElementParserThatRaisesRecognizedSectionElementSourceError( err_msg) ]), NameAndValue( '1:st parser fails by raising UnrecognizedError, 2nd=last parsers - last parser raises RE', [ SectionElementParserThatRaisesUnrecognizedSectionElementSourceError( 'err msg of UE'), SectionElementParserThatRaisesRecognizedSectionElementSourceError( err_msg) ]), NameAndValue('first parser raises RE, following parser succeeds', [ SectionElementParserThatRaisesRecognizedSectionElementSourceError( err_msg), SectionElementParserThatReturnsConstantAndConsumesCurrentLine( returned_element), ]), ] for case in cases: with self.subTest(case.name): source = ParseSource(source_text) parser = sut.ParserFromSequenceOfParsers(case.value) # ACT # with self.assertRaises( RecognizedSectionElementSourceError) as cm: parser.parse(ARBITRARY_FS_LOCATION_INFO, source) self.assertEqual(err_msg, cm.exception.message, 'error message in exception')
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 equals_instruction_without_description2( line_number: int, line_text: str, section_name: str, file_path_rel_referrer: pathlib.Path, file_inclusion_chain: List[SourceLocation], ) -> Assertion[model.SectionContentElement]: return matches_section_contents_element( ElementType.INSTRUCTION, instruction_info=matches_instruction_info_without_description( equals_instruction_in_section(InstructionInSection(section_name))), source_location_info=matches_source_location_info2( source=equals_line_sequence( single_line_sequence(line_number, line_text)), file_path_rel_referrer=asrt.equals(file_path_rel_referrer), file_inclusion_chain=equals_file_inclusion_chain( file_inclusion_chain), ))
def test_matches(self): # ARRANGE # expected_line_num = 1 expected_line_text = 'line text' expected_section_name = 'section name' expected_file_path = pathlib.Path('a path') expected_file_inclusion_chain = [ SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path')) ] assertion = sut.equals_instruction_without_description( expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) actual = new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) # ACT & ASSERT # assertion.apply_without_message(self, actual)
def test_matches(self): # ARRANGE # expected_line_num = 1 expected_line_text = 'line text' expected_section_name = 'section name' expected_file_path = pathlib.Path('a path') expected_file_inclusion_chain = [SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path'))] assertion = sut.equals_instruction_without_description(expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) actual = new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) # ACT & ASSERT # assertion.apply_without_message(self, 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)
from exactly_lib.symbol import symbol_syntax from exactly_lib.symbol.sdv_structure import SymbolDependentValue, SymbolReference, SymbolContainer, \ ReferenceRestrictions, SymbolDefinition, SymbolUsage from exactly_lib.symbol.value_type import ValueType from exactly_lib.util import line_source from exactly_lib.util.symbol_table import SymbolTable, Entry from exactly_lib_test.section_document.test_resources import source_location from exactly_lib_test.test_resources import argument_renderer as args from exactly_lib_test.test_resources.argument_renderer import ArgumentElementsRenderer 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 SDV_TYPE = TypeVar('SDV_TYPE', bound=SymbolDependentValue) ARBITRARY_LINE_SEQUENCE_FOR_DEFINITION = source_location.source_info_for_line_sequence( line_source.single_line_sequence(1, 'definition source')) class SymbolValueContext(Generic[SDV_TYPE], ABC): def __init__( self, sdv: SDV_TYPE, definition_source: Optional[SourceLocationInfo], ): self._sdv = sdv self._definition_source = definition_source @property @abstractmethod def value_type(self) -> ValueType: raise NotImplementedError('abstract method')
def single_line_sequence(line_number: int, line: str) -> SourceLocationInfo: return _FL.source_location_info_for(line_source.single_line_sequence(line_number, line))
from pathlib import Path from typing import Optional from exactly_lib.section_document import source_location as sut from exactly_lib.util.line_source import single_line_sequence from exactly_lib_test.test_resources.test_utils import NEA def suite() -> unittest.TestSuite: return unittest.TestSuite([ unittest.makeSuite(TestSourceLocationInfo), unittest.makeSuite(TestFileLocationInfo), ]) IRRELEVANT_SOURCE = single_line_sequence(1, 'irrelevant source line') class TestSourceLocationInfo(unittest.TestCase): def test_abs_path_of_dir_containing_file(self): # ARRANGE # abs_path_of_dir_containing_root_file = Path.cwd() rel_file_a = Path('rel-file-a') rel_file_b = Path('rel-file-b') rel_file_c = Path('rel-file-c') rel_file_d = Path('rel-file-d') cases = [ NEA('no inclusions',
def _source_location_info_of_current_line(self) -> SourceLocationInfo: source = line_source.single_line_sequence(self._document_source.current_line_number, self._document_source.current_line_text) return self._current_file_location.source_location_info_for(source)
def test_without_file_inclusion_chain(self): # ARRANGE # ls = single_line_sequence(1, 'the single line') a_dir = Path('a-dir') a_file = Path('a-file') cases = [ FilePathCase( referrer_location=Path('.'), file_path_rel_referrer=None, expected_file_path='' ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=None, expected_file_path='' ), FilePathCase( referrer_location=Path('.'), file_path_rel_referrer=a_file, expected_file_path=str(a_file), ), FilePathCase( referrer_location=Path('..'), file_path_rel_referrer=a_file, expected_file_path=str(Path('..') / a_file), ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=a_file, expected_file_path=str(a_dir / a_file), ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=Path('..') / a_file, expected_file_path=str(a_file), ), ] for case in cases: with self.subTest(case=case): slp = SourceLocationPath(SourceLocation(ls, case.file_path_rel_referrer), []) arrangement = Arrangement(case.referrer_location, slp) expected_location_line = ( sut.line_number(ls.first_line_number) if case.expected_file_path == '' else case.expected_file_path + ', ' + sut.line_number(ls.first_line_number) ) expected_blocks = [ [ expected_location_line, ], [ sut.SOURCE_LINE_INDENT + ls.first_line.text ], ] # ACT & ASSERT # self._check(arrangement, expected_blocks)
def test_non_empty_chain(self): # ARRANGE # 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=None', expected_value=FileInclusionChainOutput( block=[sut.line_number(ls1.first_line_number)] + expected_source_line_lines(ls1.lines), next_referrer_location=Path('.'), ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=None ) ] ), ), NIE('single link. referrer location is HERE, file_path_rel_referrer just base name', expected_value=FileInclusionChainOutput( block=expected_path_lines(base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=Path('.'), ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=base_name ) ] ), ), NIE('single link. referrer location is HERE, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block=expected_path_lines(link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=link_sub_dir, ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=link_sub_dir / base_name ) ] ), ), NIE('single link. referrer location is sub-dir, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block=expected_path_lines( referrer_location_sub_dir / link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=referrer_location_sub_dir / link_sub_dir, ), input_value=FileInclusionChainInput( referrer_location=referrer_location_sub_dir, chain=[ SourceLocation( source=ls1, file_path_rel_referrer=link_sub_dir / base_name ) ] ), ), NIE('multiple links. referrer location is sub-dir, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block= (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) ), next_referrer_location=referrer_location_sub_dir / link_sub_dir / link_sub_dir2, ), input_value=FileInclusionChainInput( referrer_location=referrer_location_sub_dir, chain=[ 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 # block, referrer_location = formatter.file_inclusion_chain(case.input_value.referrer_location, case.input_value.chain) # ASSERT # self.assertEqual(case.expected_value.block, block, 'block') self.assertEqual(case.expected_value.next_referrer_location, referrer_location, 'referrer location')
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)
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')), [] )
def test_not_matches(self): # ARRANGE # expected_line_num = 2 expected_line_text = 'line text' expected_section_name = 'section name' expected_file_path = pathlib.Path('a path') expected_file_inclusion_chain = [SourceLocation(single_line_sequence(2, 'inclusion line'), pathlib.Path('inclusion file path'))] expected_abs_path_of_dir_containing_file = pathlib.Path(pathlib.Path.cwd().root) assertion = sut.equals_instruction_without_description(expected_line_num, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) cases = [ NameAndValue('unexpected line num', new_instruction(expected_line_num + 1, expected_line_text, expected_section_name, expected_file_path, expected_file_inclusion_chain) ), NameAndValue('unexpected line text', new_instruction(expected_line_num, expected_line_text + ' unexpected', expected_section_name, expected_file_path, expected_file_inclusion_chain) ), NameAndValue('unexpected section name', new_instruction(expected_line_num, expected_line_text, expected_section_name + ' unexpected', expected_file_path, expected_file_inclusion_chain) ), NameAndValue('unexpected file path', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path / 'unexpected', expected_file_inclusion_chain) ), NameAndValue('unexpected abs path of dir containing file', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path / 'unexpected', expected_file_inclusion_chain) ), NameAndValue('unexpected file inclusion chain', new_instruction(expected_line_num, expected_line_text, expected_section_name, expected_file_path, []) ), NameAndValue('unexpected element type - empty', new_empty(expected_line_num, expected_line_text) ), NameAndValue('unexpected element type - comment', new_comment(expected_line_num, expected_line_text) ), ] for nav in cases: with self.subTest(nav.name): # ACT & ASSERT # assert_that_assertion_fails(assertion, nav.value)
from pathlib import Path from typing import Optional from exactly_lib.section_document import source_location as sut from exactly_lib.util.line_source import single_line_sequence from exactly_lib_test.test_resources.test_utils import NEA def suite() -> unittest.TestSuite: return unittest.TestSuite([ unittest.makeSuite(TestSourceLocationInfo), unittest.makeSuite(TestFileLocationInfo), ]) IRRELEVANT_SOURCE = single_line_sequence(1, 'irrelevant source line') class TestSourceLocationInfo(unittest.TestCase): def test_abs_path_of_dir_containing_file(self): # ARRANGE # abs_path_of_dir_containing_root_file = Path.cwd() rel_file_a = Path('rel-file-a') rel_file_b = Path('rel-file-b') rel_file_c = Path('rel-file-c') rel_file_d = Path('rel-file-d') cases = [ NEA('no inclusions',
def _test_all_element_types(self, builder: sut.SectionContentElementBuilder, assertion_on_file_path: ValueAssertion[pathlib.Path], assertion_on_file_inclusion_chain: ValueAssertion[Sequence[SourceLocation]] ): # ARRANGE # description = 'a description' line_sequence = single_line_sequence(10, 'the text') cases = [ NEA('empty', expected=matches_section_contents_element(ElementType.EMPTY, instruction_info=asrt.is_none, source_location_info= matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, )), actual=builder.new_empty(line_sequence)), NEA('comment', expected=matches_section_contents_element(ElementType.COMMENT, instruction_info=asrt.is_none, source_location_info= matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, ) ), actual=builder.new_comment(line_sequence)), NEA('instruction without description', expected=matches_section_contents_element(ElementType.INSTRUCTION, instruction_info=matches_instruction_info( asrt.is_none, equals_instruction_in_section(INSTRUCTION)), source_location_info= matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, ) ), actual=builder.new_instruction(line_sequence, INSTRUCTION, None)), NEA('instruction with description', expected=matches_section_contents_element(ElementType.INSTRUCTION, instruction_info=matches_instruction_info( asrt.equals(description), equals_instruction_in_section(INSTRUCTION)), source_location_info= matches_source_location_info2( source=equals_line_sequence(line_sequence), file_path_rel_referrer=assertion_on_file_path, file_inclusion_chain=assertion_on_file_inclusion_chain, ) ), actual=builder.new_instruction(line_sequence, INSTRUCTION, description)), ] for nea in cases: with self.subTest(nea.name): # ASSERT # nea.expected.apply_without_message(self, nea.actual)
def test_non_empty_chain(self): # ARRANGE # 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=None', expected_value=FileInclusionChainOutput( block=[sut.line_number(ls1.first_line_number)] + expected_source_line_lines(ls1.lines), next_referrer_location=Path('.'), ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=None ) ] ), ), NIE('single link. referrer location is HERE, file_path_rel_referrer just base name', expected_value=FileInclusionChainOutput( block=expected_file_reference_lines(base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=Path('.'), ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=base_name ) ] ), ), NIE('single link. referrer location is HERE, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block=expected_file_reference_lines(link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=link_sub_dir, ), input_value=FileInclusionChainInput( referrer_location=Path('.'), chain=[ SourceLocation( source=ls1, file_path_rel_referrer=link_sub_dir / base_name ) ] ), ), NIE('single link. referrer location is sub-dir, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block=expected_file_reference_lines( referrer_location_sub_dir / link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines), next_referrer_location=referrer_location_sub_dir / link_sub_dir, ), input_value=FileInclusionChainInput( referrer_location=referrer_location_sub_dir, chain=[ SourceLocation( source=ls1, file_path_rel_referrer=link_sub_dir / base_name ) ] ), ), NIE('multiple links. referrer location is sub-dir, file_path_rel_referrer in sub dir', expected_value=FileInclusionChainOutput( block= (expected_file_reference_lines( referrer_location_sub_dir / link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines) + expected_file_reference_lines( referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / base_name2, ls2.first_line_number) + expected_source_line_lines(ls2.lines) ), next_referrer_location=referrer_location_sub_dir / link_sub_dir / link_sub_dir2, ), input_value=FileInclusionChainInput( referrer_location=referrer_location_sub_dir, chain=[ 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 # block, referrer_location = formatter.file_inclusion_chain(case.input_value.referrer_location, case.input_value.chain) # ASSERT # self.assertEqual(case.expected_value.block, block, 'block') self.assertEqual(case.expected_value.next_referrer_location, referrer_location, 'referrer location')
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_file_reference_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_file_reference_lines( referrer_location_sub_dir / link_sub_dir / base_name, ls1.first_line_number) + expected_source_line_lines(ls1.lines) + expected_file_reference_lines( referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / base_name2, ls2.first_line_number) + expected_source_line_lines(ls2.lines) + expected_file_reference_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)
def test_without_file_inclusion_chain(self): # ARRANGE # ls = single_line_sequence(1, 'the single line') a_dir = Path('a-dir') a_file = Path('a-file') cases = [ FilePathCase( referrer_location=Path('.'), file_path_rel_referrer=None, expected_file_path='' ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=None, expected_file_path='' ), FilePathCase( referrer_location=Path('.'), file_path_rel_referrer=a_file, expected_file_path=str(a_file), ), FilePathCase( referrer_location=Path('..'), file_path_rel_referrer=a_file, expected_file_path=str(Path('..') / a_file), ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=a_file, expected_file_path=str(a_dir / a_file), ), FilePathCase( referrer_location=a_dir, file_path_rel_referrer=Path('..') / a_file, expected_file_path=str(a_file), ), ] for case in cases: with self.subTest(case=case): slp = SourceLocationPath(SourceLocation(ls, case.file_path_rel_referrer), []) arrangement = Arrangement(case.referrer_location, slp) expected_location_line = ( sut.line_number(ls.first_line_number) if case.expected_file_path == '' else case.expected_file_path + ', ' + sut.line_number(ls.first_line_number) ) expected_blocks = [ [ expected_location_line, ], [ sut.SOURCE_LINE_INDENT + ls.first_line.text ], ] # ACT & ASSERT # self._check(arrangement, expected_blocks)
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')), [])