2 * Helper to test CodeMirror highlighting modes. It pretty prints output of the
3 * highlighter and can check against expected styles.
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
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>]`.
18 * See the test.js files in the css, markdown, gfm, and stex mode
19 * directories for examples.
22 function findSingle(str, pos, ch) {
24 var found = str.indexOf(ch, pos);
25 if (found == -1) return null;
26 if (str.charAt(found + 1) != ch) return found;
31 var styleName = /[\w&-_]+/g;
32 function parseTokens(strs) {
33 var tokens = [], plain = "";
34 for (var i = 0; i < strs.length; ++i) {
36 var str = strs[i], pos = 0;
37 while (pos < str.length) {
38 var style = null, text;
39 if (str.charAt(pos) == "[" && str.charAt(pos+1) != "[") {
40 styleName.lastIndex = pos + 1;
41 var m = styleName.exec(str);
42 style = m[0].replace(/&/g, " ");
43 var textStart = pos + style.length + 2;
44 var end = findSingle(str, textStart, "]");
45 if (end == null) throw new Error("Unterminated token at " + pos + " in '" + str + "'" + style);
46 text = str.slice(textStart, end);
49 var end = findSingle(str, pos, "[");
50 if (end == null) end = str.length;
51 text = str.slice(pos, end);
54 text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);});
55 tokens.push(style, text);
59 return {tokens: tokens, plain: plain};
62 test.mode = function(name, mode, tokens, modeName) {
63 var data = parseTokens(tokens);
64 return test((modeName || mode.name) + "_" + name, function() {
65 return compare(data.plain, data.tokens, mode);
69 function compare(text, expected, mode) {
71 var expectedOutput = [];
72 for (var i = 0; i < expected.length; i += 2) {
73 var sty = expected[i];
74 if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' ');
75 expectedOutput.push(sty, expected[i + 1]);
78 var observedOutput = highlight(text, mode);
80 var pass, passStyle = "";
81 pass = highlightOutputsEqual(expectedOutput, observedOutput);
82 passStyle = pass ? 'mt-pass' : 'mt-fail';
86 s += '<div class="mt-test ' + passStyle + '">';
87 s += '<pre>' + text.replace('&', '&').replace('<', '<') + '</pre>';
88 s += '<div class="cm-s-default">';
89 s += prettyPrintOutputTable(observedOutput);
94 s += '<div class="mt-test ' + passStyle + '">';
95 s += '<pre>' + text.replace('&', '&').replace('<', '<') + '</pre>';
96 s += '<div class="cm-s-default">';
98 s += prettyPrintOutputTable(expectedOutput);
100 s += prettyPrintOutputTable(observedOutput);
108 * Emulation of CodeMirror's internal highlight routine for testing. Multi-line
109 * input is supported.
111 * @param string to highlight
113 * @param mode the mode that will do the actual highlighting
115 * @return array of [style, token] pairs
117 function highlight(string, mode) {
118 var state = mode.startState()
120 var lines = string.replace(/\r\n/g,'\n').split('\n');
121 var st = [], pos = 0;
122 for (var i = 0; i < lines.length; ++i) {
123 var line = lines[i], newLine = true;
124 var stream = new CodeMirror.StringStream(line);
125 if (line == "" && mode.blankLine) mode.blankLine(state);
126 /* Start copied code from CodeMirror.highlight */
127 while (!stream.eol()) {
128 var style = mode.token(stream, state), substr = stream.current();
129 if (style && style.indexOf(" ") > -1) style = style.split(' ').sort().join(' ');
131 stream.start = stream.pos;
132 if (pos && st[pos-2] == style && !newLine) {
135 st[pos++] = style; st[pos++] = substr;
137 // Give up when line is ridiculously long
138 if (stream.pos > 5000) {
139 st[pos++] = null; st[pos++] = this.text.slice(stream.pos);
150 * Compare two arrays of output from highlight.
152 * @param o1 array of [style, token] pairs
154 * @param o2 array of [style, token] pairs
156 * @return boolean; true iff outputs equal
158 function highlightOutputsEqual(o1, o2) {
159 if (o1.length != o2.length) return false;
160 for (var i = 0; i < o1.length; ++i)
161 if (o1[i] != o2[i]) return false;
166 * Print tokens and corresponding styles in a table. Spaces in the token are
167 * replaced with 'interpunct' dots (·).
169 * @param output array of [style, token] pairs
171 * @return html string
173 function prettyPrintOutputTable(output) {
174 var s = '<table class="mt-output">';
176 for (var i = 0; i < output.length; i += 2) {
177 var style = output[i], val = output[i+1];
179 '<td class="mt-token">' +
180 '<span class="cm-' + String(style).replace(/ +/g, " cm-") + '">' +
181 val.replace(/ /g,'\xb7').replace('&', '&').replace('<', '<') +
186 for (var i = 0; i < output.length; i += 2) {
187 s += '<td class="mt-style"><span>' + (output[i] || null) + '</span></td>';