Debian Codegraph @

tests/test_utils.py

raw
1 # -*- coding: utf-8 -*-
2 """Test CLI Helpers' utility functions and helpers."""
3
4 from __future__ import unicode_literals
5
6 from cli_helpers import utils
7
8
9 def test_bytes_to_string_hexlify():
10 """Test that bytes_to_string() hexlifies binary data."""
11 assert utils.bytes_to_string(b"\xff") == "0xff"
12
13
14 def test_bytes_to_string_decode_bytes():
15 """Test that bytes_to_string() decodes bytes."""
16 assert utils.bytes_to_string(b"foobar") == "foobar"
17
18
19 def test_bytes_to_string_unprintable():
20 """Test that bytes_to_string() hexlifies data that is valid unicode, but unprintable."""
21 assert utils.bytes_to_string(b"\0") == "0x00"
22 assert utils.bytes_to_string(b"\1") == "0x01"
23 assert utils.bytes_to_string(b"a\0") == "0x6100"
24
25
26 def test_bytes_to_string_non_bytes():
27 """Test that bytes_to_string() returns non-bytes untouched."""
28 assert utils.bytes_to_string("abc") == "abc"
29 assert utils.bytes_to_string(1) == 1
30
31
32 def test_to_string_bytes():
33 """Test that to_string() converts bytes to a string."""
34 assert utils.to_string(b"foo") == "foo"
35
36
37 def test_to_string_non_bytes():
38 """Test that to_string() converts non-bytes to a string."""
39 assert utils.to_string(1) == "1"
40 assert utils.to_string(2.29) == "2.29"
41
42
43 def test_truncate_string():
44 """Test string truncate preprocessor."""
45 val = "x" * 100
46 assert utils.truncate_string(val, 10) == "xxxxxxx..."
47
48 val = "x " * 100
49 assert utils.truncate_string(val, 10) == "x x x x..."
50
51 val = "x" * 100
52 assert utils.truncate_string(val) == "x" * 100
53
54 val = ["x"] * 100
55 val[20] = "\n"
56 str_val = "".join(val)
57 assert utils.truncate_string(str_val, 10, skip_multiline_string=True) == str_val
58
59
60 def test_intlen_with_decimal():
61 """Test that intlen() counts correctly with a decimal place."""
62 assert utils.intlen("11.1") == 2
63 assert utils.intlen("1.1") == 1
64
65
66 def test_intlen_without_decimal():
67 """Test that intlen() counts correctly without a decimal place."""
68 assert utils.intlen("11") == 2
69
70
71 def test_filter_dict_by_key():
72 """Test that filter_dict_by_key() filter unwanted items."""
73 keys = ("foo", "bar")
74 d = {"foo": 1, "foobar": 2}
75 fd = utils.filter_dict_by_key(d, keys)
76 assert len(fd) == 1
77 assert all([k in keys for k in fd])
78
79
80 def test_version_as_tuple_01():
81 """Test version_as_tuple() in the usual case."""
82 assert utils.version_as_tuple('0.10.0') == (0, 10, 0)
83
84
85 def test_version_as_tuple_02():
86 """Test version_as_tuple() with leading v."""
87 assert utils.version_as_tuple('v0.10.0') == (0, 10, 0)
88
89
90 def test_version_as_tuple_03():
91 """Test version_as_tuple() trailing padding."""
92 assert utils.version_as_tuple('0.10') == (0, 10, 0)
93
94
95 def test_version_as_tuple_04():
96 """Test version_as_tuple() stripping rc suffixes."""
97 assert utils.version_as_tuple('0.10.0rc1') == (0, 10, 0)
98
99
100 def test_version_as_tuple_05():
101 """Test version_as_tuple() deleting post1 elements."""
102 assert utils.version_as_tuple('0.10.0.post1') == (0, 10, 0)