X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;ds=sidebyside;f=to-be-integrated%2Fthird-party%2Fcodemirror-3.15%2Ftest%2Fmode_test.js;fp=to-be-integrated%2Fthird-party%2Fcodemirror-3.15%2Ftest%2Fmode_test.js;h=1439cab32cd6a981a9974074a40bd001c7c4ccbf;hb=31540dd504798e0aca69e10d8144fbedc5b16af8;hp=0000000000000000000000000000000000000000;hpb=937653fd70bbf7d95bcf870e7f2b46b4a8fec486;p=myslice.git diff --git a/to-be-integrated/third-party/codemirror-3.15/test/mode_test.js b/to-be-integrated/third-party/codemirror-3.15/test/mode_test.js new file mode 100644 index 00000000..1439cab3 --- /dev/null +++ b/to-be-integrated/third-party/codemirror-3.15/test/mode_test.js @@ -0,0 +1,192 @@ +/** + * Helper to test CodeMirror highlighting modes. It pretty prints output of the + * highlighter and can check against expected styles. + * + * Mode tests are registered by calling test.mode(testName, mode, + * tokens), where mode is a mode object as returned by + * CodeMirror.getMode, and tokens is an array of lines that make up + * the test. + * + * These lines are strings, in which styled stretches of code are + * enclosed in brackets `[]`, and prefixed by their style. For + * example, `[keyword if]`. Brackets in the code itself must be + * duplicated to prevent them from being interpreted as token + * boundaries. For example `a[[i]]` for `a[i]`. If a token has + * multiple styles, the styles must be separated by ampersands, for + * example `[tag&error ]`. + * + * See the test.js files in the css, markdown, gfm, and stex mode + * directories for examples. + */ +(function() { + function findSingle(str, pos, ch) { + for (;;) { + var found = str.indexOf(ch, pos); + if (found == -1) return null; + if (str.charAt(found + 1) != ch) return found; + pos = found + 2; + } + } + + var styleName = /[\w&-_]+/g; + function parseTokens(strs) { + var tokens = [], plain = ""; + for (var i = 0; i < strs.length; ++i) { + if (i) plain += "\n"; + var str = strs[i], pos = 0; + while (pos < str.length) { + var style = null, text; + if (str.charAt(pos) == "[" && str.charAt(pos+1) != "[") { + styleName.lastIndex = pos + 1; + var m = styleName.exec(str); + style = m[0].replace(/&/g, " "); + var textStart = pos + style.length + 2; + var end = findSingle(str, textStart, "]"); + if (end == null) throw new Error("Unterminated token at " + pos + " in '" + str + "'" + style); + text = str.slice(textStart, end); + pos = end + 1; + } else { + var end = findSingle(str, pos, "["); + if (end == null) end = str.length; + text = str.slice(pos, end); + pos = end; + } + text = text.replace(/\[\[|\]\]/g, function(s) {return s.charAt(0);}); + tokens.push(style, text); + plain += text; + } + } + return {tokens: tokens, plain: plain}; + } + + test.mode = function(name, mode, tokens, modeName) { + var data = parseTokens(tokens); + return test((modeName || mode.name) + "_" + name, function() { + return compare(data.plain, data.tokens, mode); + }); + }; + + function compare(text, expected, mode) { + + var expectedOutput = []; + for (var i = 0; i < expected.length; i += 2) { + var sty = expected[i]; + if (sty && sty.indexOf(" ")) sty = sty.split(' ').sort().join(' '); + expectedOutput.push(sty, expected[i + 1]); + } + + var observedOutput = highlight(text, mode); + + var pass, passStyle = ""; + pass = highlightOutputsEqual(expectedOutput, observedOutput); + passStyle = pass ? 'mt-pass' : 'mt-fail'; + + var s = ''; + if (pass) { + s += '
' + text.replace('&', '&').replace('<', '<') + ''; + s += '
' + text.replace('&', '&').replace('<', '<') + ''; + s += '
' + + '' + + val.replace(/ /g,'\xb7').replace('&', '&').replace('<', '<') + + '' + + ' | '; + } + s += '
' + (output[i] || null) + ' | '; + } + s += '