branch: externals/num3-mode commit a290e3de2ad0405737a843d7b4dd1c765b873dc3 Author: Michal Nazarewicz <min...@mina86.com> Commit: Michal Nazarewicz <min...@mina86.com>
Add unit tests * Makefile: New file with most notably ‘test’ command to run tests * test.el: New file with unit tests for ‘num3-mode’ --- Makefile | 15 ++++++++++ test.el | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..786f375dd7 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +EMACS = emacs + +all: num3-mode.elc + +%.elc : %.el + @echo Compiling $< + $(EMACS) -batch -q -no-site-file -L . -f batch-byte-compile $< + +test: all + $(EMACS) -batch -L . -l test.el -f ert-run-tests-batch-and-exit + +clean: + rm -f -- *.elc + +.PHONY: all clean test diff --git a/test.el b/test.el new file mode 100644 index 0000000000..59d0747f97 --- /dev/null +++ b/test.el @@ -0,0 +1,102 @@ +;;; test.el --- num3-mode tests -*- lexical-binding: t -*- + +;; Copyright (C) 2022 Free Software Foundation, Inc. + +;; Author: Michal Nazarewicz <min...@mina86.com> + +;; This program 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. + +;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>. + +;;; Code: + +(require 'ert) +(require 'num3-mode) + +(defun num3-mode-test (text want) + "Runs `num3-mode` matcher on TEXT and checks its fontification. +WANT describes expected fontification. It’s a copy of TEXT +except characters which are expected to use odd face are replaced +by underscore (‘_’) and those which use even face are replaced by +caret (‘^’). For example: + + (num3-mode-test \"6.2831853071\" + \"6.^^^___^^^_\")" + (with-temp-buffer + (insert text) + (setq text (copy-sequence text)) + (goto-char (point-min)) + (cl-letf ((num3-group-size 3) + (num3-threshold 5) + ((symbol-function 'font-lock-append-text-property) + (lambda (lo hi prop face) + (should (<= (point-min) lo hi (point-max))) + (should (< lo hi)) + (should (eq 'face prop)) + (should (memq face '(num3-face-even num3-face-odd))) + (let ((start (- lo (point-min))) + (char (if (eq face 'num3-face-even) ?^ ?_))) + (dotimes (offset (- hi lo)) + (aset text (+ start offset) char)))))) + (num3--matcher (point-max)))) + (should (equal want text))) + +(ert-deftest num3-mode-decimal-integers () + ;; Won’t fontify unless at least five digits. + (num3-mode-test "123 1234 12345 123456789" + "123 1234 ^^___ ___^^^___")) + +(ert-deftest num3-mode-octal-integers () + ;; There’s actually no special handling for octal numbers. The ‘Oo’ + ;; and ‘#o’ prefixes ignored. The end result is that we group octal + ;; numbers just like decimal numbers. + (num3-mode-test "012 0123 01234 01234567" + "012 0123 ^^___ __^^^___") + (num3-mode-test "0o123 0o1234 0o12345 0o12345671" + "0o123 0o1234 0o^^___ 0o__^^^___") + (num3-mode-test "#o123 #o1234 #o12345 #o12345671" + "#o123 #o1234 #o^^___ #o__^^^___") + (num3-mode-test "0O123 0O1234 0O12345 0O12345671" + "0O123 0O1234 0O^^___ 0O__^^^___") + (num3-mode-test "#O123 #O1234 #O12345 #O12345671" + "#O123 #O1234 #O^^___ #O__^^^___")) + +(ert-deftest num3-mode-hexadecimal-integers () + ;; Won’t fontify unless at least five digits. + (num3-mode-test "0x1b3 0x1b3d 0x1b3d5 0x1B3D5F789" + "0x1b3 0x1b3d 0x^____ 0x_^^^^____") + (num3-mode-test "#x1b3 #x1b3d #x1b3d5 #x1B3D5F789" + "#x1b3 #x1b3d #x^____ #x_^^^^____") + (num3-mode-test "0X1b3 0X1b3d 0X1b3d5 0X1B3D5F789" + "0X1b3 0X1b3d 0X^____ 0X_^^^^____") + (num3-mode-test "#X1b3 #X1b3d #X1b3d5 #X1B3D5F789" + "#X1b3 #X1b3d #X^____ #X_^^^^____") + ;; Some hexadecimal numbers are recognised even without prefix. For + ;; that to happen, they must include a decimal digit. + (num3-mode-test "ABCD5678 abcd5678 ABCDABCD" + "^^^^____ ^^^^____ ABCDABCD")) + +(ert-deftest num3-mode-binary-integers () + (num3-mode-test "0b101 0b1010 0b10101 0b101010101" + "0b101 0b1010 0b^____ 0b_^^^^____") + (num3-mode-test "#b101 #b1010 #b10101 #b101010101" + "#b101 #b1010 #b^____ #b_^^^^____") + (num3-mode-test "0B101 0B1010 0B10101 0B101010101" + "0B101 0B1010 0B^____ 0B_^^^^____") + (num3-mode-test "#B101 #B1010 #B10101 #B101010101" + "#B101 #B1010 #B^____ #B_^^^^____")) + +(ert-deftest num3-mode-decimal-fractions () + (num3-mode-test "6.28 6.2831 6.28318 23456.28318 .12345" + "6.28 6.2831 6.___^^ ^^___.___^^ .___^^")) + +;;; test.el ends here