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 95 96 97 98 99 100 101 102 103
|
# -*- coding: utf-8 -*-
# The MIT License (MIT)
#
# Copyright © 2014 Tim Bielawa <timbielawa@gmail.com>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""
Tests to verify that the formatting context manager works
"""
from . import TestCase
import bitmath
class TestContextManager(TestCase):
def test_with_format(self):
"""bitmath.format context mgr sets and restores formatting"""
to_print = [
bitmath.Byte(101),
bitmath.KiB(202),
bitmath.MB(303),
bitmath.GiB(404),
bitmath.TB(505),
bitmath.PiB(606),
bitmath.EB(707)
]
str_reps = [
"101.00-Byte",
"202.00-KiB",
"303.00-MB",
"404.00-GiB",
"505.00-TB",
"606.00-PiB",
"707.00-EB"
]
# Make sure formatting looks right BEFORE the context manager
self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")
with bitmath.format("{value:.2f}-{unit}"):
for (inst, inst_str) in zip(to_print, str_reps):
self.assertEqual(str(inst), inst_str)
# Make sure formatting looks right AFTER the context manager
self.assertEqual(str(bitmath.KiB(1.337)), "1.337 KiB")
def test_print_byte_plural(self):
"""Byte(3.0) prints out units in plural form"""
expected_result = "3Bytes"
fmt_str = "{value:.1g}{unit}"
three_Bytes = bitmath.Byte(3.0)
with bitmath.format(plural=True):
actual_result = three_Bytes.format(fmt_str)
self.assertEqual(expected_result, actual_result)
def test_print_byte_plural_fmt_in_mgr(self):
"""Byte(3.0) prints out units in plural form, setting the fmt str in the mgr"""
expected_result = "3Bytes"
with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
three_Bytes = bitmath.Byte(3.0)
actual_result = str(three_Bytes)
self.assertEqual(expected_result, actual_result)
def test_print_GiB_plural_fmt_in_mgr(self):
"""TiB(1/3.0) prints out units in plural form, setting the fmt str in the mgr"""
expected_result = "3Bytes"
with bitmath.format(fmt_str="{value:.1g}{unit}", plural=True):
three_Bytes = bitmath.Byte(3.0)
actual_result = str(three_Bytes)
self.assertEqual(expected_result, actual_result)
def test_print_GiB_singular_fmt_in_mgr(self):
"""TiB(1/3.0) prints out units in singular form, setting the fmt str in the mgr"""
expected_result = "341.3GiB"
with bitmath.format(fmt_str="{value:.1f}{unit}"):
third_tibibyte = bitmath.TiB(1 / 3.0).best_prefix()
actual_result = str(third_tibibyte)
self.assertEqual(expected_result, actual_result)
|