carto/test/specificity.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

2011-01-21 06:54:51 +08:00
var path = require('path'),
sys = require('sys'),
assert = require('assert');
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) {
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;
});
// 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) {
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++;
}
});
}
});
});
beforeExit(function() {
if (failures) {
assert.ok(false, failures + ' specificity ' + (failures != 1 ? 'orders' : 'order') + ' don\'t match.');
}
});
};
function formatJSON(arr) {
return '[\n ' + arr.map(function(t) {
return JSON.stringify(t);
}).join(',\n ') + '\n]';
}