aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/test_cli.py140
2 files changed, 140 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/test_cli.py b/tests/test_cli.py
new file mode 100644
index 0000000..82d0e00
--- /dev/null
+++ b/tests/test_cli.py
@@ -0,0 +1,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)