You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
12 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/LICENSE
  3. (function() {
  4. var mode = CodeMirror.getMode({indentUnit: 2}, "soy");
  5. function MT(name) {test.mode(name, mode, Array.prototype.slice.call(arguments, 1));}
  6. // Test of small keywords and words containing them.
  7. MT('keywords-test',
  8. '[keyword {] [keyword as] worrying [keyword and] notorious [keyword as]',
  9. ' the Fandor[operator -]alias assassin, [keyword or]',
  10. ' Corcand cannot fit [keyword in] [keyword }]');
  11. MT('let-test',
  12. '[keyword {template] [def .name][keyword }]',
  13. ' [keyword {let] [def $name]: [string "world"][keyword /}]',
  14. ' [tag&bracket <][tag h1][tag&bracket >]',
  15. ' Hello, [keyword {][variable-2 $name][keyword }]',
  16. ' [tag&bracket </][tag h1][tag&bracket >]',
  17. '[keyword {/template}]',
  18. '');
  19. MT('function-test',
  20. '[keyword {] [callee&variable css]([string "MyClass"])[keyword }]',
  21. '[tag&bracket <][tag input] [attribute value]=[string "]' +
  22. '[keyword {] [callee&variable index]([variable-2&error $list])[keyword }]' +
  23. '[string "][tag&bracket />]');
  24. MT('soy-element-composition-test',
  25. '[keyword <{][callee&variable foo]()[keyword }]',
  26. '[keyword ></>]');
  27. MT('soy-element-composition-attribute-test',
  28. '[keyword <{][callee&variable foo]()[keyword }]',
  29. '[attribute class]=[string "Foo"]',
  30. '[keyword ></>]');
  31. MT('namespace-test',
  32. '[keyword {namespace] [variable namespace][keyword }]')
  33. MT('namespace-with-attribute-test',
  34. '[keyword {namespace] [variable my.namespace.templates] ' +
  35. '[attribute requirecss]=[string "my.namespace"][keyword }]');
  36. MT('operators-test',
  37. '[keyword {] [atom 1] [operator ==] [atom 1] [keyword }]',
  38. '[keyword {] [atom 1] [operator !=] [atom 2] [keyword }]',
  39. '[keyword {] [atom 2] [operator +] [atom 2] [keyword }]',
  40. '[keyword {] [atom 2] [operator -] [atom 2] [keyword }]',
  41. '[keyword {] [atom 2] [operator *] [atom 2] [keyword }]',
  42. '[keyword {] [atom 2] [operator /] [atom 2] [keyword }]',
  43. '[keyword {] [atom 2] [operator %] [atom 2] [keyword }]',
  44. '[keyword {] [atom 2] [operator <=] [atom 2] [keyword }]',
  45. '[keyword {] [atom 2] [operator >=] [atom 2] [keyword }]',
  46. '[keyword {] [atom 3] [operator >] [atom 2] [keyword }]',
  47. '[keyword {] [atom 2] [operator >] [atom 3] [keyword }]',
  48. '[keyword {] [atom null] [operator ?:] [string ""] [keyword }]',
  49. '[keyword {] [variable-2&error $variable] [operator |] safeHtml [keyword }]')
  50. MT('primitive-test',
  51. '[keyword {] [atom true] [keyword }]',
  52. '[keyword {] [atom false] [keyword }]',
  53. '[keyword {] truethy [keyword }]',
  54. '[keyword {] falsey [keyword }]',
  55. '[keyword {] [atom 42] [keyword }]',
  56. '[keyword {] [atom .42] [keyword }]',
  57. '[keyword {] [atom 0.42] [keyword }]',
  58. '[keyword {] [atom -0.42] [keyword }]',
  59. '[keyword {] [atom -.2] [keyword }]',
  60. '[keyword {] [atom 6.03e23] [keyword }]',
  61. '[keyword {] [atom -0.03e0] [keyword }]',
  62. '[keyword {] [atom 0x1F] [keyword }]',
  63. '[keyword {] [atom 0x1F00BBEA] [keyword }]');
  64. MT('param-type-record',
  65. '[keyword {@param] [def record]: [[[property foo]: [type bool], [property bar]: [type int] ]][keyword }]'
  66. );
  67. MT('param-type-map',
  68. '[keyword {@param] [def unknown]: [type map]<[type string], [type bool]>[keyword }]'
  69. );
  70. MT('param-type-list',
  71. '[keyword {@param] [def list]: [type list]<[type ?]>[keyword }]'
  72. );
  73. MT('param-type-any',
  74. '[keyword {@param] [def unknown]: [type ?][keyword }]'
  75. );
  76. MT('param-type-nested',
  77. '[keyword {@param] [def a]: ' +
  78. '[type list]<[[[property a]: [type int], ' +
  79. '[property b]: [type map]<[type string], ' +
  80. '[type bool]>]]>][keyword }]');
  81. MT('undefined-var',
  82. '[keyword {][variable-2&error $var]');
  83. MT('param-scope-test',
  84. '[keyword {template] [def .a][keyword }]',
  85. ' [keyword {@param] [def x]: [type string][keyword }]',
  86. ' [keyword {][variable-2 $x][keyword }]',
  87. '[keyword {/template}]',
  88. '',
  89. '[keyword {template] [def .b][keyword }]',
  90. ' [keyword {][variable-2&error $x][keyword }]',
  91. '[keyword {/template}]',
  92. '');
  93. MT('if-variable-test',
  94. '[keyword {if] [variable-2&error $showThing][keyword }]',
  95. ' Yo!',
  96. '[keyword {/if}]',
  97. '');
  98. MT('defined-if-variable-test',
  99. '[keyword {template] [def .foo][keyword }]',
  100. ' [keyword {@param?] [def showThing]: [type bool][keyword }]',
  101. ' [keyword {if] [variable-2 $showThing][keyword }]',
  102. ' Yo!',
  103. ' [keyword {/if}]',
  104. '[keyword {/template}]',
  105. '');
  106. MT('template-calls-test',
  107. '[keyword {call] [variable-2 .foo][keyword /}]',
  108. '[keyword {call] [variable foo][keyword /}]',
  109. '[keyword {call] [variable foo][keyword }] [keyword {/call}]',
  110. '[keyword {call] [variable first1.second.third_3][keyword /}]',
  111. '[keyword {call] [variable first1.second.third_3] [keyword }] [keyword {/call}]',
  112. '');
  113. MT('foreach-scope-test',
  114. '[keyword {@param] [def bar]: [type string][keyword }]',
  115. '[keyword {foreach] [def $foo] [keyword in] [variable-2&error $foos][keyword }]',
  116. ' [keyword {][variable-2 $foo][keyword }]',
  117. '[keyword {/foreach}]',
  118. '[keyword {][variable-2&error $foo][keyword }]',
  119. '[keyword {][variable-2 $bar][keyword }]');
  120. MT('foreach-ifempty-indent-test',
  121. '[keyword {foreach] [def $foo] [keyword in] [variable-2&error $foos][keyword }]',
  122. ' something',
  123. '[keyword {ifempty}]',
  124. ' nothing',
  125. '[keyword {/foreach}]',
  126. '');
  127. MT('foreach-index',
  128. '[keyword {foreach] [def $foo],[def $index] [keyword in] [[]] [keyword }]',
  129. ' [keyword {][variable-2 $foo][keyword }] [keyword {][variable-2 $index][keyword }]',
  130. '[keyword {/foreach}]');
  131. MT('nested-kind-test',
  132. '[keyword {template] [def .foo] [attribute kind]=[string "html"][keyword }]',
  133. ' [tag&bracket <][tag div][tag&bracket >]',
  134. ' [keyword {call] [variable-2 .bar][keyword }]',
  135. ' [keyword {param] [property propertyName] [attribute kind]=[string "js"][keyword }]',
  136. ' [keyword var] [def bar] [operator =] [number 5];',
  137. ' [keyword {/param}]',
  138. ' [keyword {/call}]',
  139. ' [tag&bracket </][tag div][tag&bracket >]',
  140. '[keyword {/template}]',
  141. '');
  142. MT('tag-starting-with-function-call-is-not-a-keyword',
  143. '[keyword {][callee&variable index]([variable-2&error $foo])[keyword }]',
  144. '[keyword {css] [string "some-class"][keyword }]',
  145. '[keyword {][callee&variable css]([string "some-class"])[keyword }]',
  146. '');
  147. MT('allow-missing-colon-in-@param',
  148. '[keyword {template] [def .foo][keyword }]',
  149. ' [keyword {@param] [def showThing] [type bool][keyword }]',
  150. ' [keyword {if] [variable-2 $showThing][keyword }]',
  151. ' Yo!',
  152. ' [keyword {/if}]',
  153. '[keyword {/template}]',
  154. '');
  155. MT('param-type-and-default-value',
  156. '[keyword {template] [def .foo][keyword }]',
  157. ' [keyword {@param] [def bar]: [type bool] = [atom true][keyword }]',
  158. '[keyword {/template}]',
  159. '');
  160. MT('attribute-type',
  161. '[keyword {template] [def .foo][keyword }]',
  162. ' [keyword {@attribute] [def bar]: [type string][keyword }]',
  163. '[keyword {/template}]',
  164. '');
  165. MT('attribute-type-optional',
  166. '[keyword {template] [def .foo][keyword }]',
  167. ' [keyword {@attribute] [def bar]: [type string][keyword }]',
  168. '[keyword {/template}]',
  169. '');
  170. MT('attribute-type-all',
  171. '[keyword {template] [def .foo][keyword }]',
  172. ' [keyword {@attribute] [type *][keyword }]',
  173. '[keyword {/template}]',
  174. '');
  175. MT('state-variable-reference',
  176. '[keyword {template] [def .foo][keyword }]',
  177. ' [keyword {@param] [def bar]:= [atom true][keyword }]',
  178. ' [keyword {@state] [def foobar]:= [variable-2 $bar][keyword }]',
  179. '[keyword {/template}]',
  180. '');
  181. MT('param-type-template',
  182. '[keyword {template] [def .foo][keyword }]',
  183. ' [keyword {@param] [def renderer]: ([def s]:[type string])=>[type html][keyword }]',
  184. ' [keyword {call] [variable-2 $renderer] [keyword /}]',
  185. '[keyword {/template}]',
  186. '');
  187. MT('single-quote-strings',
  188. '[keyword {][string "foo"] [string \'bar\'][keyword }]',
  189. '');
  190. MT('literal-comments',
  191. '[keyword {literal}]/* comment */ // comment[keyword {/literal}]');
  192. MT('highlight-command-at-eol',
  193. '[keyword {msg]',
  194. ' [keyword }]');
  195. MT('switch-indent-test',
  196. '[keyword {let] [def $marbles]: [atom 5] [keyword /}]',
  197. '[keyword {switch] [variable-2 $marbles][keyword }]',
  198. ' [keyword {case] [atom 0][keyword }]',
  199. ' No marbles',
  200. ' [keyword {default}]',
  201. ' At least 1 marble',
  202. '[keyword {/switch}]',
  203. '');
  204. MT('if-elseif-else-indent',
  205. '[keyword {if] [atom true][keyword }]',
  206. ' [keyword {let] [def $a]: [atom 5] [keyword /}]',
  207. '[keyword {elseif] [atom false][keyword }]',
  208. ' [keyword {let] [def $bar]: [atom 5] [keyword /}]',
  209. '[keyword {else}]',
  210. ' [keyword {let] [def $bar]: [atom 5] [keyword /}]',
  211. '[keyword {/if}]');
  212. MT('msg-fallbackmsg-indent',
  213. '[keyword {msg] [attribute desc]=[string "A message"][keyword }]',
  214. ' A message',
  215. '[keyword {fallbackmsg] [attribute desc]=[string "A message"][keyword }]',
  216. ' Old message',
  217. '[keyword {/msg}]');
  218. MT('literal-indent',
  219. '[keyword {template] [def .name][keyword }]',
  220. ' [keyword {literal}]',
  221. ' Lerum',
  222. ' [keyword {/literal}]',
  223. ' Ipsum',
  224. '[keyword {/template}]');
  225. MT('special-chars',
  226. '[keyword {sp}]',
  227. '[keyword {nil}]',
  228. '[keyword {\\r}]',
  229. '[keyword {\\n}]',
  230. '[keyword {\\t}]',
  231. '[keyword {lb}]',
  232. '[keyword {rb}]');
  233. MT('let-list-literal',
  234. '[keyword {let] [def $test]: [[[[[string \'a\'] ], [[[string \'b\'] ] ] [keyword /}]');
  235. MT('let-record-literal',
  236. '[keyword {let] [def $test]: [keyword record]([property test]: [callee&variable bidiGlobalDir](), ' +
  237. '[property foo]: [atom 5]) [keyword /}]');
  238. MT('let-map-literal',
  239. '[keyword {let] [def $test]: [keyword map]([string \'outer\']: [keyword map]([atom 5]: [atom false]), ' +
  240. '[string \'foo\']: [string \'bar\']) [keyword /}]');
  241. MT('wrong-closing-tag',
  242. '[keyword {if] [atom true][keyword }]',
  243. ' Optional',
  244. '[keyword&error {/badend][keyword }]');
  245. MT('list-comprehension',
  246. '[keyword {let] [def $myList]: [[[[[string \'a\'] ] ] [keyword /}] ' +
  247. '[keyword {let] [def $test]: [[[variable $a] [operator +] [atom 1] [keyword for] ' +
  248. '[def $a] [keyword in] [variable-2 $myList] [keyword if] [variable-2 $a] [operator >=] [atom 3] ] [keyword /}]');
  249. MT('list-comprehension-index',
  250. '[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
  251. '[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]');
  252. MT('list-comprehension-variable-scope',
  253. '[keyword {let] [def $name]: [string "world"][keyword /}]',
  254. '[keyword {let] [def $test]: [[[variable $a] [operator +] [variable $index] [keyword for] ' +
  255. '[def $a],[def $index] [keyword in] [[]] [keyword if] [variable-2 $a] [operator >=] [variable-2 $index] ] [keyword /}]',
  256. '[keyword {][variable-2&error $a][keyword }]',
  257. '[keyword {][variable-2&error $index][keyword }]',
  258. '[keyword {][variable-2 $test][keyword }]',
  259. '[keyword {][variable-2 $name][keyword }]');
  260. MT('import',
  261. '[keyword import] {[def Name], [variable Person] [keyword as] [def P]} [keyword from] [string \'examples/proto/example.proto\'];');
  262. MT('velog',
  263. '[keyword {velog] [variable-2&error $data][keyword }] Logged [keyword {/velog}]');
  264. MT('extern', '[keyword {extern] [def renderer]: ([def s]:[type string])=>[type string][keyword }] [keyword {/extern}]');
  265. MT('export extern', '[keyword {export] [keyword extern] [def renderer]: ([def s]:[type string])=>[type string][keyword }] [keyword {/extern}]');
  266. MT('const',
  267. '[keyword {const] [def FOO] = [atom 5] [keyword /}]',
  268. '[keyword {export] [keyword const] [def FOO] = [atom 5] [keyword /}]');
  269. })();