]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/tests/indent.el
Merge commit '078f88ecb797b6cf2cd597417402274dd82402ce' from diff-hl
[gnu-emacs-elpa] / packages / js2-mode / 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 (require 'cl-lib)
25 (require 'js2-old-indent)
26
27 (defun js2-test-indent (content keep-indent)
28 (let ((s (replace-regexp-in-string "^ *|" "" content)))
29 (with-temp-buffer
30 (insert
31 (if keep-indent
32 s
33 (replace-regexp-in-string "^ *" "" s)))
34 (js2-mode)
35 (indent-region (point-min) (point-max))
36 (should (string= s (buffer-substring-no-properties
37 (point-min) (point)))))))
38
39 (cl-defmacro js2-deftest-indent (name content &key bind keep-indent)
40 `(ert-deftest ,(intern (format "js2-%s" name)) ()
41 (let ,(append '((js2-basic-offset 2)
42 (js2-pretty-multiline-declarations t)
43 (inhibit-point-motion-hooks t))
44 bind)
45 (js2-test-indent ,content ,keep-indent))))
46
47 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
48
49 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
50 "var foo = 1;
51 |bar = 2")
52
53 (js2-deftest-indent multiline-decl-indent-after-comma
54 "let foo = 1,
55 | bar = 2")
56
57 (js2-deftest-indent no-multiline-decl-when-disabled
58 "let foo = 1,
59 |bar = 2"
60 :bind ((js2-pretty-multiline-declarations nil)))
61
62 (js2-deftest-indent multiline-decl-with-continued-expr
63 "var foo = 100500
64 | + 1")
65
66 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
67 "var foo = 100500 /
68 | 16;")
69
70 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
71 "var foo = bar('/protocols/')
72 |baz()")
73
74 (js2-deftest-indent no-multiline-decl-implicit-semicolon
75 "var foo = 100500
76 |1")
77
78 (js2-deftest-indent multiline-decl-sees-keyword-width
79 "const foo = 1,
80 | bar = 2;")
81
82 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
83 "var foo = 1,
84 | bar = [
85 | 1, 2,
86 | 3, 4
87 | ],
88 | baz = 5;")
89
90 (js2-deftest-indent multiline-decl-first-arg-function-normal
91 "var foo = function() {
92 | return 7;
93 |},
94 | bar = 8;")
95
96 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
97 "var foo = function() {
98 | return 7;
99 | },
100 | bar = 8;"
101 :bind ((js2-pretty-multiline-declarations 'all)))
102
103 (js2-deftest-indent default-keyword-as-property
104 "var foo = {
105 | case: 'zzzz',
106 | default: 'donkey',
107 | tee: 'ornery'
108 |};")
109
110 (js2-deftest-indent multiline-string-noop
111 "`multiline string
112 | contents
113 | are kept
114 | unchanged!`"
115 :keep-indent t)
116
117 (js2-deftest-indent no-multiline-decl-first-arg-function-dynamic
118 "var foo = function() {
119 | return 7;
120 |};"
121 :bind ((js2-pretty-multiline-declarations 'dynamic)))
122
123 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic
124 "var foo = function() {
125 | return 7;
126 | },
127 | bar = 8;"
128 :bind ((js2-pretty-multiline-declarations 'dynamic)))
129
130 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-comment
131 "var foo = function() {
132 | return 7;
133 | }/* MUAHAHAHA, ah ha! */,
134 | bar = 8;"
135 :bind ((js2-pretty-multiline-declarations 'dynamic)))
136
137 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-scan-error
138 "var foo = function() {
139 | return 7;
140 | ,
141 | bar = 8;"
142 :bind ((js2-pretty-multiline-declarations 'dynamic)))