def _apply(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder): put.assertIsInstance(value, tuple, 'Expects a tuple of StringFragments') asrt.matches_sequence(self._sequence_of_element_assertions).apply(put, value, message_builder)
def test_node_with_detail_and_child(self): # ARRANGE # for child_data in [False, True]: with self.subTest(data=child_data): child = Node('the child', child_data, (), ()) detail = StringDetail('the detail') root = Node('the root', False, [detail], [child]) # EXPECTATION # expectation = asrt_struct.matches_major_block__w_plain_properties( minor_blocks=asrt.matches_sequence([ asrt_struct.matches_minor_block( line_elements=asrt.matches_sequence([ matches_header_line_element(root), matches_string_detail_line_element(detail, depth=0), ]), properties=matches_node_properties(depth=0), ), asrt_struct.matches_minor_block( line_elements=asrt.matches_singleton_sequence( matches_header_line_element(child)), properties=matches_node_properties(depth=1), ), ])) # ACT & ASSERT # _check(self, root, expectation)
def test_path_argument_SHOULD_use_posix_syntax(self): # ARRANGE # inclusion_directive_name = 'incl-directive' parser = sut.FileInclusionDirectiveParser(inclusion_directive_name) path_arg = pathlib.PurePosixPath('first') / 'second' inclusion_directive_line = inclusion_directive_name + ' ' + str(path_arg) source = source_of_lines([ inclusion_directive_line, 'second line', ]) # ACT # actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # expected_directive = is_file_inclusion_directive( matches_file_inclusion_directive( files_to_include=asrt.matches_sequence([ asrt.equals(pathlib.Path(path_arg)), ]), source=matches_line_sequence( first_line_number=asrt.equals(1), lines=asrt.matches_sequence([ asrt.equals(inclusion_directive_line) ])) )) expected_directive.apply_with_message(self, actual, 'parsed directive') expected_source = is_at_beginning_of_line(2) expected_source.apply_with_message(self, source, 'source')
def test(self): # ARRANGE # const_pi = Paragraph([]) cases = [ NEA('empty sequence of paragraphs', expected=asrt.matches_sequence([]), actual=[] ), NEA('single paragraph', expected=asrt.matches_sequence([ asrt.is__any(const_pi) ]), actual=[const_pi] ), ] for case in cases: with self.subTest(case.name): # ACT # actual = sut.constant(case.actual).apply(CONSTRUCTION_ENVIRONMENT) # ASSERT # case.expected.apply_without_message(self, actual)
def test_path_argument_SHOULD_use_posix_syntax(self): # ARRANGE # inclusion_directive_name = 'incl-directive' parser = sut.FileInclusionDirectiveParser(inclusion_directive_name) path_arg = pathlib.PurePosixPath('first') / 'second' inclusion_directive_line = inclusion_directive_name + ' ' + str( path_arg) source = source_of_lines([ inclusion_directive_line, 'second line', ]) # ACT # actual = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) # ASSERT # expected_directive = is_file_inclusion_directive( matches_file_inclusion_directive( files_to_include=asrt.matches_sequence([ asrt.equals(pathlib.Path(path_arg)), ]), source=matches_line_sequence( first_line_number=asrt.equals(1), lines=asrt.matches_sequence( [asrt.equals(inclusion_directive_line)])))) expected_directive.apply_with_message(self, actual, 'parsed directive') expected_source = is_at_beginning_of_line(2) expected_source.apply_with_message(self, source, 'source')
def _apply(self, put: unittest.TestCase, value, message_builder: asrt.MessageBuilder): put.assertIsInstance(value, tuple, 'Expects a tuple of StringFragments') asrt.matches_sequence(self._sequence_of_element_assertions).apply(put, value, message_builder)
def test_hierarchy_with_sub_sections(self): # ARRANGE # target_factory = TargetInfoFactoryTestImpl(['target_component']) expected_section_contents_object1 = doc.empty_section_contents() expected_section_contents_object2 = docs.section_contents(docs.paras('testing testing')) expected_root_initial_para = docs.para('root initial paras') expected_root_initial_paras = [expected_root_initial_para] root_header = StringText('root header') sub_section_header_1 = 'sub1' sub_section_header_2 = 'sub2' sub_section_local_target_1 = 'sub-target1' sub_section_local_target_2 = 'sub-target2' object_to_test = sut.hierarchy( root_header, paragraphs.constant(expected_root_initial_paras), [sut.child(sub_section_local_target_1, sut.leaf(sub_section_header_1, section_contents(expected_section_contents_object1))), sut.child(sub_section_local_target_2, sut.leaf(sub_section_header_2, section_contents(expected_section_contents_object2))) ]) # EXPECTATION # expected_root_target_info = target_factory.root(root_header) sub1_target = target_factory.sub(sub_section_header_1, sub_section_local_target_1) sub2_target = target_factory.sub(sub_section_header_2, sub_section_local_target_2) target_info_node_assertion = equals_target_info_node( TargetInfoNode(expected_root_target_info, [ target_info_leaf(sub1_target), target_info_leaf(sub2_target), ]), mk_equals_cross_ref_id=equals_custom_cross_ref_test_impl) section_assertion2 = section_matches( target=equals_custom_cross_ref_test_impl(expected_root_target_info.target), header=asrt_para.equals_text(expected_root_target_info.presentation_text), contents=section_contents_matches( initial_paragraphs=asrt.matches_sequence([asrt.Is(expected_root_initial_para)]), sections=asrt.matches_sequence([ section_matches( target=equals_custom_cross_ref_test_impl(sub1_target.target), header=asrt_para.equals_text(sub1_target.presentation_text), contents=asrt.Is(expected_section_contents_object1)), section_matches( target=equals_custom_cross_ref_test_impl(sub2_target.target), header=asrt_para.equals_text(sub2_target.presentation_text), contents=asrt.Is(expected_section_contents_object2)), ]))) # ACT & ASSERT # self._act_and_assert(object_to_test, target_factory, target_info_node_assertion, section_assertion2)
def test_hierarchy_with_sub_sections(self): # ARRANGE # target_factory = TargetInfoFactoryTestImpl(['target_component']) expected_section_contents_object1 = doc.empty_section_contents() expected_section_contents_object2 = docs.section_contents(docs.paras('testing testing')) expected_root_initial_para = docs.para('root initial paras') expected_root_initial_paras = [expected_root_initial_para] root_header = StringText('root header') sub_section_header_1 = 'sub1' sub_section_header_2 = 'sub2' sub_section_local_target_1 = 'sub-target1' sub_section_local_target_2 = 'sub-target2' object_to_test = sut.hierarchy( root_header, paragraphs.constant(expected_root_initial_paras), [sut.child(sub_section_local_target_1, sut.leaf(sub_section_header_1, section_contents(expected_section_contents_object1))), sut.child(sub_section_local_target_2, sut.leaf(sub_section_header_2, section_contents(expected_section_contents_object2))) ]) # EXPECTATION # expected_root_target_info = target_factory.root(root_header) sub1_target = target_factory.sub(sub_section_header_1, sub_section_local_target_1) sub2_target = target_factory.sub(sub_section_header_2, sub_section_local_target_2) target_info_node_assertion = equals_target_info_node( TargetInfoNode(expected_root_target_info, [ target_info_leaf(sub1_target), target_info_leaf(sub2_target), ]), mk_equals_cross_ref_id=equals_custom_cross_ref_test_impl) section_assertion2 = section_matches( target=equals_custom_cross_ref_test_impl(expected_root_target_info.target), header=asrt_para.equals_text(expected_root_target_info.presentation_text), contents=section_contents_matches( initial_paragraphs=asrt.matches_sequence([asrt.Is(expected_root_initial_para)]), sections=asrt.matches_sequence([ section_matches( target=equals_custom_cross_ref_test_impl(sub1_target.target), header=asrt_para.equals_text(sub1_target.presentation_text), contents=asrt.Is(expected_section_contents_object1)), section_matches( target=equals_custom_cross_ref_test_impl(sub2_target.target), header=asrt_para.equals_text(sub2_target.presentation_text), contents=asrt.Is(expected_section_contents_object2)), ]))) # ACT & ASSERT # self._act_and_assert(object_to_test, target_factory, target_info_node_assertion, section_assertion2)
def test_successful_parse_of_regex(self): # ARRANGE # regex_str = 'the_regex' symbol = NameAndValue('the_symbol_name', LineMatcherConstant(False)) regex_matcher_syntax = argument_syntax.syntax_for_regex_matcher(regex_str) matcher_argument = argument_syntax.syntax_for_and([ symbol.name, regex_matcher_syntax, ]) defined_name = 'defined_name' source = remaining_source( src('{line_match_type} {defined_name} = {matcher_argument}', defined_name=defined_name, matcher_argument=matcher_argument), following_lines=['following line'], ) # EXPECTATION # the_and_matcher = LineMatcherAnd([ symbol.value, LineMatcherRegex(re.compile(regex_str)), ]) expected_container = matches_container( assertion_on_resolver= resolved_value_equals_line_matcher( the_and_matcher, references=asrt.matches_sequence([ is_line_matcher_reference_to(symbol.name), ]), symbols=SymbolTable({ symbol.name: container(LineMatcherConstantResolver(symbol.value)), }), ) ) expectation = Expectation( symbol_usages=asrt.matches_sequence([ asrt_sym_usage.matches_definition(asrt.equals(defined_name), expected_container) ]), symbols_after_main=assert_symbol_table_is_singleton( defined_name, expected_container, ) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def test_node_with_2_details_and_2_children(self): for root_data in [False, True]: for child_1_data in [False, True]: for child_2_data in [False, True]: with self.subTest(root_data=root_data, child_1_data=child_1_data, child_11_data=child_2_data): child_1 = Node('the child 1', child_1_data, (), ()) child_2 = Node('the child 2', child_2_data, (), ()) children = [child_1, child_2] detail_1 = StringDetail('the detail 1') detail_2 = StringDetail('the detail 2') details = [detail_1, detail_2] root = Node('the root', root_data, details, children) # EXPECTATION # expectation = asrt_struct.matches_major_block__w_plain_properties( minor_blocks=asrt.matches_sequence([ asrt_struct.matches_minor_block( line_elements=asrt.matches_sequence([ matches_header_line_element(root), matches_string_detail_line_element( detail_1, depth=0), matches_string_detail_line_element( detail_2, depth=0), ]), properties=matches_node_properties( depth=0), ), asrt_struct.matches_minor_block( line_elements=asrt. matches_singleton_sequence( matches_header_line_element(child_1)), properties=matches_node_properties( depth=1), ), asrt_struct.matches_minor_block( line_elements=asrt. matches_singleton_sequence( matches_header_line_element(child_2)), properties=matches_node_properties( depth=1), ), ])) # ACT & ASSERT # _check(self, root, expectation)
def equals_test_suite(expected: TestSuiteHierarchy) -> ValueAssertion[TestSuiteHierarchy]: return matches_test_suite( source_file=asrt.equals(expected.source_file), file_inclusions_leading_to_this_file=asrt.equals(expected.suite_file_inclusions_leading_to_this_file), test_cases=asrt.matches_sequence([ equals_test_case_reference(test_case) for test_case in expected.test_cases ]), sub_test_suites=asrt.matches_sequence([ equals_test_suite(sub_test_suite) for sub_test_suite in expected.sub_test_suites ]), test_case_handling_setup=asrt.anything_goes(), )
def _is_single_pre_formatted_text( text: Assertion[str]) -> Assertion[TextRenderer]: return asrt_renderer.is_renderer_of_major_blocks( asrt.matches_sequence([ asrt_struct.matches_major_block__w_plain_properties( asrt.matches_sequence([ asrt_struct.matches_minor_block__w_plain_properties( asrt.matches_sequence([ asrt_struct. matches_line_element__w_plain_properties( asrt_struct.is_pre_formatted_string( string=text)) ])) ])) ]))
def runTest(self): # ARRANGE # named_transformer = NameAndValue('the_transformer', StringTransformerConstant( contents_transformation.ToUppercaseStringTransformer())) actual_original_contents = lines_content(['only', 'lowercase', 'letters']) reg_ex_that_matches_uppercase_character = '[A-Z]' symbol_table_with_transformer = SymbolTable({ named_transformer.name: container(named_transformer.value) }) expected_symbol_reference_to_transformer = is_reference_to_string_transformer(named_transformer.name) expected_symbol_usages = asrt.matches_sequence([ expected_symbol_reference_to_transformer ]) self._check_variants_with_expectation_type( args_constructor_for( line_matcher=syntax_for_regex_matcher(reg_ex_that_matches_uppercase_character), transformer=named_transformer.name), expected_result_of_positive_test=PassOrFail.PASS, quantifier=Quantifier.EXISTS, actual_file_contents=actual_original_contents, symbols=symbol_table_with_transformer, expected_symbol_usages=expected_symbol_usages, )
def _matches_plain_minor_block_w_single_plain_line( line: str) -> Assertion[MinorBlock]: return asrt_struct.matches_minor_block__w_plain_properties( asrt.matches_sequence([ asrt_struct.matches_line_element__w_plain_properties( asrt_struct.is_string__not_line_ended(asrt.equals(line))), ]))
def test_assignment_of_symbols_and_constant_text_within_soft_quotes(self): # ARRANGE # referred_symbol1 = SymbolWithReferenceSyntax('referred_symbol_1') referred_symbol2 = SymbolWithReferenceSyntax('referred_symbol_2') assigned_symbol = StringSymbolContext.of_sdv( 'defined_symbol', string_sdv_from_fragments([ symbol(referred_symbol1.name), constant(' between '), symbol(referred_symbol2.name), ])) source = single_line_source('{string_type} {name} = {soft_quote}{sym_ref1} between {sym_ref2}{soft_quote}', soft_quote=SOFT_QUOTE_CHAR, name=assigned_symbol.name, sym_ref1=referred_symbol1, sym_ref2=referred_symbol2) expectation = Expectation.phase_agnostic( symbol_usages=asrt.matches_sequence([ assigned_symbol.assert_matches_definition_of_sdv, ]), symbols_after_main=assert_symbol_table_is_singleton( assigned_symbol.name, assigned_symbol.value.assert_matches_container_of_sdv, ) ) # ACT & ASSERT # INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
def test_assignment_of_single_symbol_reference(self): # ARRANGE # referred_symbol = SymbolWithReferenceSyntax('referred_symbol') expected_sdv = string_sdv_from_fragments([symbol(referred_symbol.name), constant('\n')]) assigned_symbol = StringSymbolContext.of_sdv( 'defined_symbol', expected_sdv, ) sb = SB.new_with(referred_symbol=referred_symbol, name_of_defined_symbol=assigned_symbol.name) source = sb.lines( here_doc_lines(first_line_start='{string_type} {name_of_defined_symbol} = ', marker='EOF', contents_lines=[str(referred_symbol)]) ) # EXPECTATION # expectation = Expectation.phase_agnostic( symbol_usages=asrt.matches_sequence([ assigned_symbol.assert_matches_definition_of_sdv, ]), symbols_after_main=assert_symbol_table_is_singleton( assigned_symbol.name, assigned_symbol.value.assert_matches_container_of_sdv, ) ) # ACT & ASSERT # INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
def runTest(self): # ARRANGE # symbol_1 = NameAndValue('symbol_1_name', _AddLineTransformer('added by 1st transformer')) symbol_2 = NameAndValue('symbol_2_name', _AddLineTransformer('added by 2nd transformer')) symbol_3 = NameAndValue('symbol_3_name', _AddLineTransformer('added by 3rd transformer')) cases = [ NameAndValue('2 transformers', [ symbol_1, symbol_2, ] ), NameAndValue('3 transformers', [ symbol_1, symbol_2, symbol_3, ] ), ] for case in cases: sequenced_transformer_symbols = case.value arguments = st_args.syntax_for_sequence_of_transformers([ symbol.name for symbol in sequenced_transformer_symbols ]) initial_line = 'initial line\n' initial_model_lines = [initial_line] expected_output_lines = [initial_line] + [ symbol.value.line_to_add for symbol in sequenced_transformer_symbols ] # ACT & ASSERT # with self.subTest(case.name): self._check_with_source_variants( Arguments(arguments), model_construction.of_lines(initial_model_lines), Arrangement( SymbolTable({ symbol.name: container(StringTransformerConstant(symbol.value)) for symbol in sequenced_transformer_symbols }) ), Expectation( main_result=asrt_model.model_as_list_matches(asrt.equals(expected_output_lines)), symbol_references=asrt.matches_sequence([ is_reference_to_string_transformer__ref(symbol.name) for symbol in sequenced_transformer_symbols ]), ) )
def test_assignment_of_symbols_and_constant_text_within_soft_quotes(self): # ARRANGE # referred_symbol1 = SymbolWithReferenceSyntax('referred_symbol_1') referred_symbol2 = SymbolWithReferenceSyntax('referred_symbol_2') name_of_defined_symbol = 'defined_symbol' source = single_line_source('{string_type} {name} = {soft_quote}{sym_ref1} between {sym_ref2}{soft_quote}', soft_quote=SOFT_QUOTE_CHAR, name=name_of_defined_symbol, sym_ref1=referred_symbol1, sym_ref2=referred_symbol2) expected_resolver = string_resolver_from_fragments([ symbol(referred_symbol1.name), constant(' between '), symbol(referred_symbol2.name), ]) container_of_expected_resolver = container(expected_resolver) expected_definition = SymbolDefinition(name_of_defined_symbol, container_of_expected_resolver) expectation = Expectation( symbol_usages=asrt.matches_sequence([ vs_asrt.equals_symbol(expected_definition, ignore_source_line=True), ]), symbols_after_main=assert_symbol_table_is_singleton( name_of_defined_symbol, equals_container(container_of_expected_resolver), ) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess): # ARRANGE # string_to_prepend = '.' initial_model_contents = '\n' model_after_2_transformations = ''.join([string_to_prepend, string_to_prepend, initial_model_contents]) initial_model = model_construction.model_of(initial_model_contents) equals_expected_matcher = NameAndValue('EQUALS_EXPECTED', StringMatcherResolverConstantTestImpl( EqualsMatcher(model_after_2_transformations) )) prepend_transformer_symbol = NameAndValue('PREPEND_TRANSFORMER', StringTransformerResolverConstantTestImpl( PrependStringToLinesTransformer(string_to_prepend)) ) prepend_trans_arg = str_trans_syntax.syntax_for_transformer_option(prepend_transformer_symbol.name) trans_and_eq_expected_matcher_source = remaining_source('{prepend_trans_arg} {equals_expected_matcher}'.format( prepend_trans_arg=prepend_trans_arg, equals_expected_matcher=equals_expected_matcher.name, )) # ACT & ASSERT # parser = sut.string_matcher_parser() prepend_and_equals_expected_matcher_resolver = parser.parse(trans_and_eq_expected_matcher_source) prepend_and_equals_expected_matcher = NameAndValue('PREPEND_AND_EQUALS_EXPECTED', prepend_and_equals_expected_matcher_resolver) symbols = symbol_table_from_name_and_resolvers([ equals_expected_matcher, prepend_transformer_symbol, prepend_and_equals_expected_matcher, ]) expected_symbol_references = asrt.matches_sequence([ is_reference_to_string_transformer(prepend_transformer_symbol.name), is_reference_to_string_matcher(prepend_and_equals_expected_matcher.name), ]) self._check_with_source_variants( self.configuration.arguments_for( args('{prepend_trans_arg} {maybe_not} {prepend_and_equals_expected_matcher}', prepend_trans_arg=prepend_trans_arg, maybe_not=maybe_not.nothing__if_positive__not_option__if_negative, prepend_and_equals_expected_matcher=prepend_and_equals_expected_matcher.name)), initial_model, self.configuration.arrangement_for_contents( symbols=symbols), Expectation( main_result=maybe_not.pass__if_positive__fail__if_negative, symbol_usages=expected_symbol_references), )
def test_string_token(self): # ARRANGE # plain_string = 'plain' symbol_name = 'symbol_name' cases = [ Case('plain string', plain_string, Expectation( elements=[list_resolvers.str_element(plain_string)], validators=asrt.is_empty_sequence, references=asrt.is_empty_sequence, )), Case('symbol reference', symbol_reference_syntax_for_name(symbol_name), Expectation( elements=[list_resolvers.symbol_element(symbol_reference(symbol_name))], validators=asrt.is_empty_sequence, references=asrt.matches_sequence([asrt_sym_ref.matches_reference_2( symbol_name, is_any_data_type_reference_restrictions()) ]), )), ] # ACT & ASSERT # _test_cases(self, cases)
def runTest(self): output_from_program = 'first second' transformer_of_program = transformers_setup.DELETE_INITIAL_WORD_TRANSFORMER transformer_of_instruction = transformers_setup.DUPLICATE_WORDS_TRANSFORMER program_that_outputs_something = pgm_args.program( pgm_args.interpret_py_source_line( self.configuration.py_source_for_print(output_from_program)), transformation=transformer_of_program.name ) result_when_positive = pfh_expectation_type_config(ExpectationType.POSITIVE) self._check_positive_and_negated( result_when_positive, program_that_outputs_something, matcher_arguments.equals_matcher(ab.quoted_string('second second')), Expectation( symbol_usages=asrt.matches_sequence([ is_reference_to_string_transformer(transformer_of_program.name), is_reference_to_string_transformer(transformer_of_instruction.name), ]) ), arrangement=ArrangementPostAct( symbols=transformers_setup.SYMBOL_TABLE), transformation=transformer_of_instruction.name )
def runTest(self): # ARRANGE # parser = self.configuration.new_parser() lines_transformer_name = 'the_transformer' common_arguments = arguments_building.CommonArgumentsConstructor(lines_transformer_name) expected_symbol_reference_to_transformer = is_reference_to_string_transformer(lines_transformer_name) expected_symbol_usages = asrt.matches_sequence([ expected_symbol_reference_to_transformer ]) for expectation_type in ExpectationType: for quantifier in Quantifier: with self.subTest(expectation_type=expectation_type, quantifier=quantifier.name): arguments_for_implicit_file = arguments_building.ImplicitActualFileArgumentsConstructor( common_arguments, arguments_building.LineMatchesAssertionArgumentsConstructor(quantifier, syntax_for_regex_matcher( 'regex')) ).apply(expectation_type) source = self.configuration.arguments_for(arguments_for_implicit_file).as_remaining_source instruction = parser.parse(ARBITRARY_FS_LOCATION_INFO, source) assert isinstance(instruction, AssertPhaseInstruction) # Sanity check expected_symbol_usages.apply_without_message(self, instruction.symbol_usages())
def test_assignment_of_empty_list(self): symbol_name = 'the_symbol_name' sb = SB.new_with(symbol_name=symbol_name) cases = [ SourceCase('No following lines', source=sb.single_line('{list_type} {symbol_name} = '), source_assertion=asrt_source.source_is_at_end ), SourceCase('Following lines', source=sb.lines(['{list_type} {symbol_name} = ', 'following line'], ), source_assertion=asrt_source.is_at_beginning_of_line(2) ), ] expected_resolver = lrs.empty() expected_resolver_container = container(expected_resolver) for case in cases: with self.subTest(case.name): expectation = Expectation( symbol_usages=asrt.matches_sequence([ vs_asrt.equals_symbol(SymbolDefinition(symbol_name, expected_resolver_container), ignore_source_line=True) ]), symbols_after_main=assert_symbol_table_is_singleton( symbol_name, equals_container(expected_resolver_container), ), source=case.source_assertion ) self._check(case.source, ArrangementWithSds(), expectation)
def test_assignment_of_list_with_symbol_references(self): symbol_name = 'the_symbol_name' referred_symbol = SymbolWithReferenceSyntax('referred_symbol') source = remaining_source(src( '{list_type} {symbol_name} = {symbol_reference} ', symbol_name=symbol_name, symbol_reference=referred_symbol, ), ['following line'], ) expected_symbol_reference = references.reference_to_any_data_type_value(referred_symbol.name) expected_resolver = lrs.from_elements([lrs.symbol_element(expected_symbol_reference)]) expected_resolver_container = container(expected_resolver) expectation = Expectation( symbol_usages=asrt.matches_sequence([ vs_asrt.equals_symbol(SymbolDefinition(symbol_name, expected_resolver_container), ignore_source_line=True) ]), symbols_after_main=assert_symbol_table_is_singleton( symbol_name, equals_container(expected_resolver_container), ), source=asrt_source.is_at_beginning_of_line(2), ) self._check(source, ArrangementWithSds(), expectation)
def runTest(self): actual_line_num = 3 int_string_symbol = NameAndValue( 'int_string_symbol_name', string_resolvers.str_constant(str(actual_line_num)) ) arguments = arg.LineNum(int_condition__expr(comparators.EQ, symbol_reference_syntax_for_name(int_string_symbol.name))) model_that_matches = (actual_line_num, 'the line') integration_check.check( self, source= remaining_source(str(arguments)), model=model_that_matches, arrangement= integration_check.Arrangement( symbols=symbol_utils.symbol_table_from_name_and_resolvers([int_string_symbol]) ), expectation= integration_check.Expectation( symbol_references=asrt.matches_sequence([ is_reference_to_symbol_in_expression(int_string_symbol.name) ]), ) )
def test_references(self): string_constant_1 = 'string constant 1' reference_1 = TypeWithStrRenderingSymbolReference( 'symbol_1_name', is_any_type_w_str_rendering()) reference_2 = TypeWithStrRenderingSymbolReference( 'symbol_2_name', OrReferenceRestrictions([])) cases = [ ( 'no fragments', sut.StringSdv(()), asrt.is_empty_sequence, ), ( 'single string constant fragment', sut.StringSdv((impl.ConstantStringFragmentSdv(' value'), )), asrt.is_empty_sequence, ), ( 'multiple fragments of different types', sut.StringSdv(( impl.SymbolStringFragmentSdv(reference_1.reference), impl.ConstantStringFragmentSdv(string_constant_1), impl.SymbolStringFragmentSdv(reference_2.reference), )), asrt.matches_sequence([ reference_1.reference_assertion, reference_2.reference_assertion, ]), ), ] for test_name, string_sdv, expected_references_assertion in cases: with self.subTest(test_name=test_name): actual = string_sdv.references expected_references_assertion.apply_without_message( self, actual)
def equals_list_resolver_element(expected: list_resolver.Element, symbols: SymbolTable = None) -> ValueAssertion: if symbols is None: symbols = symbol_table_with_values_matching_references(list(expected.references)) expected_resolved_value_list = expected.resolve(symbols) assertion_on_resolved_value = asrt.matches_sequence( [equals_string_value(sv) for sv in expected_resolved_value_list]) component_assertions = [ asrt.sub_component('references', lambda x: list(x.references), equals_symbol_references(list(expected.references))), asrt.sub_component('resolved value', lambda x: x.resolve(symbols), assertion_on_resolved_value), ] symbol_reference_assertion = asrt.is_none if expected.symbol_reference_if_is_symbol_reference is not None: symbol_reference_assertion = equals_symbol_reference(expected.symbol_reference_if_is_symbol_reference) symbol_reference_component_assertion = asrt.sub_component('symbol_reference_if_is_symbol_reference', lambda x: x.symbol_reference_if_is_symbol_reference, symbol_reference_assertion) component_assertions.append(symbol_reference_component_assertion) return asrt.is_instance_with( list_resolver.Element, asrt.and_(component_assertions))
def test_references(self): reference_1 = TypeWithStrRenderingSymbolReference('symbol_1_name', is_any_type_w_str_rendering()) reference_2 = TypeWithStrRenderingSymbolReference('symbol_2_name', OrReferenceRestrictions([])) cases = [ ( 'no elements', sut.ListSdv([]), asrt.is_empty_sequence, ), ( 'single string constant element', sut.ListSdv([list_sdvs.str_element('string value')]), asrt.is_empty_sequence, ), ( 'multiple elements with multiple references', sut.ListSdv([ list_sdvs.symbol_element(reference_1.reference), list_sdvs.str_element('constant value'), list_sdvs.symbol_element(reference_2.reference), ]), asrt.matches_sequence([ reference_1.reference_assertion, reference_2.reference_assertion, ]), ), ] for test_name, list_sdv, expected_references_assertion in cases: with self.subTest(test_name=test_name): actual = list_sdv.references expected_references_assertion.apply_without_message(self, actual)
def test_successful_parse_WHEN_rhs_is_empty_THEN_result_SHOULD_be_identity_transformer(self): defined_name = 'defined_name' # ARRANGE # source = single_line_source( src('{lines_trans_type} {defined_name} = ', defined_name=defined_name), ) # EXPECTATION # expected_container = matches_container( resolved_value_equals_string_transformer(IdentityStringTransformer()) ) expectation = Expectation( symbol_usages=asrt.matches_sequence([ asrt_sym_usage.matches_definition(asrt.equals(defined_name), expected_container) ]), symbols_after_main=assert_symbol_table_is_singleton( defined_name, expected_container, ) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def test_assignment_of_single_constant_line(self): value_str = 'value' symbol_name = 'name1' sb = SB.new_with(value_str=value_str, symbol_name=symbol_name) source = sb.lines( here_doc_lines(first_line_start='{string_type} {symbol_name} = ', marker='EOF', contents_lines=['{value_str}']) + ['following line'] ) # EXPECTATION # expected_container = string_constant_container(value_str + '\n') expected_definition = SymbolDefinition(symbol_name, expected_container) expectation = Expectation( symbol_usages=asrt.matches_sequence([ vs_asrt.equals_symbol(expected_definition, ignore_source_line=True) ]), symbols_after_main=assert_symbol_table_is_singleton( symbol_name, equals_container(expected_container), ), source=asrt_source.is_at_beginning_of_line(4) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def runTest(self): # ARRANGE # named_transformer = NameAndValue('the_transformer', StringTransformerConstant( DeleteEverythingStringTransformer())) original_file_contents = 'some\ntext' symbols = SymbolTable({ named_transformer.name: container(named_transformer.value), SYMBOL_FOR_EMPTINESS_MATCHER.name: container(SYMBOL_FOR_EMPTINESS_MATCHER.value), }) expected_symbol_references = asrt.matches_sequence([ is_reference_to_string_transformer(named_transformer.name), is_reference_to_string_matcher(SYMBOL_FOR_EMPTINESS_MATCHER.name), ]) self._check_with_source_variants( self.configuration.arguments_for( args('{transform_option} {the_transformer} {maybe_not} {symbol_reference}', the_transformer=named_transformer.name, maybe_not=self.maybe_not.nothing__if_positive__not_option__if_negative, symbol_reference=SYMBOL_FOR_EMPTINESS_MATCHER.name)), self.configuration.arrangement_for_contents( original_file_contents, post_sds_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY, symbols=symbols), Expectation( main_result=self.maybe_not.pass__if_positive__fail__if_negative, symbol_usages=expected_symbol_references), )
def test_validation_should_fail_pre_sds_when_regex_is_invalid(self): for regex_case in failing_regex_validation_cases(): arguments = self.conf.arguments(NameRegexVariant.of(regex_case.regex_string)) for expectation_type in ExpectationType: with self.subTest(expectation_type=expectation_type, validation_case=regex_case.case_name): integration_check.CHECKER__PARSE_SIMPLE.check__w_source_variants( self, arguments= arguments.as_arguments, input_= ARBITRARY_MODEL, arrangement= arrangement_w_tcds( symbols=SymbolContext.symbol_table_of_contexts(regex_case.symbols) ), expectation= Expectation( ParseExpectation( symbol_references=asrt.matches_sequence(regex_case.reference_assertions), ), ExecutionExpectation( validation=regex_case.expectation ), ) )
def runTest(self): actual_contents = lines_content(['1', '2', '3', '4']) symbol_value = '3' constant_value = '1' operand_symbol = NameAndValue('operand_symbol', string_resolvers.str_constant(symbol_value)) expression_that_evaluates_to_actual_number_of_lines = '{sym_ref}+{const}'.format( sym_ref=symbol_reference_syntax_for_name(operand_symbol.name), const=constant_value, ) symbol_table_with_operand_symbol = SymbolTable({ operand_symbol.name: container(operand_symbol.value) }) expected_symbol_usages = asrt.matches_sequence([ is_string_made_up_of_just_strings_reference_to(operand_symbol.name) ]) self._check_variants_with_expectation_type( InstructionArgumentsVariantConstructor( operator=comparators.GTE.name, operand=expression_that_evaluates_to_actual_number_of_lines), expected_result_of_positive_test=PassOrFail.PASS, actual_file_contents=actual_contents, symbols=symbol_table_with_operand_symbol, expected_symbol_usages=expected_symbol_usages, )
def runTest(self): # ARRANGE # name_of_referenced_selector = 'SELECTOR' name_of_referenced_files_matcher = 'FILES_MATCHER' expected_symbol_usages = asrt.matches_sequence([ is_file_matcher_reference_to__ref(name_of_referenced_selector), is_reference_to_files_matcher__ref(name_of_referenced_files_matcher), ]) arguments = fsm_args.argument_constructor_for_symbol_reference( files_matcher_symbol_name=name_of_referenced_files_matcher, named_matcher=name_of_referenced_selector ).apply(expectation_type_config__non_is_success(ExpectationType.POSITIVE)) source = remaining_source(arguments) # ACT # resolver = sut.files_matcher_parser().parse(source) # ASSERT # expected_symbol_usages.apply_without_message(self, resolver.references)
def test_multiple_prune_SHOULD_be_prune_dirs_matching_any_matcher(self): # ARRANGE # helper = IntegrationCheckHelper() # ACT & ASSERT # _check_multi( self, helper, arguments=fsm_args.Prune( fm_args.SymbolReference(NAME_STARTS_WITH__P1.name), fsm_args.Prune( fm_args.SymbolReference(NAME_STARTS_WITH__P2.name), helper.files_matcher_sym_ref_arg(), ), ), symbols_common_to_all_cases=[ NAME_STARTS_WITH__P1, NAME_STARTS_WITH__P2, ], symbol_references=asrt.matches_sequence([ NAME_STARTS_WITH__P1.reference_assertion, NAME_STARTS_WITH__P2.reference_assertion, helper.symbol_reference_assertion, ]), execution_cases=MULTIPLE_PRUNE_CASES, )
def test_assignment_of_single_constant_line(self): value_str = 'value' symbol_name = 'name1' sb = SB.new_with(value_str=value_str, symbol_name=symbol_name) source = sb.lines( here_doc_lines(first_line_start='{string_type} {symbol_name} = ', marker='EOF', contents_lines=['{value_str}']) + ['following line'] ) # EXPECTATION # expected_symbol = StringConstantSymbolContext(symbol_name, value_str + '\n') expectation = Expectation.phase_agnostic( symbol_usages=asrt.matches_sequence([ expected_symbol.assert_matches_definition_of_sdv ]), symbols_after_main=assert_symbol_table_is_singleton( symbol_name, expected_symbol.value.assert_matches_container_of_sdv, ), source=asrt_source.is_at_beginning_of_line(4) ) # ACT & ASSERT # INSTRUCTION_CHECKER.check(self, source, Arrangement.phase_agnostic(), expectation)
def runTest(self): # ARRANGE # helper = IntegrationCheckHelper() arguments = fsm_args.Prune( fm_args.SymbolReference(NAME_STARTS_WITH__P1.name), helper.files_matcher_sym_ref_arg(), ).as_arguments # ACT & ASSERT # for depth, nie in COMBINATION_OF_PRUNE_AND_DEPTH_LIMITATIONS: with self.subTest(data=nie.name, depth=depth): integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, arguments=arguments, input_=helper.model_constructor_for_checked_dir__recursive( min_depth=depth, max_depth=depth), arrangement=helper.arrangement_for_contents_of_model( checked_dir_contents=nie.actual, files_matcher_symbol_value=model_checker.matcher( self, helper.dir_arg.path_sdv, nie.expected, ), additional_symbols=[NAME_STARTS_WITH__P1], ), expectation=Expectation( ParseExpectation( symbol_references=asrt.matches_sequence([ NAME_STARTS_WITH__P1.reference_assertion, helper.symbol_reference_assertion, ]), )), )
def test_successful_parse_of_arbitrary_matcher(self): defined_name = 'defined_name' # ARRANGE # source = single_line_source( src('{string_matcher_type} {defined_name} = {matcher_argument}', defined_name=defined_name, matcher_argument=arg_syntax.arbitrary_single_line_value_that_must_not_be_quoted()), ) # EXPECTATION # expected_container = matches_container( matches_string_matcher_resolver() ) expectation = Expectation( symbol_usages=asrt.matches_sequence([ asrt_sym_usage.matches_definition(asrt.equals(defined_name), expected_container) ]), symbols_after_main=assert_symbol_table_is_singleton( defined_name, expected_container, ) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def runTest(self): # ARRANGE # helper = IntegrationCheckHelper() actual_contents = [ Dir('P1-matches', [ File.empty('file-in-pruned-dir'), ]) ] arguments = conjunction([ Parentheses( fsm_args.Prune( fm_args.SymbolReference(NAME_STARTS_WITH__P1.name), fsm_args.NumFiles(int_condition(comparators.EQ, 1)))), fsm_args.NumFiles(int_condition(comparators.EQ, 2)), ]) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check__w_source_variants( self, arguments=arguments.as_arguments, input_=helper.model_constructor_for_checked_dir__recursive(), arrangement=Arrangement( symbols=NAME_STARTS_WITH__P1.symbol_table, tcds=helper.dir_arg.tcds_arrangement_dir_with_contents( actual_contents)), expectation=Expectation( ParseExpectation(symbol_references=asrt.matches_sequence([ NAME_STARTS_WITH__P1.reference_assertion, ])), EXECUTION_IS_PASS, ))
def _check_( self, instruction_source: ParseSource, etc: ExpectationTypeConfigForPfh, main_result_for_positive_expectation: PassOrFail, rel_opt_config: RelativityOptionConfiguration, contents_of_relativity_option_root: DirContents = empty_dir_contents(), test_case_name: str = '', following_symbols_setup: SymbolsArrEx = SymbolsArrEx.empty()): with self.put.subTest(case_name=test_case_name, expectation_type=etc.expectation_type.name, arguments=instruction_source.source_string): instruction_check.check( self.put, self.parser, instruction_source, ArrangementPostAct( pre_contents_population_action= MAKE_CWD_OUTSIDE_OF_EVERY_REL_OPT_DIR, tcds_contents=rel_opt_config. populator_for_relativity_option_root( contents_of_relativity_option_root), symbols=_symbol_table_of(rel_opt_config.symbols, following_symbols_setup), ), Expectation( main_result=etc.main_result( main_result_for_positive_expectation), symbol_usages=asrt.matches_sequence( rel_opt_config.symbols.usage_expectation_assertions() + following_symbols_setup.expected_usages_list), ))
def check_rel_opt_variants_with_same_result_for_every_expectation_type( self, make_instruction_arguments: InstructionArgumentsVariantConstructor, main_result: ValueAssertion, contents_of_relativity_option_root: DirContents = empty_dir_contents(), following_symbols_setup: SymbolsArrAndExpectSetup = SymbolsArrAndExpectSetup.empty()): for rel_opt_config in self.accepted_rel_opt_configurations: for expectation_type_of_test_case in ExpectationType: etc = pfh_expectation_type_config(expectation_type_of_test_case) instruction_arguments = make_instruction_arguments.apply(etc, rel_opt_config) instruction_source = remaining_source(instruction_arguments) with self.put.subTest(expectation_type=etc.expectation_type.name, arguments=instruction_arguments): instruction_check.check( self.put, self.parser, instruction_source, ArrangementPostAct( pre_contents_population_action=MAKE_CWD_OUTSIDE_OF_EVERY_REL_OPT_DIR, home_or_sds_contents=rel_opt_config.populator_for_relativity_option_root( contents_of_relativity_option_root ), symbols=_symbol_table_of(rel_opt_config.symbols, following_symbols_setup), ), Expectation( main_result=main_result, symbol_usages=asrt.matches_sequence( rel_opt_config.symbols.usage_expectation_assertions() + following_symbols_setup.expected_references_list ) ))
def _symbol_usages_assertion( reference_assertions: Sequence[Assertion[SymbolReference]] ) -> Assertion[Sequence[SymbolUsage]]: return asrt.matches_sequence([ asrt.is_instance_with(SymbolReference, sr) for sr in reference_assertions ])
def runTest(self): # ARRANGE # named_transformer = NameAndValue('the_transformer', StringTransformerConstant( _DeleteAllButFirstLine())) actual_original_contents = lines_content(['1', '2', '3']) number_of_lines_after_transformation = '1' symbol_table_with_transformer = SymbolTable({ named_transformer.name: container(named_transformer.value) }) expected_symbol_usages = asrt.matches_sequence([ is_reference_to_string_transformer(named_transformer.name) ]) self._check_variants_with_expectation_type( InstructionArgumentsVariantConstructor( operator=comparators.EQ.name, operand=number_of_lines_after_transformation, transformer=named_transformer.name), expected_result_of_positive_test=PassOrFail.PASS, actual_file_contents=actual_original_contents, symbols=symbol_table_with_transformer, expected_symbol_usages=expected_symbol_usages, )
def test_invalid_arguments_with_symbol_references(self): symbol_name = SymbolWithReferenceSyntax('symbol_name') operand_arg_with_symbol_ref_list = [ ' {op} {sym}'.format(op=comparators.EQ.name, sym=symbol_name), ] invalid_symbol_values = [ 'not_a_number', '1.5', '72 87', ] for invalid_symbol_value in invalid_symbol_values: symbol = StringConstantSymbolContext(symbol_name.name, invalid_symbol_value) for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list: arguments = self._conf().arguments_constructor.apply( operand_arg_with_symbol_ref) with self.subTest(argument=arguments, invalid_symbol_value=invalid_symbol_value): for source in equivalent_source_variants__for_expression_parser( self, Arguments(arguments)): self._check( source, Arrangement(symbols=symbol.symbol_table), Expectation( ParseExpectation( symbol_references=asrt.matches_sequence([ symbol. reference_assertion__string__w_all_indirect_refs_are_strings ]), ), ExecutionExpectation( validation=asrt_validation. ValidationAssertions. pre_sds_fails__w_any_msg(), ), ), )
def test_matches(self): cases = [ NEA( 'empty list of blocks', expected=sut.matches_major_block(asrt.is_empty_sequence), actual=MajorBlock([]), ), NEA( 'non-empty list of blocks', expected=sut.matches_major_block( asrt.matches_sequence([asrt.is_instance(MinorBlock)])), actual=MajorBlock([MinorBlock([])]), ), NEA( 'test of element properties', expected=sut.matches_major_block( asrt.anything_goes(), properties=sut.matches_element_properties( indentation=sut.matches_indentation( level=asrt.equals(72))), ), actual=MajorBlock([], ElementProperties(Indentation(72, ''), TEXT_STYLE__NEUTRAL)), ), ] for case in cases: with self.subTest(case.name): case.expected.apply_without_message(self, case.actual)
def _check_( self, instruction_source: ParseSource, etc: ExpectationTypeConfigForPfh, main_result_for_positive_expectation: PassOrFail, rel_opt_config: RelativityOptionConfiguration, contents_of_relativity_option_root: DirContents = empty_dir_contents(), test_case_name: str = '', following_symbols_setup: SymbolsArrAndExpectSetup = SymbolsArrAndExpectSetup.empty()): with self.put.subTest(case_name=test_case_name, expectation_type=etc.expectation_type.name, arguments=instruction_source.source_string): instruction_check.check( self.put, self.parser, instruction_source, ArrangementPostAct( pre_contents_population_action=MAKE_CWD_OUTSIDE_OF_EVERY_REL_OPT_DIR, home_or_sds_contents=rel_opt_config.populator_for_relativity_option_root( contents_of_relativity_option_root ), symbols=_symbol_table_of(rel_opt_config.symbols, following_symbols_setup), ), Expectation( main_result=etc.main_result(main_result_for_positive_expectation), symbol_usages=asrt.matches_sequence( rel_opt_config.symbols.usage_expectation_assertions() + following_symbols_setup.expected_references_list ), ))
def _check(self, exception_from_hierarchy_reader: SuiteReadError): # ARRANGE # str_std_out_files = StringStdOutFiles() suite_root_file_path = Path('root-suite-file') suite_hierarchy_reader = ReaderThatRaises(exception_from_hierarchy_reader) reporter = ExecutionTracingProcessingReporter() processor = Processor(DUMMY_CASE_PROCESSING, suite_hierarchy_reader, reporter, DepthFirstEnumerator(), lambda x: TestCaseProcessorThatRaisesUnconditionally()) # ACT # exit_code = processor.process(suite_root_file_path, str_std_out_files.stdout_files) # ASSERT # check_exit_code_and_empty_stdout(self, exit_values.INVALID_SUITE.exit_code, exit_code, str_std_out_files) expected_invalid_suite_invocations = asrt.matches_sequence([ asrt.equals(exit_values.INVALID_SUITE), ]) expected_invalid_suite_invocations.apply_with_message(self, reporter.report_invalid_suite_invocations, 'report_invalid_suite_invocations') ExpectedSuiteReporting.check_list(self, [], reporter.complete_suite_reporter)
def test_file_matcher_reference_is_reported(self): name_of_file_matcher = 'a_file_matcher_symbol' arguments_constructor = args.complete_arguments_constructor( self.assertion_variant.arguments, file_matcher=name_of_file_matcher) arguments = arguments_constructor.apply( pfh_expectation_type_config(ExpectationType.NEGATIVE)) source = remaining_source(arguments) # ACT # matcher = parsers().full.parse(source) assert isinstance(matcher, MatcherSdv) actual = matcher.references # ASSERT # expected_references = asrt.matches_sequence( list(self.assertion_variant.expected_references) + [is_reference_to_file_matcher(name_of_file_matcher)]) expected_references.apply_without_message(self, actual) asrt_source.is_at_end_of_line(1)
def test_assignment_of_single_symbol_reference(self): # ARRANGE # referred_symbol = SymbolWithReferenceSyntax('referred_symbol') name_of_defined_symbol = 'defined_symbol' sb = SB.new_with(referred_symbol=referred_symbol, name_of_defined_symbol=name_of_defined_symbol) source = sb.lines( here_doc_lines(first_line_start='{string_type} {name_of_defined_symbol} = ', marker='EOF', contents_lines=[str(referred_symbol)]) ) # EXPECTATION # expected_resolver = string_resolver_from_fragments([symbol(referred_symbol.name), constant('\n')]) container_of_expected_resolver = container(expected_resolver) expected_definition = SymbolDefinition(name_of_defined_symbol, container_of_expected_resolver) expectation = Expectation( symbol_usages=asrt.matches_sequence([ vs_asrt.equals_symbol(expected_definition, ignore_source_line=True), ]), symbols_after_main=assert_symbol_table_is_singleton( name_of_defined_symbol, equals_container(container_of_expected_resolver), ) ) # ACT & ASSERT # self._check(source, ArrangementWithSds(), expectation)
def test_not_matches(self): cases = [ NEA( 'actual is empty list of line elements', expected=sut.matches_major_block( asrt.not_(asrt.is_empty_sequence)), actual=MajorBlock([]), ), NEA( 'actual element is unexpected', expected=sut.matches_major_block( asrt.matches_sequence( [asrt.not_(asrt.is_instance(MinorBlock))])), actual=MajorBlock([MinorBlock([])]), ), NEA( 'unexpected element properties', expected=sut.matches_major_block( asrt.anything_goes(), properties=asrt.not_(asrt.is_instance(ElementProperties))), actual=MajorBlock( [MinorBlock([], ELEMENT_PROPERTIES__NEUTRAL)]), ), ] for case in cases: with self.subTest(case.name): assert_that_assertion_fails(case.expected, case.actual)
def test_invalid_arguments_with_symbol_references(self): symbol_name = SymbolWithReferenceSyntax('symbol_name') operand_arg_with_symbol_ref_list = [ IntegerMatcherComparisonAbsStx.of_cmp_op( comparators.EQ, StringSymbolAbsStx(symbol_name.name), ), ] invalid_symbol_values = [ 'not_a_number', '1.5', '72 87', ] for invalid_symbol_value in invalid_symbol_values: symbol = StringConstantSymbolContext(symbol_name.name, invalid_symbol_value) for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list: full_syntax = self._conf().arguments_constructor.apply( operand_arg_with_symbol_ref) with self.subTest(argument=full_syntax.as_str__default(), invalid_symbol_value=invalid_symbol_value): self._check( full_syntax, ArrangementPostAct2(symbols=symbol.symbol_table), MultiSourceExpectation( symbol_usages=asrt.matches_sequence([ symbol. reference_assertion__string__w_all_indirect_refs_are_strings ]), execution=ExecutionExpectation( validation_pre_sds=svh_asrt. is_validation_error(), ), ), )
def test_WHEN_source_does_start_with_directive_name_but_invalid_number_of_arguments_follows_THEN_exception_SHOULD_be_raised( self): # ARRANGE # inclusion_directive_name = 'include' parser = sut.FileInclusionDirectiveParser(inclusion_directive_name) cases = [ NameAndValue('missing argument', inclusion_directive_name, ), NameAndValue('superfluous arguments', inclusion_directive_name + ' arg1 superfluous', ), ] for case in cases: with self.subTest(case.name): source = source_of_lines([ case.value, 'second line', ]) # ACT # with self.assertRaises(RecognizedSectionElementSourceError) as cm: # ACT & ASSERT # parser.parse(ARBITRARY_FS_LOCATION_INFO, source) actual_exception = cm.exception assert isinstance(actual_exception, SectionElementError) expected_invalid_source = matches_line_sequence( first_line_number=asrt.equals(1), lines=asrt.matches_sequence([ asrt.equals(case.value) ])) expected_invalid_source.apply_with_message(self, actual_exception.source, 'source')
def test_invalid_arguments_with_symbol_references(self): symbol_name = SymbolWithReferenceSyntax('symbol_name') operand_arg_with_symbol_ref_list = [ ' {op} {sym}'.format(op=comparators.EQ.name, sym=symbol_name), ] invalid_symbol_values = [ 'not_a_number', '1.5', '72 87', ] for invalid_symbol_value in invalid_symbol_values: symbol = StringConstantSymbolContext(symbol_name.name, invalid_symbol_value) for operand_arg_with_symbol_ref in operand_arg_with_symbol_ref_list: arguments = self._conf().arguments_constructor.apply( operand_arg_with_symbol_ref) with self.subTest(argument=arguments, invalid_symbol_value=invalid_symbol_value): for source in equivalent_source_variants__with_source_check__consume_last_line( self, arguments): self._check( source, ArrangementPostAct(symbols=symbol.symbol_table), Expectation( validation_pre_sds=svh_asrt. is_validation_error(), symbol_usages=asrt.matches_sequence([ symbol. reference_assertion__string__w_all_indirect_refs_are_strings ]), ), )
def runTest(self): # ARRANGE # transformer_symbols = ['st1', 'st2', 'st3'] expected_references = asrt.matches_sequence([ is_reference_to_string_transformer__ref(transformer) for transformer in transformer_symbols ]) arguments = st_args.syntax_for_sequence_of_transformers([ transformer for transformer in transformer_symbols ]) parser = parse_string_transformer.parser() # ACT # actual = parser.parse(remaining_source(arguments)) # ASSERT # self.assertIsInstance(actual, StringTransformerResolver) actual_references = actual.references expected_references.apply(self, actual_references)
def test_result_SHOULD_be_true_iff_exit_code_is_0(self): # ARRANGE # program_symbol_name = 'PROGRAM_THAT_EXECUTES_PY_FILE' exit_code_symbol_name = 'EXIT_CODE_SYMBOL' # ACT && ASSERT # integration_check.CHECKER__PARSE_FULL.check_multi( self, args.RunProgram( program_args.symbol_ref_command_elements( program_symbol_name, arguments=[ symbol_reference_syntax_for_name(exit_code_symbol_name) ], )).as_arguments, ParseExpectation( source=asrt_source.is_at_end_of_line(1), symbol_references=asrt.matches_sequence([ is_reference_to_program(program_symbol_name), is_reference_to__w_str_rendering(exit_code_symbol_name), ]), ), integration_check.ARBITRARY_MODEL, test_cases.exit_code_exe_cases( program_symbol_name, exit_code_symbol_name, ), )
def test_glob_pattern(self): match_anything_glob_pattern_string_symbol = StringSymbolContext.of_constant( 'glob_pattern_symbol', '*', default_restrictions=glob_pattern.is_reference_restrictions__glob_pattern_string() ) arguments = self.conf.arguments( NameGlobPatternVariant(match_anything_glob_pattern_string_symbol.name__sym_ref_syntax) ) integration_check.CHECKER__PARSE_SIMPLE.check__w_source_variants( self, arguments= arguments.as_arguments, input_= ARBITRARY_MODEL, arrangement=arrangement_wo_tcds( symbols=match_anything_glob_pattern_string_symbol.symbol_table ), expectation=Expectation( ParseExpectation( symbol_references=asrt.matches_sequence([ match_anything_glob_pattern_string_symbol.reference_assertion, ]), ), ExecutionExpectation( main_result=asrt_matching_result.matches_value__w_header( asrt.equals(True), asrt.equals(self.conf.node_name) ) ), ) )
def runTest(self): actual_contents = lines_content(['1', '2', '3', '4']) symbol_value = '3' constant_value = '1' operand_symbol = StringSymbolContext.of_constant( 'operand_symbol', symbol_value) expression_that_evaluates_to_actual_number_of_lines = '{sym_ref}+{const}'.format( sym_ref=symbol_reference_syntax_for_name(operand_symbol.name), const=constant_value, ) symbol_table_with_operand_symbol = operand_symbol.symbol_table expected_symbol_usages = asrt.matches_sequence( [is_reference_to_symbol_in_expression(operand_symbol.name)]) self._check_variants_with_expectation_type( InstructionArgumentsVariantConstructor( operator=comparators.GTE.name, operand=expression_that_evaluates_to_actual_number_of_lines), expected_result_of_positive_test=PassOrFail.PASS, actual_file_contents=actual_contents, symbols=symbol_table_with_operand_symbol, expected_symbol_references=expected_symbol_usages, )
def runTest(self): # ARRANGE # expected_file_name = 'quoted file name.src' act_phase_instructions = [instr([str(surrounded_by_hard_quotes(expected_file_name))]), instr([''])] executor_that_records_arguments = CommandExecutorThatRecordsArguments() arrangement = integration_check.arrangement_w_tcds( hds_contents=contents_in(RelHdsOptionType.REL_HDS_ACT, DirContents([ File.empty(expected_file_name)])), process_execution=ProcessExecutionArrangement( os_services=os_services_access.new_for_cmd_exe(executor_that_records_arguments) ) ) expectation = integration_check.Expectation() # ACT # integration_check.check_execution(self, ACTOR_THAT_RUNS_PYTHON_PROGRAM_FILE, act_phase_instructions, arrangement, expectation) # ASSERT # expected_command = asrt_command.matches_command( driver=asrt_command.matches_executable_file_command_driver(asrt.anything_goes()), arguments=asrt.matches_sequence([ asrt_path.str_as_path(asrt_path.name_equals(expected_file_name)) ]) ) expected_command.apply_with_message( self, executor_that_records_arguments.command, 'command', )
def _doTest(self, maybe_not: ExpectationTypeConfigForNoneIsSuccess): expected_content_line_template = 'expected content line, with {symbol} ref' def expected_content(symbol_content: str) -> str: return expected_content_line_template.format(symbol=symbol_content) symbol = StringConstantSymbolContext('symbol_name', 'the symbol value') self._check( test_configuration.source_for( args('{maybe_not} {equals} <<EOF', maybe_not=maybe_not.nothing__if_positive__not_option__if_negative), [expected_content(symbol.name__sym_ref_syntax), 'EOF', 'following line']), model_constructor.of_str(self, lines_content([expected_content(symbol.str_value)])), arrangement_w_tcds( post_population_action=MK_SUB_DIR_OF_ACT_AND_MAKE_IT_CURRENT_DIRECTORY, symbols=symbol.symbol_table), Expectation( ParseExpectation( source=asrt_source.is_at_end_of_line(3), symbol_references=asrt.matches_sequence([ symbol.reference_assertion__w_str_rendering ]), ), ExecutionExpectation( main_result=maybe_not.pass__if_positive__fail__if_negative, ), ), )