javascript_test.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. if (typeof process !== "undefined") {
  31. require("amd-loader");
  32. }
  33. define(function(require, exports, module) {
  34. "use strict";
  35. var EditSession = require("../edit_session").EditSession;
  36. var Tokenizer = require("../tokenizer").Tokenizer;
  37. var JavaScriptMode = require("./javascript").Mode;
  38. var assert = require("../test/assertions");
  39. module.exports = {
  40. setUp : function() {
  41. this.mode = new JavaScriptMode();
  42. },
  43. "test: getTokenizer() (smoke test)" : function() {
  44. var tokenizer = this.mode.getTokenizer();
  45. assert.ok(tokenizer instanceof Tokenizer);
  46. var tokens = tokenizer.getLineTokens("'juhu'", "start").tokens;
  47. assert.equal("string", tokens[0].type);
  48. },
  49. "test: toggle comment lines should prepend '//' to each line" : function() {
  50. var session = new EditSession([" abc", "cde", "fg"]);
  51. session.setTabSize(1);
  52. this.mode.toggleCommentLines("start", session, 0, 1);
  53. assert.equal(["// abc", "// cde", "fg"].join("\n"), session.toString());
  54. },
  55. "test: toggle comment on commented lines should remove leading '//' chars" : function() {
  56. var session = new EditSession(["// abc", "//cde", "fg"]);
  57. session.setTabSize(1);
  58. this.mode.toggleCommentLines("start", session, 0, 1);
  59. assert.equal([" abc", "cde", "fg"].join("\n"), session.toString());
  60. },
  61. "test: toggle comment on all empty lines" : function() {
  62. var session = new EditSession([" ", " ", " "]);
  63. session.setTabSize(1);
  64. this.mode.toggleCommentLines("start", session, 0, 1);
  65. assert.equal([" // ", " // ", " "].join("\n"), session.toString());
  66. },
  67. "test: toggle comment with empty lines" : function() {
  68. var session = new EditSession([
  69. " abc",
  70. "",
  71. " cde",
  72. " fg"]);
  73. var initial = session.toString();
  74. this.mode.toggleCommentLines("start", session, 0, 3);
  75. assert.equal([
  76. " // abc",
  77. "",
  78. " // cde",
  79. " // fg"].join("\n"),
  80. session.toString()
  81. );
  82. this.mode.toggleCommentLines("start", session, 0, 3);
  83. assert.equal(initial, session.toString());
  84. },
  85. "test: toggle comment lines twice should return the original text" : function() {
  86. var session = new EditSession([" abc", "cde", "fg"]);
  87. this.mode.toggleCommentLines("start", session, 0, 2);
  88. this.mode.toggleCommentLines("start", session, 0, 2);
  89. assert.equal([" abc", "cde", "fg"].join("\n"), session.toString());
  90. },
  91. "test: toggle comment on multiple lines with one commented line prepend '//' to each line" : function() {
  92. var session = new EditSession([" // abc", " //cde", " fg"]);
  93. session.setTabSize(1);
  94. this.mode.toggleCommentLines("start", session, 0, 2);
  95. assert.equal([" // // abc", " // //cde", " // fg"].join("\n"), session.toString());
  96. },
  97. "test: toggle comment on a comment line with leading white space": function() {
  98. var session = new EditSession(["//cde", " //fg"]);
  99. this.mode.toggleCommentLines("start", session, 0, 1);
  100. assert.equal(["cde", " fg"].join("\n"), session.toString());
  101. },
  102. "test: toggle comment lines should take tabsize into account" : function() {
  103. var session = new EditSession([" // abc", " // cde", "// fg"]);
  104. session.setTabSize(2);
  105. this.mode.toggleCommentLines("start", session, 0, 2);
  106. assert.equal([" abc", " cde", " fg"].join("\n"), session.toString());
  107. session.setTabSize(4);
  108. this.mode.toggleCommentLines("start", session, 0, 2);
  109. assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString());
  110. this.mode.toggleCommentLines("start", session, 0, 2);
  111. assert.equal([" abc", " cde", " fg"].join("\n"), session.toString());
  112. session.insert({row: 0, column: 0}, " ");
  113. this.mode.toggleCommentLines("start", session, 0, 2);
  114. assert.equal(["// abc", "// cde", "// fg"].join("\n"), session.toString());
  115. },
  116. //there doesn't seem to be any way to make this work
  117. "!test: togglecomment on line with one space" : function() {
  118. var session = new EditSession([" abc", " // cde", "// fg"]);
  119. var initialValue = session + "";
  120. session.setTabSize(4);
  121. this.mode.toggleCommentLines("start", session, 0, 0);
  122. this.mode.toggleCommentLines("start", session, 0, 0);
  123. assert.equal(initialValue, session.toString());
  124. },
  125. "test: auto indent after opening brace" : function() {
  126. assert.equal(" ", this.mode.getNextLineIndent("start", "if () {", " "));
  127. },
  128. "test: auto indent after case" : function() {
  129. assert.equal(" ", this.mode.getNextLineIndent("start", "case 'juhu':", " "));
  130. },
  131. "test: no auto indent in object literal" : function() {
  132. assert.equal("", this.mode.getNextLineIndent("start", "{ 'juhu':", " "));
  133. },
  134. "test: no auto indent after opening brace in multi line comment" : function() {
  135. assert.equal("", this.mode.getNextLineIndent("start", "/*if () {", " "));
  136. assert.equal(" ", this.mode.getNextLineIndent("comment", " abcd", " "));
  137. },
  138. "test: no auto indent after opening brace in single line comment" : function() {
  139. assert.equal("", this.mode.getNextLineIndent("start", "//if () {", " "));
  140. assert.equal(" ", this.mode.getNextLineIndent("start", " //if () {", " "));
  141. },
  142. "test: no auto indent should add to existing indent" : function() {
  143. assert.equal(" ", this.mode.getNextLineIndent("start", " if () {", " "));
  144. assert.equal(" ", this.mode.getNextLineIndent("start", " cde", " "));
  145. assert.equal(" ", this.mode.getNextLineIndent("start", "function foo(items) {", " "));
  146. },
  147. "test: special indent in doc comments" : function() {
  148. assert.equal(" * ", this.mode.getNextLineIndent("doc-start", "/**", " "));
  149. assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " /**", " "));
  150. assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
  151. assert.equal(" * ", this.mode.getNextLineIndent("doc-start", " *", " "));
  152. assert.equal(" ", this.mode.getNextLineIndent("doc-start", " abc", " "));
  153. },
  154. "test: no indent after doc comments" : function() {
  155. assert.equal("", this.mode.getNextLineIndent("doc-start", " */", " "));
  156. },
  157. "test: trigger outdent if line is space and new text starts with closing brace" : function() {
  158. assert.ok(this.mode.checkOutdent("start", " ", " }"));
  159. assert.ok(!this.mode.checkOutdent("start", " a ", " }"));
  160. assert.ok(!this.mode.checkOutdent("start", "", "}"));
  161. assert.ok(!this.mode.checkOutdent("start", " ", "a }"));
  162. assert.ok(!this.mode.checkOutdent("start", " }", "}"));
  163. },
  164. "test: auto outdent should indent the line with the same indent as the line with the matching opening brace" : function() {
  165. var session = new EditSession([" function foo() {", " bla", " }"], new JavaScriptMode());
  166. this.mode.autoOutdent("start", session, 2);
  167. assert.equal(" }", session.getLine(2));
  168. },
  169. "test: no auto outdent if no matching brace is found" : function() {
  170. var session = new EditSession([" function foo()", " bla", " }"]);
  171. this.mode.autoOutdent("start", session, 2);
  172. assert.equal(" }", session.getLine(2));
  173. }
  174. };
  175. });
  176. if (typeof module !== "undefined" && module === require.main) {
  177. require("asyncjs").test.testcase(module.exports).exec()
  178. }