emacs-emojify

fork of https://github.com/iqbalansari/emacs-emojify
Log | Files | Refs | LICENSE

test-helper.el (7143B)


      1 ;;; test-helper.el --- Tests for emojify              -*- lexical-binding: t; -*-
      2 
      3 ;;; Commentary:
      4 ;;; Helpers to write tests for emojify
      5 
      6 ;;; Code:
      7 
      8 ;; Setup load-path, some of this is redundant when tests are run from the
      9 ;; command line
     10 (let ((project-dir (locate-dominating-file (or (buffer-file-name) load-file-name)
     11                                            ".cask")))
     12   (if (not project-dir)
     13       (user-error "Could not locate project root")
     14     (let ((default-directory (expand-file-name (format ".cask/%d.%d"
     15                                                        emacs-major-version
     16                                                        emacs-minor-version)
     17                                                project-dir)))
     18       (normal-top-level-add-subdirs-to-load-path))
     19     (add-to-list 'load-path project-dir)))
     20 
     21 ;; Libs required for tests
     22 (require 'ert)
     23 (require 'el-mock)
     24 (eval-when-compile
     25   (require 'cl))
     26 (require 'cl-lib)
     27 (require 'noflet)
     28 
     29 (when (require 'undercover nil t)
     30   (undercover "*.el"))
     31 
     32 ;; Load emojify
     33 (require 'emojify)
     34 
     35 ;; Define custom emoji config
     36 (defvar emojify-test-custom-emojis)
     37 (let* ((project-dir (locate-dominating-file (or (buffer-file-name) load-file-name)
     38                                             ".cask"))
     39        (custom-emoji-dir (expand-file-name "test/assets/" project-dir)))
     40   (setq emojify-test-custom-emojis
     41         `((":troll:" . (("name" . "Troll") ("image" . ,(expand-file-name "trollface.png" custom-emoji-dir)) ("style" . "github")))
     42           (":neckbeard:" . (("name" . "Neckbeard") ("image" . ,(expand-file-name "neckbeard.png" custom-emoji-dir)) ("style" . "github")))
     43           ("λ" .  (("name" . "Lambda") ("image" . ,(expand-file-name "lambda.png" custom-emoji-dir)) ("style" . "unicode"))))))
     44 
     45 ;; Helper macros for tests
     46 (defmacro emojify-tests-with-saved-customizations (&rest forms)
     47   "Run FORMS saving current customizations and restoring them on completion.
     48 
     49 Helps isolate tests from each other's customizations."
     50   (declare (indent 0))
     51   `(let ((emojify-saved-emoji-json emojify-emoji-json)
     52          (emojify-saved-user-emojis emojify-user-emojis)
     53          (emojify-saved-user-emojis-parsed emojify--user-emojis)
     54          (emojify-saved-emojify-regexps emojify-regexps)
     55          (emojify-saved-display-style emojify-display-style)
     56          (emojify-saved-inhibit-major-modes emojify-inhibit-major-modes)
     57          (emojify-saved-inhibit-in-buffer-functions emojify-inhibit-in-buffer-functions)
     58          (emojify-saved-emoji-style emojify-emoji-styles)
     59          (emojify-saved-program-contexts emojify-program-contexts)
     60          (emojify-saved-inhibit-functions emojify-inhibit-functions)
     61          (emojify-saved-point-entered-behaviour emojify-point-entered-behaviour)
     62          (emojify-saved-show-help emojify-show-help)
     63          (emojify-saved-reveal-on-isearch emojify-reveal-on-isearch)
     64          (emojify-saved-composed-text-p emojify-composed-text-p))
     65      (unwind-protect
     66          (progn
     67            (unless (file-exists-p (emojify-image-dir))
     68              (emojify-download-emoji emojify-emoji-set))
     69            (emojify-debug-mode +1)
     70            (setq emojify-composed-text-p nil)
     71            ,@forms)
     72        (setq emojify-emoji-json emojify-saved-emoji-json
     73              emojify-display-style emojify-saved-display-style
     74              emojify-inhibit-major-modes emojify-saved-inhibit-major-modes
     75              emojify-user-emojis emojify-saved-user-emojis
     76              emojify--user-emojis emojify-saved-user-emojis-parsed
     77              emojify-regexps emojify-saved-emojify-regexps
     78              emojify-inhibit-in-buffer-functions emojify-saved-inhibit-in-buffer-functions
     79              emojify-program-contexts emojify-saved-program-contexts
     80              emojify-inhibit-functions emojify-saved-inhibit-functions
     81              emojify-point-entered-behaviour emojify-saved-point-entered-behaviour
     82              emojify-show-help emojify-saved-show-help
     83              emojify-reveal-on-isearch emojify-saved-reveal-on-isearch
     84              emojify-composed-text-p emojify-saved-composed-text-p)
     85        (emojify-set-emoji-styles emojify-saved-emoji-style))))
     86 
     87 (defmacro emojify-tests-with-emojified-buffer (str &rest forms)
     88   "Create a buffer with STR and execute FORMS.
     89 
     90 The FORMS are executed with emojify enabled."
     91   (declare (indent 1))
     92   ;; Run tests in a new buffer
     93   `(let ((test-buffer (get-buffer-create " *emojify-test-buffer*")))
     94      (noflet ((emojify-buffer-p (buffer)
     95                                 (or (string-match-p "^ \\*emojify-test-buffer\\*" (buffer-name buffer))
     96                                     (funcall this-fn buffer))))
     97        (unwind-protect
     98            (save-window-excursion
     99              (switch-to-buffer test-buffer)
    100              ;; Rename it uniquely so that subsequent buffers do not conflict with it
    101              (rename-uniquely)
    102              ;; Save all possible customizations
    103              (emojify-tests-with-saved-customizations
    104                (setq emojify-point-entered-behaviour nil)
    105                (insert ,str)
    106                (emojify-mode +1)
    107                ;; Force refontification since JIT does it lazily
    108                (emojify-display-emojis-in-region (point-min) (point-max))
    109                (goto-char (point-min))
    110                ,@forms))
    111          ;; Keep the buffer around for interactive tests, helps debugging failing
    112          ;; tests
    113          (when noninteractive
    114            (kill-buffer test-buffer))))))
    115 
    116 (defmacro emojify-tests-with-emojified-static-buffer (str &rest forms)
    117   "Create a buffer with STR and execute FORMS.
    118 
    119 All kinds of dynamic behaviour on buffer are disabled.  See
    120 `emojify-with-saved-buffer-state'"
    121   (declare (indent 1))
    122   `(emojify-tests-with-emojified-buffer ,str
    123      (emojify-with-saved-buffer-state
    124        ,@forms)))
    125 
    126 (defun emojify-tests-should-be-emojified (point)
    127   "Assert there is an emoji at POINT."
    128   (should (get-text-property point 'emojified))
    129   (should (get-text-property point 'emojify-display))
    130   (should (get-text-property point 'emojify-buffer))
    131   (should (get-text-property point 'emojify-beginning))
    132   (should (get-text-property point 'emojify-end))
    133   (should (get-text-property point 'emojify-text))
    134   (should (get-text-property point 'display)))
    135 
    136 (defun emojify-tests-should-not-be-emojified (point)
    137   "Assert there is not emoji at POINT."
    138   (should-not (get-text-property point 'emojified))
    139   (should-not (get-text-property point 'emojify-display))
    140   (should-not (get-text-property point 'emojify-buffer))
    141   (should-not (get-text-property point 'emojify-beginning))
    142   (should-not (get-text-property point 'emojify-end))
    143   (should-not (get-text-property point 'emojify-text))
    144   (should-not (get-text-property point 'display)))
    145 
    146 (defun emojify-tests-should-be-uncovered (point)
    147   "Assert the emoji at POINT is uncovered."
    148   (should (get-text-property point 'emojified))
    149   (should (get-text-property point 'emojify-buffer))
    150   (should (get-text-property point 'emojify-beginning))
    151   (should (get-text-property point 'emojify-end))
    152   (should (get-text-property point 'emojify-text))
    153   (should-not (get-text-property point 'display)))
    154 
    155 ;;; test-helper.el ends here