scripts/CodeMirror/mode/clojure/test.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function () {
5 var mode = CodeMirror.getMode({indentUnit: 2}, "clojure");
6
7 function MT(name) {
8 test.mode(name, mode, Array.prototype.slice.call(arguments, 1));
9 }
10
11 MT("atoms",
12 "[atom false]",
13 "[atom nil]",
14 "[atom true]"
15 );
16
17 MT("keywords",
18 "[atom :foo]",
19 "[atom ::bar]",
20 "[atom :foo/bar]",
21 "[atom :foo.bar/baz]"
22 );
23
24 MT("numbers",
25 "[number 42] [number +42] [number -421]",
26 "[number 42N] [number +42N] [number -42N]",
27 "[number 0.42] [number +0.42] [number -0.42]",
28 "[number 42M] [number +42M] [number -42M]",
29 "[number 42.42M] [number +42.42M] [number -42.42M]",
30 "[number 1/42] [number +1/42] [number -1/42]",
31 "[number 0x42af] [number +0x42af] [number -0x42af]",
32 "[number 0x42AF] [number +0x42AF] [number -0x42AF]",
33 "[number 1e2] [number 1e+2] [number 1e-2]",
34 "[number +1e2] [number +1e+2] [number +1e-2]",
35 "[number -1e2] [number -1e+2] [number -1e-2]",
36 "[number -1.0e2] [number -0.1e+2] [number -1.01e-2]",
37 "[number 1E2] [number 1E+2] [number 1E-2]",
38 "[number +1E2] [number +1E+2] [number +1E-2]",
39 "[number -1E2] [number -1E+2] [number -1E-2]",
40 "[number -1.0E2] [number -0.1E+2] [number -1.01E-2]",
41 "[number 2r101010] [number +2r101010] [number -2r101010]",
42 "[number 2r101010] [number +2r101010] [number -2r101010]",
43 "[number 8r52] [number +8r52] [number -8r52]",
44 "[number 36rhello] [number +36rhello] [number -36rhello]",
45 "[number 36rz] [number +36rz] [number -36rz]",
46 "[number 36rZ] [number +36rZ] [number -36rZ]",
47
48 // invalid numbers
49 "[error 42foo]",
50 "[error 42Nfoo]",
51 "[error 42Mfoo]",
52 "[error 42.42Mfoo]",
53 "[error 42.42M!]",
54 "[error 42!]",
55 "[error 0x42afm]"
56 );
57
58 MT("characters",
59 "[string-2 \\1]",
60 "[string-2 \\a]",
61 "[string-2 \\a\\b\\c]",
62 "[string-2 \\#]",
63 "[string-2 \\\\]",
64 "[string-2 \\\"]",
65 "[string-2 \\(]",
66 "[string-2 \\A]",
67 "[string-2 \\backspace]",
68 "[string-2 \\formfeed]",
69 "[string-2 \\newline]",
70 "[string-2 \\space]",
71 "[string-2 \\return]",
72 "[string-2 \\tab]",
73 "[string-2 \\u1000]",
74 "[string-2 \\uAaAa]",
75 "[string-2 \\u9F9F]",
76 "[string-2 \\o123]",
77 "[string-2 \\符]",
78 "[string-2 \\シ]",
79 "[string-2 \\ۇ]",
80 // FIXME
81 // "[string-2 \\🙂]",
82
83 // invalid character literals
84 "[error \\abc]",
85 "[error \\a123]",
86 "[error \\a!]",
87 "[error \\newlines]",
88 "[error \\NEWLINE]",
89 "[error \\u9F9FF]",
90 "[error \\o1234]"
91 );
92
93 MT("strings",
94 "[string \"I'm a teapot.\"]",
95 "[string \"I'm a \\\"teapot\\\".\"]",
96 "[string \"I'm]", // this is
97 "[string a]", // a multi-line
98 "[string teapot.\"]" // string
99
100 // TODO unterminated (multi-line) strings?
101 );
102
103 MT("comments",
104 "[comment ; this is an in-line comment.]",
105 "[comment ;; this is a line comment.]",
106 "[keyword comment]",
107 "[bracket (][comment comment (foo 1 2 3)][bracket )]"
108 );
109
110 MT("reader macro characters",
111 "[meta #][variable _]",
112 "[meta #][variable -Inf]",
113 "[meta ##][variable Inf]",
114 "[meta ##][variable NaN]",
115 "[meta @][variable x]",
116 "[meta ^][bracket {][atom :tag] [variable String][bracket }]",
117 "[meta `][bracket (][builtin f] [variable x][bracket )]",
118 "[meta ~][variable foo#]",
119 "[meta '][number 1]",
120 "[meta '][atom :foo]",
121 "[meta '][string \"foo\"]",
122 "[meta '][variable x]",
123 "[meta '][bracket (][builtin a] [variable b] [variable c][bracket )]",
124 "[meta '][bracket [[][variable a] [variable b] [variable c][bracket ]]]",
125 "[meta '][bracket {][variable a] [number 1] [atom :foo] [number 2] [variable c] [number 3][bracket }]",
126 "[meta '#][bracket {][variable a] [number 1] [atom :foo][bracket }]"
127 );
128
129 MT("symbols",
130 "[variable foo!]",
131 "[variable foo#]",
132 "[variable foo$]",
133 "[variable foo&]",
134 "[variable foo']",
135 "[variable foo*]",
136 "[variable foo+]",
137 "[variable foo-]",
138 "[variable foo.]",
139 "[variable foo/bar]",
140 "[variable foo:bar]",
141 "[variable foo<]",
142 "[variable foo=]",
143 "[variable foo>]",
144 "[variable foo?]",
145 "[variable foo_]",
146 "[variable foo|]",
147 "[variable foobarBaz]",
148 "[variable foo¡]",
149 "[variable 符号]",
150 "[variable シンボル]",
151 "[variable ئۇيغۇر]",
152 "[variable 🙂❤🇺🇸]",
153
154 // invalid symbols
155 "[error 3foo]",
156 "[error 3+]",
157 "[error 3|]",
158 "[error 3_]"
159 );
160
161 MT("numbers and other forms",
162 "[number 42][bracket (][builtin foo][bracket )]",
163 "[number 42][bracket [[][variable foo][bracket ]]]",
164 "[number 42][meta #][bracket {][variable foo][bracket }]",
165 "[number 42][bracket {][atom :foo] [variable bar][bracket }]",
166 "[number 42][meta `][variable foo]",
167 "[number 42][meta ~][variable foo]",
168 "[number 42][meta #][variable foo]"
169 );
170
171 var specialForms = [".", "catch", "def", "do", "if", "monitor-enter",
172 "monitor-exit", "new", "quote", "recur", "set!", "throw", "try", "var"];
173
174 MT("should highlight special forms as keywords",
175 typeTokenPairs("keyword", specialForms)
176 );
177
178 var coreSymbols1 = [
179 "*", "*'", "*1", "*2", "*3", "*agent*", "*allow-unresolved-vars*", "*assert*",
180 "*clojure-version*", "*command-line-args*", "*compile-files*", "*compile-path*", "*compiler-options*",
181 "*data-readers*", "*default-data-reader-fn*", "*e", "*err*", "*file*", "*flush-on-newline*", "*fn-loader*",
182 "*in*", "*math-context*", "*ns*", "*out*", "*print-dup*", "*print-length*", "*print-level*", "*print-meta*",
183 "*print-namespace-maps*", "*print-readably*", "*read-eval*", "*reader-resolver*", "*source-path*",
184 "*suppress-read*", "*unchecked-math*", "*use-context-classloader*", "*verbose-defrecords*",
185 "*warn-on-reflection*", "+", "+'", "-", "-'", "->", "->>", "->ArrayChunk", "->Eduction", "->Vec", "->VecNode",
186 "->VecSeq", "-cache-protocol-fn", "-reset-methods", "..", "/", "<", "<=", "=", "==", ">", ">=",
187 "EMPTY-NODE", "Inst", "StackTraceElement->vec", "Throwable->map", "accessor", "aclone", "add-classpath",
188 "add-watch", "agent", "agent-error", "agent-errors", "aget", "alength", "alias", "all-ns", "alter",
189 "alter-meta!", "alter-var-root", "amap", "ancestors", "and", "any?", "apply", "areduce", "array-map",
190 "as->", "aset", "aset-boolean", "aset-byte", "aset-char", "aset-double", "aset-float", "aset-int",
191 "aset-long", "aset-short", "assert", "assoc", "assoc!", "assoc-in", "associative?", "atom", "await",
192 "await-for", "await1", "bases", "bean", "bigdec", "bigint", "biginteger", "binding", "bit-and", "bit-and-not",
193 "bit-clear", "bit-flip", "bit-not", "bit-or", "bit-set", "bit-shift-left", "bit-shift-right", "bit-test",
194 "bit-xor", "boolean", "boolean-array", "boolean?", "booleans", "bound-fn", "bound-fn*", "bound?",
195 "bounded-count", "butlast", "byte", "byte-array", "bytes", "bytes?", "case", "cast", "cat", "char",
196 "char-array", "char-escape-string", "char-name-string", "char?", "chars", "chunk", "chunk-append",
197 "chunk-buffer", "chunk-cons", "chunk-first", "chunk-next", "chunk-rest", "chunked-seq?", "class", "class?",
198 "clear-agent-errors", "clojure-version", "coll?", "comment", "commute", "comp", "comparator", "compare",
199 "compare-and-set!", "compile", "complement", "completing", "concat", "cond", "cond->", "cond->>", "condp",
200 "conj", "conj!", "cons", "constantly", "construct-proxy", "contains?", "count", "counted?", "create-ns",
201 "create-struct", "cycle", "dec", "dec'", "decimal?", "declare", "dedupe", "default-data-readers", "definline",
202 "definterface", "defmacro", "defmethod", "defmulti", "defn", "defn-", "defonce", "defprotocol", "defrecord",
203 "defstruct", "deftype", "delay", "delay?", "deliver", "denominator", "deref", "derive", "descendants",
204 "destructure", "disj", "disj!", "dissoc", "dissoc!", "distinct", "distinct?", "doall", "dorun", "doseq",
205 "dosync", "dotimes", "doto", "double", "double-array", "double?", "doubles", "drop", "drop-last", "drop-while",
206 "eduction", "empty", "empty?", "ensure", "ensure-reduced", "enumeration-seq", "error-handler", "error-mode",
207 "eval", "even?", "every-pred", "every?", "ex-data", "ex-info", "extend", "extend-protocol", "extend-type",
208 "extenders", "extends?", "false?", "ffirst", "file-seq", "filter", "filterv", "find", "find-keyword", "find-ns",
209 "find-protocol-impl", "find-protocol-method", "find-var", "first", "flatten", "float", "float-array", "float?",
210 "floats", "flush", "fn", "fn?", "fnext", "fnil", "for", "force", "format", "frequencies", "future", "future-call",
211 "future-cancel", "future-cancelled?", "future-done?", "future?", "gen-class", "gen-interface", "gensym", "get",
212 "get-in", "get-method", "get-proxy-class", "get-thread-bindings", "get-validator", "group-by", "halt-when", "hash",
213 "hash-combine", "hash-map", "hash-ordered-coll", "hash-set", "hash-unordered-coll", "ident?", "identical?",
214 "identity", "if-let", "if-not", "if-some", "ifn?", "import", "in-ns", "inc", "inc'", "indexed?", "init-proxy",
215 "inst-ms", "inst-ms*", "inst?", "instance?", "int", "int-array", "int?", "integer?", "interleave", "intern",
216 "interpose", "into", "into-array", "ints", "io!", "isa?", "iterate", "iterator-seq", "juxt", "keep", "keep-indexed",
217 "key", "keys", "keyword", "keyword?", "last", "lazy-cat", "lazy-seq", "let", "letfn", "line-seq", "list", "list*",
218 "list?", "load", "load-file", "load-reader", "load-string", "loaded-libs", "locking", "long", "long-array", "longs",
219 "loop", "macroexpand", "macroexpand-1", "make-array", "make-hierarchy", "map", "map-entry?", "map-indexed", "map?",
220 "mapcat", "mapv", "max", "max-key", "memfn", "memoize", "merge", "merge-with", "meta", "method-sig", "methods"];
221
222 var coreSymbols2 = [
223 "min", "min-key", "mix-collection-hash", "mod", "munge", "name", "namespace", "namespace-munge", "nat-int?",
224 "neg-int?", "neg?", "newline", "next", "nfirst", "nil?", "nnext", "not", "not-any?", "not-empty", "not-every?",
225 "not=", "ns", "ns-aliases", "ns-imports", "ns-interns", "ns-map", "ns-name", "ns-publics", "ns-refers", "ns-resolve",
226 "ns-unalias", "ns-unmap", "nth", "nthnext", "nthrest", "num", "number?", "numerator", "object-array", "odd?", "or",
227 "parents", "partial", "partition", "partition-all", "partition-by", "pcalls", "peek", "persistent!", "pmap", "pop",
228 "pop!", "pop-thread-bindings", "pos-int?", "pos?", "pr", "pr-str", "prefer-method", "prefers",
229 "primitives-classnames", "print", "print-ctor", "print-dup", "print-method", "print-simple", "print-str", "printf",
230 "println", "println-str", "prn", "prn-str", "promise", "proxy", "proxy-call-with-super", "proxy-mappings",
231 "proxy-name", "proxy-super", "push-thread-bindings", "pvalues", "qualified-ident?", "qualified-keyword?",
232 "qualified-symbol?", "quot", "rand", "rand-int", "rand-nth", "random-sample", "range", "ratio?", "rational?",
233 "rationalize", "re-find", "re-groups", "re-matcher", "re-matches", "re-pattern", "re-seq", "read", "read-line",
234 "read-string", "reader-conditional", "reader-conditional?", "realized?", "record?", "reduce", "reduce-kv", "reduced",
235 "reduced?", "reductions", "ref", "ref-history-count", "ref-max-history", "ref-min-history", "ref-set", "refer",
236 "refer-clojure", "reify", "release-pending-sends", "rem", "remove", "remove-all-methods", "remove-method", "remove-ns",
237 "remove-watch", "repeat", "repeatedly", "replace", "replicate", "require", "reset!", "reset-meta!", "reset-vals!",
238 "resolve", "rest", "restart-agent", "resultset-seq", "reverse", "reversible?", "rseq", "rsubseq", "run!", "satisfies?",
239 "second", "select-keys", "send", "send-off", "send-via", "seq", "seq?", "seqable?", "seque", "sequence", "sequential?",
240 "set", "set-agent-send-executor!", "set-agent-send-off-executor!", "set-error-handler!", "set-error-mode!",
241 "set-validator!", "set?", "short", "short-array", "shorts", "shuffle", "shutdown-agents", "simple-ident?",
242 "simple-keyword?", "simple-symbol?", "slurp", "some", "some->", "some->>", "some-fn", "some?", "sort", "sort-by",
243 "sorted-map", "sorted-map-by", "sorted-set", "sorted-set-by", "sorted?", "special-symbol?", "spit", "split-at",
244 "split-with", "str", "string?", "struct", "struct-map", "subs", "subseq", "subvec", "supers", "swap!", "swap-vals!",
245 "symbol", "symbol?", "sync", "tagged-literal", "tagged-literal?", "take", "take-last", "take-nth", "take-while", "test",
246 "the-ns", "thread-bound?", "time", "to-array", "to-array-2d", "trampoline", "transduce", "transient", "tree-seq",
247 "true?", "type", "unchecked-add", "unchecked-add-int", "unchecked-byte", "unchecked-char", "unchecked-dec",
248 "unchecked-dec-int", "unchecked-divide-int", "unchecked-double", "unchecked-float", "unchecked-inc", "unchecked-inc-int",
249 "unchecked-int", "unchecked-long", "unchecked-multiply", "unchecked-multiply-int", "unchecked-negate",
250 "unchecked-negate-int", "unchecked-remainder-int", "unchecked-short", "unchecked-subtract", "unchecked-subtract-int",
251 "underive", "unquote", "unquote-splicing", "unreduced", "unsigned-bit-shift-right", "update", "update-in",
252 "update-proxy", "uri?", "use", "uuid?", "val", "vals", "var-get", "var-set", "var?", "vary-meta", "vec", "vector",
253 "vector-of", "vector?", "volatile!", "volatile?", "vreset!", "vswap!", "when", "when-first", "when-let", "when-not",
254 "when-some", "while", "with-bindings", "with-bindings*", "with-in-str", "with-loading-context", "with-local-vars",
255 "with-meta", "with-open", "with-out-str", "with-precision", "with-redefs", "with-redefs-fn", "xml-seq", "zero?",
256 "zipmap"
257 ];
258
259 MT("should highlight core symbols as keywords (part 1/2)",
260 typeTokenPairs("keyword", coreSymbols1)
261 );
262
263 MT("should highlight core symbols as keywords (part 2/2)",
264 typeTokenPairs("keyword", coreSymbols2)
265 );
266
267 MT("should properly indent forms in list literals",
268 "[bracket (][builtin foo] [atom :a] [number 1] [atom true] [atom nil][bracket )]",
269 "",
270 "[bracket (][builtin foo] [atom :a]",
271 " [number 1]",
272 " [atom true]",
273 " [atom nil][bracket )]",
274 "",
275 "[bracket (][builtin foo] [atom :a] [number 1]",
276 " [atom true]",
277 " [atom nil][bracket )]",
278 "",
279 "[bracket (]",
280 " [builtin foo]",
281 " [atom :a]",
282 " [number 1]",
283 " [atom true]",
284 " [atom nil][bracket )]",
285 "",
286 "[bracket (][builtin foo] [bracket [[][atom :a][bracket ]]]",
287 " [number 1]",
288 " [atom true]",
289 " [atom nil][bracket )]"
290 );
291
292 MT("should properly indent forms in vector literals",
293 "[bracket [[][atom :a] [number 1] [atom true] [atom nil][bracket ]]]",
294 "",
295 "[bracket [[][atom :a]",
296 " [number 1]",
297 " [atom true]",
298 " [atom nil][bracket ]]]",
299 "",
300 "[bracket [[][atom :a] [number 1]",
301 " [atom true]",
302 " [atom nil][bracket ]]]",
303 "",
304 "[bracket [[]",
305 " [variable foo]",
306 " [atom :a]",
307 " [number 1]",
308 " [atom true]",
309 " [atom nil][bracket ]]]"
310 );
311
312 MT("should properly indent forms in map literals",
313 "[bracket {][atom :a] [atom :a] [atom :b] [number 1] [atom :c] [atom true] [atom :d] [atom nil] [bracket }]",
314 "",
315 "[bracket {][atom :a] [atom :a]",
316 " [atom :b] [number 1]",
317 " [atom :c] [atom true]",
318 " [atom :d] [atom nil][bracket }]",
319 "",
320 "[bracket {][atom :a]",
321 " [atom :a]",
322 " [atom :b]",
323 " [number 1]",
324 " [atom :c]",
325 " [atom true]",
326 " [atom :d]",
327 " [atom nil][bracket }]",
328 "",
329 "[bracket {]",
330 " [atom :a] [atom :a]",
331 " [atom :b] [number 1]",
332 " [atom :c] [atom true]",
333 " [atom :d] [atom nil][bracket }]"
334 );
335
336 MT("should properly indent forms in set literals",
337 "[meta #][bracket {][atom :a] [number 1] [atom true] [atom nil] [bracket }]",
338 "",
339 "[meta #][bracket {][atom :a]",
340 " [number 1]",
341 " [atom true]",
342 " [atom nil][bracket }]",
343 "",
344 "[meta #][bracket {]",
345 " [atom :a]",
346 " [number 1]",
347 " [atom true]",
348 " [atom nil][bracket }]"
349 );
350
351 var haveBodyParameter = [
352 "->", "->>", "as->", "binding", "bound-fn", "case", "catch", "cond",
353 "cond->", "cond->>", "condp", "def", "definterface", "defmethod", "defn",
354 "defmacro", "defprotocol", "defrecord", "defstruct", "deftype", "do",
355 "doseq", "dotimes", "doto", "extend", "extend-protocol", "extend-type",
356 "fn", "for", "future", "if", "if-let", "if-not", "if-some", "let",
357 "letfn", "locking", "loop", "ns", "proxy", "reify", "some->", "some->>",
358 "struct-map", "try", "when", "when-first", "when-let", "when-not",
359 "when-some", "while", "with-bindings", "with-bindings*", "with-in-str",
360 "with-loading-context", "with-local-vars", "with-meta", "with-open",
361 "with-out-str", "with-precision", "with-redefs", "with-redefs-fn"];
362
363 function testFormsThatHaveBodyParameter(forms) {
364 for (var i = 0; i < forms.length; i++) {
365 MT("should indent body argument of `" + forms[i] + "` by `options.indentUnit` spaces",
366 "[bracket (][keyword " + forms[i] + "] [variable foo] [variable bar]",
367 " [variable baz]",
368 " [variable qux][bracket )]"
369 );
370 }
371 }
372
373 testFormsThatHaveBodyParameter(haveBodyParameter);
374
375 MT("should indent body argument of `comment` by `options.indentUnit` spaces",
376 "[bracket (][comment comment foo bar]",
377 "[comment baz]",
378 "[comment qux][bracket )]"
379 );
380
381 function typeTokenPairs(type, tokens) {
382 return "[" + type + " " + tokens.join("] [" + type + " ") + "]";
383 }
384})();