]> git.eshelyaron.com Git - emacs.git/commitdiff
Use lexical-binding in hfy-cmap.el and add tests
authorStefan Kangas <stefan@marxist.se>
Wed, 21 Oct 2020 11:05:32 +0000 (13:05 +0200)
committerStefan Kangas <stefan@marxist.se>
Wed, 21 Oct 2020 11:06:38 +0000 (13:06 +0200)
* lisp/hfy-cmap.el: Use lexical-binding.
(hfy-cmap--parse-buffer): Extract from...
(htmlfontify-load-rgb-file): ...here.

* test/lisp/hfy-cmap-resources/rgb.txt:
* test/lisp/hfy-cmap-tests.el: New files.

lisp/hfy-cmap.el
test/lisp/hfy-cmap-resources/rgb.txt [new file with mode: 0644]
test/lisp/hfy-cmap-tests.el [new file with mode: 0644]

index 4cff2a4200157f272098b1f6e7917ea9f38aa9ba..a3398f6e809bb5a083a4c2010371371a29e6fe00 100644 (file)
@@ -1,4 +1,4 @@
-;;; hfy-cmap.el --- Fallback color name -> rgb mapping for `htmlfontify'
+;;; hfy-cmap.el --- Fallback color name -> rgb mapping for `htmlfontify'  -*- lexical-binding:t -*-
 
 ;; Copyright (C) 2002-2003, 2009-2020 Free Software Foundation, Inc.
 
 (defconst hfy-rgb-regex
   "^\\s-*\\([0-9]+\\)\\s-+\\([0-9]+\\)\\s-+\\([0-9]+\\)\\s-+\\(.+\\)\\s-*$")
 
+(defun hfy-cmap--parse-buffer (buffer)
+  (with-current-buffer buffer
+    (let ((end-of-rgb 0)
+          result)
+      (goto-char (point-min))
+      (htmlfontify-unload-rgb-file)
+      (while (/= end-of-rgb 1)
+        (if (looking-at hfy-rgb-regex)
+            (push (list (match-string 4)
+                        (string-to-number (match-string 1))
+                        (string-to-number (match-string 2))
+                        (string-to-number (match-string 3)))
+                  result))
+        (setq end-of-rgb (forward-line)))
+      result)))
+
 ;;;###autoload
 (defun htmlfontify-load-rgb-file (&optional file)
   "Load an X11 style rgb.txt FILE.
@@ -818,25 +834,14 @@ Loads the variable `hfy-rgb-txt-color-map', which is used by
   (interactive
    (list
     (read-file-name "rgb.txt (equivalent) file: " "" nil t (hfy-rgb-file))))
-  (let ((rgb-buffer   nil)
-       (end-of-rgb     0)
-       (rgb-txt      nil))
-    (if (and (setq rgb-txt (or file (hfy-rgb-file)))
-            (file-readable-p rgb-txt))
-       (with-current-buffer
-            (setq rgb-buffer (find-file-noselect rgb-txt 'nowarn))
-          (goto-char (point-min))
-         (htmlfontify-unload-rgb-file)
-         (while (/= end-of-rgb 1)
-           (if (looking-at hfy-rgb-regex)
-               (setq hfy-rgb-txt-color-map
-                     (cons (list (match-string 4)
-                                 (string-to-number (match-string 1))
-                                 (string-to-number (match-string 2))
-                                 (string-to-number (match-string 3)))
-                           hfy-rgb-txt-color-map)) )
-           (setq end-of-rgb (forward-line)))
-         (kill-buffer rgb-buffer)))))
+  (let ((rgb-buffer nil)
+        (rgb-txt (or file (hfy-rgb-file))))
+    (when (and rgb-txt
+               (file-readable-p rgb-txt))
+      (setq rgb-buffer (find-file-noselect rgb-txt 'nowarn))
+      (when-let ((result (hfy-cmap--parse-buffer rgb-buffer)))
+        (setq hfy-rgb-txt-color-map result))
+      (kill-buffer rgb-buffer))))
 
 (defun htmlfontify-unload-rgb-file ()
   "Unload the current color name -> rgb translation map."
diff --git a/test/lisp/hfy-cmap-resources/rgb.txt b/test/lisp/hfy-cmap-resources/rgb.txt
new file mode 100644 (file)
index 0000000..86a0053
--- /dev/null
@@ -0,0 +1,3 @@
+255 250 250            snow
+248 248 255            ghost white
+248 248 255            GhostWhite
diff --git a/test/lisp/hfy-cmap-tests.el b/test/lisp/hfy-cmap-tests.el
new file mode 100644 (file)
index 0000000..4cdc6ff
--- /dev/null
@@ -0,0 +1,55 @@
+;;; hfy-cmap-tests.el --- tests for hfy-cmap.el -*- lexical-binding: t -*-
+
+;; Copyright (C) 2020 Free Software Foundation, Inc.
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
+
+;;; Code:
+
+(require 'ert)
+(require 'ert-x)
+(require 'hfy-cmap)
+
+(defconst hfy-cmap-tests--data
+  (concat "255 250 250         snow\n"
+          "248 248 255         ghost white\n"
+          "248 248 255         GhostWhite\n"))
+
+(defconst hfy-cmap-tests--parsed
+  '(("GhostWhite" 248 248 255)
+    ("ghost white" 248 248 255)
+    ("snow" 255 250 250)))
+
+(ert-deftest test-hfy-cmap--parse-buffer ()
+  (with-temp-buffer
+    (insert hfy-cmap-tests--data)
+    (should (equal (hfy-cmap--parse-buffer (current-buffer))
+                   hfy-cmap-tests--parsed))))
+
+(ert-deftest test-htmlfontify-load-rgb-file ()
+  :tags '(:expensive-test)
+  (let (hfy-rgb-txt-color-map)
+    (htmlfontify-load-rgb-file (ert-resource-file "rgb.txt"))
+    (should (equal hfy-rgb-txt-color-map
+                   hfy-cmap-tests--parsed))))
+
+(ert-deftest test-htmlfontify-load-rgb-file/non-existent-file ()
+  (let (hfy-rgb-txt-color-map)
+    (htmlfontify-load-rgb-file "/non/existent/file")
+    (should-not hfy-rgb-txt-color-map)))
+
+(provide 'hfy-cmap-tests)
+;;; hfy-cmap-tests.el ends here