refactor test so that we run a separate test for each file
This commit is contained in:
parent
03df74d7a0
commit
7c45abd005
@ -1,61 +1,60 @@
|
||||
var path = require('path'),
|
||||
sys = require('sys'),
|
||||
assert = require('assert');
|
||||
assert = require('assert'),
|
||||
fs = require('fs');
|
||||
|
||||
var mess = require('mess');
|
||||
var tree = require('mess/tree');
|
||||
var helper = require('./support/helper');
|
||||
|
||||
exports['test specificity'] = function(beforeExit) {
|
||||
var failures = 0;
|
||||
helper.files('specificity', 'mss', function(file, content) {
|
||||
helper.files('specificity', 'mss', function(filename) {
|
||||
var testName = 'test specificity/' + filename;
|
||||
exports[testName] = function(beforeExit) {
|
||||
var file = path.join(__dirname, 'specificity', filename);
|
||||
fs.readFile(file, 'utf-8', function (err, content) {
|
||||
if (err) throw err;
|
||||
|
||||
new(mess.Parser)({
|
||||
paths: [ path.dirname(file) ],
|
||||
filename: file
|
||||
}).parse(content, function (err, tree) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
assert.ok(false);
|
||||
} else {
|
||||
var mss = tree.toMSS();
|
||||
mss = mss.map(function (item) {
|
||||
// Remove indexes; we don't care about the exact byte number.
|
||||
delete item.selector.index;
|
||||
for (var i = 0; i < item.selector.filters.length; i++) {
|
||||
delete item.selector.filters[i].index;
|
||||
}
|
||||
item.selector.elements = item.selector.elements.map(function(el) {
|
||||
return el.value;
|
||||
new(mess.Parser)({
|
||||
paths: [ path.dirname(file) ],
|
||||
filename: file
|
||||
}).parse(content, function (err, tree) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
assert.ok(false);
|
||||
} else {
|
||||
var mss = tree.toMSS();
|
||||
mss = mss.map(function (item) {
|
||||
// Remove indexes; we don't care about the exact byte number.
|
||||
delete item.selector.index;
|
||||
for (var i = 0; i < item.selector.filters.length; i++) {
|
||||
delete item.selector.filters[i].index;
|
||||
}
|
||||
item.selector.elements = item.selector.elements.map(function(el) {
|
||||
return el.value;
|
||||
});
|
||||
return item.selector.filters.length ? item.selector : item.selector.elements;
|
||||
});
|
||||
return item.selector.filters.length ? item.selector : item.selector.elements;
|
||||
});
|
||||
|
||||
// Make sure objects are plain objects.
|
||||
mss = JSON.parse(JSON.stringify(mss));
|
||||
// Make sure objects are plain objects.
|
||||
mss = JSON.parse(JSON.stringify(mss));
|
||||
|
||||
var resultFile = path.join(path.dirname(file), path.basename(file, '.mss') + '.result');
|
||||
helper.json(resultFile, function(json) {
|
||||
var resultFile = path.join(path.dirname(file), path.basename(file, '.mss') + '.result');
|
||||
helper.json(resultFile, function(json) {
|
||||
|
||||
try {
|
||||
assert.deepEqual(mss, json);
|
||||
} catch (e) {
|
||||
console.log(stylize("FAIL", 'red') + ': ' + helper.stylize(file, 'underline') + ' differs from expected result.');
|
||||
console.log(stylize('actual:', 'bold') + '\n' + formatJSON(e.actual));
|
||||
console.log(stylize('expected:', 'bold') + '\n' + formatJSON(e.expected));
|
||||
failures++;
|
||||
}
|
||||
});
|
||||
}
|
||||
try {
|
||||
assert.deepEqual(mss, json);
|
||||
} catch (e) {
|
||||
console.log(helper.stylize("Failure", 'red') + ': ' + helper.stylize(file, 'underline') + ' differs from expected result.');
|
||||
console.log(helper.stylize('actual:', 'bold') + '\n' + formatJSON(e.actual));
|
||||
console.log(helper.stylize('expected:', 'bold') + '\n' + formatJSON(e.expected));
|
||||
throw '';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
beforeExit(function() {
|
||||
if (failures) {
|
||||
assert.ok(false, failures + ' specificity ' + (failures != 1 ? 'orders' : 'order') + ' don\'t match.');
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
function formatJSON(arr) {
|
||||
|
@ -7,11 +7,7 @@ exports.files = function(dir, extension, callback) {
|
||||
extension = new RegExp('\\.' + extension + '$');
|
||||
fs.readdirSync(dir).forEach(function(filename) {
|
||||
if (extension.test(filename)) {
|
||||
var file = path.join(dir, filename);
|
||||
fs.readFile(file, 'utf-8', function (err, content) {
|
||||
if (err) throw err;
|
||||
callback(file, content);
|
||||
});
|
||||
callback(filename);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user