mode_test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * Helper to test CodeMirror highlighting modes. It pretty prints output of the
  3. * highlighter and can check against expected styles.
  4. *
  5. * Mode tests are registered by calling test.mode(testName, mode,
  6. * tokens), where mode is a mode object as returned by
  7. * CodeMirror.getMode, and tokens is an array of lines that make up
  8. * the test.
  9. *
  10. * These lines are strings, in which styled stretches of code are
  11. * enclosed in brackets `[]`, and prefixed by their style. For
  12. * example, `[keyword if]`. Brackets in the code itself must be
  13. * duplicated to prevent them from being interpreted as token
  14. * boundaries. For example `a[[i]]` for `a[i]`. If a token has
  15. * multiple styles, the styles must be separated by ampersands, for
  16. * example `[tag&error </hmtl>]`.
  17. *
  18. * See the test.js files in the css, markdown, gfm, and stex mode
  19. * directories for examples.
  20. */
  21. (function() {
  22. function findSingle(str, pos, ch) {
  23. for (;;) {
  24. var found = str.indexOf(ch, pos);
  25. if (found == -1) return null;
  26. if (str.charAt(found + 1) != ch) return found;
  27. pos = found + 2;
  28. }
  29. }
  30. var styleName = /[\w&-_]+/g;
  31. function parseTokens(strs) {
  32. var tokens = [], plain = "";
  33. for (var i = 0; i < strs.length; ++i) {
  34. if (i) plain += "\n";
  35. var str = strs[i], pos = 0;
  36. while (pos < str.length) {
  37. var style = null, text;
  38. if (str.charAt(pos) == "[" && str.charAt(pos+1) != "[") {
  39. styleName.lastIndex = pos + 1;
  40. var m = styleName.exec(str);
  41. style = m[0].replace(/&/g, " ");
  42. var textStart = pos + style.length + 2;
  43. var end = findSingle(str, textStart, "]");
  44. if (end == null) throw new Error("Unterminated token at " + pos + " in '" + str + "'" + style);
  45. text = str.slice(textStart, end);
  46. pos = end + 1;
  47. } else {
  48. var end = findSingle(str, pos, "[");
  49. if (end == null) end = str.length;
  50. text = str.slice(pos, end);
  51. pos = end;
  52. }
  53. text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);});
  54. tokens.push(style, text);
  55. plain += text;
  56. }
  57. }
  58. return {tokens: tokens, plain: plain};
  59. }
  60. test.mode = function(name, mode, tokens, modeName) {
  61. var data = parseTokens(tokens);
  62. return test((modeName || mode.name) + "_" + name, function() {
  63. return compare(data.plain, data.tokens, mode);
  64. });
  65. };
  66. function compare(text, expected, mode) {
  67. var expectedOutput = [];
  68. for (var i = 0; i < expected.length; i += 2) {
  69. var sty = expected[i];
  70. if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' ');
  71. expectedOutput.push(sty, expected[i + 1]);
  72. }
  73. var observedOutput = highlight(text, mode);
  74. var pass, passStyle = "";
  75. pass = highlightOutputsEqual(expectedOutput, observedOutput);
  76. passStyle = pass ? 'mt-pass' : 'mt-fail';
  77. var s = '';
  78. if (pass) {
  79. s += '<div class="mt-test ' + passStyle + '">';
  80. s += '<pre>' + text + '</pre>';
  81. s += '<div class="cm-s-default">';
  82. s += prettyPrintOutputTable(observedOutput);
  83. s += '</div>';
  84. s += '</div>';
  85. return s;
  86. } else {
  87. s += '<div class="mt-test ' + passStyle + '">';
  88. s += '<pre>' + text + '</pre>';
  89. s += '<div class="cm-s-default">';
  90. s += 'expected:';
  91. s += prettyPrintOutputTable(expectedOutput);
  92. s += 'observed:';
  93. s += prettyPrintOutputTable(observedOutput);
  94. s += '</div>';
  95. s += '</div>';
  96. throw s;
  97. }
  98. }
  99. /**
  100. * Emulation of CodeMirror's internal highlight routine for testing. Multi-line
  101. * input is supported.
  102. *
  103. * @param string to highlight
  104. *
  105. * @param mode the mode that will do the actual highlighting
  106. *
  107. * @return array of [style, token] pairs
  108. */
  109. function highlight(string, mode) {
  110. var state = mode.startState()
  111. var lines = string.replace(/\r\n/g,'\n').split('\n');
  112. var st = [], pos = 0;
  113. for (var i = 0; i < lines.length; ++i) {
  114. var line = lines[i], newLine = true;
  115. var stream = new CodeMirror.StringStream(line);
  116. if (line == "" && mode.blankLine) mode.blankLine(state);
  117. /* Start copied code from CodeMirror.highlight */
  118. while (!stream.eol()) {
  119. var style = mode.token(stream, state), substr = stream.current();
  120. if (style && style.indexOf(" ") > -1) style = style.split(' ').sort().join(' ');
  121. stream.start = stream.pos;
  122. if (pos && st[pos-2] == style && !newLine) {
  123. st[pos-1] += substr;
  124. } else if (substr) {
  125. st[pos++] = style; st[pos++] = substr;
  126. }
  127. // Give up when line is ridiculously long
  128. if (stream.pos > 5000) {
  129. st[pos++] = null; st[pos++] = this.text.slice(stream.pos);
  130. break;
  131. }
  132. newLine = false;
  133. }
  134. }
  135. return st;
  136. }
  137. /**
  138. * Compare two arrays of output from highlight.
  139. *
  140. * @param o1 array of [style, token] pairs
  141. *
  142. * @param o2 array of [style, token] pairs
  143. *
  144. * @return boolean; true iff outputs equal
  145. */
  146. function highlightOutputsEqual(o1, o2) {
  147. if (o1.length != o2.length) return false;
  148. for (var i = 0; i < o1.length; ++i)
  149. if (o1[i] != o2[i]) return false;
  150. return true;
  151. }
  152. /**
  153. * Print tokens and corresponding styles in a table. Spaces in the token are
  154. * replaced with 'interpunct' dots (&middot;).
  155. *
  156. * @param output array of [style, token] pairs
  157. *
  158. * @return html string
  159. */
  160. function prettyPrintOutputTable(output) {
  161. var s = '<table class="mt-output">';
  162. s += '<tr>';
  163. for (var i = 0; i < output.length; i += 2) {
  164. var style = output[i], val = output[i+1];
  165. s +=
  166. '<td class="mt-token">' +
  167. '<span class="cm-' + String(style).replace(/ +/g, " cm-") + '">' +
  168. val.replace(/ /g,'\xb7') +
  169. '</span>' +
  170. '</td>';
  171. }
  172. s += '</tr><tr>';
  173. for (var i = 0; i < output.length; i += 2) {
  174. s += '<td class="mt-style"><span>' + output[i] + '</span></td>';
  175. }
  176. s += '</table>';
  177. return s;
  178. }
  179. })();