aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_cli.py
blob: 82d0e001b4b7b7da8417766614eb3590665cd9cb (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright (C) 2023  Tomasz Kramkowski <tomasz@kramkow.ski>
# SPDX-License-Identifier: MIT
import unittest
from collections.abc import Iterator
from contextlib import contextmanager
from tempfile import TemporaryDirectory

from brightness import main


class TempSysfs:
    def __init__(self, path: str) -> None:
        self.path = path

    def read(self, name) -> str:
        with open(self.path + "/" + name) as f:
            return f.read()

    @property
    def brightness(self) -> int:
        return int(self.read("brightness").rstrip())

    def max_brightness(self) -> int:
        return int(self.read("max_brightness").rstrip())


@contextmanager
def fake_sysfs(brightness: int, max_brightness: int) -> Iterator[TempSysfs]:
    with TemporaryDirectory() as d:
        with open(d + "/brightness", "w") as f:
            f.write(str(brightness) + "\n")
        with open(d + "/max_brightness", "w") as f:
            f.write(str(max_brightness) + "\n")
        yield TempSysfs(d)


class TestCLI(unittest.TestCase):
    def test_increment_10(self):
        with fake_sysfs(brightness=70, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "+10"])
            self.assertGreater(sysfs.brightness, 70)
            self.assertLessEqual(sysfs.brightness, 255)

    def test_decrement_10(self):
        with fake_sysfs(brightness=70, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "-10"])
            self.assertGreaterEqual(sysfs.brightness, 0)
            self.assertLess(sysfs.brightness, 70)

    def test_increase_small(self):
        with fake_sysfs(brightness=0, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "+1"])
            self.assertEqual(sysfs.brightness, 1)

    def test_decrease_small(self):
        with fake_sysfs(brightness=1, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "-1"])
            self.assertEqual(sysfs.brightness, 0)

    def test_increase_zero(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "+0"])
            self.assertEqual(sysfs.brightness, 127)

    def test_decrease_zero(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "-0"])
            self.assertEqual(sysfs.brightness, 127)

    def test_double(self):
        with fake_sysfs(brightness=15, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "+" + str(1 / 8 * 100)])
            self.assertEqual(sysfs.brightness, 31)

    def test_halve(self):
        with fake_sysfs(brightness=15, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, str(-(1 / 8 * 100))])
            self.assertEqual(sysfs.brightness, 7)

    def test_clamp_low(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "-100"])
            self.assertEqual(sysfs.brightness, 0)

    def test_clamp_high(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "+100"])
            self.assertEqual(sysfs.brightness, 255)

    def test_absolute(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 0)
            main(["brightness", sysfs.path, "12.5"])
            self.assertEqual(sysfs.brightness, 1)
            main(["brightness", sysfs.path, "25.0"])
            self.assertEqual(sysfs.brightness, 3)
            main(["brightness", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 255)

    def test_min_value(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", "--min", "0", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 0)
            main(["brightness", "--min", "10", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 10)
            main(["brightness", "--min", "0%", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 0)
            main(["brightness", "--min", "12.5%", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 1)

    def test_max_value(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", "--max", "255", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 255)
            main(["brightness", "--max", "245", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 245)
            main(["brightness", "--max", "100%", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 255)
            main(["brightness", "--max", "87.5%", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 127)

    def test_min_max(self):
        with fake_sysfs(brightness=127, max_brightness=255) as sysfs:
            main(["brightness", "--min", "0", "--max", "255", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 0)
            main(["brightness", "--min", "0", "--max", "255", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 255)
            main(["brightness", "--min", "10", "--max", "245", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 10)
            main(["brightness", "--min", "10", "--max", "245", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 245)
            main(["brightness", "--min", "0%", "--max", "100%", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 0)
            main(["brightness", "--min", "0%", "--max", "100%", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 255)
            main(["brightness", "--min", "12.5%", "--max", "87.5%", sysfs.path, "0"])
            self.assertEqual(sysfs.brightness, 1)
            main(["brightness", "--min", "12.5%", "--max", "87.5%", sysfs.path, "100"])
            self.assertEqual(sysfs.brightness, 127)