test specificitiy

This commit is contained in:
Konstantin Käfer 2011-01-20 17:54:51 -05:00
parent dcf3256460
commit b444f5d8cf
4 changed files with 140 additions and 0 deletions

65
test/specificity.js Normal file
View File

@ -0,0 +1,65 @@
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]';
}

24
test/specificity/demo.mss Normal file
View File

@ -0,0 +1,24 @@
#world {
polygon-fill:#CCC;
}
#world[NAME='United States'] {
polygon-fill:#F00;
[BLUE='red'] {
}
}
#countries .countries .two {
polygon-fill:#0FF;
}
#countries, #world {
polygon-fill:#F0F;
polygon-opacity: 0.5;
}
#countries, #countries.foo.bar.baz {}

View File

@ -0,0 +1,10 @@
[
["#countries",".foo",".bar",".baz"],
["#countries",".countries",".two"],
{"elements":["#world"],"filters":[{"key":"NAME","op":{"value":"="},"val":{"value":"United States","quote":"'","is":"string"}},{"key":"BLUE","op":{"value":"="},"val":{"value":"red","quote":"'","is":"string"}}]},
{"elements":["#world"],"filters":[{"key":"NAME","op":{"value":"="},"val":{"value":"United States","quote":"'","is":"string"}}]},
["#countries"],
["#world"],
["#countries"],
["#world"]
]

41
test/support/helper.js Normal file
View File

@ -0,0 +1,41 @@
var path = require('path'),
fs = require('fs');
exports.files = function(dir, extension, callback) {
var dir = path.join(__dirname, '..', dir);
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);
});
}
});
};
exports.json = function(file, callback) {
fs.readFile(file, 'utf-8', function(err, content) {
if (err) throw err;
callback(JSON.parse(content));
});
}
// Stylize a string
exports.stylize = function(str, style) {
var styles = {
'bold' : [1, 22],
'inverse' : [7, 27],
'underline' : [4, 24],
'yellow' : [33, 39],
'green' : [32, 39],
'red' : [31, 39]
};
return '\033[' + styles[style][0] + 'm' + str +
'\033[' + styles[style][1] + 'm';
}