summaryrefslogtreecommitdiffstats
path: root/tests/test_utils.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:37:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-05-04 17:37:29 +0000
commitde734e6e5777abb6f8f16f94166ecd3dbe179421 (patch)
treefff61580a08934083aab3043d228c2f484f6f844 /tests/test_utils.py
parentInitial commit. (diff)
downloadcli-helpers-de734e6e5777abb6f8f16f94166ecd3dbe179421.tar.xz
cli-helpers-de734e6e5777abb6f8f16f94166ecd3dbe179421.zip
Adding upstream version 2.3.0.upstream/2.3.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--tests/test_utils.py77
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/test_utils.py b/tests/test_utils.py
new file mode 100644
index 0000000..ba43937
--- /dev/null
+++ b/tests/test_utils.py
@@ -0,0 +1,77 @@
+# -*- coding: utf-8 -*-
+"""Test CLI Helpers' utility functions and helpers."""
+
+from __future__ import unicode_literals
+
+from cli_helpers import utils
+
+
+def test_bytes_to_string_hexlify():
+ """Test that bytes_to_string() hexlifies binary data."""
+ assert utils.bytes_to_string(b"\xff") == "0xff"
+
+
+def test_bytes_to_string_decode_bytes():
+ """Test that bytes_to_string() decodes bytes."""
+ assert utils.bytes_to_string(b"foobar") == "foobar"
+
+
+def test_bytes_to_string_unprintable():
+ """Test that bytes_to_string() hexlifies data that is valid unicode, but unprintable."""
+ assert utils.bytes_to_string(b"\0") == "0x00"
+ assert utils.bytes_to_string(b"\1") == "0x01"
+ assert utils.bytes_to_string(b"a\0") == "0x6100"
+
+
+def test_bytes_to_string_non_bytes():
+ """Test that bytes_to_string() returns non-bytes untouched."""
+ assert utils.bytes_to_string("abc") == "abc"
+ assert utils.bytes_to_string(1) == 1
+
+
+def test_to_string_bytes():
+ """Test that to_string() converts bytes to a string."""
+ assert utils.to_string(b"foo") == "foo"
+
+
+def test_to_string_non_bytes():
+ """Test that to_string() converts non-bytes to a string."""
+ assert utils.to_string(1) == "1"
+ assert utils.to_string(2.29) == "2.29"
+
+
+def test_truncate_string():
+ """Test string truncate preprocessor."""
+ val = "x" * 100
+ assert utils.truncate_string(val, 10) == "xxxxxxx..."
+
+ val = "x " * 100
+ assert utils.truncate_string(val, 10) == "x x x x..."
+
+ val = "x" * 100
+ assert utils.truncate_string(val) == "x" * 100
+
+ val = ["x"] * 100
+ val[20] = "\n"
+ str_val = "".join(val)
+ assert utils.truncate_string(str_val, 10, skip_multiline_string=True) == str_val
+
+
+def test_intlen_with_decimal():
+ """Test that intlen() counts correctly with a decimal place."""
+ assert utils.intlen("11.1") == 2
+ assert utils.intlen("1.1") == 1
+
+
+def test_intlen_without_decimal():
+ """Test that intlen() counts correctly without a decimal place."""
+ assert utils.intlen("11") == 2
+
+
+def test_filter_dict_by_key():
+ """Test that filter_dict_by_key() filter unwanted items."""
+ keys = ("foo", "bar")
+ d = {"foo": 1, "foobar": 2}
+ fd = utils.filter_dict_by_key(d, keys)
+ assert len(fd) == 1
+ assert all([k in keys for k in fd])