|
| 1 | +'use strict'; |
| 2 | + |
| 3 | + |
| 4 | +var chalk = require("chalk"), |
| 5 | + fs = require("fs"), |
| 6 | + yaml = require("js-yaml"), |
| 7 | + mate = require("first-mate"), |
| 8 | + jsdiff = require("diff"), |
| 9 | + temp = require("temp").track(), |
| 10 | + _ = require('underscore'); |
| 11 | + |
| 12 | + |
| 13 | +function compileGrammar(grammarFile) { |
| 14 | + var tmp = temp.openSync(); |
| 15 | + |
| 16 | + try { |
| 17 | + var yamlSource = fs.readFileSync(grammarFile, 'utf8'), |
| 18 | + yamlSchema = yaml.safeLoad(yamlSource); |
| 19 | + |
| 20 | + fs.writeSync(tmp.fd, JSON.stringify(yamlSchema)); |
| 21 | + fs.closeSync(tmp.fd); |
| 22 | + |
| 23 | + var registry = new mate.GrammarRegistry, |
| 24 | + grammar = registry.loadGrammarSync(tmp.path); |
| 25 | + } |
| 26 | + finally { |
| 27 | + temp.cleanupSync(); |
| 28 | + } |
| 29 | + |
| 30 | + return grammar; |
| 31 | +} |
| 32 | + |
| 33 | + |
| 34 | +function testFile(file, grammar, options) { |
| 35 | + function padRight(str, pad) { |
| 36 | + if (str.length < pad) { |
| 37 | + return str + (new Array(pad - str.length)).join(' '); |
| 38 | + } |
| 39 | + |
| 40 | + return str; |
| 41 | + } |
| 42 | + |
| 43 | + function stripnl(str){ |
| 44 | + return str.replace(/^\n+/, '').replace(/\n+$/, ''); |
| 45 | + } |
| 46 | + |
| 47 | + function rpartition(str, separator) { |
| 48 | + if (!separator) { |
| 49 | + throw new Error('empty separator') |
| 50 | + } |
| 51 | + |
| 52 | + var seplen = separator.length, |
| 53 | + lastpos = str.lastIndexOf(separator); |
| 54 | + |
| 55 | + if (lastpos != -1) { |
| 56 | + return [str.substr(0, lastpos), |
| 57 | + separator, |
| 58 | + str.substr(lastpos + seplen)]; |
| 59 | + } |
| 60 | + else { |
| 61 | + return ['', '', str]; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function tokenize(lines) { |
| 66 | + var lines = grammar.tokenizeLines(stripnl(lines)), |
| 67 | + result = []; |
| 68 | + |
| 69 | + result = [] |
| 70 | + for (var i = 0; i < lines.length; i++) { |
| 71 | + var line = lines[i]; |
| 72 | + |
| 73 | + for (var j = 0; j < line.length; j++) { |
| 74 | + var value = line[j].value, |
| 75 | + scopes = line[j].scopes; |
| 76 | + |
| 77 | + result.push( |
| 78 | + [ |
| 79 | + padRight(value, 14), |
| 80 | + ' : ', |
| 81 | + _.chain(scopes).uniq().sort().value().join(', ') |
| 82 | + ].join('') |
| 83 | + ) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return result.join('\n') |
| 88 | + } |
| 89 | + |
| 90 | + function getDiff(test, result) { |
| 91 | + var diff = jsdiff.structuredPatch('', '', test, result, '', ''), |
| 92 | + result = []; |
| 93 | + |
| 94 | + for (var i = 0; i < diff.hunks.length; i++) { |
| 95 | + var hunk = diff.hunks[i]; |
| 96 | + |
| 97 | + for (var j = 0; j < hunk.lines.length; j++) { |
| 98 | + var line = hunk.lines[j]; |
| 99 | + if (line && !options.no_color) { |
| 100 | + if (line[0] == '+') { |
| 101 | + line = chalk.green(line) |
| 102 | + } else if (line[0] == '-') { |
| 103 | + line = chalk.red(line) |
| 104 | + } |
| 105 | + } |
| 106 | + result.push(line); |
| 107 | + } |
| 108 | + |
| 109 | + return result.join('\n'); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + var buf = fs.readFileSync(file, 'utf8'), |
| 114 | + parts = rpartition(buf, '\n\n\n'), |
| 115 | + source = parts[0], |
| 116 | + test = parts[2]; |
| 117 | + |
| 118 | + if (test) { |
| 119 | + test = stripnl(test); |
| 120 | + } |
| 121 | + |
| 122 | + if (!test) { |
| 123 | + test = null; |
| 124 | + } |
| 125 | + |
| 126 | + if (!source && test) { |
| 127 | + source = test; |
| 128 | + test = null; |
| 129 | + } |
| 130 | + |
| 131 | + if (!source) { |
| 132 | + return { |
| 133 | + file: file, |
| 134 | + status: 'fail', |
| 135 | + error: 'Empty file' |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + var result = tokenize(source); |
| 140 | + |
| 141 | + if (test != result) { |
| 142 | + if (test) { |
| 143 | + return { |
| 144 | + file: file, |
| 145 | + status: 'fail', |
| 146 | + error: 'Output different from expected', |
| 147 | + body: getDiff(test, result) |
| 148 | + } |
| 149 | + } else { |
| 150 | + return { |
| 151 | + file: file, |
| 152 | + status: 'fail', |
| 153 | + error: 'No expected output set', |
| 154 | + body: result |
| 155 | + } |
| 156 | + } |
| 157 | + } else { |
| 158 | + return { |
| 159 | + file: file, |
| 160 | + status: 'pass' |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | + |
| 166 | +function run(testFiles, grammarFile, options) { |
| 167 | + options = options || {}; |
| 168 | + |
| 169 | + var grammar = compileGrammar(grammarFile), |
| 170 | + sep = '--------', |
| 171 | + passed = 0, |
| 172 | + failed = 0, |
| 173 | + failedRes = []; |
| 174 | + |
| 175 | + console.log(sep); |
| 176 | + console.log(testFiles.length + ' tests; grammar: ' + grammarFile); |
| 177 | + console.log(sep); |
| 178 | + |
| 179 | + for (var i = 0; i < testFiles.length; i++) { |
| 180 | + var res = testFile(testFiles[i], grammar, options); |
| 181 | + |
| 182 | + if (res.status == 'fail') { |
| 183 | + process.stdout.write('E'); |
| 184 | + failed ++; |
| 185 | + failedRes.push(res); |
| 186 | + } else { |
| 187 | + process.stdout.write('.'); |
| 188 | + passed ++; |
| 189 | + } |
| 190 | + } |
| 191 | + process.stdout.write('\n'); |
| 192 | + |
| 193 | + console.log(sep); |
| 194 | + console.log(passed + ' passed; ' + failed + ' failed.'); |
| 195 | + console.log(sep); |
| 196 | + |
| 197 | + for (var i = 0; i < failedRes.length; i++) { |
| 198 | + var res = failedRes[i]; |
| 199 | + |
| 200 | + console.log(sep); |
| 201 | + console.log('Failed test ' + res.file + ': ' + res.error); |
| 202 | + console.log(sep); |
| 203 | + if (res.body) { |
| 204 | + console.log(res.body) |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (failed) { |
| 209 | + process.exit(1); |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +module.exports = run; |
0 commit comments