Exemple #1
0
    def test_empty_chain(self):
        # ARRANGE #

        cases = [
            NIE('referrer location is .',
                expected_value=([], Path('.')),
                input_value=Path('.'),
                ),
            NIE('referrer location is dir',
                expected_value=([], Path('a-dir')),
                input_value=Path('a-dir'),
                ),
        ]

        for case in cases:
            with self.subTest(case.name):
                # ACT #
                line_elements, referrer_location = sut.file_inclusion_chain(case.input_value, [])
                output = print_line_elements(line_elements)
                # ASSERT #

                self.assertEqual(lines_str(case.expected_value[0]), output,
                                 'output')

                self.assertEqual(case.expected_value[1],
                                 referrer_location,
                                 'referrer location')
Exemple #2
0
    def test(self):
        # ARRANGE #

        single_line = ['the single line']
        multiple_lines = [
            'first',
            'second',
        ]

        cases = [
            NIE('single line',
                expected_source_line_lines(single_line),
                single_line
                ),
            NIE('multiple lines',
                expected_source_line_lines(multiple_lines),
                multiple_lines,
                ),
        ]

        formatter = sut.default_formatter()

        for case in cases:
            with self.subTest(case.name):
                # ACT #

                actual = formatter.source_lines(case.input_value)

                # ASSERT #

                self.assertEqual(case.expected_value,
                                 actual)
Exemple #3
0
    def test(self):
        # ARRANGE #

        single_line = ['the single line']
        multiple_lines = [
            'first',
            'second',
        ]

        cases = [
            NIE('single line',
                expected_source_line_lines(single_line),
                single_line
                ),
            NIE('multiple lines',
                expected_source_line_lines(multiple_lines),
                multiple_lines,
                ),
        ]

        for case in cases:
            with self.subTest(case.name):
                # ACT #

                actual_line_element = sut.source_lines_element(case.input_value)
                actual = print_line_element(actual_line_element)

                # ASSERT #

                self.assertEqual(lines_str(case.expected_value),
                                 actual)
