branch: externals/bnf-mode commit aa8e9af7f8ee7240bbe397a0f86d64df9ce2ec4c Author: Serghei Iakovlev <serg...@phalconphp.com> Commit: Serghei Iakovlev <serg...@phalconphp.com>
Initial test case --- .travis.yml | 2 +- test/README.org | 6 +++ test/bnf-mode-font-test.el | 58 +++++++++++++++++++++++++ test/test-helper.el | 103 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 55dacf5..623e8a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -73,7 +73,7 @@ script: - make init # The 'checkdoc-file' present on Emacs >= 25.1 - '[[ "$EMACS_MAJOR_VERSION" = "24" ]] || make checkdoc' - # - make test + - make test notifications: email: false diff --git a/test/README.org b/test/README.org new file mode 100644 index 0000000..0e52e8f --- /dev/null +++ b/test/README.org @@ -0,0 +1,6 @@ +#+TITLE: BNF Mode Unit Tests +#+AUTHOR: Serghei Iakovlev + +The non-interactive Unit Test Suite for the BNF Mode. + +For a detailed description of the BNF Mode, refer to [[https://github.com/sergeyklay/bnf-mode/blob/master/README.org][README.org]] file. diff --git a/test/bnf-mode-font-test.el b/test/bnf-mode-font-test.el new file mode 100644 index 0000000..a0b3805 --- /dev/null +++ b/test/bnf-mode-font-test.el @@ -0,0 +1,58 @@ +;;; bnf-mode-font-test.el --- BNF Mode: Font highlighting test suite -*- lexical-binding: t; -*- + +;; Copyright (C) 2019 Serghei Iakovlev + +;; Author: Serghei Iakovlev (concat "sadhooklay" "@" "gmail" ".com") +;; Maintainer: Serghei Iakovlev +;; Version: 0.2.0 +;; URL: https://github.com/sergeyklay/bnf-mode + +;; This file is not part of GNU Emacs. + +;;; License + +;; This file 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 file 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 file; if not, write to the Free Software +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; Automate tests from the "test" directory using `ert', which comes bundled +;; with Emacs >= 24.1. + +;;; Code: + + +;;;; Utilities + +(defun bnf-test-face-at (pos &optional content) + "Get the face at POS in CONTENT. + +If CONTENT is not given, return the face at POS in the current +buffer." + (if content + (bnf-test-with-temp-buffer content + (get-text-property pos 'face)) + (get-text-property pos 'face))) + + +;;;; Font locking + +(ert-deftest bnf-mode-syntax-table/fontify-dq-string () + :tags '(fontification syntax-table) + (should (eq (bnf-test-face-at 11 "<foo> ::= \"bar\"") 'font-lock-string-face))) + +(provide 'bnf-mode-font-test) + +;;; bnf-mode-font-test.el ends here diff --git a/test/test-helper.el b/test/test-helper.el new file mode 100644 index 0000000..a051fbe --- /dev/null +++ b/test/test-helper.el @@ -0,0 +1,103 @@ +;;; test-helper.el --- BNF Mode: Non-interactive unit-test setup -*- lexical-binding: t; -*- + +;; Copyright (C) 2019 Serghei Iakovlev + +;; Author: Serghei Iakovlev (concat "sadhooklay" "@" "gmail" ".com") +;; Maintainer: Serghei Iakovlev +;; Version: 0.2.0 +;; URL: https://github.com/sergeyklay/bnf-mode + +;; This file is not part of GNU Emacs. + +;;; License + +;; This file 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 file 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 file; if not, write to the Free Software +;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +;; 02110-1301, USA. + +;;; Commentary: + +;; Non-interactive test suite setup for ERT Runner. + +;;; Code: + +(require 'ert-x) ; `ert-with-test-buffer' +(require 'cl-lib) ; `cl-defmacro' + +;; Make sure the exact Emacs version can be found in the build output +(message "Running tests on Emacs %s" emacs-version) + +;; The test fixtures assume an indentation width of 4, so we need to set that +;; up for the tests. +(setq-default default-tab-width 4 + indent-tabs-mode nil) + +(when (require 'undercover nil t) + (undercover "bnf-mode.el")) + +(let* ((current-file (if load-in-progress load-file-name (buffer-file-name))) + (source-directory (locate-dominating-file current-file "Cask")) + ;; Don't load old byte-compiled versions + (load-prefer-newer t)) + ;; Load the file under test + (load (expand-file-name "bnf-mode" source-directory))) + +;; Helpers + +(cl-defmacro bnf-deftest (name args &body body) + (declare (indent 2)) + `(ert-deftest ,(intern (format "bnf-ert-%s" name)) () + "" + ,@args)) + +(cl-defmacro bnf-ert-with-test-buffer ((&rest args) initial-contents &body body) + (declare (indent 2)) + `(ert-with-test-buffer (,@args) + (bnf-mode) + (insert ,initial-contents) + ,@body)) + +(defmacro bnf-test-with-temp-buffer (content &rest body) + "Evaluate BODY in a temporary buffer with CONTENT." + (declare (debug t) + (indent 1)) + `(with-temp-buffer + (insert ,content) + (bnf-mode) + (font-lock-fontify-buffer) + (goto-char (point-min)) + ,@body)) + +(cl-defmacro bnf-def-indentation-test (name args initial-contents expected-output) + (declare (indent 2)) + `(bnf-deftest ,name ,args + (bnf-ert-with-test-buffer (:name ,(format "(Expected)" name)) + ,initial-contents + (let ((indented (ert-buffer-string-reindented))) + (delete-region (point-min) (point-max)) + (insert ,expected-output) + (ert-with-test-buffer (:name ,(format "(Actual)" name)) + (bnf-mode) + (insert indented) + (should (equal indented ,expected-output))))))) + +(when (s-contains? "--win" (getenv "ERT_RUNNER_ARGS")) + (defun ert-runner/run-tests-batch-and-exit (selector) + (ert-run-tests-interactively selector))) + +;; Local Variables: +;; indent-tabs-mode: nil +;; End: + +;;; test-helper.el ends here