diff --git a/test/specificity.js b/test/specificity.js new file mode 100644 index 0000000..01ae1dd --- /dev/null +++ b/test/specificity.js @@ -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]'; +} diff --git a/test/specificity/demo.mss b/test/specificity/demo.mss new file mode 100644 index 0000000..6594d05 --- /dev/null +++ b/test/specificity/demo.mss @@ -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 {} diff --git a/test/specificity/demo.result b/test/specificity/demo.result new file mode 100644 index 0000000..a5b9e81 --- /dev/null +++ b/test/specificity/demo.result @@ -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"] +] \ No newline at end of file diff --git a/test/support/helper.js b/test/support/helper.js new file mode 100644 index 0000000..0c45b58 --- /dev/null +++ b/test/support/helper.js @@ -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'; +}