summaryrefslogtreecommitdiff
path: root/obnamlib/sizeparse_tests.py
blob: b035701644af1eb85f2a1a99ddfdd11d64a94523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Copyright 2010-2015  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_returns_an_int(self):
        self.assert_(isinstance(self.p.parse('123'), int))

    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_kilo_unit(self):
        self.assertEqual(self.p.parse('123 k'), 123 * 1000)

    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_mega_unit(self):
        self.assertEqual(self.p.parse('123 m'), 123 * 1000**2)

    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_giga_unit(self):
        self.assertEqual(self.p.parse('123 g'), 123 * 1000**3)

    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)

    def test_raises_error_for_empty_string(self):
        self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, '')

    def test_raises_error_for_missing_size(self):
        self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, 'KiB')

    def test_raises_error_for_bad_unit(self):
        self.assertRaises(obnamlib.SizeSyntaxError, self.p.parse, '1 km')

    def test_raises_error_for_bad_unit_thats_similar_to_real_one(self):
        self.assertRaises(obnamlib.UnitNameError, self.p.parse, '1 ib')

    def test_raises_error_for_bad_default_unit(self):
        self.assertRaises(obnamlib.UnitNameError,
                          self.p.set_default_unit, 'km')

    def test_size_syntax_error_includes_input_string(self):
        text = 'asdf asdf'
        e = obnamlib.SizeSyntaxError(size=text)
        self.assert_(text in str(e), str(e))

    def test_unit_name_error_includes_input_string(self):
        text = 'asdf asdf'
        e = obnamlib.UnitNameError(unit=text)
        self.assert_(text in str(e), str(e))