]> code.delx.au - gnu-emacs-elpa/blob - tests/indent.el
9bc1c749f5be4ee258a6e5d6bb1891c9dc479384
[gnu-emacs-elpa] / tests / indent.el
1 ;;; tests/indent.el --- Some tests for js2-mode.
2
3 ;; Copyright (C) 2009, 2011-2013 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is free software: you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
19
20 ;;; Code:
21
22 (require 'ert)
23 (require 'js2-mode)
24
25 (defun js2-test-indent (content)
26 (let ((s (replace-regexp-in-string "^ *|" "" content)))
27 (with-temp-buffer
28 (insert (replace-regexp-in-string "^ *" "" s))
29 (js2-mode)
30 (indent-region (point-min) (point-max))
31 (should (string= s (buffer-substring-no-properties
32 (point-min) (point)))))))
33
34 (defmacro* js2-deftest-indent (name content &key bind)
35 `(ert-deftest ,(intern (format "js2-%s" name)) ()
36 (let ,(append '((js2-basic-offset 2)
37 (js2-pretty-multiline-declarations t)
38 (inhibit-point-motion-hooks t))
39 bind)
40 (js2-test-indent ,content))))
41
42 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
43
44 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
45 "var foo = 1;
46 |bar = 2")
47
48 (js2-deftest-indent multiline-decl-indent-after-comma
49 "let foo = 1,
50 | bar = 2")
51
52 (js2-deftest-indent no-multiline-decl-when-disabled
53 "let foo = 1,
54 |bar = 2"
55 :bind ((js2-pretty-multiline-declarations nil)))
56
57 (js2-deftest-indent multiline-decl-with-continued-expr
58 "var foo = 100500
59 | + 1")
60
61 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
62 "var foo = 100500 /
63 | 16;")
64
65 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
66 "var foo = bar('/protocols/')
67 |baz()")
68
69 (js2-deftest-indent no-multiline-decl-implicit-semicolon
70 "var foo = 100500
71 |1")
72
73 (js2-deftest-indent multiline-decl-sees-keyword-width
74 "const foo = 1,
75 | bar = 2;")
76
77 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
78 "var foo = 1,
79 | bar = [
80 | 1, 2,
81 | 3, 4
82 | ],
83 | baz = 5;")
84
85 (js2-deftest-indent multiline-decl-first-arg-function-normal
86 "var foo = function() {
87 | return 7;
88 |},
89 | bar = 8;")
90
91 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
92 "var foo = function() {
93 | return 7;
94 | },
95 | bar = 8;"
96 :bind ((js2-pretty-multiline-declarations 'all)))
97
98 (js2-deftest-indent default-keyword-as-property
99 "var foo = {
100 | case: 'zzzz',
101 | default: 'donkey',
102 | tee: 'ornery'
103 |};")