Back to index

obnam  1.1
sizeparse_tests.py
Go to the documentation of this file.
00001 # Copyright 2010  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import unittest
00018 
00019 import obnamlib
00020 
00021 
00022 class ByteSizeParserTests(unittest.TestCase):
00023 
00024     def setUp(self):
00025         self.p = obnamlib.ByteSizeParser()
00026         
00027     def test_parses_zero(self):
00028         self.assertEqual(self.p.parse('0'), 0)
00029         
00030     def test_parses_unadorned_size_as_bytes(self):
00031         self.assertEqual(self.p.parse('123'), 123)
00032         
00033     def test_returns_an_int(self):
00034         self.assert_(isinstance(self.p.parse('123'), int))
00035         
00036     def test_parses_unadorned_size_using_default_unit(self):
00037         self.p.set_default_unit('KiB')
00038         self.assertEqual(self.p.parse('123'), 123 * 1024)
00039         
00040     def test_parses_size_with_byte_unit(self):
00041         self.assertEqual(self.p.parse('123 B'), 123)
00042         
00043     def test_parses_size_with_kilo_unit(self):
00044         self.assertEqual(self.p.parse('123 k'), 123 * 1000)
00045         
00046     def test_parses_size_with_kilobyte_unit(self):
00047         self.assertEqual(self.p.parse('123 kB'), 123 * 1000)
00048         
00049     def test_parses_size_with_kibibyte_unit(self):
00050         self.assertEqual(self.p.parse('123 KiB'), 123 * 1024)
00051         
00052     def test_parses_size_with_mega_unit(self):
00053         self.assertEqual(self.p.parse('123 m'), 123 * 1000**2)
00054         
00055     def test_parses_size_with_megabyte_unit(self):
00056         self.assertEqual(self.p.parse('123 MB'), 123 * 1000**2)
00057         
00058     def test_parses_size_with_mebibyte_unit(self):
00059         self.assertEqual(self.p.parse('123 MiB'), 123 * 1024**2)
00060         
00061     def test_parses_size_with_giga_unit(self):
00062         self.assertEqual(self.p.parse('123 g'), 123 * 1000**3)
00063         
00064     def test_parses_size_with_gigabyte_unit(self):
00065         self.assertEqual(self.p.parse('123 GB'), 123 * 1000**3)
00066         
00067     def test_parses_size_with_gibibyte_unit(self):
00068         self.assertEqual(self.p.parse('123 GiB'), 123 * 1024**3)
00069 
00070     def test_raises_error_for_empty_string(self):
00071         self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, '')
00072 
00073     def test_raises_error_for_missing_size(self):
00074         self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, 'KiB')
00075 
00076     def test_raises_error_for_bad_unit(self):
00077         self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, '1 km')
00078 
00079     def test_raises_error_for_bad_unit_thats_similar_to_real_one(self):
00080         self.assertRaises(obnamlib.UnitNameError, self.p.parse, '1 ib')
00081 
00082     def test_raises_error_for_bad_default_unit(self):
00083         self.assertRaises(obnamlib.UnitNameError, 
00084                           self.p.set_default_unit, 'km')
00085 
00086     def test_size_syntax_error_includes_input_string(self):
00087         text = 'asdf asdf'
00088         e = obnamlib.SizeSyntaxError(text)
00089         self.assert_(text in str(e), str(e))
00090 
00091     def test_unit_name_error_includes_input_string(self):
00092         text = 'asdf asdf'
00093         e = obnamlib.UnitNameError(text)
00094         self.assert_(text in str(e), str(e))
00095