Beispiel #1
0
    def test_valid_section_with_fragmented_sections(self):
        # ARRANGE #
        source_lines = ['[section 1]',
                        'COMMENT 1',
                        '',
                        '[section 2]',
                        'instruction 2',
                        '[section 1]',
                        'instruction 1'
                        ]
        parser = parser_for_sections(['section 1', 'section 2'])
        expected = {
            'section 1': [
                equals_comment_element(2, 'COMMENT 1'),
                equals_empty_element(3, ''),
                equals_instruction_without_description(7, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                       NO_FILE_INCLUSIONS)
            ],
            'section 2': [
                equals_instruction_without_description(5, 'instruction 2', 'section 2', EXPECTED_SOURCE_FILE_PATH,
                                                       NO_FILE_INCLUSIONS),

            ]
        }
        # ACT & ASSERT #
        self._parse_and_check(parser, source_lines, expected)
Beispiel #2
0
 def test_valid_default_and_named_section(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'default'],
                                  default_section_name='default')
     source_lines = ['COMMENT default',
                     '',
                     'instruction default',
                     '[section 1]',
                     'COMMENT 1',
                     'instruction 1']
     default_section_contents = (
         equals_comment_element(1, 'COMMENT default'),
         equals_empty_element(2, ''),
         equals_instruction_without_description(3, 'instruction default', 'default', EXPECTED_SOURCE_FILE_PATH,
                                                NO_FILE_INCLUSIONS)
     )
     section1_contents = (
         equals_comment_element(5, 'COMMENT 1'),
         equals_instruction_without_description(6, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                NO_FILE_INCLUSIONS)
     )
     expected = {
         'default': default_section_contents,
         'section 1': section1_contents,
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #3
0
 def test_valid_default_and_named_section(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'default'],
                                  default_section_name='default')
     source_lines = [
         'COMMENT default', '', 'instruction default', '[section 1]',
         'COMMENT 1', 'instruction 1'
     ]
     default_section_contents = (equals_comment_element(
         1, 'COMMENT default'), equals_empty_element(2, ''),
                                 equals_instruction_without_description(
                                     3, 'instruction default', 'default',
                                     EXPECTED_SOURCE_FILE_PATH,
                                     NO_FILE_INCLUSIONS))
     section1_contents = (equals_comment_element(5, 'COMMENT 1'),
                          equals_instruction_without_description(
                              6, 'instruction 1', 'section 1',
                              EXPECTED_SOURCE_FILE_PATH,
                              NO_FILE_INCLUSIONS))
     expected = {
         'default': default_section_contents,
         'section 1': section1_contents,
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #4
0
 def test_mix_of_instructions_without_default_section(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1'])
     source_lines = ['',
                     '[section 1]',
                     'instruction 1',
                     'MULTI-LINE-INSTRUCTION 2-1',
                     'MULTI-LINE-INSTRUCTION 2-2',
                     'MULTI-LINE-INSTRUCTION 2-3',
                     'instruction 3']
     expected = {
         'section 1': [
             equals_instruction_without_description(3, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS),
             equals_multi_line_instruction_without_description(4,
                                                               ['MULTI-LINE-INSTRUCTION 2-1',
                                                                'MULTI-LINE-INSTRUCTION 2-2',
                                                                'MULTI-LINE-INSTRUCTION 2-3'],
                                                               'section 1',
                                                               EXPECTED_SOURCE_FILE_PATH,
                                                               NO_FILE_INCLUSIONS),
             equals_instruction_without_description(7, 'instruction 3', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS),
         ],
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #5
0
    def test_single_multi_line_instruction_in_default_section_ended_by_section_header(self):
        # ARRANGE #
        parser = parser_for_sections(['section 1', 'default'],
                                     default_section_name='default')
        source_lines = ['',
                        'MULTI-LINE-INSTRUCTION 1',
                        'MULTI-LINE-INSTRUCTION 2',
                        '[section 1]',
                        'instruction 1',
                        ]
        expected = {
            'default': [
                equals_empty_element(1, ''),
                equals_multi_line_instruction_without_description(2,
                                                                  ['MULTI-LINE-INSTRUCTION 1',
                                                                   'MULTI-LINE-INSTRUCTION 2'],
                                                                  'default',
                                                                  EXPECTED_SOURCE_FILE_PATH,
                                                                  NO_FILE_INCLUSIONS),

            ],
            'section 1': [
                equals_instruction_without_description(5, 'instruction 1',
                                                       'section 1',
                                                       EXPECTED_SOURCE_FILE_PATH,
                                                       NO_FILE_INCLUSIONS),
            ],
        }
        # ACT & ASSERT #
        self._parse_and_check(parser, source_lines, expected)
Beispiel #6
0
 def test_not_matches(self):
     # ARRANGE #
     expected_line_num = 2
     expected_line_text = 'line text'
     expected_section_name = 'section name'
     expected_file_path = pathlib.Path('a path')
     expected_file_inclusion_chain = [
         SourceLocation(single_line_sequence(2, 'inclusion line'),
                        pathlib.Path('inclusion file path'))
     ]
     expected_abs_path_of_dir_containing_file = pathlib.Path(
         pathlib.Path.cwd().root)
     assertion = sut.equals_instruction_without_description(
         expected_line_num, expected_line_text, expected_section_name,
         expected_file_path, expected_file_inclusion_chain)
     cases = [
         NameAndValue(
             'unexpected line num',
             new_instruction(expected_line_num + 1, expected_line_text,
                             expected_section_name, expected_file_path,
                             expected_file_inclusion_chain)),
         NameAndValue(
             'unexpected line text',
             new_instruction(expected_line_num,
                             expected_line_text + ' unexpected',
                             expected_section_name, expected_file_path,
                             expected_file_inclusion_chain)),
         NameAndValue(
             'unexpected section name',
             new_instruction(expected_line_num, expected_line_text,
                             expected_section_name + ' unexpected',
                             expected_file_path,
                             expected_file_inclusion_chain)),
         NameAndValue(
             'unexpected file path',
             new_instruction(expected_line_num, expected_line_text,
                             expected_section_name,
                             expected_file_path / 'unexpected',
                             expected_file_inclusion_chain)),
         NameAndValue(
             'unexpected abs path of dir containing file',
             new_instruction(expected_line_num, expected_line_text,
                             expected_section_name,
                             expected_file_path / 'unexpected',
                             expected_file_inclusion_chain)),
         NameAndValue(
             'unexpected file inclusion chain',
             new_instruction(expected_line_num, expected_line_text,
                             expected_section_name, expected_file_path,
                             [])),
         NameAndValue('unexpected element type - empty',
                      new_empty(expected_line_num, expected_line_text)),
         NameAndValue('unexpected element type - comment',
                      new_comment(expected_line_num, expected_line_text)),
     ]
     for nav in cases:
         with self.subTest(nav.name):
             # ACT & ASSERT #
             assert_that_assertion_fails(assertion, nav.value)
Beispiel #7
0
 def test_valid_section_with_fragmented_sections(self):
     # ARRANGE #
     source_lines = [
         '[section 1]', 'COMMENT 1', '', '[section 2]', 'instruction 2',
         '[section 1]', 'instruction 1'
     ]
     parser = parser_for_sections(['section 1', 'section 2'])
     expected = {
         'section 1': [
             equals_comment_element(2, 'COMMENT 1'),
             equals_empty_element(3, ''),
             equals_instruction_without_description(
                 7, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                 NO_FILE_INCLUSIONS)
         ],
         'section 2': [
             equals_instruction_without_description(
                 5, 'instruction 2', 'section 2', EXPECTED_SOURCE_FILE_PATH,
                 NO_FILE_INCLUSIONS),
         ]
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #8
0
 def test_valid_section_with_comment_and_instruction(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'section 2'])
     source_lines = ['[section 1]', 'COMMENT', 'instruction']
     expected = {
         'section 1': [
             equals_comment_element(2, 'COMMENT'),
             equals_instruction_without_description(
                 3, 'instruction', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                 NO_FILE_INCLUSIONS)
         ]
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #9
0
 def test_the_instruction_parser_for_the_current_section_should_be_used(self):
     # ARRANGE #
     parser = parser_for_section2_that_fails_unconditionally()
     source_lines = ['[section 1]',
                     'instruction 1',
                     ]
     expected = {
         'section 1': [
             equals_instruction_without_description(2, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS),
         ]
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #10
0
 def test_single_multi_line_instruction_that_is_actually_only_a_single_line_in_default_section(self):
     # ARRANGE #
     parser = parser_for_sections(['default'],
                                  default_section_name='default')
     source_lines = ['MULTI-LINE-INSTRUCTION 1'
                     ]
     expected = {
         'default': [
             equals_instruction_without_description(1, 'MULTI-LINE-INSTRUCTION 1', 'default',
                                                    EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS),
         ],
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #11
0
 def test_valid_section_with_comment_and_instruction(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'section 2'])
     source_lines = ['[section 1]',
                     'COMMENT',
                     'instruction'
                     ]
     expected = {
         'section 1': [
             equals_comment_element(2, 'COMMENT'),
             equals_instruction_without_description(3, 'instruction', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS)
         ]
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #12
0
 def test_the_instruction_parser_for_the_current_section_should_be_used(
         self):
     # ARRANGE #
     parser = parser_for_section2_that_fails_unconditionally()
     source_lines = [
         '[section 1]',
         'instruction 1',
     ]
     expected = {
         'section 1': [
             equals_instruction_without_description(
                 2, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                 NO_FILE_INCLUSIONS),
         ]
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #13
0
 def test_matches(self):
     # ARRANGE #
     expected_line_num = 1
     expected_line_text = 'line text'
     expected_section_name = 'section name'
     expected_file_path = pathlib.Path('a path')
     expected_file_inclusion_chain = [
         SourceLocation(single_line_sequence(2, 'inclusion line'),
                        pathlib.Path('inclusion file path'))
     ]
     assertion = sut.equals_instruction_without_description(
         expected_line_num, expected_line_text, expected_section_name,
         expected_file_path, expected_file_inclusion_chain)
     actual = new_instruction(expected_line_num, expected_line_text,
                              expected_section_name, expected_file_path,
                              expected_file_inclusion_chain)
     # ACT & ASSERT #
     assertion.apply_without_message(self, actual)
 def test_matches(self):
     # ARRANGE #
     expected_line_num = 1
     expected_line_text = 'line text'
     expected_section_name = 'section name'
     expected_file_path = pathlib.Path('a path')
     expected_file_inclusion_chain = [SourceLocation(single_line_sequence(2, 'inclusion line'),
                                                     pathlib.Path('inclusion file path'))]
     assertion = sut.equals_instruction_without_description(expected_line_num,
                                                            expected_line_text,
                                                            expected_section_name,
                                                            expected_file_path,
                                                            expected_file_inclusion_chain)
     actual = new_instruction(expected_line_num,
                              expected_line_text,
                              expected_section_name,
                              expected_file_path,
                              expected_file_inclusion_chain)
     # ACT & ASSERT #
     assertion.apply_without_message(self, actual)
Beispiel #15
0
 def test_initial_empty_lines_and_comment_lines_should_be_ignored_when_there_is_no_default_section(self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'section 2'])
     source_lines = ['# standard-comment default',
                     '',
                     '[section 1]',
                     'COMMENT 1',
                     '',
                     'instruction 1',
                     ]
     expected = {
         'section 1': [
             equals_comment_element(4, 'COMMENT 1'),
             equals_empty_element(5, ''),
             equals_instruction_without_description(6, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                                                    NO_FILE_INCLUSIONS),
         ],
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
Beispiel #16
0
 def test_initial_empty_lines_and_comment_lines_should_be_ignored_when_there_is_no_default_section(
         self):
     # ARRANGE #
     parser = parser_for_sections(['section 1', 'section 2'])
     source_lines = [
         '# standard-comment default',
         '',
         '[section 1]',
         'COMMENT 1',
         '',
         'instruction 1',
     ]
     expected = {
         'section 1': [
             equals_comment_element(4, 'COMMENT 1'),
             equals_empty_element(5, ''),
             equals_instruction_without_description(
                 6, 'instruction 1', 'section 1', EXPECTED_SOURCE_FILE_PATH,
                 NO_FILE_INCLUSIONS),
         ],
     }
     # ACT & ASSERT #
     self._parse_and_check(parser, source_lines, expected)
 def test_not_matches(self):
     # ARRANGE #
     expected_line_num = 2
     expected_line_text = 'line text'
     expected_section_name = 'section name'
     expected_file_path = pathlib.Path('a path')
     expected_file_inclusion_chain = [SourceLocation(single_line_sequence(2, 'inclusion line'),
                                                     pathlib.Path('inclusion file path'))]
     expected_abs_path_of_dir_containing_file = pathlib.Path(pathlib.Path.cwd().root)
     assertion = sut.equals_instruction_without_description(expected_line_num,
                                                            expected_line_text,
                                                            expected_section_name,
                                                            expected_file_path,
                                                            expected_file_inclusion_chain)
     cases = [
         NameAndValue('unexpected line num',
                      new_instruction(expected_line_num + 1,
                                      expected_line_text,
                                      expected_section_name,
                                      expected_file_path,
                                      expected_file_inclusion_chain)
                      ),
         NameAndValue('unexpected line text',
                      new_instruction(expected_line_num,
                                      expected_line_text + ' unexpected',
                                      expected_section_name,
                                      expected_file_path,
                                      expected_file_inclusion_chain)
                      ),
         NameAndValue('unexpected section name',
                      new_instruction(expected_line_num,
                                      expected_line_text,
                                      expected_section_name + ' unexpected',
                                      expected_file_path,
                                      expected_file_inclusion_chain)
                      ),
         NameAndValue('unexpected file path',
                      new_instruction(expected_line_num,
                                      expected_line_text,
                                      expected_section_name,
                                      expected_file_path / 'unexpected',
                                      expected_file_inclusion_chain)
                      ),
         NameAndValue('unexpected abs path of dir containing file',
                      new_instruction(expected_line_num,
                                      expected_line_text,
                                      expected_section_name,
                                      expected_file_path / 'unexpected',
                                      expected_file_inclusion_chain)
                      ),
         NameAndValue('unexpected file inclusion chain',
                      new_instruction(expected_line_num,
                                      expected_line_text,
                                      expected_section_name,
                                      expected_file_path,
                                      [])
                      ),
         NameAndValue('unexpected element type - empty',
                      new_empty(expected_line_num, expected_line_text)
                      ),
         NameAndValue('unexpected element type - comment',
                      new_comment(expected_line_num, expected_line_text)
                      ),
     ]
     for nav in cases:
         with self.subTest(nav.name):
             # ACT & ASSERT #
             assert_that_assertion_fails(assertion, nav.value)