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.

118 lines
4.7 KiB

7 months ago
  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("emptyIndentedLine", "javascript", function(cm) {
  68. cm.lineComment(Pos(1, 2), Pos(1, 2), {indent: true});
  69. }, "function foo() {\n \n}", "function foo() {\n // \n}");
  70. test("singleEmptyLine", "javascript", function(cm) {
  71. cm.setCursor(1);
  72. cm.execCommand("toggleComment");
  73. }, "a;\n\nb;", "a;\n// \nb;");
  74. test("dontMessWithStrings", "javascript", function(cm) {
  75. cm.execCommand("toggleComment");
  76. }, "console.log(\"/*string*/\");", "// console.log(\"/*string*/\");");
  77. test("dontMessWithStrings2", "javascript", function(cm) {
  78. cm.execCommand("toggleComment");
  79. }, "console.log(\"// string\");", "// console.log(\"// string\");");
  80. test("dontMessWithStrings3", "javascript", function(cm) {
  81. cm.execCommand("toggleComment");
  82. }, "// console.log(\"// string\");", "console.log(\"// string\");");
  83. test("includeLastLine", "javascript", function(cm) {
  84. cm.execCommand("selectAll")
  85. cm.execCommand("toggleComment")
  86. }, "// foo\n// bar\nbaz", "// // foo\n// // bar\n// baz")
  87. test("uncommentWithTrailingBlockEnd", "xml", function(cm) {
  88. cm.execCommand("toggleComment")
  89. }, "<!-- foo --> -->", "foo -->")
  90. test("dontCommentInComment", "xml", function(cm) {
  91. cm.setCursor(1, 0)
  92. cm.execCommand("toggleComment")
  93. }, "<!-- foo\nbar -->", "<!-- foo\nbar -->")
  94. })();