comment_test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. namespace = "comment_";
  2. (function() {
  3. function test(name, mode, run, before, after) {
  4. return testCM(name, function(cm) {
  5. run(cm);
  6. eq(cm.getValue(), after);
  7. }, {value: before, mode: mode});
  8. }
  9. var simpleProg = "function foo() {\n return bar;\n}";
  10. var inlineBlock = "foo(/* bar */ true);";
  11. var inlineBlocks = "foo(/* bar */ true, /* baz */ false);";
  12. var multiLineInlineBlock = ["above();", "foo(/* bar */ true);", "below();"];
  13. test("block", "javascript", function(cm) {
  14. cm.blockComment(Pos(0, 3), Pos(3, 0), {blockCommentLead: " *"});
  15. }, simpleProg + "\n", "/* function foo() {\n * return bar;\n * }\n */");
  16. test("blockToggle", "javascript", function(cm) {
  17. cm.blockComment(Pos(0, 3), Pos(2, 0), {blockCommentLead: " *"});
  18. cm.uncomment(Pos(0, 3), Pos(2, 0), {blockCommentLead: " *"});
  19. }, simpleProg, simpleProg);
  20. test("blockToggle2", "javascript", function(cm) {
  21. cm.setCursor({line: 0, ch: 7 /* inside the block comment */});
  22. cm.execCommand("toggleComment");
  23. }, inlineBlock, "foo(bar true);");
  24. // This test should work but currently fails.
  25. // test("blockToggle3", "javascript", function(cm) {
  26. // cm.setCursor({line: 0, ch: 7 /* inside the first block comment */});
  27. // cm.execCommand("toggleComment");
  28. // }, inlineBlocks, "foo(bar true, /* baz */ false);");
  29. test("line", "javascript", function(cm) {
  30. cm.lineComment(Pos(1, 1), Pos(1, 1));
  31. }, simpleProg, "function foo() {\n// return bar;\n}");
  32. test("lineToggle", "javascript", function(cm) {
  33. cm.lineComment(Pos(0, 0), Pos(2, 1));
  34. cm.uncomment(Pos(0, 0), Pos(2, 1));
  35. }, simpleProg, simpleProg);
  36. test("fallbackToBlock", "css", function(cm) {
  37. cm.lineComment(Pos(0, 0), Pos(2, 1));
  38. }, "html {\n border: none;\n}", "/* html {\n border: none;\n} */");
  39. test("fallbackToLine", "ruby", function(cm) {
  40. cm.blockComment(Pos(0, 0), Pos(1));
  41. }, "def blah()\n return hah\n", "# def blah()\n# return hah\n");
  42. test("ignoreExternalBlockComments", "javascript", function(cm) {
  43. cm.execCommand("toggleComment");
  44. }, inlineBlocks, "// " + inlineBlocks);
  45. test("ignoreExternalBlockComments2", "javascript", function(cm) {
  46. cm.setCursor({line: 0, ch: null /* eol */});
  47. cm.execCommand("toggleComment");
  48. }, inlineBlocks, "// " + inlineBlocks);
  49. test("ignoreExternalBlockCommentsMultiLineAbove", "javascript", function(cm) {
  50. cm.setSelection({line: 0, ch: 0}, {line: 1, ch: 1});
  51. cm.execCommand("toggleComment");
  52. }, multiLineInlineBlock.join("\n"), ["// " + multiLineInlineBlock[0],
  53. "// " + multiLineInlineBlock[1],
  54. multiLineInlineBlock[2]].join("\n"));
  55. test("ignoreExternalBlockCommentsMultiLineBelow", "javascript", function(cm) {
  56. cm.setSelection({line: 1, ch: 13 /* after end of block comment */}, {line: 2, ch: 1});
  57. cm.execCommand("toggleComment");
  58. }, multiLineInlineBlock.join("\n"), [multiLineInlineBlock[0],
  59. "// " + multiLineInlineBlock[1],
  60. "// " + multiLineInlineBlock[2]].join("\n"));
  61. test("commentRange", "javascript", function(cm) {
  62. cm.blockComment(Pos(1, 2), Pos(1, 13), {fullLines: false});
  63. }, simpleProg, "function foo() {\n /*return bar;*/\n}");
  64. test("indented", "javascript", function(cm) {
  65. cm.lineComment(Pos(1, 0), Pos(2), {indent: true});
  66. }, simpleProg, "function foo() {\n// return bar;\n// }");
  67. test("singleEmptyLine", "javascript", function(cm) {
  68. cm.setCursor(1);
  69. cm.execCommand("toggleComment");
  70. }, "a;\n\nb;", "a;\n// \nb;");
  71. test("dontMessWithStrings", "javascript", function(cm) {
  72. cm.execCommand("toggleComment");
  73. }, "console.log(\"/*string*/\");", "// console.log(\"/*string*/\");");
  74. test("dontMessWithStrings2", "javascript", function(cm) {
  75. cm.execCommand("toggleComment");
  76. }, "console.log(\"// string\");", "// console.log(\"// string\");");
  77. test("dontMessWithStrings3", "javascript", function(cm) {
  78. cm.execCommand("toggleComment");
  79. }, "// console.log(\"// string\");", "console.log(\"// string\");");
  80. test("includeLastLine", "javascript", function(cm) {
  81. cm.execCommand("selectAll")
  82. cm.execCommand("toggleComment")
  83. }, "// foo\n// bar\nbaz", "// // foo\n// // bar\n// baz")
  84. test("uncommentWithTrailingBlockEnd", "xml", function(cm) {
  85. cm.execCommand("toggleComment")
  86. }, "<!-- foo --> -->", "foo -->")
  87. test("dontCommentInComment", "xml", function(cm) {
  88. cm.setCursor(1, 0)
  89. cm.execCommand("toggleComment")
  90. }, "<!-- foo\nbar -->", "<!-- foo\nbar -->")
  91. })();