|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# The MIT License (MIT) |
| 4 | +# |
| 5 | +# Copyright © 2014 Tim Case <timbielawa@gmail.com> |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person |
| 8 | +# obtaining a copy of this software and associated documentation files |
| 9 | +# (the "Software"), to deal in the Software without restriction, |
| 10 | +# including without limitation the rights to use, copy, modify, merge, |
| 11 | +# publish, distribute, sublicense, and/or sell copies of the Software, |
| 12 | +# and to permit persons to whom the Software is furnished to do so, |
| 13 | +# subject to the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be |
| 16 | +# included in all copies or substantial portions of the Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 19 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 20 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 21 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 22 | +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 23 | +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 24 | +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +# SOFTWARE. |
| 26 | + |
| 27 | + |
| 28 | +""" |
| 29 | +Tests for parse_string (strict=True, the default) and public entry-point behaviour. |
| 30 | +""" |
| 31 | + |
| 32 | +import warnings |
| 33 | + |
| 34 | +from . import TestCase |
| 35 | +import bitmath |
| 36 | + |
| 37 | + |
| 38 | +class TestParseStrict(TestCase): |
| 39 | + def test_parse_b(self): |
| 40 | + """parse_string works on bit strings""" |
| 41 | + self.assertEqual( |
| 42 | + bitmath.parse_string("123b"), |
| 43 | + bitmath.Bit(123)) |
| 44 | + |
| 45 | + def test_parse_B(self): |
| 46 | + """parse_string works on byte strings""" |
| 47 | + self.assertEqual( |
| 48 | + bitmath.parse_string("321B"), |
| 49 | + bitmath.Byte(321)) |
| 50 | + |
| 51 | + def test_parse_Gb(self): |
| 52 | + """parse_string works on gigabit strings""" |
| 53 | + self.assertEqual( |
| 54 | + bitmath.parse_string("456Gb"), |
| 55 | + bitmath.Gb(456)) |
| 56 | + |
| 57 | + def test_parse_MiB(self): |
| 58 | + """parse_string works on mebibyte strings""" |
| 59 | + self.assertEqual( |
| 60 | + bitmath.parse_string("654 MiB"), |
| 61 | + bitmath.MiB(654)) |
| 62 | + |
| 63 | + ###################################################################### |
| 64 | + # NIST 'octet' based units |
| 65 | + def test_parse_Mio(self): |
| 66 | + """parse_string works on mebioctet strings""" |
| 67 | + self.assertEqual( |
| 68 | + bitmath.parse_string("654 Mio"), |
| 69 | + bitmath.MiB(654)) |
| 70 | + |
| 71 | + def test_parse_Eio(self): |
| 72 | + """parse_string works on exbioctet strings""" |
| 73 | + self.assertEqual( |
| 74 | + bitmath.parse_string("654 Eio"), |
| 75 | + bitmath.EiB(654)) |
| 76 | + |
| 77 | + def test_parse_Zio(self): |
| 78 | + """parse_string works on zebioctet strings""" |
| 79 | + self.assertEqual( |
| 80 | + bitmath.parse_string("654 Zio"), |
| 81 | + bitmath.ZiB(654)) |
| 82 | + |
| 83 | + def test_parse_Yio(self): |
| 84 | + """parse_string works on yobioctet strings""" |
| 85 | + self.assertEqual( |
| 86 | + bitmath.parse_string("654 Yio"), |
| 87 | + bitmath.YiB(654)) |
| 88 | + |
| 89 | + # SI 'octet' based units |
| 90 | + def test_parse_Mo(self): |
| 91 | + """parse_string works on megaoctet strings""" |
| 92 | + self.assertEqual( |
| 93 | + bitmath.parse_string("654 Mo"), |
| 94 | + bitmath.MB(654)) |
| 95 | + |
| 96 | + def test_parse_Eo(self): |
| 97 | + """parse_string works on exaoctet strings""" |
| 98 | + self.assertEqual( |
| 99 | + bitmath.parse_string("654 Eo"), |
| 100 | + bitmath.EB(654)) |
| 101 | + |
| 102 | + def test_parse_Zo(self): |
| 103 | + """parse_string works on zettaoctet strings""" |
| 104 | + self.assertEqual( |
| 105 | + bitmath.parse_string("654 Zo"), |
| 106 | + bitmath.ZB(654)) |
| 107 | + |
| 108 | + def test_parse_Yo(self): |
| 109 | + """parse_string works on yottaoctet strings""" |
| 110 | + self.assertEqual( |
| 111 | + bitmath.parse_string("654 Yo"), |
| 112 | + bitmath.YB(654)) |
| 113 | + |
| 114 | + ###################################################################### |
| 115 | + |
| 116 | + def test_parse_bad_float(self): |
| 117 | + """parse_string can identify invalid float values""" |
| 118 | + with self.assertRaises(ValueError): |
| 119 | + bitmath.parse_string("1.23.45 kb") |
| 120 | + |
| 121 | + def test_parse_bad_unit(self): |
| 122 | + """parse_string can identify invalid prefix units""" |
| 123 | + with self.assertRaises(ValueError): |
| 124 | + bitmath.parse_string("1.23 GIB") |
| 125 | + |
| 126 | + def test_parse_bad_unit2(self): |
| 127 | + """parse_string can identify other prefix units""" |
| 128 | + with self.assertRaises(ValueError): |
| 129 | + bitmath.parse_string("1.23 QB") |
| 130 | + |
| 131 | + def test_parse_no_unit(self): |
| 132 | + """parse_string can identify strings without units at all""" |
| 133 | + with self.assertRaises(ValueError): |
| 134 | + bitmath.parse_string("12345") |
| 135 | + |
| 136 | + def test_parse_string_non_string_input(self): |
| 137 | + """parse_string can identify a non-string input""" |
| 138 | + with self.assertRaises(ValueError): |
| 139 | + bitmath.parse_string(12345) |
| 140 | + |
| 141 | + def test_parse_string_unicode(self): |
| 142 | + """parse_string can handle a unicode string""" |
| 143 | + self.assertEqual( |
| 144 | + bitmath.parse_string(u"750 GiB"), |
| 145 | + bitmath.GiB(750)) |
| 146 | + |
| 147 | + ###################################################################### |
| 148 | + # Deprecated public entry point tests |
| 149 | + |
| 150 | + def test_parse_string_unsafe_deprecation_warning(self): |
| 151 | + """parse_string_unsafe emits DeprecationWarning as of 2.0.0""" |
| 152 | + with warnings.catch_warnings(record=True) as w: |
| 153 | + warnings.simplefilter("always") |
| 154 | + bitmath.parse_string_unsafe("100 GiB") |
| 155 | + self.assertEqual(len(w), 1) |
| 156 | + self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 157 | + self.assertIn("2.0.0", str(w[0].message)) |
| 158 | + self.assertIn("parse_string", str(w[0].message)) |
| 159 | + |
| 160 | + def test_parse_string_unsafe_request_NIST(self): |
| 161 | + """parse_string_unsafe still delegates correctly with explicit system""" |
| 162 | + with warnings.catch_warnings(record=True): |
| 163 | + warnings.simplefilter("always") |
| 164 | + |
| 165 | + _parsed = bitmath.parse_string_unsafe("100M", system=bitmath.NIST) |
| 166 | + self.assertEqual(_parsed, bitmath.MiB(100)) |
| 167 | + self.assertIs(type(_parsed), bitmath.MiB) |
| 168 | + |
| 169 | + _parsed2 = bitmath.parse_string_unsafe("100k", system=bitmath.NIST) |
| 170 | + self.assertEqual(_parsed2, bitmath.KiB(100)) |
| 171 | + self.assertIs(type(_parsed2), bitmath.KiB) |
| 172 | + |
| 173 | + _parsed3 = bitmath.parse_string_unsafe("100", system=bitmath.NIST) |
| 174 | + self.assertEqual(_parsed3, bitmath.Byte(100)) |
| 175 | + self.assertIs(type(_parsed3), bitmath.Byte) |
| 176 | + |
| 177 | + _parsed4 = bitmath.parse_string_unsafe("100kb", system=bitmath.NIST) |
| 178 | + self.assertEqual(_parsed4, bitmath.KiB(100)) |
| 179 | + self.assertIs(type(_parsed4), bitmath.KiB) |
0 commit comments