From: philipp Date: Tue, 2 Feb 2016 21:10:02 +0000 (+0000) Subject: Implemented some missing test cases. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wrpylib.git/commitdiff_plain/5eac54985c8b2b3ff7d2be7e3df77ed269d3c6f8?ds=sidebyside Implemented some missing test cases. git-svn-id: http://www.winterrodeln.org/svn/wrpylib/trunk@2447 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/tests/test_wrvalidators.py b/tests/test_wrvalidators.py index a132df9..d6e0311 100644 --- a/tests/test_wrvalidators.py +++ b/tests/test_wrvalidators.py @@ -5,9 +5,23 @@ import wrpylib.wrvalidators import unittest from wrpylib.wrvalidators import * -# TODO: optional converter +# optional converter # ------------------ +class TestOpt(unittest.TestCase): + def test_from_str(self): + self.assertEqual(None, opt_from_str('', str_from_str)) + self.assertEqual('abc', opt_from_str('abc', str_from_str)) + self.assertEqual(None, opt_from_str('', int_from_str)) + self.assertEqual(4, opt_from_str('4', int_from_str)) + + def test_to_str(self): + self.assertEqual('', opt_to_str(None, str_to_str)) + self.assertEqual('abc', opt_to_str('abc', str_to_str)) + self.assertEqual('', opt_to_str(None, int_to_str)) + self.assertEqual('4', opt_to_str(4, int_to_str)) + + # "no" converter # -------------- @@ -38,12 +52,36 @@ class TestOptNoGerman(unittest.TestCase): self.assertEqual('', opt_no_german_to_str((None, None), str_to_str)) -# TODO: choice converter +# choice converter # ---------------- +class TestCoice(unittest.TestCase): + def setUp(self): + self.choices = ['abc', 'def', 'ghi'] -# TODO: dict converter -# -------------- + def from_str(self): + self.assertEqual('abc', choice_from_str('abc', self.choices)) + self.assertEqual('ghi', choice_from_str('ghi', self.choices)) + with self.assertRaises(ValueError): + choice_from_str('jkl', self.choices) + + +# dictkey converter +# ----------------- + +class TestDictkey(unittest.TestCase): + def setUp(self): + self.choices = {'abc': '1', 'def': '2', 'ghi': '3'} + + def test_from_str(self): + self.assertEqual('abc', dictkey_from_str('1', self.choices)) + self.assertEqual('ghi', dictkey_from_str('3', self.choices)) + with self.assertRaises(ValueError): + dictkey_from_str('4', self.choices) + + def test_to_str(self): + self.assertEqual('1', dictkey_to_str('abc', self.choices)) + self.assertEqual('3', dictkey_to_str('ghi', self.choices)) # enum/"list" converter @@ -702,7 +740,7 @@ class TestRodelbahnbox(unittest.TestCase): self.assertEqual(self.value, rodelbahnbox_to_str(value)) -# TODO: Gasthausbox converter +# Gasthausbox converter # --------------------- class TestGasthausbox(unittest.TestCase): diff --git a/wrpylib/wrvalidators.py b/wrpylib/wrvalidators.py index 61a961d..a36c791 100644 --- a/wrpylib/wrvalidators.py +++ b/wrpylib/wrvalidators.py @@ -86,8 +86,8 @@ def choice_from_str(value, choices): return value -# dict converter -# -------------- +# dictkey converter +# ----------------- def dictkey_from_str(value, key_str_dict): """Returns the key of an entry in the key_str_dict if the value of the entry corresponds to the given value."""