blob: 6ad8384ad8825ef2686bc5f3b09f597308407bb5 (
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
;;; test-wttrin-geolocation--internals.el --- Tests for geolocation internal helpers -*- lexical-binding: t; -*-
;; Copyright (C) 2026 Craig Jennings
;;; Commentary:
;; Unit tests for the four pure helpers used by the geolocation parsers:
;; - wttrin-geolocation--decode-json
;; - wttrin-geolocation--format-city-region
;; - wttrin-geolocation--lookup-provider
;; - wttrin-geolocation--extract-body
;;
;; These functions are exercised indirectly through the parser tests
;; (test-wttrin-geolocation--parse-ipapi.el and friends), but were not
;; covered directly. This file fills the gap so each helper's edge
;; cases are locked in isolation.
;;; Code:
(require 'ert)
(require 'wttrin-geolocation)
;;; --------------------------------------------------------------------------
;;; wttrin-geolocation--decode-json
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-geolocation--decode-json-normal-valid-object ()
"Valid JSON object decodes to an alist with symbol keys."
(let ((result (wttrin-geolocation--decode-json "{\"city\":\"Paris\",\"region\":\"IDF\"}")))
(should (equal (cdr (assq 'city result)) "Paris"))
(should (equal (cdr (assq 'region result)) "IDF"))))
;;; Boundary Cases
(ert-deftest test-wttrin-geolocation--decode-json-boundary-empty-object ()
"Empty JSON object decodes to nil (an empty alist)."
(should (equal (wttrin-geolocation--decode-json "{}") nil)))
;;; Error Cases
(ert-deftest test-wttrin-geolocation--decode-json-error-nil-input ()
"Nil input returns nil with no signal."
(should-not (wttrin-geolocation--decode-json nil)))
(ert-deftest test-wttrin-geolocation--decode-json-error-empty-string ()
"Empty string returns nil with no signal."
(should-not (wttrin-geolocation--decode-json "")))
(ert-deftest test-wttrin-geolocation--decode-json-error-malformed-json ()
"Malformed JSON returns nil with no signal."
(should-not (wttrin-geolocation--decode-json "not valid json {")))
;;; --------------------------------------------------------------------------
;;; wttrin-geolocation--format-city-region
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-geolocation--format-city-region-normal-both-present ()
"City and region both present produces \"City, Region\"."
(let ((data '((city . "Paris") (region . "Île-de-France"))))
(should (equal (wttrin-geolocation--format-city-region data)
"Paris, Île-de-France"))))
;;; Boundary Cases
(ert-deftest test-wttrin-geolocation--format-city-region-boundary-empty-city ()
"Empty city string returns nil even when region is present."
(let ((data '((city . "") (region . "Île-de-France"))))
(should-not (wttrin-geolocation--format-city-region data))))
(ert-deftest test-wttrin-geolocation--format-city-region-boundary-empty-region ()
"Empty region string returns nil even when city is present."
(let ((data '((city . "Paris") (region . ""))))
(should-not (wttrin-geolocation--format-city-region data))))
;;; Error Cases
(ert-deftest test-wttrin-geolocation--format-city-region-error-missing-city-key ()
"Missing city key returns nil."
(let ((data '((region . "Île-de-France"))))
(should-not (wttrin-geolocation--format-city-region data))))
(ert-deftest test-wttrin-geolocation--format-city-region-error-missing-region-key ()
"Missing region key returns nil."
(let ((data '((city . "Paris"))))
(should-not (wttrin-geolocation--format-city-region data))))
(ert-deftest test-wttrin-geolocation--format-city-region-error-nil-data ()
"Nil data returns nil with no signal."
(should-not (wttrin-geolocation--format-city-region nil)))
;;; --------------------------------------------------------------------------
;;; wttrin-geolocation--lookup-provider
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-geolocation--lookup-provider-normal-builtin-ipapi ()
"Built-in ipapi provider returns its plist with :name, :url, :parser."
(let ((result (wttrin-geolocation--lookup-provider 'ipapi)))
(should (equal (plist-get result :name) "ipapi.co"))
(should (stringp (plist-get result :url)))
(should (eq (plist-get result :parser) 'wttrin-geolocation--parse-ipapi))))
(ert-deftest test-wttrin-geolocation--lookup-provider-normal-builtin-ipinfo ()
"Built-in ipinfo provider returns its plist."
(let ((result (wttrin-geolocation--lookup-provider 'ipinfo)))
(should (equal (plist-get result :name) "ipinfo.io"))
(should (eq (plist-get result :parser) 'wttrin-geolocation--parse-ipinfo))))
;;; Boundary Cases
(ert-deftest test-wttrin-geolocation--lookup-provider-boundary-custom-registered ()
"User-registered provider is reachable via lookup."
(let ((wttrin-geolocation--providers
(cons '(test-provider
:name "Test"
:url "https://example.invalid/"
:parser ignore)
wttrin-geolocation--providers)))
(let ((result (wttrin-geolocation--lookup-provider 'test-provider)))
(should (equal (plist-get result :name) "Test"))
(should (eq (plist-get result :parser) 'ignore)))))
;;; Error Cases
(ert-deftest test-wttrin-geolocation--lookup-provider-error-unknown-symbol ()
"Unknown provider symbol signals error."
(should-error (wttrin-geolocation--lookup-provider 'definitely-not-registered)
:type 'error))
;;; --------------------------------------------------------------------------
;;; wttrin-geolocation--extract-body
;;; --------------------------------------------------------------------------
;;; Normal Cases
(ert-deftest test-wttrin-geolocation--extract-body-normal-200-with-body ()
"200 OK response with CRLF separator returns the body."
(with-temp-buffer
(insert "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"city\":\"Paris\"}")
(should (equal (wttrin-geolocation--extract-body)
"{\"city\":\"Paris\"}"))))
(ert-deftest test-wttrin-geolocation--extract-body-normal-utf8-body ()
"UTF-8 body bytes are decoded correctly."
(with-temp-buffer
(set-buffer-multibyte nil)
(insert "HTTP/1.1 200 OK\r\n\r\n")
(insert (encode-coding-string "{\"city\":\"São Paulo\"}" 'utf-8))
(set-buffer-multibyte t)
(should (equal (wttrin-geolocation--extract-body)
"{\"city\":\"São Paulo\"}"))))
;;; Boundary Cases
(ert-deftest test-wttrin-geolocation--extract-body-boundary-empty-body ()
"200 response with empty body returns the empty string."
(with-temp-buffer
(insert "HTTP/1.1 200 OK\r\n\r\n")
(should (equal (wttrin-geolocation--extract-body) ""))))
;;; Error Cases
(ert-deftest test-wttrin-geolocation--extract-body-error-404 ()
"404 response returns nil."
(with-temp-buffer
(insert "HTTP/1.1 404 Not Found\r\n\r\nNot Found")
(should-not (wttrin-geolocation--extract-body))))
(ert-deftest test-wttrin-geolocation--extract-body-error-500 ()
"500 response returns nil."
(with-temp-buffer
(insert "HTTP/1.1 500 Internal Server Error\r\n\r\noops")
(should-not (wttrin-geolocation--extract-body))))
(ert-deftest test-wttrin-geolocation--extract-body-error-missing-separator ()
"Response without a blank-line separator returns nil."
(with-temp-buffer
(insert "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nbody-without-separator")
(should-not (wttrin-geolocation--extract-body))))
(provide 'test-wttrin-geolocation--internals)
;;; test-wttrin-geolocation--internals.el ends here
|