blob: 9bb514ae9aa7f58f271ea05ba985c233c348d5cb (
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
|
;;; test-wttrin--build-url.el --- Tests for wttrin--build-url -*- lexical-binding: t; -*-
;; Copyright (C) 2024-2026 Craig Jennings
;;; Commentary:
;; Unit tests for wttrin--build-url function.
;; Tests URL construction from location queries with proper encoding and parameters.
;;; Code:
(require 'ert)
(require 'wttrin)
(require 'testutil-wttrin)
;;; Setup and Teardown
(defun test-wttrin--build-url-setup ()
"Setup for build-url tests."
(testutil-wttrin-setup))
(defun test-wttrin--build-url-teardown ()
"Teardown for build-url tests."
(testutil-wttrin-teardown))
;;; Normal Cases
(ert-deftest test-wttrin--build-url-normal-simple-city-returns-correct-url ()
"Test URL building with simple city name."
(let ((wttrin-unit-system nil))
(should (string= "https://wttr.in/Paris?A"
(wttrin--build-url "Paris")))))
(ert-deftest test-wttrin--build-url-normal-city-with-country-returns-encoded-url ()
"Test URL building with city and country code."
(let ((wttrin-unit-system nil))
(should (string= "https://wttr.in/London%2C%20GB?A"
(wttrin--build-url "London, GB")))))
(ert-deftest test-wttrin--build-url-normal-metric-system-includes-m-flag ()
"Test URL building with metric unit system."
(let ((wttrin-unit-system "m"))
(should (string= "https://wttr.in/Tokyo?mA"
(wttrin--build-url "Tokyo")))))
(ert-deftest test-wttrin--build-url-normal-imperial-system-includes-u-flag ()
"Test URL building with USCS/imperial unit system."
(let ((wttrin-unit-system "u"))
(should (string= "https://wttr.in/NewYork?uA"
(wttrin--build-url "NewYork")))))
;;; Boundary Cases
(ert-deftest test-wttrin--build-url-boundary-unicode-city-name-returns-encoded-url ()
"Test URL building with Unicode characters in city name."
(let ((wttrin-unit-system nil))
(let ((url (wttrin--build-url "北京"))) ; Beijing in Chinese
(should (string-match-p "https://wttr.in/%[0-9A-F]+.*\\?A" url))
(should (string-prefix-p "https://wttr.in/" url))
(should (string-suffix-p "?A" url)))))
(ert-deftest test-wttrin--build-url-boundary-special-characters-returns-encoded-url ()
"Test URL building with special characters requiring encoding."
(let ((wttrin-unit-system nil))
(let ((url (wttrin--build-url "São Paulo")))
(should (string-match-p "https://wttr.in/S%C3%A3o%20Paulo\\?A" url)))))
(ert-deftest test-wttrin--build-url-boundary-spaces-returns-percent-encoded ()
"Test that spaces in query are properly URL-encoded."
(let ((wttrin-unit-system nil))
(let ((url (wttrin--build-url "New York")))
(should (string= "https://wttr.in/New%20York?A" url))
(should-not (string-match-p " " url)))))
(ert-deftest test-wttrin--build-url-boundary-airport-code-returns-correct-url ()
"Test URL building with 3-letter airport code."
(let ((wttrin-unit-system nil))
(should (string= "https://wttr.in/SFO?A"
(wttrin--build-url "SFO")))))
(ert-deftest test-wttrin--build-url-boundary-gps-coordinates-returns-correct-url ()
"Test URL building with GPS coordinates."
(let ((wttrin-unit-system nil))
(let ((url (wttrin--build-url "-78.46,106.79")))
(should (string-match-p "https://wttr.in/-78\\.46.*106\\.79\\?A" url)))))
(ert-deftest test-wttrin--build-url-boundary-domain-name-returns-encoded-url ()
"Test URL building with domain name (wttr.in supports @domain).
The @ symbol should be URL-encoded as %40."
(let ((wttrin-unit-system nil))
(let ((url (wttrin--build-url "@github.com")))
(should (string-prefix-p "https://wttr.in/" url))
(should (string-match-p "%40github\\.com" url)))))
;;; Display Options Cases
(ert-deftest test-wttrin--build-url-normal-display-options-appended-after-A ()
"Display options string is appended after the A flag."
(let ((wttrin-unit-system nil)
(wttrin-display-options "0Fq"))
(should (string= "https://wttr.in/Paris?A0Fq"
(wttrin--build-url "Paris")))))
(ert-deftest test-wttrin--build-url-normal-display-options-with-unit-system ()
"Display options coexist with unit system, both inside the same query string."
(let ((wttrin-unit-system "m")
(wttrin-display-options "0F"))
(should (string= "https://wttr.in/Tokyo?mA0F"
(wttrin--build-url "Tokyo")))))
(ert-deftest test-wttrin--build-url-normal-display-options-narrow-quiet ()
"Common combination: narrow + quiet."
(let ((wttrin-unit-system nil)
(wttrin-display-options "nq"))
(should (string= "https://wttr.in/SFO?Anq"
(wttrin--build-url "SFO")))))
(ert-deftest test-wttrin--build-url-boundary-display-options-nil-omits-flags ()
"Nil display-options leaves URL unchanged from baseline."
(let ((wttrin-unit-system nil)
(wttrin-display-options nil))
(should (string= "https://wttr.in/Paris?A"
(wttrin--build-url "Paris")))))
(ert-deftest test-wttrin--build-url-boundary-display-options-empty-string-omits-flags ()
"Empty display-options string is treated like nil."
(let ((wttrin-unit-system nil)
(wttrin-display-options ""))
(should (string= "https://wttr.in/Paris?A"
(wttrin--build-url "Paris")))))
(ert-deftest test-wttrin--build-url-boundary-display-options-single-character ()
"Single-character display-options works."
(let ((wttrin-unit-system nil)
(wttrin-display-options "F"))
(should (string= "https://wttr.in/London?AF"
(wttrin--build-url "London")))))
;;; Error Cases
(ert-deftest test-wttrin--build-url-error-nil-query-signals-error ()
"Test that nil query signals an error."
(let ((wttrin-unit-system nil))
(should-error (wttrin--build-url nil)
:type 'error)))
(ert-deftest test-wttrin--build-url-error-nil-query-has-descriptive-message ()
"Test that nil query error has descriptive message."
(let ((wttrin-unit-system nil))
(condition-case err
(wttrin--build-url nil)
(error
(should (string-match-p "cannot be nil" (error-message-string err)))))))
(provide 'test-wttrin--build-url)
;;; test-wttrin--build-url.el ends here
|