Exemple #4
0
    def test_empty_chain(self):
        # ARRANGE #

        cases = [
            NIE('referrer location is .',
                expected_value=([], Path('.')),
                input_value=Path('.'),
                ),
            NIE('referrer location is dir',
                expected_value=([], Path('a-dir')),
                input_value=Path('a-dir'),
                ),
        ]

        formatter = sut.default_formatter()

        for case in cases:
            with self.subTest(case.name):
                # ACT #
                block, referrer_location = formatter.file_inclusion_chain(case.input_value,
                                                                          [])

                # ASSERT #

                self.assertEqual(case.expected_value[0], block,
                                 'block')

                self.assertEqual(case.expected_value[1],
                                 referrer_location,
                                 'referrer location')
    def test_system_program(self):
        # ARRANGE #
        program_name = 'program_name'
        cases = [
            NIE('without arguments',
                expected_value=[program_name],
                input_value=[],
                ),
            NIE('with arguments',
                expected_value=[program_name, 'arg1', 'arg2'],
                input_value=['arg1', 'arg2'],
                ),
        ]
        for case in cases:
            with self.subTest(case.name):
                command = Command(CommandDriverForSystemProgram(program_name),
                                  case.input_value)
                # ACT #
                actual = self.factory.make(command)
                # ASSERT #
                self.assertFalse(actual.is_shell)

                self.assertIsInstance(actual.arg_list_or_str, list)

                self.assertEqual(case.expected_value, actual.arg_list_or_str)
    def test_shell(self):
        # ARRANGE #
        command_line = 'command line'
        cases = [
            NIE('without arguments',
                expected_value=command_line,
                input_value=[],
                ),
            NIE('with arguments',
                expected_value=command_line + ' arg1 arg2',
                input_value=['arg1', 'arg2'],
                ),
        ]
        for case in cases:
            with self.subTest(case.name):
                command = Command(CommandDriverForShell(command_line),
                                  case.input_value)
                # ACT #
                actual = self.factory.make(command)
                # ASSERT #
                self.assertTrue(actual.is_shell)

                self.assertIsInstance(actual.arg_list_or_str, str)

                self.assertEqual(case.expected_value, actual.arg_list_or_str)
 def test_not_matches(self):
     cases = [
         NIE(
             'default assertion/not a renderer',
             input_value=self._LINE_ELEMENT,
             expected_value=sut.is_renderer_of_line_elements(),
         ),
         NIE(
             'default assertion/not a sequence',
             input_value=rend_comb.ConstantR(self._LINE_ELEMENT),
             expected_value=sut.is_renderer_of_line_elements(),
         ),
         NIE(
             'default assertion/not a line element',
             input_value=rend_comb.ConstantSequenceR([MajorBlock([])]),
             expected_value=sut.is_renderer_of_line_elements(),
         ),
         NIE(
             'default assertion/invalid contents of line element',
             input_value=rend_comb.ConstantSequenceR([
                 LineElement(StringLineObject('string'), 'not properties')
             ]),
             expected_value=sut.is_renderer_of_line_elements(),
         ),
         NIE(
             'custom assertion/unexpected number of blocks',
             input_value=rend_comb.ConstantSequenceR([]),
             expected_value=sut.is_renderer_of_line_elements(
                 asrt.len_equals(1)),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             assert_that_assertion_fails(case.expected_value,
                                         case.input_value)
 def test_not_matches(self):
     cases = [
         NIE(
             'default assertion/not a renderer',
             input_value=MajorBlock([]),
             expected_value=sut.is_renderer_of_major_blocks(),
         ),
         NIE(
             'default assertion/not a sequence',
             input_value=rend_comb.ConstantR(MajorBlock([])),
             expected_value=sut.is_renderer_of_major_blocks(),
         ),
         NIE(
             'default assertion/not a major block',
             input_value=rend_comb.ConstantSequenceR([MinorBlock([])]),
             expected_value=sut.is_renderer_of_major_blocks(),
         ),
         NIE(
             'default assertion/invalid contents of block',
             input_value=rend_comb.ConstantSequenceR(
                 [MajorBlock(['not a minor block'])]),
             expected_value=sut.is_renderer_of_major_blocks(),
         ),
         NIE(
             'custom assertion/unexpected number of blocks',
             input_value=rend_comb.ConstantSequenceR([]),
             expected_value=sut.is_renderer_of_major_blocks(
                 asrt.len_equals(1)),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             assert_that_assertion_fails(case.expected_value,
                                         case.input_value)
    def test_executable_file(self):
        # ARRANGE #
        the_file = pathlib.Path('the dir') / 'the file'
        cases = [
            NIE('without arguments',
                expected_value=[str(the_file)],
                input_value=[],
                ),
            NIE('with arguments',
                expected_value=[str(the_file), 'arg1', 'arg2'],
                input_value=['arg1', 'arg2'],
                ),
        ]
        for case in cases:
            with self.subTest(case.name):
                command = Command(CommandDriverForExecutableFile(new_primitive(the_file)),
                                  case.input_value)
                # ACT #
                actual = self.factory.make(command)
                # ASSERT #
                self.assertFalse(actual.is_shell)

                self.assertIsInstance(actual.arg_list_or_str, list)

                self.assertEqual(case.expected_value, actual.arg_list_or_str)
Exemple #10
0
 def runTest(self):
     # ARRANGE #
     opt_new_line = ['the', 'optional', 'new', 'line']
     layout_spec = LayoutSpec(optional_new_line=opt_new_line)
     cases = [
         NIE(
             'first',
             input_value={TokenPosition.FIRST},
             expected_value=[],
         ),
         NIE(
             'last',
             input_value={TokenPosition.LAST},
             expected_value=[],
         ),
         NIE(
             'first and last',
             input_value={TokenPosition.FIRST, TokenPosition.LAST},
             expected_value=[],
         ),
         NIE(
             'neither first nor last',
             input_value=set(),
             expected_value=['\n'],
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             # ACT #
             actual = sut.NEW_LINE_IF_NOT_FIRST_OR_LAST.layout(
                 layout_spec, case.input_value)
             # ASSERT #
             self.assertEqual(case.expected_value, actual)
 def runTest(self):
     # ARRANGE #
     cases = [
         NIE(
             'infix op of precedence 1',
             input_value=_src_of([Pa.name, IOP_1u, Pb.name]),
             expected_value=InfixOp1u([Pa.value, Pb.value]),
         ),
         NIE(
             'infix op of precedence 1, and prefix operator',
             input_value=_src_of([POP, Pa.name, IOP_1u, POP, Pb.name]),
             expected_value=InfixOp1u(
                 [PrefixOp(Pa.value),
                  PrefixOp(Pb.value)]),
         ),
         NIE(
             'infix op of precedence 2',
             input_value=_src_of([Pa.name, IOP_2u, Pb.name]),
             expected_value=InfixOp2u([Pa.value, Pb.value]),
         ),
         NIE(
             'infix op of precedence 2, and prefix operator',
             input_value=_src_of([POP, Pa.name, IOP_2u, POP, Pb.name]),
             expected_value=InfixOp2u(
                 [PrefixOp(Pa.value),
                  PrefixOp(Pb.value)]),
         ),
     ]
     # ACT & ASSERT #
     check__full_and_simple_within_parentheses__multi(
         self,
         cases,
     )
Exemple #12
0
 def runTest(self):
     cases = [
         NIE(
             'start=1, width=0',
             ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
             Conf(start=1, width=0, num_items_to_take=11),
         ),
         NIE(
             'start=1, width=1',
             ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11'],
             Conf(start=1, width=1, num_items_to_take=11),
         ),
         NIE(
             'start=0, width=2',
             ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11'],
             Conf(start=0, width=2, num_items_to_take=12),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             conf = case.input_value
             # ACT #
             actual = sut.int_strings(conf.start, conf.width)
             # ASSERT #
             actual_to_take = list(itertools.islice(actual, conf.num_items_to_take))
             self.assertEqual(case.expected_value,
                              actual_to_take)
 def runTest(self):
     # ARRANGE #
     cases = [
         NIE(
             'single infix op of every precedence / highest first',
             input_value=_src_of(
                 [Pa.name, IOP_1u, Pb.name, IOP_2u, Pc.name]),
             expected_value=InfixOp2u([
                 InfixOp1u([Pa.value, Pb.value]),
                 Pc.value,
             ]),
         ),
         NIE(
             'single infix op of every precedence / lowest first',
             input_value=_src_of(
                 [Pa.name, IOP_2u, Pb.name, IOP_1u, Pc.name]),
             expected_value=InfixOp2u([
                 Pa.value,
                 InfixOp1u([Pb.value, Pc.value]),
             ]),
         ),
         NIE(
             'infix op of lowest, surrounded by infix-op-expr of higher on each side',
             input_value=_src_of([
                 Pa.name, IOP_1u, Pb.name, IOP_2u, Pc.name, IOP_1u, Pd.name
             ]),
             expected_value=InfixOp2u([
                 InfixOp1u([Pa.value, Pb.value]),
                 InfixOp1u([Pc.value, Pd.value]),
             ]),
         ),
         NIE(
             '2 infix-op of lowest, with 3 arguments: simple, infix-op-highest-expr, simple',
             input_value=_src_of([
                 Pa.name, IOP_2u, Pb.name, IOP_1u, Pc.name, IOP_2u, Pd.name
             ]),
             expected_value=InfixOp2u([
                 Pa.value,
                 InfixOp1u([Pb.value, Pc.value]),
                 Pd.value,
             ]),
         ),
         NIE(
             '',
             input_value=_src_of([
                 Pa.name, IOP_1u, Pb.name, IOP_2u, Pc.name, IOP_1v, Pd.name,
                 IOP_1u, Pe.name
             ]),
             expected_value=InfixOp2u([
                 InfixOp1u([Pa.value, Pb.value]),
                 InfixOp1u([InfixOp1v([Pc.value, Pd.value]), Pe.value]),
             ]),
         ),
     ]
     # ACT & ASSERT #
     check__full_and_simple_within_parentheses__multi(
         self,
         cases,
     )
Exemple #14
0
    def test_removal_of_new_lines_SHOULD_join_lines(self):
        # ARRANGE #
        cases = [
            NIE('final line not ended by new-line',
                input_value=[
                    '1\n',
                    '2\n',
                    '3',
                ],
                expected_value=[
                    '123',
                ]),
            NIE('final line ended by new-line',
                input_value=[
                    '1\n',
                    '2\n',
                    '3\n',
                ],
                expected_value=[
                    '123',
                ]),
        ]
        for line_filter_case in LINE_FILTER_CASES:
            for case in cases:
                with self.subTest(model=case.name,
                                  line_filtering=line_filter_case.name):
                    nl_string_symbol = StringConstantSymbolContext(
                        'NL',
                        '\n',
                        default_restrictions=asrt_regex.
                        is_reference_restrictions__regex(),
                    )
                    all_symbols = line_filter_case.value.symbols + [
                        nl_string_symbol
                    ]
                    source = ReplaceRegexAbsStx(
                        nl_string_symbol.abstract_syntax,
                        StringLiteralAbsStx.empty_string(),
                        preserve_new_lines=False,
                        lines_filter=line_filter_case.value.syntax,
                    )

                    # ACT & ASSERT #
                    integration_check.CHECKER__PARSE_SIMPLE.check__abs_stx(
                        self, source,
                        model_constructor.of_lines(self, case.input_value),
                        arrangement_w_tcds(
                            symbols=SymbolContext.symbol_table_of_contexts(
                                all_symbols), ),
                        expectation_of_successful_replace_execution(
                            symbol_references=SymbolContext.
                            references_assertion_of_contexts(all_symbols),
                            may_depend_on_external_resources=line_filter_case.
                            value.may_depend_on_external_resources,
                            output_lines=case.expected_value,
                        ))
 def runTest(self):
     max_num_lines = 2
     max_num_chars = 10
     reader = sut.InitialPartReaderWithRestIndicator(
         max_num_lines, max_num_chars)
     cases = [
         NIE(
             'read all, single line',
             input_value='x' * (max_num_chars - 1),
             expected_value='x' * (max_num_chars - 1),
         ),
         NIE(
             'read all, multiple lines',
             input_value='1\n2\n',
             expected_value='1\n2\n',
         ),
         NIE(
             'read limited (num chars), single line',
             input_value='x' * (max_num_chars + 1),
             expected_value=('x' * max_num_chars) +
             sut.InitialPartReaderWithRestIndicator.REST_INDICATOR,
         ),
         NIE(
             'read limited (num chars), multiple lines',
             input_value='1234567\n1234567\n',
             expected_value='1234567\n12' +
             sut.InitialPartReaderWithRestIndicator.REST_INDICATOR,
         ),
         NIE(
             'read limited (num lines)',
             input_value='1\n2\n3\n',
             expected_value='1\n2\n' +
             sut.InitialPartReaderWithRestIndicator.REST_INDICATOR,
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             input_file = io.StringIO(case.input_value)
             # ACT #
             actual = reader.read(input_file)
             # ASSERT #
             self.assertEqual(case.expected_value, actual)
 def test_no_source_and_no_description(self):
     # ARRANGE #
     section_name = 'section-name'
     cases = [
         NIE(
             'with no section',
             expected_value=asrt.is_empty_sequence,
             input_value=None,
         ),
         NIE('with section',
             expected_value=asrt.matches_sequence([
                 _matches_plain_minor_block_w_single_plain_line(
                     section_line(section_name))
             ]),
             input_value=section_name),
     ]
     for case in cases:
         with self.subTest(case.name, blocks_renderer='minor blocks'):
             # ACT #
             actual_renderer = source_location.location_minor_blocks_renderer(
Exemple #17
0
def equivalent_source_variants_for_consume_until_end_of_last_line_3(
        arguments: str) -> List[NIE[ParseSource, Assertion[ParseSource]]]:
    return [
        NIE(
            'following_lines={}'.format(repr(extra_lines)),
            assertion,
            _remaining_source__original_plus_extra_lines(
                arguments, extra_lines),
        ) for extra_lines, assertion in
        _equivalent_source_variants_for_consume_until_end_of_last_line(
            arguments.count('\n') + 1)
    ]
Exemple #18
0
def equivalent_source_variants_for_consume_until_end_of_last_line2(
        arguments: Arguments
) -> List[NIE[ParseSource, Assertion[ParseSource]]]:
    return [
        NIE(
            repr(extra_lines),
            assertion,
            arguments.followed_by_lines(extra_lines).as_remaining_source,
        ) for extra_lines, assertion in
        _equivalent_source_variants_for_consume_until_end_of_last_line(
            arguments.num_lines)
    ]
Exemple #19
0
    def test_header_and_value_detail(self):
        # ARRANGE #
        text_style__non_neutral = TextStyle(font_style=FontStyle.UNDERLINE)
        cases = [
            NIE(
                'without text style',
                TEXT_STYLE__NEUTRAL,
                TEXT_STYLE__NEUTRAL,
            ),
            NIE(
                'with text style',
                text_style__non_neutral,
                text_style__non_neutral,
            ),
        ]
        for case in cases:
            with self.subTest(case.name):
                value_detail = StringDetail('the value detail')
                header_and_value_detail = HeaderAndValueDetail(
                    'the header', [value_detail], case.input_value)
                root = Node('the root', False, [header_and_value_detail], ())

                # EXPECTATION #

                expectation = matches_trace_with_details(
                    root,
                    [
                        matches_detail_line_element(
                            header_and_value_detail.header,
                            depth=0,
                            text_style=case.expected_value),
                        matches_string_detail_line_element(value_detail,
                                                           depth=1),
                    ],
                )

                # ACT & ASSERT #

                _check(self, root, expectation)
 def test_matches(self):
     cases = [
         NIE(
             'default assertion/empty list of blocks',
             input_value=rend_comb.ConstantSequenceR([]),
             expected_value=sut.is_renderer_of_minor_blocks(),
         ),
         NIE(
             'default assertion/non-empty list of blocks',
             input_value=rend_comb.ConstantSequenceR([MinorBlock([])]),
             expected_value=sut.is_renderer_of_minor_blocks(),
         ),
         NIE(
             'custom assertion',
             input_value=rend_comb.ConstantSequenceR([MinorBlock([])]),
             expected_value=sut.is_renderer_of_minor_blocks(
                 asrt.len_equals(1)),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             case.expected_value.apply_without_message(
                 self, case.input_value)
 def test_matches(self):
     cases = [
         NIE(
             'default assertion',
             input_value=ErrorInfo(_ERROR_DESCRIPTION_OF_EMPTY_MESSAGE),
             expected_value=sut.error_info_matches(),
         ),
         NIE(
             'description',
             input_value=ErrorInfo(_ERROR_DESCRIPTION_OF_EMPTY_MESSAGE),
             expected_value=sut.error_info_matches(
                 description=asrt_err_descr.matches_message()),
         ),
         NIE(
             'source location path',
             input_value=ErrorInfo(
                 _ERROR_DESCRIPTION_OF_EMPTY_MESSAGE,
                 source_location_path=ARBITRARY_SOURCE_LOCATION_PATH,
             ),
             expected_value=sut.error_info_matches(
                 source_location_path=asrt.is_instance(SourceLocationPath)),
         ),
         NIE(
             'section_name',
             input_value=ErrorInfo(
                 _ERROR_DESCRIPTION_OF_EMPTY_MESSAGE,
                 section_name=self._SECTION_NAME,
             ),
             expected_value=sut.error_info_matches(
                 section_name=asrt.equals(self._SECTION_NAME)),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             case.expected_value.apply_without_message(
                 self, case.input_value)
 def test_not_matches(self):
     cases = [
         NIE(
             'default assertion/object of unexpected type',
             input_value='not an ErrorInfo',
             expected_value=sut.error_info_matches(),
         ),
         NIE(
             'description',
             input_value=ErrorInfo(_ERROR_DESCRIPTION_OF_EMPTY_MESSAGE),
             expected_value=sut.error_info_matches(
                 description=asrt_err_descr.matches_exception()),
         ),
         NIE(
             'source location path',
             input_value=ErrorInfo(
                 _ERROR_DESCRIPTION_OF_EMPTY_MESSAGE,
                 source_location_path=ARBITRARY_SOURCE_LOCATION_PATH,
             ),
             expected_value=sut.error_info_matches(
                 source_location_path=asrt.is_none),
         ),
         NIE(
             'section_name',
             input_value=ErrorInfo(
                 _ERROR_DESCRIPTION_OF_EMPTY_MESSAGE,
                 section_name=self._SECTION_NAME + 'unexpected',
             ),
             expected_value=sut.error_info_matches(
                 section_name=asrt.equals(self._SECTION_NAME)),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             assert_that_assertion_fails(case.expected_value,
                                         case.input_value)
 def runTest(self):
     # ARRANGE #
     cases = [
         NIE(
             'just simple',
             input_value=_src_of([Pa.name]),
             expected_value=Pa.value,
         ),
         NIE(
             'prefix op of simple',
             input_value=_src_of([POP, Pa.name]),
             expected_value=PrefixOp(Pa.value),
         ),
         NIE(
             'reference',
             input_value=_src_of([REF.name]),
             expected_value=REF.value,
         ),
     ]
     # ACT & ASSERT #
     check__simple_and_full__multi(
         self,
         cases,
     )
Exemple #24
0
def trailing_new_lines_cases_w_leading_space(
) -> List[NIE[List[str], List[str]]]:
    return [
        NIE(
            'single line (non-empty) ended by new-line',
            input_value=['last\n'],
            expected_value=['last'],
        ),
        NIE(
            'single line (empty) ended by new-line',
            input_value=['\n'],
            expected_value=[],
        ),
        NIE(
            'non-empty and empty line',
            input_value=['1\n', '\n'],
            expected_value=['1'],
        ),
        NIE(
            'multiple lines - first non-empty',
            input_value=['1\n', '\n', '\n'],
            expected_value=['1'],
        ),
        NIE(
            'multiple lines - every line empty',
            input_value=['\n', '\n', '\n'],
            expected_value=[],
        ),
        NIE(
            'multiple lines - empty sequence before non-empty contents - ended by new-line',
            input_value=['\n', '\n', 'non-empty\n'],
            expected_value=['\n', '\n', 'non-empty'],
        ),
        NIE(
            'multiple lines - empty sequence before non-empty contents - ended sequence of empty lines',
            input_value=['\n', '\n', 'non-empty\n', '\n', '\n'],
            expected_value=['\n', '\n', 'non-empty'],
        ),
    ]
Exemple #25
0
 def runTest(self):
     cases = [
         NIE(
             'constant 5',
             5,
             StringLiteralAbsStx('5'),
         ),
         NIE(
             'constant 5 / quoted',
             5,
             StringLiteralAbsStx('5', QuoteType.SOFT),
         ),
         NIE(
             'constant 75',
             75,
             StringLiteralAbsStx('75'),
         ),
         NIE(
             'python expr',
             5,
             StringLiteralAbsStx('2+3'),
         ),
         NIE(
             'python expr with space',
             7,
             StringLiteralAbsStx('2*3 + 1', QuoteType.HARD),
         ),
         NIE(
             'python expr len',
             11,
             StringLiteralAbsStx('len("hello world")', QuoteType.HARD),
         ),
     ]
     for case in cases:
         with self.subTest(case.name):
             CHECKER.check__abs_stx__layout_and_source_variants(
                 self,
                 InstructionArgumentsAbsStx.of_int(case.input_value),
                 Arrangement.phase_agnostic(),
                 MultiSourceExpectation.phase_agnostic(
                     instruction_settings=asrt_instr_settings.matches(
                         timeout=asrt.equals(case.expected_value))),
             )
Exemple #26
0
    def test_successful_parse(self):
        name_pattern = 'the name pattern'
        non_matching_name = 'non-matching name'

        glob_pattern_arguments = file_matcher_arguments(
            name_pattern=name_pattern)
        sut_parser = parse_file_matcher.parsers().full
        expected_glob_pattern_matcher_sdv = sut_parser.parse(
            remaining_source(glob_pattern_arguments))

        expected_glob_pattern_matcher = resolving_helper__fake(
        ).resolve_matcher(expected_glob_pattern_matcher_sdv)

        cases = [
            NIE(
                'name pattern in RHS SHOULD give selection of name pattern',
                asrt_matcher.is_equivalent_to(expected_glob_pattern_matcher, [
                    asrt_matcher.ModelInfo(
                        file_matcher_models.FileMatcherModelForDescribedPath(
                            described_path.new_primitive(
                                pathlib.Path(name_pattern)), )),
                    asrt_matcher.ModelInfo(
                        file_matcher_models.FileMatcherModelForDescribedPath(
                            described_path.new_primitive(
                                pathlib.Path(non_matching_name)), )),
                ]),
                glob_pattern_arguments,
            ),
        ]
        # ARRANGE #
        defined_name = 'defined_name'
        argument_cases = [
            NameAndValue('value on same line', '{selector_argument}'),
            NameAndValue('value on following line',
                         '{new_line} {selector_argument}'),
        ]

        for case in cases:
            for argument_case in argument_cases:
                with self.subTest(case.name, arguments=argument_case.name):
                    source = single_line_source(
                        src2(ValueType.FILE_MATCHER,
                             defined_name,
                             argument_case.value,
                             selector_argument=case.input_value), )

                    expected_container = matches_container(
                        asrt.equals(ValueType.FILE_MATCHER),
                        type_sdv_assertions.matches_sdv_of_file_matcher(
                            references=asrt.is_empty_sequence,
                            primitive_value=case.expected_value))

                    expectation = Expectation.phase_agnostic(
                        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 #
                    INSTRUCTION_CHECKER.check(self, source,
                                              Arrangement.phase_agnostic(),
                                              expectation)
Exemple #27
0

def _zero_or_more__choice(choice: str) -> str:
    return '[{}]...'.format(choice)


def _one_or_more__choice(choice: str) -> str:
    return '({})...'.format(choice)


def _choice_w_multiple_args_from_strings(
    multiplicity: arg.Multiplicity,
    choices: List[List[str]],
) -> arg.Choice:
    return arg.Choice.of_multiple_argument_choices(
        multiplicity, [[arg.Named(choice_arg) for choice_arg in choice]
                       for choice in choices])


_CHOICE_MULTIPLE_CHOICE_ARGUMENTS_CASES = [
    NIE('two arguments',
        input_value=[['a1', 'a2'], ['b1', 'b2']],
        expected_value='|'.join(['a1 a2', 'b1 b2'])),
    NIE('mixed number of arguments',
        input_value=[['a'], ['b1', 'b2'], ['c1', 'c2', 'c3']],
        expected_value='|'.join(['a', 'b1 b2', 'c1 c2 c3'])),
]

if __name__ == '__main__':
    unittest.TextTestRunner().run(suite())
    def runTest(self):
        # ARRANGE #
        checked_dir = DirArgumentHelper(RelOptionType.REL_ACT, 'a-dir')

        fc_file_1 = 'file-1'
        fc_file_2 = 'file-2'
        file_name_not_in_fc = 'not-in-fc'

        unconditionally_matching_file_matcher = FileMatcherSymbolContext.of_primitive_constant(
            'unconditionally_matching_file_matcher', True)

        unconditionally_matching_file_matcher_sym_ref_arg = fm_args.SymbolReferenceWReferenceSyntax(
            unconditionally_matching_file_matcher.name)
        unconditionally_matching_file_matcher_sym_ref_assertion = unconditionally_matching_file_matcher.reference_assertion
        symbol_table_with_unconditionally_matching_file_matcher = unconditionally_matching_file_matcher.symbol_table

        files_condition_w_2_files_cases = [
            NIE(
                'no file matcher',
                asrt.is_empty_sequence,
                fc_args.FilesCondition([
                    fc_args.FileCondition(fc_file_1),
                    fc_args.FileCondition(fc_file_2),
                ]),
            ),
            NIE(
                'file matcher on one file',
                asrt.matches_sequence([
                    unconditionally_matching_file_matcher_sym_ref_assertion,
                ]),
                fc_args.FilesCondition([
                    fc_args.FileCondition(
                        fc_file_1,
                        unconditionally_matching_file_matcher_sym_ref_arg),
                    fc_args.FileCondition(fc_file_2),
                ]),
            ),
            NIE(
                'file matcher on all files',
                asrt.matches_sequence([
                    unconditionally_matching_file_matcher_sym_ref_assertion,
                    unconditionally_matching_file_matcher_sym_ref_assertion,
                ]),
                fc_args.FilesCondition([
                    fc_args.FileCondition(
                        fc_file_1,
                        unconditionally_matching_file_matcher_sym_ref_arg),
                    fc_args.FileCondition(
                        fc_file_2,
                        unconditionally_matching_file_matcher_sym_ref_arg),
                ]),
            ),
        ]

        expectation_of_matching_giving_false = PrimAndExeExpectation.of_exe(
            main_result=asrt.equals(False))

        model_contents_cases = [
            NExArr(
                'model is empty',
                expectation_of_matching_giving_false,
                Arrangement(
                    symbols=
                    symbol_table_with_unconditionally_matching_file_matcher,
                    tcds=checked_dir.tcds_arrangement_dir_with_contents([])),
            ),
            NExArr(
                'model contains single file with name in FILES-CONDITION',
                expectation_of_matching_giving_false,
                Arrangement(
                    symbols=
                    symbol_table_with_unconditionally_matching_file_matcher,
                    tcds=checked_dir.tcds_arrangement_dir_with_contents(
                        [File.empty(fc_file_1)])),
            ),
            NExArr(
                'model contains single file with name not in FILES-CONDITION',
                expectation_of_matching_giving_false,
                Arrangement(
                    symbols=
                    symbol_table_with_unconditionally_matching_file_matcher,
                    tcds=checked_dir.tcds_arrangement_dir_with_contents(
                        [File.empty(file_name_not_in_fc)])),
            ),
        ]
        for files_condition_w_2_files_case in files_condition_w_2_files_cases:
            with self.subTest(files_condition_w_2_files_case.name):
                # ACT & ASSERT #
                check_non_full_and_full__multi(
                    self,
                    files_condition_w_2_files_case.input_value,
                    symbol_references=files_condition_w_2_files_case.
                    expected_value,
                    model=model_constructor__non_recursive(
                        checked_dir.path_sdv),
                    execution=model_contents_cases,
                )
Exemple #29
0
    def test_non_empty_chain(self):
        # ARRANGE #

        ls1 = single_line_sequence(1, 'line sequence 1')
        ls2 = single_line_sequence(2, 'line sequence 2')

        referrer_location_sub_dir = Path('referrer-location-sub-dir')
        link_sub_dir = Path('link-sub-dir')
        link_sub_dir2 = Path('link-sub-dir-2')
        base_name = Path('base-name')
        base_name2 = Path('base-name-2')

        cases = [
            NIE('single link. referrer location is HERE, file_path_rel_referrer=None',
                expected_value=FileInclusionChainOutput(
                    block=[sut.line_number(ls1.first_line_number)] +
                          expected_source_line_lines(ls1.lines),
                    next_referrer_location=Path('.'),
                ),
                input_value=FileInclusionChainInput(
                    referrer_location=Path('.'),
                    chain=[
                        SourceLocation(
                            source=ls1,
                            file_path_rel_referrer=None
                        )
                    ]
                ),
                ),
            NIE('single link. referrer location is HERE, file_path_rel_referrer just base name',
                expected_value=FileInclusionChainOutput(
                    block=expected_path_lines(base_name, ls1.first_line_number) +
                          expected_source_line_lines(ls1.lines),
                    next_referrer_location=Path('.'),
                ),
                input_value=FileInclusionChainInput(
                    referrer_location=Path('.'),
                    chain=[
                        SourceLocation(
                            source=ls1,
                            file_path_rel_referrer=base_name
                        )
                    ]
                ),
                ),
            NIE('single link. referrer location is HERE, file_path_rel_referrer in sub dir',
                expected_value=FileInclusionChainOutput(
                    block=expected_path_lines(link_sub_dir / base_name, ls1.first_line_number) +
                          expected_source_line_lines(ls1.lines),
                    next_referrer_location=link_sub_dir,
                ),
                input_value=FileInclusionChainInput(
                    referrer_location=Path('.'),
                    chain=[
                        SourceLocation(
                            source=ls1,
                            file_path_rel_referrer=link_sub_dir / base_name
                        )
                    ]
                ),
                ),
            NIE('single link. referrer location is sub-dir, file_path_rel_referrer in sub dir',
                expected_value=FileInclusionChainOutput(
                    block=expected_path_lines(
                        referrer_location_sub_dir / link_sub_dir / base_name,
                        ls1.first_line_number) +
                          expected_source_line_lines(ls1.lines),
                    next_referrer_location=referrer_location_sub_dir / link_sub_dir,
                ),
                input_value=FileInclusionChainInput(
                    referrer_location=referrer_location_sub_dir,
                    chain=[
                        SourceLocation(
                            source=ls1,
                            file_path_rel_referrer=link_sub_dir / base_name
                        )
                    ]
                ),
                ),
            NIE('multiple links. referrer location is sub-dir, file_path_rel_referrer in sub dir',
                expected_value=FileInclusionChainOutput(
                    block=
                    (expected_path_lines(
                        referrer_location_sub_dir / link_sub_dir / base_name,
                        ls1.first_line_number)
                     +
                     expected_source_line_lines(ls1.lines)
                     +
                     expected_path_lines(
                         referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / base_name2,
                         ls2.first_line_number)
                     +
                     expected_source_line_lines(ls2.lines)
                     ),
                    next_referrer_location=referrer_location_sub_dir / link_sub_dir / link_sub_dir2,
                ),
                input_value=FileInclusionChainInput(
                    referrer_location=referrer_location_sub_dir,
                    chain=[
                        SourceLocation(
                            source=ls1,
                            file_path_rel_referrer=link_sub_dir / base_name,
                        ),
                        SourceLocation(
                            source=ls2,
                            file_path_rel_referrer=link_sub_dir2 / base_name2,
                        ),
                    ]
                ),
                ),
        ]

        formatter = sut.default_formatter()

        for case in cases:
            with self.subTest(case.name):
                # ACT #
                block, referrer_location = formatter.file_inclusion_chain(case.input_value.referrer_location,
                                                                          case.input_value.chain)

                # ASSERT #

                self.assertEqual(case.expected_value.block, block,
                                 'block')

                self.assertEqual(case.expected_value.next_referrer_location,
                                 referrer_location,
                                 'referrer location')
Exemple #30
0
    def test_with_file_inclusion_chain(self):
        # ARRANGE #

        final_loc_ls = single_line_sequence(0, 'line sequence 0')
        final_loc_dir = Path('final-loc-dir')
        final_loc_base_name = Path('final-loc-base-name')
        final_location = SourceLocation(final_loc_ls,
                                        final_loc_dir / final_loc_base_name)
        ls1 = single_line_sequence(1, 'line sequence 1')
        ls2 = single_line_sequence(2, 'line sequence 2')

        referrer_location_sub_dir = Path('referrer-location-sub-dir')
        link_sub_dir = Path('link-sub-dir')
        link_sub_dir2 = Path('link-sub-dir-2')
        base_name = Path('base-name')
        base_name2 = Path('base-name-2')

        cases = [
            NIE('single link. referrer location is HERE, file_path_rel_referrer/inclusion=None',
                expected_value=[
                    (
                            [sut.line_number(ls1.first_line_number)]
                            +
                            expected_source_line_lines(ls1.lines)
                            +
                            expected_path_lines(
                                final_loc_dir / final_loc_base_name,
                                final_loc_ls.first_line_number)
                    ),
                    expected_source_line_lines(final_loc_ls.lines)
                ],
                input_value=SourceLocationPathInput(
                    referrer_location=Path('.'),
                    source_location_path=SourceLocationPath(
                        final_location,
                        [
                            SourceLocation(
                                source=ls1,
                                file_path_rel_referrer=None
                            )
                        ])
                ),
                ),
            NIE('multiple links. referrer location is sub-dir, file_path_rel_referrer in sub dir',
                expected_value=[
                    (expected_path_lines(
                        referrer_location_sub_dir / link_sub_dir / base_name,
                        ls1.first_line_number)
                     +
                     expected_source_line_lines(ls1.lines)
                     +
                     expected_path_lines(
                         referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / base_name2,
                         ls2.first_line_number)
                     +
                     expected_source_line_lines(ls2.lines)
                     +
                     expected_path_lines(
                         referrer_location_sub_dir / link_sub_dir / link_sub_dir2 / final_loc_dir / final_loc_base_name,
                         final_loc_ls.first_line_number)
                     ),
                    expected_source_line_lines(final_loc_ls.lines)
                ],
                input_value=SourceLocationPathInput(
                    referrer_location=referrer_location_sub_dir,
                    source_location_path=SourceLocationPath(
                        final_location,
                        [
                            SourceLocation(
                                source=ls1,
                                file_path_rel_referrer=link_sub_dir / base_name,
                            ),
                            SourceLocation(
                                source=ls2,
                                file_path_rel_referrer=link_sub_dir2 / base_name2,
                            ),
                        ])
                ),
                ),
        ]

        formatter = sut.default_formatter()

        for case in cases:
            with self.subTest(case.name):
                # ACT #
                blocks = formatter.source_location_path(case.input_value.referrer_location,
                                                        case.input_value.source_location_path)

                # ASSERT #

                self.assertEqual(case.expected_value, blocks)