| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# This program is free software; you can redistribute it and/or modify |
| 3 |
# it under the terms of the GNU General Public License as published by |
| 4 |
# the Free Software Foundation; either version 2 of the License, or |
| 5 |
# (at your option) any later version. |
| 6 |
# |
| 7 |
# This program is distributed in the hope that it will be useful, |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
# GNU General Public License for more details. |
| 11 |
# |
| 12 |
# You should have received a copy of the GNU General Public License |
| 13 |
# along with this program; if not, write to the Free Software |
| 14 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 15 |
# MA 02110-1301, USA. |
| 16 |
# |
| 17 |
# Author: Mauro Soria |
| 18 |
|
| 19 |
import io |
| 20 |
|
| 21 |
from unittest import TestCase |
| 22 |
|
| 23 |
from lib.parse.config import ConfigParser |
| 24 |
|
| 25 |
|
| 26 |
config_data = """ |
| 27 |
[test] |
| 28 |
string = foo |
| 29 |
integer = 1 |
| 30 |
float = 2.7 |
| 31 |
boolean = True |
| 32 |
list = ["foo", "bar"] |
| 33 |
list2 = test |
| 34 |
""" |
| 35 |
config = ConfigParser() |
| 36 |
config.read_file(io.StringIO(config_data)) |
| 37 |
|
| 38 |
|
| 39 |
class TestConfigParser(TestCase): |
| 40 |
def test_safe_get(self): |
| 41 |
self.assertEqual(config.safe_get("test", "string"), "foo") |
| 42 |
self.assertEqual(config.safe_get("non-existent", "string", default="default"), "default") |
| 43 |
self.assertEqual(config.safe_get("test", "non-existent", default="default"), "default") |
| 44 |
self.assertEqual(config.safe_get("test", "string", default="default", allowed=("bar",)), "default") |
| 45 |
|
| 46 |
def test_safe_getint(self): |
| 47 |
self.assertEqual(config.safe_getint("test", "integer"), 1) |
| 48 |
|
| 49 |
def test_safe_getfloat(self): |
| 50 |
self.assertEqual(config.safe_getfloat("test", "float"), 2.7) |
| 51 |
|
| 52 |
def test_safe_getboolean(self): |
| 53 |
self.assertEqual(config.safe_getboolean("test", "boolean"), True) |
| 54 |
|
| 55 |
def test_safe_getlist(self): |
| 56 |
self.assertEqual(config.safe_getlist("test", "list"), ["foo", "bar"]) |
| 57 |
self.assertEqual(config.safe_getlist("test", "list2"), ["test"]) |
| 58 |
self.assertEqual(config.safe_getlist("test", "list", default=["default"], allowed=("foo",)), ["default"]) |