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