def is_section_contents(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder = asrt.MessageBuilder()): asrt.And([ asrt.IsInstance(doc.SectionContents), asrt.sub_component_sequence('initial_paragraphs', doc.SectionContents.initial_paragraphs.fget, is_paragraph_item) ]).apply(put, value, message_builder) assert isinstance(value, doc.SectionContents) sections_message = asrt.sub_component_builder('sections', message_builder) asrt.IsInstance(list).apply(put, value.sections, sections_message) for (idx, section) in enumerate(value.sections): self.is_section_item(put, section, asrt.sub_component_builder('[%d]' % idx, sections_message))
def is_section_item(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder = asrt.MessageBuilder()): asrt.IsInstance(doc.SectionItem).apply(put, value, message_builder) assert isinstance(value, doc.SectionItem) asrt.sub_component('header', doc.SectionItem.header.fget, is_text ).apply(put, value, message_builder) asrt.sub_component('tags', doc.SectionItem.tags.fget, asrt.is_instance_with(set, asrt.on_transformed(list, asrt.is_sequence_of(asrt.is_instance(str))) ) ).apply(put, value, message_builder) asrt.sub_component('target', doc.SectionItem.target.fget, asrt.is_instance(core.CrossReferenceTarget)), if isinstance(value, doc.Article): is_article_contents.apply(put, value.contents, message_builder.for_sub_component('article_contents')) elif isinstance(value, doc.Section): is_section_contents.apply(put, value.contents, message_builder.for_sub_component('section_contents')) else: asrt.fail('Not a {}: {}'.format(str(doc.SectionItem), str(value)))
def test_source(self): self._check( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, single_line_source(), Arrangement.phase_agnostic(), Expectation.phase_agnostic(source=asrt.IsInstance(ParseSource)), )
def test_fail_due_to_fail_of_side_effects_on_tcds(self): with self.assertRaises(utils.TestError): self._check( utils.ParserThatGives(SUCCESSFUL_INSTRUCTION), single_line_source(), sut.Arrangement(), sut.Expectation( main_side_effects_on_tcds=asrt.IsInstance(bool)))
class _IsSeeAlsoItemAssertion(AssertionBase): _class_assertion = asrt.IsInstance(struct.SeeAlsoItem) def _apply(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder): self._class_assertion.apply(put, value, message_builder) _IsSeeAlsoItem(put, message_builder).visit(value)
def test_tcds__dummy(self): self._check_source_and_exe_variants( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, Arrangement.phase_agnostic(), MultiSourceExpectation.phase_agnostic( side_effects_on_tcds=asrt.IsInstance(TestCaseDs) ), )
def test_fail_due_to_fail_of_side_effects_on_tcds(self): with self.assertRaises(utils.TestError): self._check( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, single_line_source(), sut.arrangement(), sut.Expectation( main_side_effects_on_tcds=asrt.IsInstance(bool)), )
def test_side_effects_on_files(self): self._check_source_and_exe_variants( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, Arrangement.phase_agnostic( tcds=TcdsArrangement(), ), MultiSourceExpectation.phase_agnostic( main_side_effects_on_sds=asrt.IsInstance(SandboxDs) ), )
def test_hds(self): self._check_source_and_exe_variants( PARSER_THAT_GIVES_SUCCESSFUL_INSTRUCTION, Arrangement.phase_agnostic( tcds=TcdsArrangement(), ), MultiSourceExpectation.phase_agnostic( side_effects_on_hds=asrt.IsInstance(pathlib.Path) ), )
def visit_table(self, table: Table): format_assertion = asrt.sub_component('format', Table.format.fget, asrt.IsInstance(TableFormat)) is_table_row = asrt.every_element('table row cells', is_table_cell) rows_assertion = asrt.sub_component_sequence('rows', Table.rows.fget, is_table_row, '/') assertion = asrt.And([format_assertion, rows_assertion]) assertion.apply(self.put, table, self.message_builder)
from exactly_lib.util.description import Description, DescriptionWithSubSections from exactly_lib_test.test_resources.value_assertions import value_assertion as asrt from exactly_lib_test.util.textformat.test_resources import structure as struct_check is_description = asrt.And([ asrt.IsInstance(Description), asrt.sub_component( 'single line description', Description.single_line_description.fget, struct_check.is_text), asrt.sub_component_sequence( 'rest paragraph-items', Description.rest.fget, struct_check.is_paragraph_item), ]) is_description_with_sub_sections = asrt.And([ asrt.IsInstance(DescriptionWithSubSections), asrt.sub_component( 'single line description', DescriptionWithSubSections.single_line_description.fget, struct_check.is_text), asrt.sub_component_sequence( 'rest sub-sections', DescriptionWithSubSections.rest.fget, struct_check.is_section_contents), ])
def test_false__with_message_builder(self): with self.assertRaises(TestException): sut.IsInstance(int).apply(self.put, 'not an int', sut.MessageBuilder('head'))
def _is_str(component_name: str, component_getter: types.FunctionType) -> Assertion: return asrt.sub_component(component_name, component_getter, asrt.IsInstance(str))
def is_svh_and( assertion: Assertion[svh.SuccessOrValidationErrorOrHardError] ) -> Assertion[svh.SuccessOrValidationErrorOrHardError]: return asrt.And( [asrt.IsInstance(svh.SuccessOrValidationErrorOrHardError), assertion])
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, AssertionBase def is_paragraph_item_list(name: str = '') -> Assertion: return asrt.every_element(name, is_paragraph_item) is_tags_set = asrt.is_instance_with(set, asrt.every_element('set of tags', asrt.is_instance(str))) is_string_text = asrt.is_instance_with(core.StringText, asrt.and_([ asrt.sub_component('value', core.StringText.value.fget, asrt.IsInstance(str)), asrt.sub_component('tags', core.StringText.tags.fget, is_tags_set), ])) is_cross_reference_text = asrt.is_instance_with(core.CrossReferenceText, asrt.And([ asrt.sub_component('target', core.CrossReferenceText.target.fget, asrt.IsInstance(core.CrossReferenceTarget)), asrt.sub_component('title', core.CrossReferenceText.title_text.fget, is_string_text),
def _apply(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder): asrt.IsInstance(pathlib.Path).apply(put, value, message_builder) self._check_path(put, value, message_builder)
import unittest from exactly_lib.util.textformat.section_target_hierarchy.targets import TargetInfo, TargetInfoNode from exactly_lib.util.textformat.structure import core from exactly_lib.util.textformat.structure.core import CrossReferenceTarget 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, AssertionBase from exactly_lib_test.util.textformat.test_resources.structure import is_string_text is_target_info = asrt.And([ asrt.IsInstance(TargetInfo), asrt.sub_component('presentation', TargetInfo.presentation_text.fget, is_string_text), asrt.sub_component('target', TargetInfo.target.fget, asrt.IsInstance(core.CrossReferenceTarget)), ]) def equals_target_info( expected: TargetInfo, mk_equals_cross_ref_id=lambda x: asrt.anything_goes() ) -> Assertion: return _IsTargetInfoAndEquals(expected, mk_equals_cross_ref_id) def equals_target_info_node( expected: TargetInfoNode, mk_equals_cross_ref_id=lambda x: asrt.anything_goes() ) -> Assertion: """ Traverses every node in the node tree, and checks that it is equal to the corresponding
def _check_result_of_main__sh(self, actual): validation = asrt.IsInstance( sh.SuccessOrHardError, 'The result from main must be an instance of ' + str(sh.SuccessOrHardError)) validation.apply(self.put, actual)
from exactly_lib.common.help.syntax_contents_structure import InvokationVariant, SyntaxElementDescription from exactly_lib_test.test_resources.value_assertions import value_assertion as asrt from exactly_lib_test.util.textformat.test_resources import structure as asrt_struct is_invokation_variant = asrt.is_instance_with( InvokationVariant, asrt.And([ asrt.sub_component('syntax', lambda iv: iv.syntax, asrt.IsInstance(str)), asrt.sub_component_sequence('description_rest', lambda iv: iv.description_rest, asrt_struct.is_paragraph_item) ])) is_syntax_element_description = asrt.is_instance_with( SyntaxElementDescription, asrt.And([ asrt.sub_component('name', lambda sed: sed.element_name, asrt.IsInstance(str)), asrt.sub_component_sequence('before_invokation_variants', lambda sed: sed.before_invokation_variants, asrt_struct.is_paragraph_item), asrt.sub_component_sequence('after_invokation_variants', lambda sed: sed.after_invokation_variants, asrt_struct.is_paragraph_item), asrt.sub_component_sequence('invokation_variants', lambda sed: sed.invokation_variants, is_invokation_variant) ]))
def test_true(self): sut.IsInstance(int).apply(self.put, 1) sut.IsInstance(int).apply(self.put, 1, sut.MessageBuilder('head'))
def _check_instruction(self, expected_class: type, instruction): validation = asrt.IsInstance( expected_class, 'The instruction must be an instance of ' + str(expected_class)) validation.apply(self.put, instruction)
def visit_literal_layout(self, literal_layout: LiteralLayout): assertion = asrt.sub_component('literal_layout/literal_text', LiteralLayout.literal_text.fget, asrt.IsInstance(str)) assertion.apply(self.put, literal_layout, self.message_builder)
def _check_instance_and(self, object_name: str, expected_class: type, additional: Assertion, actual): asrt.And([asrt.IsInstance(expected_class), additional]).apply(self.put, actual, asrt.MessageBuilder(object_name))
def test_false__sans_message_builder(self): with self.assertRaises(TestException): sut.IsInstance(int).apply(self.put, 'not an int')