reporter.js | |
---|---|
Steal vows console object | var consoleObj = require("../lib/console.js");
var options = { tail: '\n' };
var stylize = consoleObj.stylize,
puts = consoleObj.puts(options); |
Construct a report object | var report = { |
store contexts | "contexts": {}, |
When vows fires report we add various data to our internal structure For a context, just inject a context object into our internal list For vow we add a vows object to that context For subject set the subject For finish, print the structure, then print the report | "add": function _add(obj) {
if (obj[0] === "context") {
this.contexts[obj[1]] = { vows: {} };
} else if (obj[0] === "vow") {
var vow = obj[1];
this.contexts[vow.context].vows[vow.title] = vow;
} else if (obj[0] === "subject") {
this.subject = obj[1];
} else if (obj[0] === "finish") {
this.print();
this.printReport(obj[1]);
}
}, |
Code horrors :( | "print": function _print() {
var self = this;
puts('\n♢ ' + stylize(this.subject, 'bold') + '\n');
var contexts = {}; |
Find the parent context based on the name | function findParent(contexts, text) {
var obj = false;
var newKey = text;
for (var key in contexts) {
var context = contexts[key];
if (context.oldKey) {
key = context.oldKey;
}
if (context.contexts) {
var arr = findParent(context.contexts, text);
if (arr) {
return arr;
}
}
if (text.indexOf(key) !== -1) {
newKey = text.replace(key, "");
obj = context;
break;
}
}
if (obj) {
return [obj, newKey];
} else {
return false;
}
}; |
Create the real contexts object from this.contexts | Object.keys(this.contexts).sort().forEach(function(key) {
var arr = findParent(contexts, key);
if (arr) {
var context = arr[0];
var newKey = arr[1];
}
if (!arr) {
contexts[key] = self.contexts[key];
if (!contexts[key].contexts) {
contexts[key].contexts = {};
}
} else {
context.contexts[newKey] = self.contexts[key];
context.contexts[newKey].oldKey = key;
if (!context.contexts[newKey].contexts) {
context.contexts[newKey].contexts = {};
}
}
}); |
print a context recursively | function print(context, key, depth) {
var whitespace = (new Array(depth + 1)).join(" ");
puts(whitespace + consoleObj.contextText(key));
Object.keys(context.vows).forEach(function(key2) {
puts(whitespace + " " + consoleObj.vowText(context.vows[key2]));
});
if (context.contexts) {
Object.keys(context.contexts).forEach(function(key) {
print(context.contexts[key], key, depth + 1);
});
}
} |
For each context print it | Object.keys(contexts).forEach(function(key) {
print(contexts[key], key, 0);
});
}, |
delegate printing of the report to vows console | "printReport": function _printReport(report) {
puts(consoleObj.result(report).join('\n'));
}
}
exports.name = 'prettyprint'; |
Might be needed. Who knows o/ | exports.setStream = function _setStream(s) {
options.stream = s;
}; |
When vows reports ask our report to add the object | exports.report = function _report(obj) {
report.add(obj);
};
|