browser_gcli_tokenize.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright 2012, Mozilla Foundation and contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. "use strict";
  17. // THIS FILE IS GENERATED FROM SOURCE IN THE GCLI PROJECT
  18. // PLEASE TALK TO SOMEONE IN DEVELOPER TOOLS BEFORE EDITING IT
  19. const exports = {};
  20. function test() {
  21. helpers.runTestModule(exports, "browser_gcli_tokenize.js");
  22. }
  23. // var assert = require('../testharness/assert');
  24. var cli = require("gcli/cli");
  25. exports.testBlanks = function (options) {
  26. var args;
  27. args = cli.tokenize("");
  28. assert.is(args.length, 1);
  29. assert.is(args[0].text, "");
  30. assert.is(args[0].prefix, "");
  31. assert.is(args[0].suffix, "");
  32. args = cli.tokenize(" ");
  33. assert.is(args.length, 1);
  34. assert.is(args[0].text, "");
  35. assert.is(args[0].prefix, " ");
  36. assert.is(args[0].suffix, "");
  37. };
  38. exports.testTokSimple = function (options) {
  39. var args;
  40. args = cli.tokenize("s");
  41. assert.is(args.length, 1);
  42. assert.is(args[0].text, "s");
  43. assert.is(args[0].prefix, "");
  44. assert.is(args[0].suffix, "");
  45. assert.is(args[0].type, "Argument");
  46. args = cli.tokenize("s s");
  47. assert.is(args.length, 2);
  48. assert.is(args[0].text, "s");
  49. assert.is(args[0].prefix, "");
  50. assert.is(args[0].suffix, "");
  51. assert.is(args[0].type, "Argument");
  52. assert.is(args[1].text, "s");
  53. assert.is(args[1].prefix, " ");
  54. assert.is(args[1].suffix, "");
  55. assert.is(args[1].type, "Argument");
  56. };
  57. exports.testJavascript = function (options) {
  58. var args;
  59. args = cli.tokenize("{x}");
  60. assert.is(args.length, 1);
  61. assert.is(args[0].text, "x");
  62. assert.is(args[0].prefix, "{");
  63. assert.is(args[0].suffix, "}");
  64. assert.is(args[0].type, "ScriptArgument");
  65. args = cli.tokenize("{ x }");
  66. assert.is(args.length, 1);
  67. assert.is(args[0].text, "x");
  68. assert.is(args[0].prefix, "{ ");
  69. assert.is(args[0].suffix, " }");
  70. assert.is(args[0].type, "ScriptArgument");
  71. args = cli.tokenize("{x} {y}");
  72. assert.is(args.length, 2);
  73. assert.is(args[0].text, "x");
  74. assert.is(args[0].prefix, "{");
  75. assert.is(args[0].suffix, "}");
  76. assert.is(args[0].type, "ScriptArgument");
  77. assert.is(args[1].text, "y");
  78. assert.is(args[1].prefix, " {");
  79. assert.is(args[1].suffix, "}");
  80. assert.is(args[1].type, "ScriptArgument");
  81. args = cli.tokenize("{x}{y}");
  82. assert.is(args.length, 2);
  83. assert.is(args[0].text, "x");
  84. assert.is(args[0].prefix, "{");
  85. assert.is(args[0].suffix, "}");
  86. assert.is(args[0].type, "ScriptArgument");
  87. assert.is(args[1].text, "y");
  88. assert.is(args[1].prefix, "{");
  89. assert.is(args[1].suffix, "}");
  90. assert.is(args[1].type, "ScriptArgument");
  91. args = cli.tokenize("{");
  92. assert.is(args.length, 1);
  93. assert.is(args[0].text, "");
  94. assert.is(args[0].prefix, "{");
  95. assert.is(args[0].suffix, "");
  96. assert.is(args[0].type, "ScriptArgument");
  97. args = cli.tokenize("{ ");
  98. assert.is(args.length, 1);
  99. assert.is(args[0].text, "");
  100. assert.is(args[0].prefix, "{ ");
  101. assert.is(args[0].suffix, "");
  102. assert.is(args[0].type, "ScriptArgument");
  103. args = cli.tokenize("{x");
  104. assert.is(args.length, 1);
  105. assert.is(args[0].text, "x");
  106. assert.is(args[0].prefix, "{");
  107. assert.is(args[0].suffix, "");
  108. assert.is(args[0].type, "ScriptArgument");
  109. };
  110. exports.testRegularNesting = function (options) {
  111. var args;
  112. args = cli.tokenize('{"x"}');
  113. assert.is(args.length, 1);
  114. assert.is(args[0].text, '"x"');
  115. assert.is(args[0].prefix, "{");
  116. assert.is(args[0].suffix, "}");
  117. assert.is(args[0].type, "ScriptArgument");
  118. args = cli.tokenize("{'x'}");
  119. assert.is(args.length, 1);
  120. assert.is(args[0].text, "'x'");
  121. assert.is(args[0].prefix, "{");
  122. assert.is(args[0].suffix, "}");
  123. assert.is(args[0].type, "ScriptArgument");
  124. args = cli.tokenize('"{x}"');
  125. assert.is(args.length, 1);
  126. assert.is(args[0].text, "{x}");
  127. assert.is(args[0].prefix, '"');
  128. assert.is(args[0].suffix, '"');
  129. assert.is(args[0].type, "Argument");
  130. args = cli.tokenize("'{x}'");
  131. assert.is(args.length, 1);
  132. assert.is(args[0].text, "{x}");
  133. assert.is(args[0].prefix, "'");
  134. assert.is(args[0].suffix, "'");
  135. assert.is(args[0].type, "Argument");
  136. };
  137. exports.testDeepNesting = function (options) {
  138. var args;
  139. args = cli.tokenize("{{}}");
  140. assert.is(args.length, 1);
  141. assert.is(args[0].text, "{}");
  142. assert.is(args[0].prefix, "{");
  143. assert.is(args[0].suffix, "}");
  144. assert.is(args[0].type, "ScriptArgument");
  145. args = cli.tokenize("{{x} {y}}");
  146. assert.is(args.length, 1);
  147. assert.is(args[0].text, "{x} {y}");
  148. assert.is(args[0].prefix, "{");
  149. assert.is(args[0].suffix, "}");
  150. assert.is(args[0].type, "ScriptArgument");
  151. args = cli.tokenize("{{w} {{{x}}}} {y} {{{z}}}");
  152. assert.is(args.length, 3);
  153. assert.is(args[0].text, "{w} {{{x}}}");
  154. assert.is(args[0].prefix, "{");
  155. assert.is(args[0].suffix, "}");
  156. assert.is(args[0].type, "ScriptArgument");
  157. assert.is(args[1].text, "y");
  158. assert.is(args[1].prefix, " {");
  159. assert.is(args[1].suffix, "}");
  160. assert.is(args[1].type, "ScriptArgument");
  161. assert.is(args[2].text, "{{z}}");
  162. assert.is(args[2].prefix, " {");
  163. assert.is(args[2].suffix, "}");
  164. assert.is(args[2].type, "ScriptArgument");
  165. args = cli.tokenize("{{w} {{{x}}} {y} {{{z}}}");
  166. assert.is(args.length, 1);
  167. assert.is(args[0].text, "{w} {{{x}}} {y} {{{z}}}");
  168. assert.is(args[0].prefix, "{");
  169. assert.is(args[0].suffix, "");
  170. assert.is(args[0].type, "ScriptArgument");
  171. };
  172. exports.testStrangeNesting = function (options) {
  173. var args;
  174. // Note: When we get real JS parsing this should break
  175. args = cli.tokenize('{"x}"}');
  176. assert.is(args.length, 2);
  177. assert.is(args[0].text, '"x');
  178. assert.is(args[0].prefix, "{");
  179. assert.is(args[0].suffix, "}");
  180. assert.is(args[0].type, "ScriptArgument");
  181. assert.is(args[1].text, "}");
  182. assert.is(args[1].prefix, '"');
  183. assert.is(args[1].suffix, "");
  184. assert.is(args[1].type, "Argument");
  185. };
  186. exports.testComplex = function (options) {
  187. var args;
  188. args = cli.tokenize(" 1234 '12 34'");
  189. assert.is(args.length, 2);
  190. assert.is(args[0].text, "1234");
  191. assert.is(args[0].prefix, " ");
  192. assert.is(args[0].suffix, "");
  193. assert.is(args[0].type, "Argument");
  194. assert.is(args[1].text, "12 34");
  195. assert.is(args[1].prefix, " '");
  196. assert.is(args[1].suffix, "'");
  197. assert.is(args[1].type, "Argument");
  198. args = cli.tokenize('12\'34 "12 34" \\'); // 12'34 "12 34" \
  199. assert.is(args.length, 3);
  200. assert.is(args[0].text, "12'34");
  201. assert.is(args[0].prefix, "");
  202. assert.is(args[0].suffix, "");
  203. assert.is(args[0].type, "Argument");
  204. assert.is(args[1].text, "12 34");
  205. assert.is(args[1].prefix, ' "');
  206. assert.is(args[1].suffix, '"');
  207. assert.is(args[1].type, "Argument");
  208. assert.is(args[2].text, "\\");
  209. assert.is(args[2].prefix, " ");
  210. assert.is(args[2].suffix, "");
  211. assert.is(args[2].type, "Argument");
  212. };
  213. exports.testPathological = function (options) {
  214. var args;
  215. args = cli.tokenize('a\\ b \\t\\n\\r \\\'x\\\" \'d'); // a_b \t\n\r \'x\" 'd
  216. assert.is(args.length, 4);
  217. assert.is(args[0].text, "a\\ b");
  218. assert.is(args[0].prefix, "");
  219. assert.is(args[0].suffix, "");
  220. assert.is(args[0].type, "Argument");
  221. assert.is(args[1].text, "\\t\\n\\r");
  222. assert.is(args[1].prefix, " ");
  223. assert.is(args[1].suffix, "");
  224. assert.is(args[1].type, "Argument");
  225. assert.is(args[2].text, '\\\'x\\"');
  226. assert.is(args[2].prefix, " ");
  227. assert.is(args[2].suffix, "");
  228. assert.is(args[2].type, "Argument");
  229. assert.is(args[3].text, "d");
  230. assert.is(args[3].prefix, " '");
  231. assert.is(args[3].suffix, "");
  232. assert.is(args[3].type, "Argument");
  233. };