aboutsummaryrefslogtreecommitdiffstats
path: root/test_cal_notif.py
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-27 19:51:09 +0200
committerDanilo M. <danix@danix.xyz>2026-09-27 19:51:09 +0200
commit1abb3002de93f9af64fa419109d2ef9114b99e76 (patch)
tree7857f668c794bbfb28c5b2813bb57259896fde4c /test_cal_notif.py
parent2dd08a66fe9e443564fe9dc3ee8cedda1c8a7425 (diff)
downloadcal-notif-1abb3002de93f9af64fa419109d2ef9114b99e76.tar.gz
cal-notif-1abb3002de93f9af64fa419109d2ef9114b99e76.zip
Add cal-notif skeleton with duration parsing and Italian spoken form
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'test_cal_notif.py')
-rw-r--r--test_cal_notif.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/test_cal_notif.py b/test_cal_notif.py
new file mode 100644
index 0000000..f025ff4
--- /dev/null
+++ b/test_cal_notif.py
@@ -0,0 +1,60 @@
+#!/usr/bin/python3
+# Tests for cal-notif. Run: /usr/bin/python3 test_cal_notif.py
+# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
+# Licensed under the GNU General Public License version 2 only.
+
+import importlib.machinery
+import importlib.util
+import os
+import tempfile
+import time
+from datetime import datetime, timedelta
+from pathlib import Path
+from zoneinfo import ZoneInfo
+
+os.environ["TZ"] = "Europe/Rome" # all-day and floating times are local
+time.tzset()
+
+HERE = Path(__file__).resolve().parent
+_loader = importlib.machinery.SourceFileLoader("cal_notif", str(HERE / "cal-notif"))
+_spec = importlib.util.spec_from_loader("cal_notif", _loader)
+cn = importlib.util.module_from_spec(_spec)
+_loader.exec_module(cn)
+
+ROME = ZoneInfo("Europe/Rome")
+H, M, D = timedelta(hours=1), timedelta(minutes=1), timedelta(days=1)
+
+
+def ics(*events):
+ body = "".join(f"BEGIN:VEVENT\n{e.strip()}\nEND:VEVENT\n" for e in events)
+ return f"BEGIN:VCALENDAR\nVERSION:2.0\n{body}END:VCALENDAR\n".encode()
+
+
+def at(y, mo, d, h=0, mi=0):
+ return datetime(y, mo, d, h, mi, tzinfo=ROME)
+
+
+def test_durations():
+ assert cn.parse_duration("1d 2h 15m") == D + 2 * H + 15 * M
+ assert cn.parse_duration("90m") == 90 * M
+ assert cn.parse_duration("0m") == timedelta()
+ for bad in ("", "5", "1x", "m", "2h1d"):
+ try:
+ cn.parse_duration(bad)
+ raise AssertionError(bad)
+ except ValueError:
+ pass
+ assert cn.fmt_duration(D + 2 * H) == "1d 2h"
+ assert cn.fmt_duration(timedelta()) == "0m"
+ assert cn.spoken_it(10 * M) == "dieci minuti"
+ assert cn.spoken_it(H) == "un'ora"
+ assert cn.spoken_it(D + 2 * H) == "un giorno e due ore"
+ assert cn.spoken_it(2 * D + H + 21 * M) == "due giorni, un'ora e ventuno minuti"
+ assert cn.number_it(28) == "ventotto" and cn.number_it(45) == "quarantacinque"
+
+
+if __name__ == "__main__":
+ for name, fn in list(globals().items()):
+ if name.startswith("test_"):
+ fn()
+ print("ok", name)