예제 #1
0
class ReadmeReadFileObjTests(unittest.TestCase):
    """
    Testing reading our README files
    """
    def setUp(self):
        """
        Initialize some vars we'll need on every test.
        """
        self.readme = Readme(0)
        self.df = io.StringIO()

    def set_df_contents(self, lines):
        """
        Sets the contents of the "file" that we're gonna read in.
        """
        for line in lines:
            print(line, file=self.df)
        self.df.seek(0)

    def read(self):
        """
        Read in our file.  Note that the `is_markdown` flag is
        currently not actually used, so we're not bothering to
        test for it at all.
        """
        self.readme.read_file_obj(self.df, False)

    def test_load_empty(self):
        self.set_df_contents([])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
        })

    def test_initial_comment(self):
        self.set_df_contents([
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_two_comments(self):
        self.set_df_contents([
            'Testing',
            'Testing 2',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing', 'Testing 2'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_hash_section(self):
        for hashes in ['#', '##', '###', '####']:
            with self.subTest(hashes=hashes):
                self.set_df_contents([
                    '{} Section'.format(hashes),
                    'Testing',
                ])
                self.read()
                self.assertEqual(self.readme.mapping, {
                    '(default)': [],
                    'section': ['Testing'],
                })
                self.assertEqual(self.readme.first_section, 'section')

    def test_default_and_one_hash_section(self):
        self.set_df_contents([
            'Initial',
            '# Section',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Initial'],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_one_hash_section_two_lines(self):
        self.set_df_contents([
            '# Section',
            'Testing',
            'Testing 2',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing', 'Testing 2'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_double_underline_section(self):
        self.set_df_contents([
            'Section',
            '=======',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_single_underline_section(self):
        self.set_df_contents([
            'Section',
            '-------',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_initial_double_line(self):
        self.set_df_contents([
            '=======',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['=======', 'Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_single_line(self):
        self.set_df_contents([
            '-------',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['-------', 'Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_dash_section(self):
        self.set_df_contents([
            '- Section',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_sections(self):
        self.set_df_contents([
            '- Section',
            'Testing',
            '# Section 2',
            'More Testing',
        ])
        self.read()
        self.assertEqual(
            self.readme.mapping, {
                '(default)': [],
                'section': ['Testing'],
                'section 2': ['More Testing'],
            })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_sections_multiline(self):
        self.set_df_contents([
            'Initial',
            'Text',
            '- Section',
            'Testing',
            'Foo',
            '# Section 2',
            'More Testing',
            'Bar',
        ])
        self.read()
        self.assertEqual(
            self.readme.mapping, {
                '(default)': ['Initial', 'Text'],
                'section': ['Testing', 'Foo'],
                'section 2': ['More Testing', 'Bar'],
            })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_default_blank(self):
        self.set_df_contents([
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_default_blank_double(self):
        self.set_df_contents([
            '',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_initial_section_blank(self):
        self.set_df_contents([
            '- Section',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_initial_section_blank_double(self):
        self.set_df_contents([
            '- Section',
            '',
            '',
            'Testing',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_trailing_blank(self):
        self.set_df_contents([
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_trailing_section_blank(self):
        self.set_df_contents([
            '# Section',
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': [],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, 'section')

    def test_two_trailing_section_blank(self):
        self.set_df_contents([
            'Main',
            '',
            '# Section',
            'Testing',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Main'],
            'section': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')

    def test_double_trailing_blank(self):
        self.set_df_contents([
            'Testing',
            '',
            '',
        ])
        self.read()
        self.assertEqual(self.readme.mapping, {
            '(default)': ['Testing'],
        })
        self.assertEqual(self.readme.first_section, '(default)')
class ReadmeReadFindMatchingTests(unittest.TestCase):
    """
    Testing finding matching entries in README files
    """
    def setUp(self):
        """
        Initialize some vars we'll need on every test.
        """
        self.readme = Readme(0)
        self.df = io.StringIO()

    def set_df_contents(self, lines):
        """
        Sets the contents of the "file" that we're gonna read in.
        """
        for line in lines:
            print(line, file=self.df)
        self.df.seek(0)

    def read(self, lines):
        """
        Read in our file.  Note that the `is_markdown` flag is
        currently not actually used, so we're not bothering to
        test for it at all.
        """
        self.set_df_contents(lines)
        self.readme.read_file_obj(self.df, False)

    def test_single_mod_overview(self):
        self.read([
            'Beginning Text',
            '# Overview',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_exact(self):
        self.read([
            'Beginning Text',
            '# xyzzy',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_fuzzy(self):
        self.read([
            'Beginning Text',
            '# xyzzyz',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_default(self):
        self.read([
            'Beginning Text',
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Beginning Text'])

    def test_single_mod_first_section(self):
        self.read([
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         ['Testing'])

    def test_single_mod_empty(self):
        self.read([])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=True),
                         [])

    def test_multi_mod_exact(self):
        self.read([
            'Beginning Text',
            '# xyzzy',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Testing'])

    def test_multi_mod_fuzzy(self):
        self.read([
            'Beginning Text',
            '# xyzzyz',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Testing'])

    def test_multi_mod_nomatch(self):
        self.read([
            'Beginning Text',
            '# Unrelated',
            'Testing',
            '# Foobar',
            'Not Matched',
        ])
        # Unlike the BL2/TPS cabinet, multi-mod matches *will* include the
        # first category.
        self.assertEqual(self.readme.find_matching('xyzzy', single_mod=False),
                         ['Beginning Text'])