summaryrefslogtreecommitdiff
path: root/tests/test_util.py
blob: 3a3ecdb217d819fbb5aec299ea555979bb888a81 (plain)
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
104
105
106
107
108
109
110
"""Test util functions."""
import unittest
import unittest.mock
import io
import os
import pathlib
import time

from pywal import util


# Import colors.
COLORS = util.read_file_json("tests/test_files/test_file.json")


class TestUtil(unittest.TestCase):
    """Test the util functions."""

    def test_set_grey(self):
        """> Get grey color based on brightness of color0"""
        colors = [list(COLORS["colors"].values())]
        result = util.set_grey(colors[0])
        self.assertEqual(result, "#666666")

    def test_read_file(self):
        """> Read colors from a file."""
        result = util.read_file("tests/test_files/test_file")
        self.assertEqual(result[0], "/home/dylan/Pictures/Wallpapers/1.jpg")

    def test_read_file_start(self):
        """> Read colors from a file."""
        result = util.read_file_json("tests/test_files/test_file.json")
        self.assertEqual(result["colors"]["color0"], "#1F211E")

    def test_read_file_end(self):
        """> Read colors from a file."""
        result = util.read_file_json("tests/test_files/test_file.json")
        self.assertEqual(result["colors"]["color15"], "#F5F1F4")

    def test_read_wallpaper(self):
        """> Read wallpaper from json file."""
        result = util.read_file_json("tests/test_files/test_file.json")
        self.assertEqual(result["wallpaper"], "5.png")

    def test_save_file(self):
        """> Save colors to a file."""
        tmp_file = pathlib.Path("/tmp/test_file")
        util.save_file("Hello, world", tmp_file)
        result = tmp_file.is_file()
        self.assertTrue(result)

    def test_save_file_json(self):
        """> Save colors to a file."""
        tmp_file = pathlib.Path("/tmp/test_file.json")
        util.save_file_json(COLORS, tmp_file)
        result = tmp_file.is_file()
        self.assertTrue(result)

    def test_create_dir(self):
        """> Create a directoru."""
        tmp_dir = pathlib.Path("/tmp/test_dir")
        util.create_dir(tmp_dir)
        result = tmp_dir.is_dir()
        self.assertTrue(result)
        os.rmdir(tmp_dir)

    def test_hex_to_rgb_black(self):
        """> Convert #000000 to RGB."""
        result = util.hex_to_rgb("#000000")
        self.assertEqual(result, "0,0,0")

    def test_hex_to_rgb_white(self):
        """> Convert #FFFFFF to RGB."""
        result = util.hex_to_rgb("#FFFFFF")
        self.assertEqual(result, "255,255,255")

    def test_hex_to_rgb_rand(self):
        """> Convert #98AEC2 to RGB."""
        result = util.hex_to_rgb("#98AEC2")
        self.assertEqual(result, "152,174,194")

    def test_hex_to_xrgba(self):
        """> Convert #98AEC2 to XRGBA."""
        result = util.hex_to_xrgba("#98AEC2")
        self.assertEqual(result, "98/ae/c2/ff")

    def test_disown(self):
        """> Test disown command."""
        test_file = pathlib.Path("/tmp/wal-test-disown")
        util.disown("touch", test_file)

        # We won't know when 'disown' will finish so we
        # sleep here.
        time.sleep(10)

        result = test_file.is_file()
        self.assertTrue(result)
        os.remove(test_file)

    def test_msg(self):
        """> Test displaying a message."""
        # Since this function just prints a message we redirect
        # it's output so that we can read it.
        with unittest.mock.patch('sys.stdout', new=io.StringIO()) as fake_out:
            util.msg("test", True)
            self.assertEqual(fake_out.getvalue().strip(), "test")


if __name__ == "__main__":
    unittest.main()