summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2010-07-04 10:21:35 +1200
committerLars Wirzenius <liw@liw.fi>2010-07-04 10:21:35 +1200
commit501d2c92b84106e4eef95cba2d419d90e1692393 (patch)
treeb0d3378a4aaf06c41f3d769222b9b647d9c1b8d8
parente59555fd1d83c77c39b882024f393b9c901bda9f (diff)
downloadobnam-501d2c92b84106e4eef95cba2d419d90e1692393.tar.gz
Add tests for a library to parse file sizes.
-rw-r--r--obnamlib/__init__.py2
-rw-r--r--obnamlib/sizeparse.py28
-rw-r--r--obnamlib/sizeparse_tests.py57
3 files changed, 87 insertions, 0 deletions
diff --git a/obnamlib/__init__.py b/obnamlib/__init__.py
index 79bb17ef..b0870df4 100644
--- a/obnamlib/__init__.py
+++ b/obnamlib/__init__.py
@@ -29,6 +29,8 @@ class Error(Exception):
CHUNK_SIZE = 4096
CHUNK_GROUP_SIZE = 16
+from sizeparse import ByteSizeParser
+
from hooks import Hook, HookManager
from cfg import Configuration
from interp import Interpreter
diff --git a/obnamlib/sizeparse.py b/obnamlib/sizeparse.py
new file mode 100644
index 00000000..d0649646
--- /dev/null
+++ b/obnamlib/sizeparse.py
@@ -0,0 +1,28 @@
+# Copyright 2010 Lars Wirzenius
+#
+# This program 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.
+#
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+class ByteSizeParser(object):
+
+ '''Parse sizes of data in bytes, kilobytes, kibibytes, etc.'''
+
+ def __init__(self):
+ pass
+
+ def set_default_unit(self, unit):
+ pass
+
+ def parse(self, string):
+ pass
diff --git a/obnamlib/sizeparse_tests.py b/obnamlib/sizeparse_tests.py
new file mode 100644
index 00000000..85ddcf55
--- /dev/null
+++ b/obnamlib/sizeparse_tests.py
@@ -0,0 +1,57 @@
+# Copyright 2010 Lars Wirzenius
+#
+# This program 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.
+#
+# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+import unittest
+
+import obnamlib
+
+
+class ByteSizeParserTests(unittest.TestCase):
+
+ def setUp(self):
+ self.p = obnamlib.ByteSizeParser()
+
+ def test_parses_zero(self):
+ self.assertEqual(self.p.parse('0'), 0)
+
+ def test_parses_unadorned_size_as_bytes(self):
+ self.assertEqual(self.p.parse('123'), 123)
+
+ def test_parses_unadorned_size_using_default_unit(self):
+ self.p.set_default_unit('KiB')
+ self.assertEqual(self.p.parse('123'), 123 * 1024)
+
+ def test_parses_size_with_byte_unit(self):
+ self.assertEqual(self.p.parse('123 B'), 123)
+
+ def test_parses_size_with_kilobyte_unit(self):
+ self.assertEqual(self.p.parse('123 kB'), 123 * 1000)
+
+ def test_parses_size_with_kibibyte_unit(self):
+ self.assertEqual(self.p.parse('123 KiB'), 123 * 1024)
+
+ def test_parses_size_with_megabyte_unit(self):
+ self.assertEqual(self.p.parse('123 MB'), 123 * 1000**2)
+
+ def test_parses_size_with_mebibyte_unit(self):
+ self.assertEqual(self.p.parse('123 MiB'), 123 * 1024**2)
+
+ def test_parses_size_with_gigabyte_unit(self):
+ self.assertEqual(self.p.parse('123 GB'), 123 * 1000**3)
+
+ def test_parses_size_with_gibibyte_unit(self):
+ self.assertEqual(self.p.parse('123 GiB'), 123 * 1024**3)
+