carto/test/mess-test.js

69 lines
1.9 KiB
JavaScript
Raw Normal View History

2010-02-26 04:50:46 +08:00
var path = require('path'),
fs = require('fs'),
sys = require('sys');
require.paths.unshift(__dirname, path.join(__dirname, '..'));
2010-02-26 04:50:46 +08:00
2011-01-18 01:08:16 +08:00
var mess = require('lib/mess');
2010-03-06 03:56:47 +08:00
2011-01-18 01:08:16 +08:00
sys.puts("\n" + stylize("MESS", 'underline') + "\n");
2011-01-18 01:08:16 +08:00
fs.readdirSync('mess').forEach(function (file) {
if (! /\.mss/.test(file)) { return }
2011-01-18 02:19:14 +08:00
toCSS('mess/' + file, function (err, mess_result) {
2011-01-18 01:08:16 +08:00
var name = path.basename(file, '.mss');
2010-03-06 08:36:51 +08:00
2011-01-18 01:08:16 +08:00
fs.readFile(path.join('xml', name) + '.xml',
'utf-8', function (e, css) {
if (e) console.log(e);
2010-03-06 08:36:51 +08:00
sys.print("- " + name + ": ")
2011-01-18 02:19:14 +08:00
if (mess_result === css) { sys.print(stylize('OK', 'green')) }
else if (err) {
2010-03-06 08:36:51 +08:00
sys.print(stylize("ERROR: " + (err && err.message), 'red'));
} else {
2010-03-06 08:36:51 +08:00
sys.print(stylize("FAIL", 'yellow'));
2011-01-18 02:19:14 +08:00
sys.print(mess_result);
}
2010-02-26 04:50:46 +08:00
sys.puts("");
});
2010-02-26 08:48:09 +08:00
});
2010-02-26 04:50:46 +08:00
});
function toCSS(path, callback) {
var tree, css;
fs.readFile(path, 'utf-8', function (e, str) {
2010-02-26 04:50:46 +08:00
if (e) { return callback(e) }
2011-01-18 01:08:16 +08:00
new(mess.Parser)({
paths: [require('path').dirname(path)],
optimization: 0
}).parse(str, function (err, tree) {
if (err) {
callback(err);
} else {
try {
css = tree.toCSS();
callback(null, css);
} catch (e) {
callback(e);
}
}
});
2010-02-26 04:50:46 +08:00
});
}
2010-03-06 08:36:51 +08:00
// Stylize a string
function stylize(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';
}