def test_raise_exception_WHEN_invalid_assertion_variant(self): parser = sut.parsers(must_be_on_current_line=True).full cases = [ NameAndValue( 'Matcher is missing', args.complete_arguments_constructor( InvalidAssertionVariantArgumentsConstructor('')), ), NameAndValue( 'Matcher has invalid syntax', args.complete_arguments_constructor( InvalidAssertionVariantArgumentsConstructor( NOT_A_VALID_SYMBOL_NAME_NOR_PRIMITIVE_GRAMMAR_ELEMENT_NAME )), ), ] for case in cases: for expectation_type in ExpectationType: etc = pfh_expectation_type_config(expectation_type) instruction_arguments = case.value.apply(etc) source = remaining_source(instruction_arguments) with self.subTest(case_name=case.name, expectation_type=str(expectation_type)): with self.assertRaises( SingleInstructionInvalidArgumentException): parser.parse(source)
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_it_SHOULD_be_possible_to_create_multiple_intermediate_files(self): # ARRANGE # original_file_contents = 'original_file_contents' expected_transformer_contents = original_file_contents.upper() transform_to_uppercase = test_transformers_setup.TO_UPPER_CASE_TRANSFORMER symbol_table_with_lines_transformer = transform_to_uppercase.symbol_table expected_symbol_references = asrt.matches_sequence([ is_reference_to_string_transformer(transform_to_uppercase.name) ]) for_all__equals__arguments = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.ALL, file_contents_arg2(sm_arg.Transformed(transform_to_uppercase.name, sm_arg.Equals.eq_string(expected_transformer_contents))) , )) relativity_root_conf = AN_ACCEPTED_SDS_REL_OPT_CONFIG etc = expectation_type_config__non_is_success(ExpectationType.POSITIVE) arguments = for_all__equals__arguments.apply(etc) # ACT & ASSERT # integration_check.CHECKER__PARSE_FULL.check( self, remaining_source(arguments), model.model_with_rel_root_as_source_path(relativity_root_conf), arrangement= arrangement_w_tcds( tcds_contents=relativity_root_conf.populator_for_relativity_option_root( DirContents([ File('1.txt', original_file_contents), File('2.txt', original_file_contents), ]) ), symbols=symbol_table_with_lines_transformer ), expectation= Expectation( ParseExpectation( symbol_references=expected_symbol_references ), ExecutionExpectation( main_result=etc.pass__if_positive__fail__if_negative, ), ) )
def test_raise_exception_WHEN_selector_option_argument_is_missing(self): instruction_args_without_valid_file_matcher = args.complete_arguments_constructor( self.assertion_variant.arguments, file_matcher=' ') parser = parsers().full for expectation_type in ExpectationType: etc = pfh_expectation_type_config(expectation_type) instruction_arguments_without_valid_file_matcher_arg = instruction_args_without_valid_file_matcher.apply( etc) source = remaining_source( instruction_arguments_without_valid_file_matcher_arg) with self.subTest( arguments= instruction_arguments_without_valid_file_matcher_arg, expectation_type=str(expectation_type)): with self.assertRaises( SingleInstructionInvalidArgumentException): parser.parse(source)
def test_hard_error_when_there_is_a_single_file_that_is_not_a_regular_file(self): name_of_checked_dir = 'checked-dir' relativity_root_conf = rel_opt_conf.default_conf_rel_any(RelOptionType.REL_CWD) the_model = model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir)(relativity_root_conf) non_regular_files = [ Dir.empty('a-directory'), sym_link('sym-link', 'non-existing-target') ] for file_contents_assertion in self.file_content_assertion_variants: for quantifier in Quantifier: arguments_constructor = args.complete_arguments_constructor( FileQuantificationAssertionVariant( quantifier, file_contents_arg2(file_contents_assertion))) for expectation_type in ExpectationType: arguments = arguments_constructor.apply( expectation_type_config__non_is_success(expectation_type)) for non_regular_file in non_regular_files: with self.subTest( quantifier=quantifier.name, expectation_type=expectation_type.name, arguments=arguments, non_regular_file=non_regular_file.name): integration_check.CHECKER__PARSE_FULL.check( self, remaining_source(arguments), the_model, arrangement= arrangement_w_tcds( tcds_contents=relativity_root_conf.populator_for_relativity_option_root( DirContents([ Dir(name_of_checked_dir, [ non_regular_file, ]), ]) ) ), expectation= Expectation( execution=ExecutionExpectation( is_hard_error=asrt.anything_goes() ), ) )
def test_raise_exception_WHEN_there_is_an_initial_illegal_option(self): valid_instruction_arguments_con = args.complete_arguments_constructor( self.assertion_variant.arguments) parser = parsers().full for expectation_type in ExpectationType: etc = pfh_expectation_type_config(expectation_type) valid_instruction_arguments = valid_instruction_arguments_con.apply( etc) instruction_args_with_invalid_initial_option = '{invalid_option} {valid_arguments}'.format( invalid_option=long_option_syntax('illegalOption'), valid_arguments=valid_instruction_arguments, ) with self.subTest( arguments=instruction_args_with_invalid_initial_option, expectation_type=str(expectation_type)): for source in equivalent_source_variants( self, instruction_args_with_invalid_initial_option): with self.assertRaises( SingleInstructionInvalidArgumentException): parser.parse(source)
class TestExistsTypeDir(unittest.TestCase): name_of_checked_dir = 'checked-dir' exists__type_dir__arguments = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.EXISTS, fm_args.WithOptionalNegation(fm_args.Type(file_properties.FileType.DIRECTORY))) ) @property def instruction_checker(self) -> MatcherChecker: return MatcherChecker(self, sut.parsers().full) def test_one_dir_and_one_regular(self): self.instruction_checker.check_expectation_type_variants( self.exists__type_dir__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), main_result_for_positive_expectation=PassOrFail.PASS, root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File.empty('regular.txt'), Dir.empty('dir'), ]), ]), ) def test_just_one_regular(self): self.instruction_checker.check_expectation_type_variants( self.exists__type_dir__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, main_result_for_positive_expectation=PassOrFail.FAIL, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File.empty('regular.txt'), ]), ]), )
def test_symbols_from_contents_assertion_SHOULD_be_reported(self): # ARRANGE # operand_sym_ref = SymbolReference('operand_symbol_name', is_string__all_indirect_refs_are_strings()) condition_str = '{operator} {symbol_reference}'.format( operator=comparators.EQ.name, symbol_reference=symbol_reference_syntax_for_name(operand_sym_ref.name) ) arguments_constructor = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.ALL, file_contents_arg2(sm_arg.NumLines(condition_str)))) argument = arguments_constructor.apply( expectation_type_config__non_is_success(ExpectationType.NEGATIVE) ) source = remaining_source(argument) # ACT # matcher_sdv = sut.parsers().full.parse(source) assert isinstance(matcher_sdv, MatcherSdv) actual_symbol_references = matcher_sdv.references # ASSERT # expected_symbol_references = [ operand_sym_ref, ] assertion = equals_symbol_references__w_str_rendering(expected_symbol_references) assertion.apply_without_message(self, actual_symbol_references)
def test__exists__SHOULD_consider_only_files_matched_by_the_file_matcher(self): # ARRANGE # name_of_checked_dir = 'name-of-checked-dir' name_starts_with_selected = FileMatcherSymbolContext.of_primitive( 'a_file_matcher_symbol', FileMatcherThatMatchesAnyFileWhosNameStartsWith('selected')) files_in_checked_dir = Dir(name_of_checked_dir, [ File( 'selected-non-empty-file.txt', 'contents of non-emtpy file'), File.empty('un-selected-empty-file.txt'), Dir.empty('un-selected-dir'), sym_link( 'un-selected-sym-link-to-dir', 'un-selected-dir'), sym_link( 'un-selected-broken-sym-link', 'non-existing-file'), ]) symbol_table_with_file_matcher = name_starts_with_selected.symbol_table relativity_root_conf = AN_ACCEPTED_SDS_REL_OPT_CONFIG expected_symbol_references = asrt.matches_sequence([ is_reference_to_file_matcher(name_starts_with_selected.name) ]) # ACT & ASSERT # for file_is_empty_assertion in self.file_content_assertion_variants_that_pass_iff_file_is_empty: arguments_constructor = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.EXISTS, file_contents_arg2(file_is_empty_assertion)), file_matcher=name_starts_with_selected.name ) for expectation_type in ExpectationType: etc = expectation_type_config__non_is_success(expectation_type) arguments = arguments_constructor.apply(etc) with self.subTest( expectation_type=expectation_type.name, arguments=arguments): integration_check.CHECKER__PARSE_FULL.check( self, remaining_source(arguments), model.model_with_source_path_as_sub_dir_of_rel_root(name_of_checked_dir)(relativity_root_conf), arrangement= arrangement_w_tcds( tcds_contents=relativity_root_conf.populator_for_relativity_option_root( DirContents([ files_in_checked_dir, ]) ), symbols=symbol_table_with_file_matcher ), expectation= Expectation( ParseExpectation( symbol_references=expected_symbol_references ), ExecutionExpectation( main_result=etc.fail__if_positive__pass_if_negative, ), ) )
class TestForAllContentsEmptyFile(unittest.TestCase): name_of_checked_dir = 'checked-dir' for_all__empty__arguments = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.ALL, file_contents_arg2(sm_arg.Empty())) ) @property def instruction_checker(self) -> MatcherChecker: return MatcherChecker(self, sut.parsers().full) def test_one_empty_and_one_non_empty_file(self): self.instruction_checker.check_expectation_type_variants( self.for_all__empty__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), main_result_for_positive_expectation=PassOrFail.FAIL, root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File.empty('empty-file.txt'), File('non-empty-file.txt', 'contents of non-empty file'), ]), ]), ) def test_just_one_empty_file(self): self.instruction_checker.check_expectation_type_variants( self.for_all__empty__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, main_result_for_positive_expectation=PassOrFail.PASS, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File.empty('empty-file.txt'), ]), ]), ) def test_just_one_non_empty_file(self): self.instruction_checker.check_expectation_type_variants( self.for_all__empty__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, main_result_for_positive_expectation=PassOrFail.FAIL, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, [ File('non-empty-file.txt', 'contents of non-empty file'), ]), ]), ) def test_no_files(self): self.instruction_checker.check_expectation_type_variants( self.for_all__empty__arguments, model.model_with_source_path_as_sub_dir_of_rel_root(self.name_of_checked_dir), root_dir_of_dir_contents=AN_ACCEPTED_SDS_REL_OPT_CONFIG, main_result_for_positive_expectation=PassOrFail.PASS, contents_of_relativity_option_root=DirContents([ Dir(self.name_of_checked_dir, []), ]), )
def apply(self, condition_str: str) -> str: arguments_constructor = args.complete_arguments_constructor( FileQuantificationAssertionVariant( Quantifier.ALL, file_contents_arg2(sm_arg.NumLines(condition_str)))) return arguments_constructor.apply(expectation_type_config__non_is_success(ExpectationType.POSITIVE))