From f04813886ecfa84d0d2b9d16ff6be30351235630 Mon Sep 17 00:00:00 2001 From: Matus Goljer Date: Tue, 4 Aug 2015 22:50:39 +0200 Subject: [PATCH] Add new ert-runner tests. --- ...ffer-modified-sp-skip-closing-pair-test.el | 25 +++++++ test/smartparens-get-prefix-test.el | 20 ++++++ test/smartparens-get-suffix-test.el | 20 ++++++ ...ens-insertion-specification-parser-test.el | 57 ++++++++++++++++ ...ens-python-autoescape-empty-string-test.el | 13 ++++ test/smartparens-region-ok-test.el | 23 +++++++ test/test-helper.el | 65 +++++++++++++++++++ 7 files changed, 223 insertions(+) create mode 100644 test/smartparens-buffer-modified-sp-skip-closing-pair-test.el create mode 100644 test/smartparens-get-prefix-test.el create mode 100644 test/smartparens-get-suffix-test.el create mode 100644 test/smartparens-insertion-specification-parser-test.el create mode 100644 test/smartparens-python-autoescape-empty-string-test.el create mode 100644 test/smartparens-region-ok-test.el create mode 100644 test/test-helper.el diff --git a/test/smartparens-buffer-modified-sp-skip-closing-pair-test.el b/test/smartparens-buffer-modified-sp-skip-closing-pair-test.el new file mode 100644 index 00000000..12f69191 --- /dev/null +++ b/test/smartparens-buffer-modified-sp-skip-closing-pair-test.el @@ -0,0 +1,25 @@ +(ert-deftest sp-test-buffer-modified-sp-skip-closing-pair () + "Test the correct setting of `buffer-modified-p' flag after +executing `sp-skip-closing-pair'." + (with-temp-buffer + (insert "foobar") + (should (eq (buffer-modified-p) t)) + (goto-char (point-min)) + (insert "(") + (goto-char (point-max)) + (insert ")") + (backward-char 1) + (sp-skip-closing-pair ")") + (should (eq (buffer-modified-p) t)) + (backward-char 1) + (set-buffer-modified-p nil) + (sp-skip-closing-pair ")") + (should (eq (buffer-modified-p) nil)) + (let ((sp-autoskip-closing-pair 'always)) + (goto-char 3) + (sp-skip-closing-pair ")") + (should (eq (buffer-modified-p) nil)) + (goto-char 3) + (insert "a") + (sp-skip-closing-pair ")") + (should (eq (buffer-modified-p) t))))) diff --git a/test/smartparens-get-prefix-test.el b/test/smartparens-get-prefix-test.el new file mode 100644 index 00000000..4f90e54d --- /dev/null +++ b/test/smartparens-get-prefix-test.el @@ -0,0 +1,20 @@ +;; #488 +(ert-deftest sp-test-get-prefix-full-length () + (let ((sp-sexp-prefix '((emacs-lisp-mode regexp "#?[`',]@?")))) + (sp-test-with-temp-elisp-buffer "#'(fo|o)" + (should (equal (sp--get-prefix 3 "(") "#'"))))) + +(ert-deftest sp-test-get-prefix-shorter-prefix () + (let ((sp-sexp-prefix '((emacs-lisp-mode regexp "#?[`',]@?")))) + (sp-test-with-temp-elisp-buffer "'(fo|o)" + (should (equal (sp--get-prefix 2 "(") "'"))))) + +(ert-deftest sp-test-get-prefix-nonsense-prefix () + (let ((sp-sexp-prefix '((emacs-lisp-mode regexp "#?[`',]@?")))) + (sp-test-with-temp-elisp-buffer "ad(fo|o)" + (should (equal (sp--get-prefix 3 "(") ""))))) + +(ert-deftest sp-test-get-prefix-no-prefix () + (let ((sp-sexp-prefix '((emacs-lisp-mode regexp "#?[`',]@?")))) + (sp-test-with-temp-elisp-buffer "(fo|o)" + (should (equal (sp--get-prefix 1 "(") ""))))) diff --git a/test/smartparens-get-suffix-test.el b/test/smartparens-get-suffix-test.el new file mode 100644 index 00000000..e286ab8f --- /dev/null +++ b/test/smartparens-get-suffix-test.el @@ -0,0 +1,20 @@ +;; #488 +(ert-deftest sp-test-get-suffix-short-suffix () + (let ((sp-sexp-suffix '((emacs-lisp-mode regexp "'-?")))) + (sp-test-with-temp-elisp-buffer "(foo)'" + (should (equal (sp--get-suffix 6 "(") "'"))))) + +(ert-deftest sp-test-get-suffix-full-suffix () + (let ((sp-sexp-suffix '((emacs-lisp-mode regexp "'-?")))) + (sp-test-with-temp-elisp-buffer "(foo)'-" + (should (equal (sp--get-suffix 6 "(") "'-"))))) + +(ert-deftest sp-test-get-suffix-nonsense-suffix () + (let ((sp-sexp-suffix '((emacs-lisp-mode regexp "'-?")))) + (sp-test-with-temp-elisp-buffer "(foo)-" + (should (equal (sp--get-suffix 6 "(") ""))))) + +(ert-deftest sp-test-get-suffix-no-suffix () + (let ((sp-sexp-suffix '((emacs-lisp-mode regexp "'-?")))) + (sp-test-with-temp-elisp-buffer "(foo)" + (should (equal (sp--get-suffix 6 "(") ""))))) diff --git a/test/smartparens-insertion-specification-parser-test.el b/test/smartparens-insertion-specification-parser-test.el new file mode 100644 index 00000000..88b6c909 --- /dev/null +++ b/test/smartparens-insertion-specification-parser-test.el @@ -0,0 +1,57 @@ +;; TODO: separate into separate tests for each invocation -> easier +;; debugging +(ert-deftest sp-test-insertion-specification-parser () + (should (equal (sp--parse-insertion-spec "ab") + '(progn (insert "ab")))) + (should (equal (sp--parse-insertion-spec "a|b") + '(progn + (insert "a") + (save-excursion + (insert "b"))))) + (should (equal (sp--parse-insertion-spec "a\\|b") + '(progn (insert "a|") (insert "b")))) + (should (equal (sp--parse-insertion-spec "a\\||b") + '(progn + (insert "a|") + (save-excursion + (insert "b"))))) + (should (equal (sp--parse-insertion-spec "a\\[b]") + '(progn (insert "a[") (insert "b]")))) + (should (equal (sp--parse-insertion-spec "a\\[b[i]") + '(progn + (insert "a[") + (insert "b") + (indent-according-to-mode)))) + (should (equal (sp--parse-insertion-spec "a||b") + '(progn + (insert "a") + (save-excursion + (insert "b")) + (indent-according-to-mode)))) + (should (equal (sp--parse-insertion-spec "a|[i]b") + '(progn + (insert "a") + (save-excursion + (indent-according-to-mode) + (insert "b"))))) + (should (equal (sp--parse-insertion-spec "a|b[i]") + '(progn + (insert "a") + (save-excursion + (insert "b") + (indent-according-to-mode))))) + (should (equal (sp--parse-insertion-spec "[i]a|b") + '(progn + (indent-according-to-mode) + (insert "a") + (save-excursion + (insert "b"))))) + (should (equal (sp--parse-insertion-spec "[i]") + '(progn + (indent-according-to-mode)))) + (should (equal (sp--parse-insertion-spec "[d3]") + '(progn + (delete-char 3)))) + (should (equal (sp--parse-insertion-spec "[d12]") + '(progn + (delete-char 12))))) diff --git a/test/smartparens-python-autoescape-empty-string-test.el b/test/smartparens-python-autoescape-empty-string-test.el new file mode 100644 index 00000000..5913d0fa --- /dev/null +++ b/test/smartparens-python-autoescape-empty-string-test.el @@ -0,0 +1,13 @@ +(ert-deftest sp-test-sp-autoescape-string-quote-if-empty () + (let ((python-indent-offset 4)) + (with-temp-buffer + (python-mode) + (smartparens-mode 1) + (insert "def foo():\n ") + (pop-to-buffer (current-buffer)) + (let ((sp-autoescape-string-quote-if-empty '(python-mode)) + (sp-autoescape-string-quote t) + (sp-autoskip-closing-pair 'always) + (sp-undo-pairs-separately nil)) + (execute-kbd-macro "\"\"\"")) + (should (equal (buffer-string) "def foo():\n \"\"\"\"\"\""))))) diff --git a/test/smartparens-region-ok-test.el b/test/smartparens-region-ok-test.el new file mode 100644 index 00000000..68649aac --- /dev/null +++ b/test/smartparens-region-ok-test.el @@ -0,0 +1,23 @@ +(defun sp-test--string-valid-p (str) + (with-temp-buffer + (insert str) + (emacs-lisp-mode) + (sp-region-ok-p (point-min) (point-max)))) + +(ert-deftest sp-test-region-ok-unbalanced-paren () + (should-not (sp-test--string-valid-p "foo)"))) + +(ert-deftest sp-test-region-ok-unbalanced-string () + (should-not (sp-test--string-valid-p "foo\""))) + +(ert-deftest sp-test-region-ok-balanced-string () + (should (sp-test--string-valid-p "\"foo\""))) + +(ert-deftest sp-test-region-ok-balanced-parens () + (should (sp-test--string-valid-p "(foo)"))) + +(ert-deftest sp-test-region-ok-with-trailing-garbage () + (should (sp-test--string-valid-p "(foo) asdf!$#$^"))) + +(ert-deftest sp-test-region-ok-unbalanced-paren-in-string () + (should (sp-test--string-valid-p "(foo \")\")"))) diff --git a/test/test-helper.el b/test/test-helper.el new file mode 100644 index 00000000..c3b0ae28 --- /dev/null +++ b/test/test-helper.el @@ -0,0 +1,65 @@ +;;; test-helper.el --- Helper for tests. + +;; Copyright (C) 2015 Matus Goljer + +;; Author: Matus Goljer +;; Maintainer: Matus Goljer +;; Created: 4 Aug 2015 + +;;; Commentary: + +;; Grab bag of utilities for running smartparens tests. + +;;; Code: + +(require 'ert) +(require 'dash) +(require 'f) +(require 'cl-lib) + +(let ((sp-dir (f-parent (f-dirname (f-this-file))))) + (add-to-list 'load-path sp-dir)) +(require 'smartparens) + +(defmacro sp-test-with-temp-buffer (initial initform &rest forms) + "Setup a new buffer, then run FORMS. + +First, INITFORM are run in the newly created buffer. + +Then `smartparens-mode' is turned on. Then INITIAL is +inserted (it is expected to evaluate to string). If INITIAL +contains | put point there as the initial position (the character +is then removed). If it contains M, put mark there (the +character is then removed). + +Finally, FORMS are run." + (declare (indent 2) + (debug (form form body))) + `(save-window-excursion + (with-temp-buffer + (set-input-method nil) + ,initform + (smartparens-mode 1) + (pop-to-buffer (current-buffer)) + (insert ,initial) + (goto-char (point-min)) + (when (search-forward "M" nil t) + (delete-char -1) + (set-mark (point)) + (activate-mark)) + (goto-char (point-min)) + (when (search-forward "|" nil t) + (delete-char -1)) + ,@forms))) + +(defmacro sp-test-with-temp-elisp-buffer (initial &rest forms) + "Setup a new `emacs-lisp-mode' test buffer. + +See `sp-test-with-temp-buffer'." + (declare (indent 1) + (debug (form body))) + `(sp-test-with-temp-buffer ,initial + (emacs-lisp-mode) + ,@forms)) + +;;; test-helper.el ends here