auto-render-spec.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* global beforeEach: false */
  2. /* global expect: false */
  3. /* global it: false */
  4. /* global describe: false */
  5. import splitAtDelimiters from "../splitAtDelimiters";
  6. beforeEach(function() {
  7. expect.extend({
  8. toSplitInto: function(actual, left, right, result) {
  9. const message = {
  10. pass: true,
  11. message: "'" + actual + "' split correctly",
  12. };
  13. const startData = [{type: "text", data: actual}];
  14. const split =
  15. splitAtDelimiters(startData, left, right, false);
  16. if (split.length !== result.length) {
  17. message.pass = false;
  18. message.message = "Different number of splits: " +
  19. split.length + " vs. " + result.length + " (" +
  20. JSON.stringify(split) + " vs. " +
  21. JSON.stringify(result) + ")";
  22. return message;
  23. }
  24. for (let i = 0; i < split.length; i++) {
  25. const real = split[i];
  26. const correct = result[i];
  27. let good = true;
  28. let diff;
  29. if (real.type !== correct.type) {
  30. good = false;
  31. diff = "type";
  32. } else if (real.data !== correct.data) {
  33. good = false;
  34. diff = "data";
  35. } else if (real.display !== correct.display) {
  36. good = false;
  37. diff = "display";
  38. }
  39. if (!good) {
  40. message.pass = false;
  41. message.message = "Difference at split " +
  42. (i + 1) + ": " + JSON.stringify(real) +
  43. " vs. " + JSON.stringify(correct) +
  44. " (" + diff + " differs)";
  45. break;
  46. }
  47. }
  48. return message;
  49. },
  50. });
  51. });
  52. describe("A delimiter splitter", function() {
  53. it("doesn't split when there are no delimiters", function() {
  54. expect("hello").toSplitInto("(", ")", [{type: "text", data: "hello"}]);
  55. });
  56. it("doesn't create a math node with only one left delimiter", function() {
  57. expect("hello ( world").toSplitInto(
  58. "(", ")",
  59. [
  60. {type: "text", data: "hello "},
  61. {type: "text", data: "( world"},
  62. ]);
  63. });
  64. it("doesn't split when there's only a right delimiter", function() {
  65. expect("hello ) world").toSplitInto(
  66. "(", ")",
  67. [
  68. {type: "text", data: "hello ) world"},
  69. ]);
  70. });
  71. it("splits when there are both delimiters", function() {
  72. expect("hello ( world ) boo").toSplitInto(
  73. "(", ")",
  74. [
  75. {type: "text", data: "hello "},
  76. {type: "math", data: " world ",
  77. rawData: "( world )", display: false},
  78. {type: "text", data: " boo"},
  79. ]);
  80. });
  81. it("splits on multi-character delimiters", function() {
  82. expect("hello [[ world ]] boo").toSplitInto(
  83. "[[", "]]",
  84. [
  85. {type: "text", data: "hello "},
  86. {type: "math", data: " world ",
  87. rawData: "[[ world ]]", display: false},
  88. {type: "text", data: " boo"},
  89. ]);
  90. });
  91. it("splits mutliple times", function() {
  92. expect("hello ( world ) boo ( more ) stuff").toSplitInto(
  93. "(", ")",
  94. [
  95. {type: "text", data: "hello "},
  96. {type: "math", data: " world ",
  97. rawData: "( world )", display: false},
  98. {type: "text", data: " boo "},
  99. {type: "math", data: " more ",
  100. rawData: "( more )", display: false},
  101. {type: "text", data: " stuff"},
  102. ]);
  103. });
  104. it("leaves the ending when there's only a left delimiter", function() {
  105. expect("hello ( world ) boo ( left").toSplitInto(
  106. "(", ")",
  107. [
  108. {type: "text", data: "hello "},
  109. {type: "math", data: " world ",
  110. rawData: "( world )", display: false},
  111. {type: "text", data: " boo "},
  112. {type: "text", data: "( left"},
  113. ]);
  114. });
  115. it("doesn't split when close delimiters are in {}s", function() {
  116. expect("hello ( world { ) } ) boo").toSplitInto(
  117. "(", ")",
  118. [
  119. {type: "text", data: "hello "},
  120. {type: "math", data: " world { ) } ",
  121. rawData: "( world { ) } )", display: false},
  122. {type: "text", data: " boo"},
  123. ]);
  124. expect("hello ( world { { } ) } ) boo").toSplitInto(
  125. "(", ")",
  126. [
  127. {type: "text", data: "hello "},
  128. {type: "math", data: " world { { } ) } ",
  129. rawData: "( world { { } ) } )", display: false},
  130. {type: "text", data: " boo"},
  131. ]);
  132. });
  133. it("doesn't split at escaped delimiters", function() {
  134. expect("hello ( world \\) ) boo").toSplitInto(
  135. "(", ")",
  136. [
  137. {type: "text", data: "hello "},
  138. {type: "math", data: " world \\) ",
  139. rawData: "( world \\) )", display: false},
  140. {type: "text", data: " boo"},
  141. ]);
  142. /* TODO(emily): make this work maybe?
  143. expect("hello \\( ( world ) boo").toSplitInto(
  144. "(", ")",
  145. [
  146. {type: "text", data: "hello \\( "},
  147. {type: "math", data: " world ",
  148. rawData: "( world )", display: false},
  149. {type: "text", data: " boo"},
  150. ]);
  151. */
  152. });
  153. it("splits when the right and left delimiters are the same", function() {
  154. expect("hello $ world $ boo").toSplitInto(
  155. "$", "$",
  156. [
  157. {type: "text", data: "hello "},
  158. {type: "math", data: " world ",
  159. rawData: "$ world $", display: false},
  160. {type: "text", data: " boo"},
  161. ]);
  162. });
  163. it("remembers which delimiters are display-mode", function() {
  164. const startData = [{type: "text", data: "hello ( world ) boo"}];
  165. expect(splitAtDelimiters(startData, "(", ")", true)).toEqual(
  166. [
  167. {type: "text", data: "hello "},
  168. {type: "math", data: " world ",
  169. rawData: "( world )", display: true},
  170. {type: "text", data: " boo"},
  171. ]);
  172. });
  173. it("works with more than one start datum", function() {
  174. const startData = [
  175. {type: "text", data: "hello ( world ) boo"},
  176. {type: "math", data: "math", rawData: "(math)", display: true},
  177. {type: "text", data: "hello ( world ) boo"},
  178. ];
  179. expect(splitAtDelimiters(startData, "(", ")", false)).toEqual(
  180. [
  181. {type: "text", data: "hello "},
  182. {type: "math", data: " world ",
  183. rawData: "( world )", display: false},
  184. {type: "text", data: " boo"},
  185. {type: "math", data: "math", rawData: "(math)", display: true},
  186. {type: "text", data: "hello "},
  187. {type: "math", data: " world ",
  188. rawData: "( world )", display: false},
  189. {type: "text", data: " boo"},
  190. ]);
  191. });
  192. it("doesn't do splitting inside of math nodes", function() {
  193. const startData = [
  194. {type: "text", data: "hello ( world ) boo"},
  195. {type: "math", data: "hello ( world ) boo",
  196. rawData: "(hello ( world ) boo)", display: true},
  197. ];
  198. expect(splitAtDelimiters(startData, "(", ")", false)).toEqual(
  199. [
  200. {type: "text", data: "hello "},
  201. {type: "math", data: " world ",
  202. rawData: "( world )", display: false},
  203. {type: "text", data: " boo"},
  204. {type: "math", data: "hello ( world ) boo",
  205. rawData: "(hello ( world ) boo)", display: true},
  206. ]);
  207. });
  208. });