unified-el-spec.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. describe('Unified expression language grammar', function() {
  2. let grammar = null;
  3. beforeEach(function() {
  4. atom.config.set('core.useTreeSitterParsers', false);
  5. waitsForPromise(() => atom.packages.activatePackage('language-java'));
  6. runs(() => grammar = atom.grammars.grammarForScopeName('source.java.el'));
  7. });
  8. it('parses the grammar', function() {
  9. expect(grammar).toBeTruthy();
  10. expect(grammar.scopeName).toBe('source.java.el');
  11. });
  12. describe('operators', function() {
  13. it('tokenizes the ternary operator', function() {
  14. const {tokens} = grammar.tokenizeLine('true ? 0 : 1');
  15. expect(tokens[2]).toEqual({value: '?', scopes: ['source.java.el', 'keyword.control.ternary.java.el']});
  16. expect(tokens[6]).toEqual({value: ':', scopes: ['source.java.el', 'keyword.control.ternary.java.el']});
  17. });
  18. it('parses the comparison operator `==`', function() {
  19. const {tokens} = grammar.tokenizeLine('1 == 1');
  20. expect(tokens[2]).toEqual({value: '==', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  21. });
  22. it('parses the comparison operator `!=`', function() {
  23. const {tokens} = grammar.tokenizeLine('1 != 1');
  24. expect(tokens[2]).toEqual({value: '!=', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  25. });
  26. it('parses the comparison operator `<=`', function() {
  27. const {tokens} = grammar.tokenizeLine('1 <= 1');
  28. expect(tokens[2]).toEqual({value: '<=', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  29. });
  30. it('parses the comparison operator `>=`', function() {
  31. const {tokens} = grammar.tokenizeLine('1 >= 1');
  32. expect(tokens[2]).toEqual({value: '>=', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  33. });
  34. it('parses the comparison operator `<`', function() {
  35. const {tokens} = grammar.tokenizeLine('1 < 1');
  36. expect(tokens[2]).toEqual({value: '<', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  37. });
  38. it('parses the comparison operator `>`', function() {
  39. const {tokens} = grammar.tokenizeLine('1 > 1');
  40. expect(tokens[2]).toEqual({value: '>', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  41. });
  42. it('parses the comparison operator `eq`', function() {
  43. const {tokens} = grammar.tokenizeLine('1 eq 1');
  44. expect(tokens[2]).toEqual({value: 'eq', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  45. });
  46. it('parses the comparison operator `ne`', function() {
  47. const {tokens} = grammar.tokenizeLine('1 ne 1');
  48. expect(tokens[2]).toEqual({value: 'ne', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  49. });
  50. it('parses the comparison operator `le`', function() {
  51. const {tokens} = grammar.tokenizeLine('1 le 1');
  52. expect(tokens[2]).toEqual({value: 'le', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  53. });
  54. it('parses the comparison operator `gt`', function() {
  55. const {tokens} = grammar.tokenizeLine('1 gt 1');
  56. expect(tokens[2]).toEqual({value: 'gt', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  57. });
  58. it('parses the comparison operator `lt`', function() {
  59. const {tokens} = grammar.tokenizeLine('1 lt 1');
  60. expect(tokens[2]).toEqual({value: 'lt', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  61. });
  62. it('parses the comparison operator `gt`', function() {
  63. const {tokens} = grammar.tokenizeLine('1 gt 1');
  64. expect(tokens[2]).toEqual({value: 'gt', scopes: ['source.java.el', 'keyword.operator.comparison.java.el']});
  65. });
  66. it('parses the empty operators', function() {
  67. const {tokens} = grammar.tokenizeLine('empty foo');
  68. expect(tokens[0]).toEqual({value: 'empty', scopes: ['source.java.el', 'keyword.operator.empty.java.el']});
  69. });
  70. it('parses the arithmetic operator `-`', function() {
  71. const {tokens} = grammar.tokenizeLine('1 - 1');
  72. expect(tokens[2]).toEqual({value: '-', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  73. });
  74. it('parses the arithmetic operator `+`', function() {
  75. const {tokens} = grammar.tokenizeLine('1 + 1');
  76. expect(tokens[2]).toEqual({value: '+', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  77. });
  78. it('parses the arithmetic operator `*`', function() {
  79. const {tokens} = grammar.tokenizeLine('1 * 1');
  80. expect(tokens[2]).toEqual({value: '*', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  81. });
  82. it('parses the arithmetic operator `/`', function() {
  83. const {tokens} = grammar.tokenizeLine('1 / 1');
  84. expect(tokens[2]).toEqual({value: '/', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  85. });
  86. it('parses the arithmetic operator `%`', function() {
  87. const {tokens} = grammar.tokenizeLine('1 % 1');
  88. expect(tokens[2]).toEqual({value: '%', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  89. });
  90. it('parses the arithmetic operator `div`', function() {
  91. const {tokens} = grammar.tokenizeLine('1 div 1');
  92. expect(tokens[2]).toEqual({value: 'div', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  93. });
  94. it('parses the arithmetic operator `mod`', function() {
  95. const {tokens} = grammar.tokenizeLine('1 mod 1');
  96. expect(tokens[2]).toEqual({value: 'mod', scopes: ['source.java.el', 'keyword.operator.arithmetic.java.el']});
  97. });
  98. it('parses the logical operator `!`', function() {
  99. const {tokens} = grammar.tokenizeLine('!foo');
  100. expect(tokens[0]).toEqual({value: '!', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  101. });
  102. it('parses the logical operator `&&`', function() {
  103. const {tokens} = grammar.tokenizeLine('1 && 1');
  104. expect(tokens[2]).toEqual({value: '&&', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  105. });
  106. it('parses the logical operator `||`', function() {
  107. const {tokens} = grammar.tokenizeLine('1 || 1');
  108. expect(tokens[2]).toEqual({value: '||', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  109. });
  110. it('parses the logical operator `not`', function() {
  111. const {tokens} = grammar.tokenizeLine('1 not 1');
  112. expect(tokens[2]).toEqual({value: 'not', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  113. });
  114. it('parses the logical operator `and`', function() {
  115. const {tokens} = grammar.tokenizeLine('1 and 1');
  116. expect(tokens[2]).toEqual({value: 'and', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  117. });
  118. it('parses the logical operator `or`', function() {
  119. const {tokens} = grammar.tokenizeLine('1 or 1');
  120. expect(tokens[2]).toEqual({value: 'or', scopes: ['source.java.el', 'keyword.operator.logical.java.el']});
  121. });
  122. });
  123. describe('literals', function() {
  124. it('parses boolean literals', function() {
  125. let {tokens} = grammar.tokenizeLine('true');
  126. expect(tokens[0]).toEqual({value: 'true', scopes: ['source.java.el', 'constant.boolean.java.el']});
  127. ({tokens} = grammar.tokenizeLine('false'));
  128. expect(tokens[0]).toEqual({value: 'false', scopes: ['source.java.el', 'constant.boolean.java.el']});
  129. });
  130. it('parses the null literal', function() {
  131. let tokens;
  132. return ({tokens} = grammar.tokenizeLine('null'));
  133. });
  134. it('parses numeric literals', function() {
  135. let {tokens} = grammar.tokenizeLine('0');
  136. expect(tokens[0]).toEqual({value: '0', scopes: ['source.java.el', 'constant.numeric.java.el']});
  137. ({tokens} = grammar.tokenizeLine('9804'));
  138. expect(tokens[0]).toEqual({value: '9804', scopes: ['source.java.el', 'constant.numeric.java.el']});
  139. ({tokens} = grammar.tokenizeLine('0.54'));
  140. expect(tokens[0]).toEqual({value: '0.54', scopes: ['source.java.el', 'constant.numeric.java.el']});
  141. ({tokens} = grammar.tokenizeLine('13.12'));
  142. expect(tokens[0]).toEqual({value: '13.12', scopes: ['source.java.el', 'constant.numeric.java.el']});
  143. });
  144. it('tokenizes single quoted string literals', function() {
  145. const {tokens} = grammar.tokenizeLine("'foo\\n bar \\\'baz'");
  146. expect(tokens[0]).toEqual({value: "'", scopes: ['source.java.el', 'string.quoted.single.java.el', 'punctuation.definition.string.begin.java.el']});
  147. expect(tokens[1]).toEqual({value: 'foo', scopes: ['source.java.el', 'string.quoted.single.java.el']});
  148. expect(tokens[2]).toEqual({value: '\\n', scopes: ['source.java.el', 'string.quoted.single.java.el', 'constant.character.escape.java.el']});
  149. expect(tokens[3]).toEqual({value: ' bar ', scopes: ['source.java.el', 'string.quoted.single.java.el']});
  150. expect(tokens[4]).toEqual({value: '\\\'', scopes: ['source.java.el', 'string.quoted.single.java.el', 'constant.character.escape.java.el']});
  151. expect(tokens[5]).toEqual({value: 'baz', scopes: ['source.java.el', 'string.quoted.single.java.el']});
  152. expect(tokens[6]).toEqual({value: "'", scopes: ['source.java.el', 'string.quoted.single.java.el', 'punctuation.definition.string.end.java.el']});
  153. });
  154. it('tokenizes double quoted string literals', function() {
  155. const {tokens} = grammar.tokenizeLine('"foo\\n bar \\\"baz"');
  156. expect(tokens[0]).toEqual({value: '"', scopes: ['source.java.el', 'string.quoted.double.java.el', 'punctuation.definition.string.begin.java.el']});
  157. expect(tokens[1]).toEqual({value: 'foo', scopes: ['source.java.el', 'string.quoted.double.java.el']});
  158. expect(tokens[2]).toEqual({value: '\\n', scopes: ['source.java.el', 'string.quoted.double.java.el', 'constant.character.escape.java.el']});
  159. expect(tokens[3]).toEqual({value: ' bar ', scopes: ['source.java.el', 'string.quoted.double.java.el']});
  160. expect(tokens[4]).toEqual({value: '\\\"', scopes: ['source.java.el', 'string.quoted.double.java.el', 'constant.character.escape.java.el']});
  161. expect(tokens[5]).toEqual({value: 'baz', scopes: ['source.java.el', 'string.quoted.double.java.el']});
  162. expect(tokens[6]).toEqual({value: '"', scopes: ['source.java.el', 'string.quoted.double.java.el', 'punctuation.definition.string.end.java.el']});
  163. });
  164. });
  165. it('tokenizes function calls', function() {
  166. const {tokens} = grammar.tokenizeLine('fn:split(foo, bar)');
  167. expect(tokens[0]).toEqual({value: 'fn', scopes: ['source.java.el', 'namespace.java.el']});
  168. expect(tokens[1]).toEqual({value: ':', scopes: ['source.java.el', 'namespace.java.el', 'punctuation.separator.namespace.java.el']});
  169. expect(tokens[2]).toEqual({value: 'split', scopes: ['source.java.el']});
  170. expect(tokens[3]).toEqual({value: '(', scopes: ['source.java.el', 'meta.brace.round.java.el']});
  171. expect(tokens[4]).toEqual({value: 'foo', scopes: ['source.java.el']});
  172. expect(tokens[5]).toEqual({value: ',', scopes: ['source.java.el', 'meta.delimiter.java.el']});
  173. expect(tokens[6]).toEqual({value: ' bar', scopes: ['source.java.el']});
  174. expect(tokens[7]).toEqual({value: ')', scopes: ['source.java.el', 'meta.brace.round.java.el']});
  175. });
  176. it('tokenizes a computed property access', function() {
  177. const {tokens} = grammar.tokenizeLine('foo[0]');
  178. expect(tokens[0]).toEqual({value: 'foo', scopes: ['source.java.el']});
  179. expect(tokens[1]).toEqual({value: '[', scopes: ['source.java.el', 'meta.brace.square.java.el']});
  180. expect(tokens[2]).toEqual({value: '0', scopes: ['source.java.el', 'constant.numeric.java.el']});
  181. expect(tokens[3]).toEqual({value: ']', scopes: ['source.java.el', 'meta.brace.square.java.el']});
  182. });
  183. });