text_test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. require("../test/mockdom");
  33. }
  34. define(function(require, exports, module) {
  35. "use strict";
  36. var assert = require("../test/assertions");
  37. var EditSession = require("../edit_session").EditSession;
  38. var TextLayer = require("./text").Text;
  39. var JavaScriptMode = require("../mode/javascript").Mode;
  40. module.exports = {
  41. setUp: function(next) {
  42. this.session = new EditSession("");
  43. this.session.setMode(new JavaScriptMode());
  44. this.textLayer = new TextLayer(document.createElement("div"));
  45. this.textLayer.setSession(this.session);
  46. this.textLayer.config = {
  47. characterWidth: 10,
  48. lineHeight: 20
  49. };
  50. next()
  51. },
  52. "test: render line with hard tabs should render the same as lines with soft tabs" : function() {
  53. this.session.setValue("a\ta\ta\t\na a a \n");
  54. this.textLayer.$computeTabString();
  55. // row with hard tabs
  56. var stringBuilder = [];
  57. this.textLayer.$renderLine(stringBuilder, 0);
  58. // row with soft tabs
  59. var stringBuilder2 = [];
  60. this.textLayer.$renderLine(stringBuilder2, 1);
  61. assert.equal(stringBuilder.join(""), stringBuilder2.join(""));
  62. },
  63. "test rendering width of ideographic space (U+3000)" : function() {
  64. this.session.setValue("\u3000");
  65. var stringBuilder = [];
  66. this.textLayer.$renderLine(stringBuilder, 0, true);
  67. assert.equal(stringBuilder.join(""), "<span class='ace_cjk' style='width:20px'></span>");
  68. this.textLayer.setShowInvisibles(true);
  69. var stringBuilder = [];
  70. this.textLayer.$renderLine(stringBuilder, 0, true);
  71. assert.equal(
  72. stringBuilder.join(""),
  73. "<span class='ace_cjk ace_invisible ace_invisible_space' style='width:20px'>" + this.textLayer.SPACE_CHAR + "</span>"
  74. + "<span class='ace_invisible ace_invisible_eol'>\xB6</span>"
  75. );
  76. },
  77. "test rendering of indent guides" : function() {
  78. var textLayer = this.textLayer
  79. var EOL = "<span class='ace_invisible ace_invisible_eol'>" + textLayer.EOL_CHAR + "</span>";
  80. var SPACE = function(i) {return Array(i+1).join(" ")}
  81. var DOT = function(i) {return Array(i+1).join(textLayer.SPACE_CHAR)}
  82. var TAB = function(i) {return Array(i+1).join(textLayer.TAB_CHAR)}
  83. function testRender(results) {
  84. for (var i = results.length; i--; ) {
  85. var stringBuilder = [];
  86. textLayer.$renderLine(stringBuilder, i, true);
  87. assert.equal(stringBuilder.join(""), results[i]);
  88. }
  89. }
  90. this.session.setValue(" \n\t\tf\n ");
  91. testRender([
  92. "<span class='ace_indent-guide'>" + SPACE(4) + "</span>" + SPACE(2),
  93. "<span class='ace_indent-guide'>" + SPACE(4) + "</span>" + SPACE(4) + "<span class='ace_identifier'>f</span>",
  94. SPACE(3)
  95. ]);
  96. this.textLayer.setShowInvisibles(true);
  97. testRender([
  98. "<span class='ace_indent-guide ace_invisible ace_invisible_space'>" + DOT(4) + "</span><span class='ace_invisible ace_invisible_space'>" + DOT(2) + "</span>" + EOL,
  99. "<span class='ace_indent-guide ace_invisible ace_invisible_tab'>" + TAB(4) + "</span><span class='ace_invisible ace_invisible_tab'>" + TAB(4) + "</span><span class='ace_identifier'>f</span>" + EOL,
  100. ]);
  101. this.textLayer.setDisplayIndentGuides(false);
  102. testRender([
  103. "<span class='ace_invisible ace_invisible_space'>" + DOT(6) + "</span>" + EOL,
  104. "<span class='ace_invisible ace_invisible_tab'>" + TAB(4) + "</span><span class='ace_invisible ace_invisible_tab'>" + TAB(4) + "</span><span class='ace_identifier'>f</span>" + EOL
  105. ]);
  106. }
  107. };
  108. });
  109. if (typeof module !== "undefined" && module === require.main) {
  110. require("asyncjs").test.testcase(module.exports).exec()
  111. }