aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_theme.py
blob: f14b0f11f47504db91ac4c7bdd0070bb73f551e8 (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
# deck-it - tests for theme
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import unittest

import deckit.theme as theme

PALETTE = """* {
    base:      #24273aff;
    text:      #cad3f5ff;
    surface0:  #363a4fff;
}"""
ACCENT = "* { accent: #b7bdf8; }"


class Theme(unittest.TestCase):
    def test_parses_palette(self):
        self.assertEqual(theme.parse_palette(PALETTE),
                         {"base": "#24273aff", "text": "#cad3f5ff", "surface0": "#363a4fff"})

    def test_parses_accent(self):
        self.assertEqual(theme.parse_accent(ACCENT), "#b7bdf8")
        self.assertIsNone(theme.parse_accent("nothing"))

    def test_css_exposes_vars_and_falls_back(self):
        out = theme.css({"base": "#24273a"}, None)
        self.assertIn("--udt-base: #24273a", out)
        self.assertIn("--udt-accent:", out)  # fallback accent

    def test_parses_border_hues(self):
        self.assertEqual(
            theme.parse_border('return { "rgb(58a6ff)", "rgb(a371f7)", "rgb(db61a2)" }'),
            ["#58a6ff", "#a371f7", "#db61a2"])

    def test_css_exposes_border_vars(self):
        out = theme.css({"base": "#24273a"}, None, ["#111111", "#222222", "#333333"])
        for index, value in enumerate(["#111111", "#222222", "#333333"], 1):
            self.assertIn(f"--udt-border-{index}: {value}", out)

    def test_font_size_prefers_override_then_base(self):
        self.assertEqual(theme.parse_font_size('{"base": 16, "override": {"deckit": 18.6}}'), 19)
        self.assertEqual(theme.parse_font_size('{"base": 16, "override": {"rofi": 14}}'), 16)
        for bad in ('{"base": 99}', '{"base": "16"}', "not json", "[]"):
            self.assertIsNone(theme.parse_font_size(bad))

    def test_css_exposes_font_size(self):
        self.assertIn("--udt-font-size: 17px", theme.css({}, None, font_size=17))
        self.assertIn(f"--udt-font-size: {theme.FONT_SIZE}px", theme.css({}, None))

    def test_load_falls_back_when_files_are_missing(self):
        saved = (theme.PALETTE_PATH, theme.ACCENT_PATH)
        theme.PALETTE_PATH = Path("/nonexistent/palette.rasi")
        theme.ACCENT_PATH = Path("/nonexistent/accent.rasi")
        try:
            out = theme.load()
        finally:
            theme.PALETTE_PATH, theme.ACCENT_PATH = saved
        self.assertIn("--udt-base: #24273a", out)


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