2017-06-27 03:41:04 +02:00
|
|
|
"""Test util functions."""
|
|
|
|
import unittest
|
2017-07-23 12:13:43 +02:00
|
|
|
import os
|
2017-06-27 03:41:04 +02:00
|
|
|
import pathlib
|
2017-06-27 09:24:27 +02:00
|
|
|
|
2017-06-27 03:41:04 +02:00
|
|
|
from pywal import util
|
|
|
|
|
|
|
|
|
2017-06-29 04:15:41 +02:00
|
|
|
# Import colors.
|
|
|
|
COLORS = util.read_file_json("tests/test_files/test_file.json")
|
|
|
|
|
|
|
|
|
2017-06-27 03:41:04 +02:00
|
|
|
class TestUtil(unittest.TestCase):
|
|
|
|
"""Test the util functions."""
|
|
|
|
|
2017-06-30 02:15:59 +02:00
|
|
|
def test_set_grey(self):
|
|
|
|
"""> Get grey color based on brightness of color0"""
|
|
|
|
colors = [list(COLORS["colors"].values())]
|
|
|
|
result = util.set_grey(colors[0])
|
2017-07-09 15:57:53 +02:00
|
|
|
self.assertEqual(result, "#666666")
|
2017-06-30 02:15:59 +02:00
|
|
|
|
2017-06-29 04:15:41 +02:00
|
|
|
def test_read_file(self):
|
2017-06-27 09:02:42 +02:00
|
|
|
"""> Read colors from a file."""
|
2017-06-27 09:34:34 +02:00
|
|
|
result = util.read_file("tests/test_files/test_file")
|
2017-06-29 04:15:41 +02:00
|
|
|
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")
|
2017-07-09 15:57:53 +02:00
|
|
|
self.assertEqual(result["colors"]["color0"], "#1F211E")
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
def test_read_file_end(self):
|
2017-06-27 09:02:42 +02:00
|
|
|
"""> Read colors from a file."""
|
2017-06-29 04:15:41 +02:00
|
|
|
result = util.read_file_json("tests/test_files/test_file.json")
|
2017-07-09 15:57:53 +02:00
|
|
|
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")
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
def test_save_file(self):
|
2017-06-27 09:02:42 +02:00
|
|
|
"""> Save colors to a file."""
|
2017-06-27 03:41:04 +02:00
|
|
|
tmp_file = pathlib.Path("/tmp/test_file")
|
|
|
|
util.save_file("Hello, world", tmp_file)
|
|
|
|
result = tmp_file.is_file()
|
|
|
|
self.assertTrue(result)
|
|
|
|
|
2017-06-29 04:15:41 +02:00
|
|
|
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)
|
|
|
|
|
2017-06-27 03:41:04 +02:00
|
|
|
def test_create_dir(self):
|
2017-07-23 15:12:57 +02:00
|
|
|
"""> Create a directory."""
|
2017-06-27 03:41:04 +02:00
|
|
|
tmp_dir = pathlib.Path("/tmp/test_dir")
|
|
|
|
util.create_dir(tmp_dir)
|
|
|
|
result = tmp_dir.is_dir()
|
|
|
|
self.assertTrue(result)
|
2017-07-23 12:13:43 +02:00
|
|
|
os.rmdir(tmp_dir)
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
def test_hex_to_rgb_black(self):
|
2017-06-27 09:02:42 +02:00
|
|
|
"""> Convert #000000 to RGB."""
|
2017-06-27 03:41:04 +02:00
|
|
|
result = util.hex_to_rgb("#000000")
|
2017-07-28 03:56:30 +02:00
|
|
|
self.assertEqual(result, (0, 0, 0))
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
def test_hex_to_rgb_white(self):
|
2017-07-28 03:56:30 +02:00
|
|
|
"""> Convert #ffffff to RGB."""
|
|
|
|
result = util.hex_to_rgb("#ffffff")
|
|
|
|
self.assertEqual(result, (255, 255, 255))
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
def test_hex_to_rgb_rand(self):
|
2017-07-28 03:56:30 +02:00
|
|
|
"""> Convert #98aec2 to RGB."""
|
|
|
|
result = util.hex_to_rgb("#98aec2")
|
|
|
|
self.assertEqual(result, (152, 174, 194))
|
2017-06-27 03:41:04 +02:00
|
|
|
|
2017-07-08 14:28:45 +02:00
|
|
|
def test_hex_to_xrgba(self):
|
2017-07-28 03:56:30 +02:00
|
|
|
"""> Convert #98aec2 to XRGBA."""
|
|
|
|
result = util.hex_to_xrgba("#98aec2")
|
2017-07-08 14:28:45 +02:00
|
|
|
self.assertEqual(result, "98/ae/c2/ff")
|
2017-06-27 03:41:04 +02:00
|
|
|
|
2017-07-28 03:56:30 +02:00
|
|
|
def test_rgb_to_hex(self):
|
|
|
|
"""> Convert 152,174,194 to HEX."""
|
|
|
|
result = util.rgb_to_hex((152, 174, 194))
|
|
|
|
self.assertEqual(result, "#98aec2")
|
|
|
|
|
|
|
|
def test_darken_color(self):
|
|
|
|
"""> Darken #ffffff by 0.25."""
|
|
|
|
result = util.darken_color("#ffffff", 0.25)
|
|
|
|
self.assertEqual(result, "#bfbfbf")
|
|
|
|
|
2017-06-27 03:41:04 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
unittest.main()
|