]> code.delx.au - gnu-emacs-elpa/blob - tests/indent.el
Merge pull request #282 from jacksonrayhamilton/js2-jsx-mode
[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 (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-jsx-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 '(indent-tabs-mode
42 (js2-basic-offset 2)
43 (js2-pretty-multiline-declarations t)
44 (inhibit-point-motion-hooks t))
45 bind)
46 (js2-test-indent ,content ,keep-indent))))
47
48 (put 'js2-deftest-indent 'lisp-indent-function 'defun)
49
50 (js2-deftest-indent no-multiline-decl-indent-after-semicolon
51 "var foo = 1;
52 |bar = 2")
53
54 (js2-deftest-indent multiline-decl-indent-after-comma
55 "let foo = 1,
56 | bar = 2")
57
58 (js2-deftest-indent no-multiline-decl-when-disabled
59 "let foo = 1,
60 |bar = 2"
61 :bind ((js2-pretty-multiline-declarations nil)))
62
63 (js2-deftest-indent multiline-decl-with-continued-expr
64 "var foo = 100500
65 | + 1")
66
67 (js2-deftest-indent multiline-decl-with-continued-expr-same-line
68 "var foo = 100500 /
69 | 16;")
70
71 (js2-deftest-indent no-multiline-decl-with-operator-inside-string
72 "var foo = bar('/protocols/')
73 |baz()")
74
75 (js2-deftest-indent no-multiline-decl-implicit-semicolon
76 "var foo = 100500
77 |1")
78
79 (js2-deftest-indent multiline-decl-sees-keyword-width
80 "const foo = 1,
81 | bar = 2;")
82
83 (js2-deftest-indent multiline-decl-second-arg-value-parenthesised
84 "var foo = 1,
85 | bar = [
86 | 1, 2,
87 | 3, 4
88 | ],
89 | baz = 5;")
90
91 (js2-deftest-indent multiline-decl-first-arg-function-normal
92 "var foo = function() {
93 | return 7;
94 |},
95 | bar = 8;")
96
97 (js2-deftest-indent multiline-decl-first-arg-function-indent-all
98 "var foo = function() {
99 | return 7;
100 | },
101 | bar = 8;"
102 :bind ((js2-pretty-multiline-declarations 'all)))
103
104 (js2-deftest-indent default-keyword-as-property
105 "var foo = {
106 | case: 'zzzz',
107 | default: 'donkey',
108 | tee: 'ornery'
109 |};")
110
111 (js2-deftest-indent multiline-string-noop
112 "`multiline string
113 | contents
114 | are kept
115 | unchanged!`"
116 :keep-indent t)
117
118 (js2-deftest-indent no-multiline-decl-first-arg-function-dynamic
119 "var foo = function() {
120 | return 7;
121 |};"
122 :bind ((js2-pretty-multiline-declarations 'dynamic)))
123
124 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic
125 "var foo = function() {
126 | return 7;
127 | },
128 | bar = 8;"
129 :bind ((js2-pretty-multiline-declarations 'dynamic)))
130
131 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-comment
132 "var foo = function() {
133 | return 7;
134 | }/* MUAHAHAHA, ah ha! */,
135 | bar = 8;"
136 :bind ((js2-pretty-multiline-declarations 'dynamic)))
137
138 (js2-deftest-indent multiline-decl-first-arg-function-indent-dynamic-scan-error
139 "var foo = function() {
140 | return 7;
141 | ,
142 | bar = 8;"
143 :bind ((js2-pretty-multiline-declarations 'dynamic)))
144
145 (js2-deftest-indent indent-generator-method
146 "class A {
147 | * x() {
148 | return 1
149 | * 2;
150 | }
151 |}")
152
153 (js2-deftest-indent case-inside-switch
154 "switch(true) {
155 |case 'true':
156 | return 1;
157 |}")
158
159 (js2-deftest-indent case-inside-switch-with-extra-indent
160 "switch(true) {
161 | case 'true':
162 | return 1;
163 |}"
164 :bind ((js2-indent-switch-body t)))
165
166 (js2-deftest-indent case-inside-switch-with-extra-indent-curly-after-newline
167 "switch(true)
168 |{
169 | case 'true':
170 | return 1;
171 |}"
172 :bind ((js2-indent-switch-body t)))
173
174 (js2-deftest-indent jsx-one-line
175 "var foo = <div></div>;")
176
177 (js2-deftest-indent jsx-children-parentheses
178 "return (
179 | <div>
180 | </div>
181 | <div>
182 | <div></div>
183 | <div>
184 | <div></div>
185 | </div>
186 | </div>
187 |);")
188
189 (js2-deftest-indent jsx-children-unclosed
190 "return (
191 | <div>
192 | <div>")
193
194 (js2-deftest-indent jsx-argument
195 "React.render(
196 | <div>
197 | <div></div>
198 | </div>,
199 | {
200 | a: 1
201 | },
202 | <div>
203 | <div></div>
204 | </div>
205 |);")
206
207 (js2-deftest-indent jsx-leading-comment
208 "return (
209 | // Sneaky!
210 | <div></div>
211 |);")
212
213 (js2-deftest-indent jsx-trailing-comment
214 "return (
215 | <div></div>
216 | // Sneaky!
217 |);")
218
219 (js2-deftest-indent jsx-self-closing
220 ;; This ensures we know the bounds of a self-closing element
221 "React.render(
222 | <input
223 | />,
224 | {
225 | a: 1
226 | }
227 |);"
228 :bind ((sgml-attribute-offset 1))) ; Emacs 24.5 -> 25 compat
229
230 (js2-deftest-indent jsx-embedded-js-content
231 "return (
232 | <div>
233 | {array.map(function () {
234 | return {
235 | a: 1
236 | };
237 | })}
238 | </div>
239 |);")
240
241 (js2-deftest-indent jsx-embedded-js-unclosed
242 "return (
243 | <div>
244 | {array.map(function () {
245 | return {
246 | a: 1")
247
248 (js2-deftest-indent jsx-embedded-js-attribute
249 "return (
250 | <div attribute={array.map(function () {
251 | return {
252 | a: 1
253 | };
254 |
255 | return {
256 | a: 1
257 | };
258 |
259 | return {
260 | a: 1
261 | };
262 | })}>
263 | </div>
264 |);")