Example #1
0
 def setUp(self):
     help_set_up()
     self.config = ConfigParserWithComments()
     self.config.add_section('Section')
     self.config.set('Section', 'key', 'value')
     with open(test_configfile_path, 'wb') as configfile:
         configfile.write('')
Example #2
0
class TestConfigParserWithComments(unittest.TestCase):
    def setUp(self):
        help_set_up()
        self.config = ConfigParserWithComments()
        self.config.add_section('Section')
        self.config.set('Section', 'key', 'value')
        with open(test_configfile_path, 'wb') as configfile:
            configfile.write('')

    def test_ConfigParser_add_comment(self):
        self.config.add_comment('Section', 'this is the comment')
        self.config.set('Section', 'key', 'value')
        with open(test_configfile_path, 'wb') as configfile:
            self.config.write(configfile)
        with open(test_configfile_path, 'rb') as configfile:
            file_content = configfile.readlines()
            self.assertEqual(file_content[0], '[Section]\n')
            self.assertEqual(file_content[1], 'key = value\n')
            self.assertEqual(file_content[2], '# this is the comment\n')

    def test_ConfigParser_start_del_comment(self):
        self.config.add_comment('Section', 'this is the comment')
        self.assertTrue(self.config.del_comment('Section', 'this is the comment'))
        self.assertFalse(self.config.del_comment('Section', 'this is the comment'))
        self.assertRaises(NoSectionError, self.config.del_comment, *('Section2', 'this is the comment'))

    def test_ConfigParser_add_start_comment(self):
        self.config.set_start_comment('# moj komentarz\n# linia2')
        with open(test_configfile_path, 'wb') as configfile:
            self.config.write(configfile)
        with open(test_configfile_path, 'rb') as configfile:
            file_content = configfile.readlines()
            self.assertEqual(file_content[0], '# moj komentarz\n')
            self.assertEqual(file_content[1], '# linia2\n')
            self.assertEqual(file_content[2], '[Section]\n')

    def test_ConfigParser_add_start_comment_raises_error(self):
        self.assertRaises(TypeError, self.config.set_start_comment, 1)
        self.assertRaises(ConfigParserBadCommentError, self.config.set_start_comment, 'comm')
        self.assertRaises(ConfigParserBadCommentError, self.config.set_start_comment, 'comm\ncomme2')

    def test_ConfigParser_start_get_start_comment(self):
        self.config.set_start_comment('# moj komentarz\n# linia2')
        my_comment = self.config.get_start_comment()
        self.assertEqual(my_comment, '# moj komentarz\n# linia2')

    def test_ConfigParser_start_del_start_comment(self):
        self.config.set_start_comment('# moj komentarz\n# linia2')
        self.config.del_start_comment()
        self.assertFalse(hasattr(self, '_start_comment_str'))

    def test_ConfigParser_start_del_start_comment_raises_error(self):
        self.assertRaises(ConfigParserLackOfStartCommentError, self.config.del_start_comment)

    def test_ConfigParser_start_get_start_comment_raises_error(self):
        self.assertRaises(ConfigParserLackOfStartCommentError, self.config.get_start_comment)

    def tearDown(self):
        help_tear_down()