summaryrefslogtreecommitdiff
path: root/tests/testutil-wttrin.el
blob: 0e109adb4dfa091ba0541bff533fb25cffdb9e56 (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
;;; testutil-wttrin.el --- Test utilities for wttrin -*- lexical-binding: t; -*-

;; Copyright (C) 2025 Craig Jennings

;;; Commentary:

;; Shared test utilities for wttrin test suite.
;; Provides helper functions, fixtures, and common setup/teardown functionality.

;;; Code:

(require 'ert)

;;; Test Data Fixtures

(defconst testutil-wttrin-sample-weather-response
  "Weather report: Paris, France

     \\  /       Partly cloudy
   _ /\"\".-. 22 °C
     \\_(   ).  ↓ 15 km/h
     /(___(__)  10 km
                0.0 mm"
  "Sample weather response for testing parsing logic.")

(defconst testutil-wttrin-sample-error-response
  "ERROR: Unknown location; please try ~curl wttr.in/:help"
  "Sample error response from wttr.in service.")

;;; Cache Testing Helpers

(defun testutil-wttrin-clear-cache ()
  "Clear the wttrin cache for test isolation."
  (clrhash wttrin--cache))

(defun testutil-wttrin-add-to-cache (location data &optional age-seconds)
  "Add DATA to cache for LOCATION, optionally aged by AGE-SECONDS."
  (let* ((cache-key (wttrin--make-cache-key location))
         (timestamp (if age-seconds
                        (- (float-time) age-seconds)
                      (float-time))))
    (puthash cache-key (cons timestamp data) wttrin--cache)))

(defun testutil-wttrin-cache-size ()
  "Return the current number of entries in the cache."
  (hash-table-count wttrin--cache))

;;; Custom Variable Management

(defmacro testutil-wttrin-with-unit-system (unit-system &rest body)
  "Execute BODY with wttrin-unit-system temporarily set to UNIT-SYSTEM."
  (declare (indent 1))
  `(let ((wttrin-unit-system ,unit-system))
     ,@body))

(defmacro testutil-wttrin-with-cache-ttl (ttl &rest body)
  "Execute BODY with wttrin-cache-ttl temporarily set to TTL."
  (declare (indent 1))
  `(let ((wttrin-cache-ttl ,ttl))
     ,@body))

(defmacro testutil-wttrin-with-cache-max (max-entries &rest body)
  "Execute BODY with wttrin-cache-max-entries temporarily set to MAX-ENTRIES."
  (declare (indent 1))
  `(let ((wttrin-cache-max-entries ,max-entries))
     ,@body))

;;; Test Setup and Teardown

(defun testutil-wttrin-setup ()
  "Common setup for wttrin tests.
Call this at the beginning of each test."
  (testutil-wttrin-clear-cache)
  (setq wttrin--force-refresh nil))

(defun testutil-wttrin-teardown ()
  "Common teardown for wttrin tests.
Call this at the end of each test."
  (testutil-wttrin-clear-cache)
  (setq wttrin--force-refresh nil))

(provide 'testutil-wttrin)
;;; testutil-wttrin.el ends here