test.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function() {
  4. var mode = CodeMirror.getMode({indentUnit: 2}, "javascript");
  5. function MT(name) { test.mode(name, mode, Array.prototype.slice.call(arguments, 1)); }
  6. MT("locals",
  7. "[keyword function] [variable foo]([def a], [def b]) { [keyword var] [def c] [operator =] [number 10]; [keyword return] [variable-2 a] [operator +] [variable-2 c] [operator +] [variable d]; }");
  8. MT("comma-and-binop",
  9. "[keyword function](){ [keyword var] [def x] [operator =] [number 1] [operator +] [number 2], [def y]; }");
  10. MT("destructuring",
  11. "([keyword function]([def a], [[[def b], [def c] ]]) {",
  12. " [keyword let] {[def d], [property foo]: [def c][operator =][number 10], [def x]} [operator =] [variable foo]([variable-2 a]);",
  13. " [[[variable-2 c], [variable y] ]] [operator =] [variable-2 c];",
  14. "})();");
  15. MT("class_body",
  16. "[keyword class] [variable Foo] {",
  17. " [property constructor]() {}",
  18. " [property sayName]() {",
  19. " [keyword return] [string-2 `foo${][variable foo][string-2 }oo`];",
  20. " }",
  21. "}");
  22. MT("class",
  23. "[keyword class] [variable Point] [keyword extends] [variable SuperThing] {",
  24. " [property get] [property prop]() { [keyword return] [number 24]; }",
  25. " [property constructor]([def x], [def y]) {",
  26. " [keyword super]([string 'something']);",
  27. " [keyword this].[property x] [operator =] [variable-2 x];",
  28. " }",
  29. "}");
  30. MT("module",
  31. "[keyword module] [string 'foo'] {",
  32. " [keyword export] [keyword let] [def x] [operator =] [number 42];",
  33. " [keyword export] [keyword *] [keyword from] [string 'somewhere'];",
  34. "}");
  35. MT("import",
  36. "[keyword function] [variable foo]() {",
  37. " [keyword import] [def $] [keyword from] [string 'jquery'];",
  38. " [keyword module] [def crypto] [keyword from] [string 'crypto'];",
  39. " [keyword import] { [def encrypt], [def decrypt] } [keyword from] [string 'crypto'];",
  40. "}");
  41. MT("const",
  42. "[keyword function] [variable f]() {",
  43. " [keyword const] [[ [def a], [def b] ]] [operator =] [[ [number 1], [number 2] ]];",
  44. "}");
  45. MT("for/of",
  46. "[keyword for]([keyword let] [variable of] [keyword of] [variable something]) {}");
  47. MT("generator",
  48. "[keyword function*] [variable repeat]([def n]) {",
  49. " [keyword for]([keyword var] [def i] [operator =] [number 0]; [variable-2 i] [operator <] [variable-2 n]; [operator ++][variable-2 i])",
  50. " [keyword yield] [variable-2 i];",
  51. "}");
  52. MT("fatArrow",
  53. "[variable array].[property filter]([def a] [operator =>] [variable-2 a] [operator +] [number 1]);",
  54. "[variable a];", // No longer in scope
  55. "[keyword let] [variable f] [operator =] ([[ [def a], [def b] ]], [def c]) [operator =>] [variable-2 a] [operator +] [variable-2 c];",
  56. "[variable c];");
  57. MT("spread",
  58. "[keyword function] [variable f]([def a], [meta ...][def b]) {",
  59. " [variable something]([variable-2 a], [meta ...][variable-2 b]);",
  60. "}");
  61. MT("comprehension",
  62. "[keyword function] [variable f]() {",
  63. " [[([variable x] [operator +] [number 1]) [keyword for] ([keyword var] [def x] [keyword in] [variable y]) [keyword if] [variable pred]([variable-2 x]) ]];",
  64. " ([variable u] [keyword for] ([keyword var] [def u] [keyword of] [variable generateValues]()) [keyword if] ([variable-2 u].[property color] [operator ===] [string 'blue']));",
  65. "}");
  66. MT("quasi",
  67. "[variable re][string-2 `fofdlakj${][variable x] [operator +] ([variable re][string-2 `foo`]) [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  68. MT("quasi_no_function",
  69. "[variable x] [operator =] [string-2 `fofdlakj${][variable x] [operator +] [string-2 `foo`] [operator +] [number 1][string-2 }fdsa`] [operator +] [number 2]");
  70. MT("indent_statement",
  71. "[keyword var] [variable x] [operator =] [number 10]",
  72. "[variable x] [operator +=] [variable y] [operator +]",
  73. " [atom Infinity]",
  74. "[keyword debugger];");
  75. MT("indent_if",
  76. "[keyword if] ([number 1])",
  77. " [keyword break];",
  78. "[keyword else] [keyword if] ([number 2])",
  79. " [keyword continue];",
  80. "[keyword else]",
  81. " [number 10];",
  82. "[keyword if] ([number 1]) {",
  83. " [keyword break];",
  84. "} [keyword else] [keyword if] ([number 2]) {",
  85. " [keyword continue];",
  86. "} [keyword else] {",
  87. " [number 10];",
  88. "}");
  89. MT("indent_for",
  90. "[keyword for] ([keyword var] [variable i] [operator =] [number 0];",
  91. " [variable i] [operator <] [number 100];",
  92. " [variable i][operator ++])",
  93. " [variable doSomething]([variable i]);",
  94. "[keyword debugger];");
  95. MT("indent_c_style",
  96. "[keyword function] [variable foo]()",
  97. "{",
  98. " [keyword debugger];",
  99. "}");
  100. MT("indent_else",
  101. "[keyword for] (;;)",
  102. " [keyword if] ([variable foo])",
  103. " [keyword if] ([variable bar])",
  104. " [number 1];",
  105. " [keyword else]",
  106. " [number 2];",
  107. " [keyword else]",
  108. " [number 3];");
  109. MT("indent_funarg",
  110. "[variable foo]([number 10000],",
  111. " [keyword function]([def a]) {",
  112. " [keyword debugger];",
  113. "};");
  114. MT("indent_below_if",
  115. "[keyword for] (;;)",
  116. " [keyword if] ([variable foo])",
  117. " [number 1];",
  118. "[number 2];");
  119. MT("multilinestring",
  120. "[keyword var] [variable x] [operator =] [string 'foo\\]",
  121. "[string bar'];");
  122. MT("scary_regexp",
  123. "[string-2 /foo[[/]]bar/];");
  124. MT("indent_strange_array",
  125. "[keyword var] [variable x] [operator =] [[",
  126. " [number 1],,",
  127. " [number 2],",
  128. "]];",
  129. "[number 10];");
  130. var jsonld_mode = CodeMirror.getMode(
  131. {indentUnit: 2},
  132. {name: "javascript", jsonld: true}
  133. );
  134. function LD(name) {
  135. test.mode(name, jsonld_mode, Array.prototype.slice.call(arguments, 1));
  136. }
  137. LD("json_ld_keywords",
  138. '{',
  139. ' [meta "@context"]: {',
  140. ' [meta "@base"]: [string "http://example.com"],',
  141. ' [meta "@vocab"]: [string "http://xmlns.com/foaf/0.1/"],',
  142. ' [property "likesFlavor"]: {',
  143. ' [meta "@container"]: [meta "@list"]',
  144. ' [meta "@reverse"]: [string "@beFavoriteOf"]',
  145. ' },',
  146. ' [property "nick"]: { [meta "@container"]: [meta "@set"] },',
  147. ' [property "nick"]: { [meta "@container"]: [meta "@index"] }',
  148. ' },',
  149. ' [meta "@graph"]: [[ {',
  150. ' [meta "@id"]: [string "http://dbpedia.org/resource/John_Lennon"],',
  151. ' [property "name"]: [string "John Lennon"],',
  152. ' [property "modified"]: {',
  153. ' [meta "@value"]: [string "2010-05-29T14:17:39+02:00"],',
  154. ' [meta "@type"]: [string "http://www.w3.org/2001/XMLSchema#dateTime"]',
  155. ' }',
  156. ' } ]]',
  157. '}');
  158. LD("json_ld_fake",
  159. '{',
  160. ' [property "@fake"]: [string "@fake"],',
  161. ' [property "@contextual"]: [string "@identifier"],',
  162. ' [property "user@domain.com"]: [string "@graphical"],',
  163. ' [property "@ID"]: [string "@@ID"]',
  164. '}');
  165. })();