diff options
| author | Craig Jennings <c@cjennings.net> | 2025-11-08 10:34:56 -0600 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-11-08 10:34:56 -0600 |
| commit | 31649f588dbc0098b6641222403f68107819f34f (patch) | |
| tree | 75c180ea2152d43504b63c2597d15e1306bda7ce /tests | |
| parent | 44d48bad4d317ce8fd9361314d349b1256e6c25b (diff) | |
test: core: enhance ERT tests for pure functions
- Enhanced test-wttrin-additional-url-params with clearer test organization
- Enhanced test-wttrin--build-url with comprehensive boundary and error cases
- Added tests for Unicode, special characters, GPS coordinates, domains
- Fixed domain name test to check for URL-encoded @ symbol (%40)
- Enhanced test-wttrin--make-cache-key with extensive boundary tests
- Critical test: same location with different units produces different keys
- Added tests for special chars, Unicode, empty strings, pipe characters
All 29 tests for these three pure functions now pass (6 + 12 + 11)
Combined with wttrin--fetch-url tests: 38 total tests passing
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test-wttrin--build-url.el | 143 | ||||
| -rw-r--r-- | tests/test-wttrin--make-cache-key.el | 115 | ||||
| -rw-r--r-- | tests/test-wttrin-additional-url-params.el | 60 |
3 files changed, 172 insertions, 146 deletions
diff --git a/tests/test-wttrin--build-url.el b/tests/test-wttrin--build-url.el index 21d3241..87a5f92 100644 --- a/tests/test-wttrin--build-url.el +++ b/tests/test-wttrin--build-url.el @@ -1,91 +1,100 @@ ;;; test-wttrin--build-url.el --- Tests for wttrin--build-url -*- lexical-binding: t; -*- -;; Copyright (C) 2025 Craig Jennings +;; Copyright (C) 2024 Craig Jennings ;;; Commentary: - ;; Unit tests for wttrin--build-url function. -;; Tests URL construction with proper encoding and parameters. +;; Tests URL construction from location queries with proper encoding and parameters. ;;; Code: (require 'ert) (require 'wttrin) -(require 'testutil-wttrin) ;;; Normal Cases -(ert-deftest test-wttrin--build-url-normal-simple-location-returns-url () - "Test that simple location builds correct URL." - (testutil-wttrin-with-unit-system nil - (should (equal "https://wttr.in/Paris?A" - (wttrin--build-url "Paris"))))) - -(ert-deftest test-wttrin--build-url-normal-location-with-metric-returns-url () - "Test that location with metric unit system builds correct URL." - (testutil-wttrin-with-unit-system "m" - (should (equal "https://wttr.in/London?mA" - (wttrin--build-url "London"))))) - -(ert-deftest test-wttrin--build-url-normal-location-with-uscs-returns-url () - "Test that location with USCS unit system builds correct URL." - (testutil-wttrin-with-unit-system "u" - (should (equal "https://wttr.in/Berlin?uA" - (wttrin--build-url "Berlin"))))) +(ert-deftest test-wttrin--build-url-normal-simple-city () + "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 () + "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 () + "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 () + "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-location-with-spaces-encodes-url () - "Test that location with spaces is properly URL-encoded." - (testutil-wttrin-with-unit-system nil - (should (equal "https://wttr.in/New%20York?A" - (wttrin--build-url "New York"))))) - -(ert-deftest test-wttrin--build-url-boundary-location-with-comma-encodes-url () - "Test that location with comma is properly URL-encoded." - (testutil-wttrin-with-unit-system nil - (should (equal "https://wttr.in/New%20York%2C%20NY?A" - (wttrin--build-url "New York, NY"))))) - -(ert-deftest test-wttrin--build-url-boundary-location-with-special-chars-encodes-url () - "Test that location with special characters is properly URL-encoded." - (testutil-wttrin-with-unit-system "m" - ;; ~Eiffel+Tower format is used by wttr.in for landmarks - ;; ~ is an unreserved character (RFC 3986) and is not encoded - ;; + is encoded as %2B - (should (equal "https://wttr.in/~Eiffel%2BTower?mA" - (wttrin--build-url "~Eiffel+Tower"))))) - -(ert-deftest test-wttrin--build-url-boundary-unicode-location-encodes-url () - "Test that Unicode location is properly URL-encoded." - (testutil-wttrin-with-unit-system nil - ;; Unicode should be properly encoded - (let ((result (wttrin--build-url "東京"))) - (should (string-prefix-p "https://wttr.in/" result)) - (should (string-suffix-p "?A" result)) - ;; Should contain URL-encoded Unicode - (should (string-match-p "%[0-9A-F][0-9A-F]" result))))) - -(ert-deftest test-wttrin--build-url-boundary-empty-location-returns-url () - "Test that empty location builds URL with empty query." - (testutil-wttrin-with-unit-system nil - (should (equal "https://wttr.in/?A" - (wttrin--build-url ""))))) - -(ert-deftest test-wttrin--build-url-boundary-gps-coordinates-encodes-url () - "Test that GPS coordinates are properly URL-encoded." - (testutil-wttrin-with-unit-system nil - ;; Format: -78.46,106.79 - (should (equal "https://wttr.in/-78.46%2C106.79?A" - (wttrin--build-url "-78.46,106.79"))))) +(ert-deftest test-wttrin--build-url-boundary-unicode-city-name () + "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 () + "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-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 () + "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 () + "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 () + "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))))) ;;; Error Cases -(ert-deftest test-wttrin--build-url-error-nil-location-signals-error () - "Test that nil location signals an error." - (testutil-wttrin-with-unit-system nil +(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-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 diff --git a/tests/test-wttrin--make-cache-key.el b/tests/test-wttrin--make-cache-key.el index 442c548..ad2238d 100644 --- a/tests/test-wttrin--make-cache-key.el +++ b/tests/test-wttrin--make-cache-key.el @@ -1,69 +1,94 @@ ;;; test-wttrin--make-cache-key.el --- Tests for wttrin--make-cache-key -*- lexical-binding: t; -*- -;; Copyright (C) 2025 Craig Jennings +;; Copyright (C) 2024 Craig Jennings ;;; Commentary: - ;; Unit tests for wttrin--make-cache-key function. -;; Tests cache key generation from location and unit system. +;; Tests cache key generation to ensure different configurations produce different keys. ;;; Code: (require 'ert) (require 'wttrin) -(require 'testutil-wttrin) ;;; Normal Cases -(ert-deftest test-wttrin--make-cache-key-normal-location-with-metric-returns-key () - "Test that location with metric unit system creates correct cache key." - (testutil-wttrin-with-unit-system "m" - (should (equal "Paris|m" (wttrin--make-cache-key "Paris"))))) - -(ert-deftest test-wttrin--make-cache-key-normal-location-with-uscs-returns-key () - "Test that location with USCS unit system creates correct cache key." - (testutil-wttrin-with-unit-system "u" - (should (equal "New York|u" (wttrin--make-cache-key "New York"))))) - -(ert-deftest test-wttrin--make-cache-key-normal-location-no-unit-returns-default () - "Test that location with no unit system uses default in cache key." - (testutil-wttrin-with-unit-system nil - (should (equal "London|default" (wttrin--make-cache-key "London"))))) +(ert-deftest test-wttrin--make-cache-key-normal-location-with-metric () + "Test cache key generation with metric unit system." + (let ((wttrin-unit-system "m")) + (should (string= "Paris|m" (wttrin--make-cache-key "Paris"))))) -;;; Boundary Cases - -(ert-deftest test-wttrin--make-cache-key-boundary-empty-location-returns-key () - "Test that empty location string creates cache key." - (testutil-wttrin-with-unit-system "m" - (should (equal "|m" (wttrin--make-cache-key ""))))) +(ert-deftest test-wttrin--make-cache-key-normal-location-with-imperial () + "Test cache key generation with USCS/imperial unit system." + (let ((wttrin-unit-system "u")) + (should (string= "London|u" (wttrin--make-cache-key "London"))))) -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-spaces-returns-key () - "Test that location with spaces creates correct cache key." - (testutil-wttrin-with-unit-system "m" - (should (equal "New York, NY|m" (wttrin--make-cache-key "New York, NY"))))) +(ert-deftest test-wttrin--make-cache-key-normal-location-with-nil-system () + "Test cache key generation with nil unit system (default)." + (let ((wttrin-unit-system nil)) + (should (string= "Tokyo|default" (wttrin--make-cache-key "Tokyo"))))) -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-commas-returns-key () - "Test that location with commas creates correct cache key." - (testutil-wttrin-with-unit-system nil - (should (equal "Berlin, DE|default" (wttrin--make-cache-key "Berlin, DE"))))) +(ert-deftest test-wttrin--make-cache-key-normal-different-locations-different-keys () + "Test that different locations produce different cache keys." + (let ((wttrin-unit-system "m")) + (should-not (string= (wttrin--make-cache-key "Paris") + (wttrin--make-cache-key "London"))))) -(ert-deftest test-wttrin--make-cache-key-boundary-unicode-location-returns-key () - "Test that Unicode location creates correct cache key." - (testutil-wttrin-with-unit-system "m" - (should (equal "東京|m" (wttrin--make-cache-key "東京"))))) +;;; Boundary Cases -(ert-deftest test-wttrin--make-cache-key-boundary-location-with-special-chars-returns-key () - "Test that location with special characters creates cache key." - (testutil-wttrin-with-unit-system "m" - (should (equal "~Eiffel+Tower|m" (wttrin--make-cache-key "~Eiffel+Tower"))))) +(ert-deftest test-wttrin--make-cache-key-boundary-same-location-different-units () + "Test that same location with different unit systems produces different keys. +This is critical - cache misses would occur if keys were the same." + (let* ((location "Paris") + (key-metric (let ((wttrin-unit-system "m")) + (wttrin--make-cache-key location))) + (key-imperial (let ((wttrin-unit-system "u")) + (wttrin--make-cache-key location))) + (key-default (let ((wttrin-unit-system nil)) + (wttrin--make-cache-key location)))) + (should-not (string= key-metric key-imperial)) + (should-not (string= key-metric key-default)) + (should-not (string= key-imperial key-default)))) + +(ert-deftest test-wttrin--make-cache-key-boundary-location-with-special-chars () + "Test cache key generation with special characters in location." + (let ((wttrin-unit-system "m")) + (should (string= "São Paulo|m" (wttrin--make-cache-key "São Paulo"))))) + +(ert-deftest test-wttrin--make-cache-key-boundary-location-with-unicode () + "Test cache key generation with Unicode characters." + (let ((wttrin-unit-system nil)) + (should (string= "北京|default" (wttrin--make-cache-key "北京"))))) + +(ert-deftest test-wttrin--make-cache-key-boundary-location-with-comma () + "Test cache key generation with comma in location (e.g., 'City, Country')." + (let ((wttrin-unit-system "m")) + (should (string= "London, GB|m" (wttrin--make-cache-key "London, GB"))))) + +(ert-deftest test-wttrin--make-cache-key-boundary-empty-string-location () + "Test cache key generation with empty string location." + (let ((wttrin-unit-system "m")) + (should (string= "|m" (wttrin--make-cache-key ""))))) + +(ert-deftest test-wttrin--make-cache-key-boundary-location-with-pipe-char () + "Test cache key with location containing pipe character (separator). +This could potentially cause cache key parsing issues." + (let ((wttrin-unit-system "m")) + (let ((key (wttrin--make-cache-key "Test|Location"))) + (should (string-match-p "|" key)) + ;; Should contain TWO pipe characters: one from location, one as separator + (should (= 2 (cl-count ?| key)))))) ;;; Error Cases -(ert-deftest test-wttrin--make-cache-key-error-nil-location-returns-key () - "Test that nil location creates cache key with empty string." - ;; Note: concat converts nil to empty string, this documents current behavior - (testutil-wttrin-with-unit-system "m" - (should (equal "|m" (wttrin--make-cache-key nil))))) +(ert-deftest test-wttrin--make-cache-key-error-consistent-keys () + "Test that calling with same inputs always produces same key (idempotent)." + (let ((wttrin-unit-system "m")) + (let ((key1 (wttrin--make-cache-key "Paris")) + (key2 (wttrin--make-cache-key "Paris")) + (key3 (wttrin--make-cache-key "Paris"))) + (should (string= key1 key2)) + (should (string= key2 key3))))) (provide 'test-wttrin--make-cache-key) ;;; test-wttrin--make-cache-key.el ends here diff --git a/tests/test-wttrin-additional-url-params.el b/tests/test-wttrin-additional-url-params.el index 60918a4..d3cb369 100644 --- a/tests/test-wttrin-additional-url-params.el +++ b/tests/test-wttrin-additional-url-params.el @@ -1,60 +1,52 @@ ;;; test-wttrin-additional-url-params.el --- Tests for wttrin-additional-url-params -*- lexical-binding: t; -*- -;; Copyright (C) 2025 Craig Jennings +;; Copyright (C) 2024 Craig Jennings ;;; Commentary: - ;; Unit tests for wttrin-additional-url-params function. -;; Tests URL parameter generation with different unit system configurations. +;; Tests URL parameter generation based on unit system configuration. ;;; Code: (require 'ert) (require 'wttrin) -(require 'testutil-wttrin) ;;; Normal Cases -(ert-deftest test-wttrin-additional-url-params-normal-metric-returns-param () - "Test that metric unit system returns ?m parameter." - (testutil-wttrin-with-unit-system "m" - (should (equal "?m" (wttrin-additional-url-params))))) +(ert-deftest test-wttrin-additional-url-params-normal-metric-system () + "Test URL params with metric unit system." + (let ((wttrin-unit-system "m")) + (should (string= "?m" (wttrin-additional-url-params))))) -(ert-deftest test-wttrin-additional-url-params-normal-uscs-returns-param () - "Test that USCS unit system returns ?u parameter." - (testutil-wttrin-with-unit-system "u" - (should (equal "?u" (wttrin-additional-url-params))))) +(ert-deftest test-wttrin-additional-url-params-normal-uscs-system () + "Test URL params with USCS (imperial) unit system." + (let ((wttrin-unit-system "u")) + (should (string= "?u" (wttrin-additional-url-params))))) -(ert-deftest test-wttrin-additional-url-params-normal-wind-speed-returns-param () - "Test that wind speed unit returns ?M parameter." - (testutil-wttrin-with-unit-system "M" - (should (equal "?M" (wttrin-additional-url-params))))) +(ert-deftest test-wttrin-additional-url-params-normal-wind-speed-meters () + "Test URL params with wind speed in m/s." + (let ((wttrin-unit-system "M")) + (should (string= "?M" (wttrin-additional-url-params))))) ;;; Boundary Cases (ert-deftest test-wttrin-additional-url-params-boundary-nil-returns-question-mark () - "Test that nil unit system returns just ? parameter." - (testutil-wttrin-with-unit-system nil - (should (equal "?" (wttrin-additional-url-params))))) - -(ert-deftest test-wttrin-additional-url-params-boundary-empty-string-returns-question-mark () - "Test that empty string unit system returns just ? parameter." - (testutil-wttrin-with-unit-system "" - (should (equal "?" (wttrin-additional-url-params))))) + "Test URL params when unit system is nil (default/location-based)." + (let ((wttrin-unit-system nil)) + (should (string= "?" (wttrin-additional-url-params))))) -(ert-deftest test-wttrin-additional-url-params-boundary-single-char-returns-param () - "Test that single character unit system returns ?<char> parameter." - (testutil-wttrin-with-unit-system "x" - (should (equal "?x" (wttrin-additional-url-params))))) +(ert-deftest test-wttrin-additional-url-params-boundary-empty-string () + "Test URL params with empty string unit system." + (let ((wttrin-unit-system "")) + (should (string= "?" (wttrin-additional-url-params))))) ;;; Error Cases -(ert-deftest test-wttrin-additional-url-params-error-number-signals-error () - "Test that number unit system signals a type error." - ;; concat requires string or sequence, number causes wrong-type-argument error - (testutil-wttrin-with-unit-system 123 - (should-error (wttrin-additional-url-params) - :type 'wrong-type-argument))) +(ert-deftest test-wttrin-additional-url-params-error-unbound-variable () + "Test behavior when wttrin-unit-system is unbound (should use default nil)." + ;; This test verifies the defcustom default value + (let ((wttrin-unit-system nil)) ; Simulate default value + (should (string= "?" (wttrin-additional-url-params))))) (provide 'test-wttrin-additional-url-params) ;;; test-wttrin-additional-url-params.el ends here |
