From 28f7d4d7153242ee97149befdfd5791e7378ff43 Mon Sep 17 00:00:00 2001 From: Guillermo Rauch Date: Tue, 3 Jan 2012 13:11:09 -0800 Subject: [PATCH] Added instrumentation support for the client during tests. --- test/common.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/common.js b/test/common.js index f455d387..efd222d4 100644 --- a/test/common.js +++ b/test/common.js @@ -1,4 +1,36 @@ +/** + * Instrument. + */ + +var fs = require('fs'); + +if (process.env.DEBUG) { + require.extensions['.js'] = function(mod, filename){ + var js = fs.readFileSync(filename, 'utf8'); + + // Profiling support + js = js.replace(/^ *\/\/ *(start|end): *([^\n]+)/gm, function(_, type, expr){ + switch (type) { + case 'start': return 'console.time(' + expr + ');'; + case 'end': return 'console.timeEnd(' + expr + ');'; + } + }); + + // Debugging + js = js.replace(/^ *\/\/ *debug: *([^\n,]+) *([^\n]+)?/gm, function(_, fmt, args){ + fmt = fmt.replace(/"/g, '\\"'); + return 'console.error(" client\033[90m ' + fmt + '\033[0m"' + (args || '') + ');'; + }); + + js = js.replace(/^ *\/\/ *assert: ([^,]+) *, *([^\n]+)/gm, function(_, expr, msg){ + return 'if (!(' + expr + ')) console.error(" client assert\033[31m %s. (%s)\033[0m", ' + msg + ', "' + expr + '");'; + }); + + mod._compile(js, filename); + }; +} + /** * Expose `eio` global. */