Skip to content

Commit

Permalink
Add new ert-runner tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuco1 committed Aug 7, 2015
1 parent f8f2e28 commit f048138
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/smartparens-buffer-modified-sp-skip-closing-pair-test.el
Original file line number Diff line number Diff line change
@@ -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)))))
20 changes: 20 additions & 0 deletions test/smartparens-get-prefix-test.el
Original file line number Diff line number Diff line change
@@ -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 "(") "")))))
20 changes: 20 additions & 0 deletions test/smartparens-get-suffix-test.el
Original file line number Diff line number Diff line change
@@ -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 "(") "")))))
57 changes: 57 additions & 0 deletions test/smartparens-insertion-specification-parser-test.el
Original file line number Diff line number Diff line change
@@ -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)))))
13 changes: 13 additions & 0 deletions test/smartparens-python-autoescape-empty-string-test.el
Original file line number Diff line number Diff line change
@@ -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 \"\"\"\"\"\"")))))
23 changes: 23 additions & 0 deletions test/smartparens-region-ok-test.el
Original file line number Diff line number Diff line change
@@ -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 \")\")")))
65 changes: 65 additions & 0 deletions test/test-helper.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
;;; test-helper.el --- Helper for tests.

;; Copyright (C) 2015 Matus Goljer

;; Author: Matus Goljer <[email protected]>
;; Maintainer: Matus Goljer <[email protected]>
;; 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

0 comments on commit f048138

Please sign in to comment.