aboutsummaryrefslogtreecommitdiff
path: root/tests/test-wttrin-geolocation--parse-ipinfo.el
blob: 4543e32a0e7d5096390cf4afdfbf1bd2c0ac0c59 (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
;;; test-wttrin-geolocation--parse-ipinfo.el --- Tests for ipinfo.io response parser -*- lexical-binding: t; -*-

;; Copyright (C) 2026 Craig Jennings

;;; Commentary:
;; Unit tests for `wttrin-geolocation--parse-ipinfo'.  Pure function — no
;; network, no async.

;;; Code:

(require 'ert)
(require 'wttrin-geolocation)

;;; Setup and Teardown

(defun test-wttrin-geolocation--parse-ipinfo-setup ()
  "Setup for ipinfo parser tests."
  nil)

(defun test-wttrin-geolocation--parse-ipinfo-teardown ()
  "Teardown for ipinfo parser tests."
  nil)

;;; Normal Cases

(ert-deftest test-wttrin-geolocation--parse-ipinfo-normal-full-response-returns-formatted-string ()
  "A full ipinfo.io response yields \"City, Region\"."
  (let ((json "{\"ip\":\"8.8.8.8\",\"city\":\"Mountain View\",\"region\":\"California\",\"country\":\"US\",\"loc\":\"37.3860,-122.0838\",\"org\":\"AS15169 Google LLC\",\"postal\":\"94035\",\"timezone\":\"America/Los_Angeles\"}"))
    (should (string= "Mountain View, California"
                     (wttrin-geolocation--parse-ipinfo json)))))

;;; Boundary Cases

(ert-deftest test-wttrin-geolocation--parse-ipinfo-boundary-minimal-fields-works ()
  "A response with only city and region parses correctly."
  (let ((json "{\"city\":\"Berlin\",\"region\":\"Berlin\"}"))
    (should (string= "Berlin, Berlin"
                     (wttrin-geolocation--parse-ipinfo json)))))

;;; Error Cases

(ert-deftest test-wttrin-geolocation--parse-ipinfo-error-nil-input-returns-nil ()
  "A nil input string returns nil."
  (should-not (wttrin-geolocation--parse-ipinfo nil)))

(ert-deftest test-wttrin-geolocation--parse-ipinfo-error-malformed-json-returns-nil ()
  "Malformed JSON returns nil rather than signalling."
  (should-not (wttrin-geolocation--parse-ipinfo "not json at all")))

(ert-deftest test-wttrin-geolocation--parse-ipinfo-error-missing-city-returns-nil ()
  "A response without a city field returns nil."
  (let ((json "{\"region\":\"California\",\"country\":\"US\"}"))
    (should-not (wttrin-geolocation--parse-ipinfo json))))

(ert-deftest test-wttrin-geolocation--parse-ipinfo-error-missing-region-returns-nil ()
  "A response without a region field returns nil."
  (let ((json "{\"city\":\"Mountain View\",\"country\":\"US\"}"))
    (should-not (wttrin-geolocation--parse-ipinfo json))))

(provide 'test-wttrin-geolocation--parse-ipinfo)
;;; test-wttrin-geolocation--parse-ipinfo.el ends here