Beispiel #1
0
    def _check_cases_for_no_lines(
            self, get_assertion_part_function: Callable[
                [ExpectationType, LineMatcherSdv],
                AssertionPart[StringSource, pfh.PassOrFailOrHardError]],
            expected_result_when_positive_expectation: PassOrFail):
        empty_file_contents = ''
        environment = fake_post_sds_environment()
        os_services = new_for_current_os()

        matchers = [
            ('unconditionally true', MatcherWithConstantResult(True)),
            ('unconditionally false', MatcherWithConstantResult(False)),
        ]
        with string_source_factory() as source_factory:
            # This test is expected to not create files using the above object,
            # but to be sure, one is used that creates and destroys temporary files.
            with tmp_file_containing(empty_file_contents) as actual_file_path:
                for expectation_type in ExpectationType:
                    for matcher_name, matcher in matchers:
                        with self.subTest(expectation_type=expectation_type,
                                          matcher_name=matcher_name):
                            model = source_factory.of_file__poorly_described(
                                actual_file_path)
                            matcher_sdv = sdv_components.matcher_sdv_from_constant_primitive(
                                matcher)
                            assertion_part = get_assertion_part_function(
                                expectation_type, matcher_sdv)
                            # ACT #
                            actual = assertion_part.check_and_return_pfh(
                                environment, os_services, model)
                            # ASSERT #
                            pfh_assertion = pfh_expectation_type_config(
                                expectation_type).main_result(
                                    expected_result_when_positive_expectation)
                            pfh_assertion.apply_without_message(self, actual)
Beispiel #2
0
    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 check_parsing_with_different_source_variants(
        self,
        make_instruction_arguments: InstructionArgumentsVariantConstructor,
        default_relativity: RelOptionType,
        non_default_relativity: RelOptionType,
        main_result_for_positive_expectation: PassOrFail,
        contents_of_relativity_option_root: DirContents = empty_dir_contents(),
        following_symbols_setup: SymbolsArrEx = SymbolsArrEx.empty()):

        rel_opt_configs = [
            rel_opt_conf.default_conf_rel_any(default_relativity),
            rel_opt_conf.conf_rel_any(non_default_relativity),
        ]

        for rel_opt_config in rel_opt_configs:
            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)

                for source in equivalent_source_variants__with_source_check__consume_last_line(
                        self.put, instruction_arguments):
                    self._check_(
                        source,
                        etc,
                        main_result_for_positive_expectation,
                        rel_opt_config,
                        contents_of_relativity_option_root,
                        following_symbols_setup=following_symbols_setup)
Beispiel #4
0
    def _check_cases_with_non_empty_file(self,
                                         get_assertion_part_function: Callable[
                                             [ExpectationType, LineMatcherSdv],
                                             FileContentsAssertionPart],
                                         actual_file_contents: str,
                                         matcher_cases: Sequence[Case]):

        environment = fake_post_sds_environment()
        os_services = new_for_current_os()

        with string_source_factory() as source_factory:
            # This test is expected to not create files using the above object,
            # but to be sure, one is used that creates and destroys temporary files.
            with tmp_file_containing(actual_file_contents) as actual_file_path:
                for case in matcher_cases:
                    for expectation_type in ExpectationType:
                        with self.subTest(case=case.name,
                                          expectation_type=expectation_type):
                            model = source_factory.of_file__poorly_described(
                                actual_file_path)
                            matcher_sdv = sdv_components.matcher_sdv_from_constant_primitive(
                                case.matcher)
                            assertion_part = get_assertion_part_function(
                                expectation_type, matcher_sdv)
                            # ACT #
                            actual = assertion_part.check_and_return_pfh(
                                environment, os_services, model)
                            # ASSERT #
                            pfh_assertion = pfh_expectation_type_config(
                                expectation_type).main_result(
                                    case.
                                    expected_result_for_positive_expectation)
                            pfh_assertion.apply_without_message(self, actual)
Beispiel #5
0
 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)
Beispiel #6
0
    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')),
            lambda main_result: Expectation(
                main_result=main_result,
                symbol_usages=asrt.matches_sequence([
                    is_reference_to_string_transformer__usage(
                        transformer_of_program.name),
                    is_reference_to_string_transformer__usage(
                        transformer_of_instruction.name),
                ])),
            arrangement=ArrangementPostAct(
                symbols=transformers_setup.SYMBOL_TABLE),
            transformation=transformer_of_instruction.name)
Beispiel #7
0
    def runTest(self):
        program_that_outputs_nothing = pgm_args.program(
            pgm_args.interpret_py_source_line(
                self.configuration.py_source_for_print('')))
        result_when_positive = pfh_expectation_type_config(
            ExpectationType.POSITIVE)

        self._check_positive_and_negated(
            result_when_positive, program_that_outputs_nothing,
            matcher_arguments.emptiness_matcher(),
            lambda main_result: Expectation(main_result=main_result))
Beispiel #8
0
 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 check_rel_opt_variants(
        self,
        make_instruction_arguments: InstructionArgumentsVariantConstructor,
        main_result_for_positive_expectation: PassOrFail,
        contents_of_relativity_option_root: DirContents = empty_dir_contents(),
        test_case_name: str = '',
        following_symbols_setup: SymbolsArrEx = SymbolsArrEx.empty()):

        for rel_opt_config in self.accepted_rel_opt_configurations:
            etc = pfh_expectation_type_config(ExpectationType.POSITIVE)
            instruction_arguments = make_instruction_arguments.apply(
                etc, rel_opt_config)
            self._check_(remaining_source(instruction_arguments),
                         etc,
                         main_result_for_positive_expectation,
                         rel_opt_config,
                         contents_of_relativity_option_root,
                         test_case_name,
                         following_symbols_setup=following_symbols_setup)
Beispiel #10
0
    def runTest(self):
        program_that_outputs_something = pgm_args.program(
            pgm_args.interpret_py_source_line(
                self.configuration.py_source_for_print('some output')),
            transformation=transformers_setup.DELETE_EVERYTHING_TRANSFORMER.
            name)
        result_when_positive = pfh_expectation_type_config(
            ExpectationType.POSITIVE)

        self._check_positive_and_negated(
            result_when_positive, program_that_outputs_something,
            matcher_arguments.emptiness_matcher(),
            lambda main_result: Expectation(
                main_result=main_result,
                symbol_usages=asrt.matches_sequence([
                    is_reference_to_string_transformer__usage(
                        transformers_setup.DELETE_EVERYTHING_TRANSFORMER.name),
                ])),
            ArrangementPostAct(symbols=transformers_setup.SYMBOL_TABLE))
Beispiel #11
0
 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)
    def check_rel_opt_variants_with_same_result_for_every_expectation_type(
        self,
        make_instruction_arguments: InstructionArgumentsVariantConstructor,
        main_result: Assertion,
        contents_of_relativity_option_root: DirContents = empty_dir_contents(),
        following_symbols_setup: SymbolsArrEx = SymbolsArrEx.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,
                            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=main_result,
                            symbol_usages=asrt.matches_sequence(
                                rel_opt_config.symbols.
                                usage_expectation_assertions() +
                                following_symbols_setup.expected_usages_list)))
Beispiel #13
0
 def __init__(self, instruction_configuration: InstructionTestConfiguration,
              option_configuration: RelativityOptionConfiguration,
              expectation_type: ExpectationType):
     super().__init__(instruction_configuration)
     self.rel_opt = option_configuration
     self.not_opt = pfh_expectation_type_config(expectation_type)