diff options
author | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2008-05-30 17:10:19 +0200 |
---|---|---|
committer | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2008-05-30 17:10:19 +0200 |
commit | 56868bdc7e1631fd9f945337649654e3bd2da91c (patch) | |
tree | 5d1b8d41efd42cce02b56929ec5e560627a2ae97 /test/unittest_textutils.py | |
parent | 8e88ddfbd9e655b63bcdc691d781f3a7ae51dde5 (diff) | |
download | logilab-common-56868bdc7e1631fd9f945337649654e3bd2da91c.tar.gz |
add the apply_units function
Diffstat (limited to 'test/unittest_textutils.py')
-rw-r--r-- | test/unittest_textutils.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/test/unittest_textutils.py b/test/unittest_textutils.py index 535052b..c878c0f 100644 --- a/test/unittest_textutils.py +++ b/test/unittest_textutils.py @@ -103,6 +103,65 @@ class GetCsvTC(TestCase): def test_known(self): self.assertEquals(tu.get_csv('a, b,c '), ['a', 'b', 'c']) +class UnitsTC(TestCase): + + def setUp(self): + self.units = { + 'm':60, + 'KB':1024, + 'MB':1024*1024, + } + + def test_empty_base(self): + self.assertEquals(tu.apply_units('17', {}), 17) + + def test_empty_inter(self): + def inter(value): + return int(float(value)) * 2 + result = tu.apply_units('12.4', {}, inter=inter) + self.assertEquals(result, 12 * 2) + self.assertIsInstance(result, float) + + def test_empty_final(self): + # int('12.4') raise value error + self.assertRaises(ValueError, tu.apply_units,'12.4', {}, final=int) + + def test_empty_inter_final(self): + result = tu.apply_units('12.4', {}, inter=float,final=int) + self.assertEquals(result, 12) + self.assertIsInstance(result, int) + + def test_blank_base(self): + result = tu.apply_units(' 42 ', {}, final=int) + self.assertEquals(result, 42) + + def test_blank_space(self): + result = tu.apply_units(' 1 337 ', {}, final=int) + self.assertEquals(result, 1337) + + def test_blank_coma(self): + result = tu.apply_units(' 4,298.42 ', {}) + self.assertEquals(result, 4298.42) + + def test_blank_mixed(self): + result = tu.apply_units('45, 317, 337', {},final=int) + self.assertEquals(result, 45317337) + + def test_unit_singleunit_singleletter(self): + result = tu.apply_units('15m', self.units) + self.assertEquals(result, 15 * self.units['m'] ) + + def test_unit_singleunit_multipleletter(self): + result = tu.apply_units('47KB', self.units) + self.assertEquals(result, 47 * self.units['KB'] ) + + def test_unit_multipleunit(self): + result = tu.apply_units('47KB 1.5MB', self.units) + self.assertEquals(result, 47 * self.units['KB'] + 1.5 * self.units['MB']) + + def test_unit_with_blank(self): + result = tu.apply_units('1 000 KB', self.units) + self.assertEquals(result, 1000 * self.units['KB']) RGX = re.compile('abcd') class PrettyMatchTC(TestCase): |