Fix: merge conflict
[myslice.git] / third-party / codemirror-3.15 / test / mode_test.js
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
31   var styleName = /[\w&-_]+/g;
32   function parseTokens(strs) {
33     var tokens = [], plain = "";
34     for (var i = 0; i < strs.length; ++i) {
35       if (i) plain += "\n";
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);
47           pos = end + 1;
48         } else {
49           var end = findSingle(str, pos, "[");
50           if (end == null) end = str.length;
51           text = str.slice(pos, end);
52           pos = end;
53         }
54         text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);});
55         tokens.push(style, text);
56         plain += text;
57       }
58     }
59     return {tokens: tokens, plain: plain};
60   }
61
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);
66     });
67   };
68
69   function compare(text, expected, mode) {
70
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]);
76     }
77
78     var observedOutput = highlight(text, mode);
79
80     var pass, passStyle = "";
81     pass = highlightOutputsEqual(expectedOutput, observedOutput);
82     passStyle = pass ? 'mt-pass' : 'mt-fail';
83
84     var s = '';
85     if (pass) {
86       s += '<div class="mt-test ' + passStyle + '">';
87       s +=   '<pre>' + text.replace('&', '&amp;').replace('<', '&lt;') + '</pre>';
88       s +=   '<div class="cm-s-default">';
89       s +=   prettyPrintOutputTable(observedOutput);
90       s +=   '</div>';
91       s += '</div>';
92       return s;
93     } else {
94       s += '<div class="mt-test ' + passStyle + '">';
95       s +=   '<pre>' + text.replace('&', '&amp;').replace('<', '&lt;') + '</pre>';
96       s +=   '<div class="cm-s-default">';
97       s += 'expected:';
98       s +=   prettyPrintOutputTable(expectedOutput);
99       s += 'observed:';
100       s +=   prettyPrintOutputTable(observedOutput);
101       s +=   '</div>';
102       s += '</div>';
103       throw s;
104     }
105   }
106
107   /**
108    * Emulation of CodeMirror's internal highlight routine for testing. Multi-line
109    * input is supported.
110    *
111    * @param string to highlight
112    *
113    * @param mode the mode that will do the actual highlighting
114    *
115    * @return array of [style, token] pairs
116    */
117   function highlight(string, mode) {
118     var state = mode.startState()
119
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(' ');
130
131         stream.start = stream.pos;
132         if (pos && st[pos-2] == style && !newLine) {
133           st[pos-1] += substr;
134         } else if (substr) {
135           st[pos++] = style; st[pos++] = substr;
136         }
137         // Give up when line is ridiculously long
138         if (stream.pos > 5000) {
139           st[pos++] = null; st[pos++] = this.text.slice(stream.pos);
140           break;
141         }
142         newLine = false;
143       }
144     }
145
146     return st;
147   }
148
149   /**
150    * Compare two arrays of output from highlight.
151    *
152    * @param o1 array of [style, token] pairs
153    *
154    * @param o2 array of [style, token] pairs
155    *
156    * @return boolean; true iff outputs equal
157    */
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;
162     return true;
163   }
164
165   /**
166    * Print tokens and corresponding styles in a table. Spaces in the token are
167    * replaced with 'interpunct' dots (&middot;).
168    *
169    * @param output array of [style, token] pairs
170    *
171    * @return html string
172    */
173   function prettyPrintOutputTable(output) {
174     var s = '<table class="mt-output">';
175     s += '<tr>';
176     for (var i = 0; i < output.length; i += 2) {
177       var style = output[i], val = output[i+1];
178       s +=
179       '<td class="mt-token">' +
180         '<span class="cm-' + String(style).replace(/ +/g, " cm-") + '">' +
181         val.replace(/ /g,'\xb7').replace('&', '&amp;').replace('<', '&lt;') +
182         '</span>' +
183         '</td>';
184     }
185     s += '</tr><tr>';
186     for (var i = 0; i < output.length; i += 2) {
187       s += '<td class="mt-style"><span>' + (output[i] || null) + '</span></td>';
188     }
189     s += '</table>';
190     return s;
191   }
192 })();