final tt updates
[emacs-init.git] / elpa / typescript-mode-20200430.1232 / typescript-mode-test-utilities.el
1 ;;; typescript-mode-test-utilities --- This file contains test utilities for typescript-mode.el
2
3 ;;; Commentary:
4 ;; See typescript-mode-tests.el and typescript-mode-jsdoc-tests.el
5
6 ;;; Code:
7
8 (require 'ert)
9 (require 'typescript-mode)
10
11 ;; Adapted from jdee-mode's test suite.
12 (defmacro test-with-temp-buffer (content &rest body)
13   "Fill a temporary buffer with `CONTENT' and eval `BODY' in it."
14   (declare (debug t)
15            (indent 1))
16   `(with-temp-buffer
17      (insert ,content)
18      (typescript-mode)
19      (goto-char (point-min))
20      ;; We need this so that tests that simulate user actions operate on the right buffer.
21      (switch-to-buffer (current-buffer))
22      ,@body))
23
24 (defmacro test-with-fontified-buffer (content &rest body)
25   "Fill a temporary buffer with `CONTENT' and eval `BODY' in it."
26   (declare (debug t)
27            (indent 1))
28   `(test-with-temp-buffer
29     ,content
30      (font-lock-fontify-buffer)
31      ,@body))
32
33 (defun get-face-at (loc)
34   "Get the face at `LOC'.
35 If it is not a number, then we `re-search-forward' with `LOC'
36 as the search pattern."
37   (when (not (numberp loc))
38     (save-excursion
39       (re-search-forward loc)
40       (setq loc (match-beginning 0))))
41   (get-text-property loc 'face))
42
43 (defun font-lock-test (contents expected)
44   "Perform a test on our template.
45 `CONTENTS' is the string to put in the temporary buffer.
46 `EXPECTED' is the expected results.
47 It should be a list of (LOCATION . FACE) pairs, where
48 LOCATION can be either a single location, or list of locations,
49 that are all expected to have the same face."
50   (test-with-fontified-buffer
51    contents
52    ;; Make sure our propertize function has been applied to the whole
53    ;; buffer.
54    (syntax-propertize (point-max))
55    (dolist (spec expected)
56      (if (listp (car spec))
57          (dolist (source (car spec))
58            (should (eq (get-face-at source) (cdr spec))))
59        (should (eq (get-face-at (car spec)) (cdr spec)))))))
60
61 (provide 'typescript-mode-test-utilities)
62
63 ;;; typescript-mode-test-utilities.el ends here