# Copyright (C) 2010 Leonard Thomas # # This file is part of Dodai. # # Dodai is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # Dodai is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Dodai. If not, see . import sys import os import ConfigParser import unittest path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', '..')) sys.path.append(path) from dodai.tools.himo import Himo from dodai.tools.himo import String2Himo from dodai.config.sections import ConfigSections class TestSections(unittest.TestCase): def setUp(self): path = os.path.dirname(__file__) filepath = os.path.join(path, 'config.cfg') self.parser = ConfigParser.ConfigParser() self.parser.readfp(open(filepath)) self.sections = ConfigSections(String2Himo()) def test_call(self): self.sections(self.parser) count = len(self.parser.sections()) self.assertEqual(count, len(self.sections)) def test_iterator(self): result = True for section in self.sections: if not section.get_title() in self.parser.sections(): result = False self.assertTrue(result==True) def test_get_section(self): self.sections(self.parser) self.sections(self.parser) check = self.sections.get_section('test_db') self.assertEqual(check.get_title(), 'test_db') def test_build_string_object(self): sections = ConfigSections() sections(self.parser) count = len(self.parser.sections()) self.assertEqual(count, len(sections)) def test_getitem(self): self.sections(self.parser) title = self.sections['test_db'].get_title() self.assertEqual(title, 'test_db') def test_getattr(self): self.sections(self.parser) title = self.sections.test_db.get_title() self.assertEqual(title, 'test_db') def test_get_keys(self): self.sections(self.parser) keys = self.sections.keys() self.assertTrue(len(keys)) def test_section_object_one(self): self.sections(self.parser) keys = self.sections.test_db.keys() self.assertTrue(len(keys)) def test_section_object_two(self): self.sections(self.parser) keys = self.sections.test_db.___options___.keys() self.assertTrue(len(keys)) def test_section_object_three(self): self.sections(self.parser) self.sections.test_db.___blah___ = 'test' val = self.sections.test_db.__getattr__('___blah___') self.assertTrue(val == 'test') def test_section_object_four(self): self.sections(self.parser) self.sections.test_db.foo = 'bar' val = self.sections.test_db.__getattr__('foo') self.assertTrue(val == 'bar') def test_section_object_five(self): self.sections(self.parser) keys = [] for key in self.sections.test_db: keys.append(key) self.assertTrue(keys) def test_section_object_six(self): self.sections(self.parser) self.sections.test_db.foo = 'bar' val = self.sections.test_db['foo'] self.assertTrue(val == 'bar') def test_section_object_seven(self): self.sections(self.parser, 'unicode_escape') self.assertEqual(self.sections.extra.name, u'\u8c61') if __name__ == '__main__': unittest.main()