From e8d031a368b3b6ecbfa80b3051dcce09f6f6d752 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Tue, 14 Apr 2020 16:23:38 +0100 Subject: [PATCH] filesize tests --- rich/filesize.py | 50 ------------------------------------------ tests/test_filesize.py | 17 ++++++++++++++ 2 files changed, 17 insertions(+), 50 deletions(-) create mode 100644 tests/test_filesize.py diff --git a/rich/filesize.py b/rich/filesize.py index 92339868..1067417f 100644 --- a/rich/filesize.py +++ b/rich/filesize.py @@ -42,56 +42,6 @@ def pick_unit_and_suffix(size: int, suffixes: List[str], base: int) -> Tuple[int return unit, suffix -def traditional(size: int) -> str: - """Convert a filesize in to a string (powers of 1024, JDEC prefixes). - - In this convention, ``1024 B = 1 KB``. - - This is the format that was used to display the size of DVDs - (*700 MB* meaning actually about *734 003 200 bytes*) before - standardisation of IEC units among manufacturers, and still - used by **Windows** to report the storage capacity of hard - drives (*279.4 GB* meaning *279.4 × 1024³ bytes*). - - Arguments: - size (int): A file size. - - Returns: - `str`: A string containing an abbreviated file size and units. - - Example: - >>> filesize.traditional(30000) - '29.3 KB' - - """ - return _to_str(size, ("KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"), 1024) - - -def binary(size: int) -> str: - """Convert a filesize in to a string (powers of 1024, IEC prefixes). - - In this convention, ``1024 B = 1 KiB``. - - This is the format that has gained adoption among manufacturers - to avoid ambiguity regarding size units, since it explicitly states - using a binary base (*KiB = kibi bytes = kilo binary bytes*). - This format is notably being used by the **Linux** kernel (see - ``man 7 units``). - - Arguments: - int (size): A file size. - - Returns: - `str`: A string containing a abbreviated file size and units. - - Example: - >>> filesize.binary(30000) - '29.3 KiB' - - """ - return _to_str(size, ("KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"), 1024) - - def decimal(size: int) -> str: """Convert a filesize in to a string (powers of 1000, SI prefixes). diff --git a/tests/test_filesize.py b/tests/test_filesize.py new file mode 100644 index 00000000..57268a39 --- /dev/null +++ b/tests/test_filesize.py @@ -0,0 +1,17 @@ +from rich import filesize + + +def test_traditional(): + assert filesize.decimal(0) == "0 bytes" + assert filesize.decimal(1) == "1 byte" + assert filesize.decimal(2) == "2 bytes" + assert filesize.decimal(1000) == "1.0 kB" + assert filesize.decimal(1.5 * 1000 * 1000) == "1.5 MB" + + +def test_pick_unit_and_suffix(): + assert filesize.pick_unit_and_suffix(50, ["foo", "bar", "baz"], 100) == (1, "bytes") + assert filesize.pick_unit_and_suffix(1500, ["foo", "bar", "baz"], 100) == ( + 10000, + "foo", + )