No Description

TextHelperSpec.ts 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {assert} from "chai";
  2. import {getSurroundingWord, insertText} from "../src/util/MarkdownUtil";
  3. describe("TextHelperSpec.js", () => {
  4. describe("insertText", () => {
  5. it("should work mid-string", () => {
  6. const text = insertText("bob school", "went to ", 4);
  7. assert.deepEqual(text, {newText: "bob went to school", insertionLength: 8});
  8. });
  9. it("should work in the start", () => {
  10. const text = insertText("went to school", "bob ", 0);
  11. assert.deepEqual(text, {newText: "bob went to school", insertionLength: 4});
  12. });
  13. it("should work in the end", () => {
  14. const text = insertText("bob went to", " school", 11);
  15. assert.deepEqual(text, {newText: "bob went to school", insertionLength: 7});
  16. });
  17. it("should work with a position greater than the max", () => {
  18. const text = insertText("bob went to", " school", 20);
  19. assert.deepEqual(text, {newText: "bob went to school", insertionLength: 7});
  20. });
  21. });
  22. });
  23. describe("getSurroundingWord", () => {
  24. // tests without line breaks
  25. it("Basic usage", () => {
  26. const result = getSurroundingWord("bob went to school", 5);
  27. assert.strictEqual(result.word, "went");
  28. assert.strictEqual(result.position.start, 4);
  29. assert.strictEqual(result.position.end, 8);
  30. });
  31. it("Word edge to the right", () => {
  32. const result = getSurroundingWord("bob went to school", 8);
  33. assert.strictEqual(result.word, "went");
  34. assert.strictEqual(result.position.start, 4);
  35. assert.strictEqual(result.position.end, 8);
  36. });
  37. it("Word edge to the left", () => {
  38. const result = getSurroundingWord("bob went to school", 4);
  39. assert.strictEqual(result.word, "went");
  40. assert.strictEqual(result.position.start, 4);
  41. assert.strictEqual(result.position.end, 8);
  42. });
  43. it("text beginning", () => {
  44. const result = getSurroundingWord("bob went to school", 0);
  45. assert.strictEqual(result.word, "bob");
  46. assert.strictEqual(result.position.start, 0);
  47. assert.strictEqual(result.position.end, 3);
  48. });
  49. it("text ending", () => {
  50. const result = getSurroundingWord("bob went to school", 18);
  51. assert.strictEqual(result.word, "school");
  52. assert.strictEqual(result.position.start, 12);
  53. assert.strictEqual(result.position.end, 18);
  54. });
  55. // tests with line breaks
  56. it("Basic usage with line breaks", () => {
  57. const result = getSurroundingWord("bob\nwent\nto school", 5);
  58. assert.strictEqual(result.word, "went");
  59. assert.strictEqual(result.position.start, 4);
  60. assert.strictEqual(result.position.end, 8);
  61. });
  62. it("text ending with line break", () => {
  63. const result = getSurroundingWord("bob went to school\n", 18);
  64. assert.strictEqual(result.word, "school");
  65. assert.strictEqual(result.position.start, 12);
  66. assert.strictEqual(result.position.end, 18);
  67. });
  68. it("text beginning with line break", () => {
  69. const result = getSurroundingWord("\nbob went to school", 1);
  70. assert.strictEqual(result.word, "bob");
  71. assert.strictEqual(result.position.start, 1);
  72. assert.strictEqual(result.position.end, 4);
  73. });
  74. // edge cases
  75. it("within spaces", () => {
  76. const result = getSurroundingWord(" ", 1);
  77. assert.strictEqual(result.word, "");
  78. assert.strictEqual(result.position.start, 1);
  79. assert.strictEqual(result.position.end, 1);
  80. });
  81. it("within spaces 2", () => {
  82. const result = getSurroundingWord(" ", 2);
  83. assert.strictEqual(result.word, "");
  84. assert.strictEqual(result.position.start, 2);
  85. assert.strictEqual(result.position.end, 2);
  86. });
  87. it("within line-breaks", () => {
  88. const result = getSurroundingWord("\n\n\n", 1);
  89. assert.strictEqual(result.word, "");
  90. assert.strictEqual(result.position.start, 1);
  91. assert.strictEqual(result.position.end, 1);
  92. });
  93. });