|
| 1 | +;;; purescript-font-lock-tests.el --- Unit tests for purescript font-lock -*- lexical-binding: t -*- |
| 2 | + |
| 3 | +;; Copyright (c) 2025 Konstantin Kharlamov. All rights reserved. |
| 4 | + |
| 5 | +;; This file is free software; you can redistribute it and/or modify |
| 6 | +;; it under the terms of the GNU General Public License as published by |
| 7 | +;; the Free Software Foundation; either version 3, or (at your option) |
| 8 | +;; any later version. |
| 9 | + |
| 10 | +;; This file is distributed in the hope that it will be useful, |
| 11 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +;; GNU General Public License for more details. |
| 14 | + |
| 15 | +;; You should have received a copy of the GNU General Public License |
| 16 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +;;; Code: |
| 19 | + |
| 20 | +(require 'ert) |
| 21 | +(require 'purescript-mode) |
| 22 | + |
| 23 | +(defun purescript-test-ranges (text ranges-list) |
| 24 | + (with-temp-buffer |
| 25 | + (insert text) |
| 26 | + (purescript-mode) |
| 27 | + (font-lock-ensure) |
| 28 | + (let ((ret |
| 29 | + (catch 'fail |
| 30 | + (dolist (range ranges-list) |
| 31 | + (let ((begin (nth 0 range)) |
| 32 | + (end (nth 1 range)) |
| 33 | + (face-expected (nth 2 range))) |
| 34 | + (dolist (pos (number-sequence begin end)) |
| 35 | + (let ((face-found (get-char-property pos 'face))) |
| 36 | + (when (not (eq face-found face-expected)) |
| 37 | + (throw 'fail `(,begin ,end ,face-expected ,face-found ,pos))))))) |
| 38 | + nil))) |
| 39 | + (when ret |
| 40 | + (message "Range [%d:%d] has face %s (expected %s) at %d" |
| 41 | + (nth 0 ret) (nth 1 ret) (nth 3 ret) (nth 2 ret) (nth 4 ret)) |
| 42 | + (should-not ret))))) |
| 43 | + |
| 44 | +(ert-deftest imports () |
| 45 | + (purescript-test-ranges |
| 46 | + "import Data.Array (many) |
| 47 | +import Data.Array as Array |
| 48 | +import Data.Either (Either(..)) |
| 49 | +" '((1 6 font-lock-keyword-face) |
| 50 | + (8 17 font-lock-type-face) |
| 51 | + (26 31 font-lock-keyword-face) |
| 52 | + (33 42 font-lock-type-face) |
| 53 | + (44 45 font-lock-keyword-face) |
| 54 | + (47 51 font-lock-type-face) |
| 55 | + (53 58 font-lock-keyword-face) |
| 56 | + (60 70 font-lock-type-face) |
| 57 | + (73 78 font-lock-type-face) |
| 58 | + (80 81 font-lock-variable-name-face)))) |
| 59 | + |
| 60 | +(ert-deftest string () |
| 61 | + (purescript-test-ranges |
| 62 | + "foo = \"hello\"" |
| 63 | + '((1 3 font-lock-function-name-face) |
| 64 | + (5 5 font-lock-variable-name-face) |
| 65 | + (7 13 font-lock-string-face)))) |
| 66 | + |
| 67 | +(ert-deftest multiline-string () |
| 68 | + (purescript-test-ranges |
| 69 | + "foo = \"\"\" |
| 70 | +hello |
| 71 | +\"\"\" |
| 72 | +" |
| 73 | + '((1 3 font-lock-function-name-face) |
| 74 | + (5 5 font-lock-variable-name-face) |
| 75 | + (7 19 font-lock-string-face)))) |
| 76 | + |
| 77 | +(ert-deftest multiline-string-with-hash () |
| 78 | + :expected-result :failed |
| 79 | + (purescript-test-ranges |
| 80 | + "foo = \"\"\" |
| 81 | +# a string with hashtag |
| 82 | + # another # one |
| 83 | +\"\"\" |
| 84 | +" |
| 85 | + '((1 3 font-lock-function-name-face) |
| 86 | + (5 5 font-lock-variable-name-face) |
| 87 | + (7 55 font-lock-string-face)))) |
| 88 | + |
| 89 | +(ert-deftest multiline-string-with-embedded-strings () |
| 90 | + :expected-result :failed |
| 91 | + (purescript-test-ranges |
| 92 | + "foo = \"\"\" |
| 93 | +this = \"still a string\" |
| 94 | +\"\"\" |
| 95 | +" |
| 96 | + '((1 3 font-lock-function-name-face) |
| 97 | + (5 5 font-lock-variable-name-face) |
| 98 | + (7 37 font-lock-string-face)))) |
0 commit comments