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_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 equals_container( expected: SymbolContainer, ignore_source_line: bool = True) -> Assertion[SymbolContainer]: """ :param expected: Must contain a data type value """ component_assertions = [ asrt.sub_component('value_type', SymbolContainer.value_type.fget, asrt.is_(expected.value_type)), ] if not ignore_source_line: component_assertions.append( asrt.sub_component( 'definition_source', SymbolContainer.definition_source.fget, equals_line_sequence(expected.definition_source))) component_assertions.append( asrt.sub_component( 'source_location', SymbolContainer.source_location.fget, asrt.is_none_or_instance_with( SourceLocationInfo, asrt_src_loc.is_valid_source_location_info())), ) expected_sdv = expected.sdv assert isinstance(expected_sdv, DataTypeSdv), 'All actual values must be DataTypeSdv' component_assertions.append( asrt.sub_component('sdv', SymbolContainer.sdv.fget, equals_data_type_sdv(expected_sdv))) return asrt.is_instance_with(SymbolContainer, asrt.and_(component_assertions))
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 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')
def test_matches(self): # ARRANGE # element_type = ElementType.COMMENT actual_element = ParsedNonInstructionElement(LINE_SEQUENCE, element_type) assertion = sut.matches_non_instruction(equals_line_sequence(LINE_SEQUENCE), asrt.equals(element_type)) # ACT & ASSERT # assertion.apply_without_message(self, actual_element)
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_matches(self): # ARRANGE # 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)
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_not_matches(self): # ARRANGE # element_type = ElementType.COMMENT actual_element = ParsedNonInstructionElement(LINE_SEQUENCE, element_type) cases = [ NameAndValue('mismatch on source', sut.matches_non_instruction(asrt.not_(equals_line_sequence(LINE_SEQUENCE)), asrt.equals(element_type)) ), NameAndValue('mismatch on element type', sut.matches_non_instruction(equals_line_sequence(LINE_SEQUENCE), asrt.not_(asrt.equals(element_type))) ), ] for nav in cases: with self.subTest(nav.name): # ACT & ASSERT # assert_that_assertion_fails(nav.value, actual_element)
def file_source_error_equals_line( line: Line, maybe_section_name: Assertion[str] = asrt.anything_goes() ) -> Assertion[FileSourceError]: return asrt.and_([ asrt.sub_component('maybe_section_name', FileSourceError.maybe_section_name.fget, maybe_section_name), asrt.sub_component('source', FileSourceError.source.fget, equals_line_sequence( line_sequence_from_line(line))), ])
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 file_source_error_equals_line(line: Line, maybe_section_name: ValueAssertion[str] = asrt.anything_goes() ) -> ValueAssertion[FileSourceError]: return asrt.and_([ asrt.sub_component('maybe_section_name', FileSourceError.maybe_section_name.fget, maybe_section_name), asrt.sub_component('source', FileSourceError.source.fget, equals_line_sequence(line_sequence_from_line(line))), ])
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 test_matches(self): # ARRANGE # instruction = Instruction() description = 'the description' instruction_info = InstructionInfo(instruction, description) actual_element = ParsedInstruction(LINE_SEQUENCE, instruction_info) assertion = sut.matches_instruction(equals_line_sequence(LINE_SEQUENCE), matches_instruction_info(asrt.equals(description), asrt.is_(instruction))) # ACT & ASSERT # assertion.apply_without_message(self, actual_element)
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 equals_container(expected: rs.SymbolContainer, ignore_source_line: bool = True) -> ValueAssertion[rs.SymbolContainer]: component_assertions = [] if not ignore_source_line: component_assertions.append(asrt.sub_component('source', rs.SymbolContainer.definition_source.fget, equals_line_sequence(expected.definition_source))) expected_resolver = expected.resolver assert isinstance(expected_resolver, DataValueResolver), 'All actual values must be DataValueResolver' component_assertions.append(asrt.sub_component('resolver', rs.SymbolContainer.resolver.fget, equals_resolver(expected_resolver))) return asrt.is_instance_with(rs.SymbolContainer, asrt.and_(component_assertions))
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 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_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)
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 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_multi_line_instruction_without_description( line_number: int, lines: list, section_name: str, file_path: 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( line_source.LineSequence(line_number, tuple(lines))), file_path_rel_referrer=asrt.equals(file_path), file_inclusion_chain=equals_file_inclusion_chain( file_inclusion_chain), ))
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)
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)
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)
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_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')
def equals_non_instruction( expected: ParsedNonInstructionElement ) -> Assertion[ParsedSectionElement]: return matches_non_instruction(equals_line_sequence(expected.source), asrt.equals(expected.element_type))
def equals_comment_element( source: line_source.LineSequence) -> Assertion[ParsedSectionElement]: return matches_non_instruction(equals_line_sequence(source), asrt.equals(ElementType.COMMENT))
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 equals_source_location(expected: SourceLocation) -> ValueAssertion[SourceLocation]: return matches_source_location(source=equals_line_sequence(expected.source), file_path_rel_referrer=asrt.equals(expected.file_path_rel_referrer))
def equals_file_inclusion_directive( expected: ParsedFileInclusionDirective ) -> Assertion[ParsedSectionElement]: return matches_file_inclusion_directive( equals_line_sequence(expected.source), asrt.equals(expected.files_to_include))
def equals_source_location(expected: SourceLocation) -> Assertion[SourceLocation]: return matches_source_location(source=equals_line_sequence(expected.source), file_path_rel_referrer=asrt.equals(expected.file_path_rel_referrer))