Commit cea904e9 cea904e99be3029ccfe4d1c8690071e321fce2c5 by Nicolas Perriault

refactored xunit.js module

1 parent 95a987c6
......@@ -236,6 +236,25 @@ function mergeObjects(obj1, obj2) {
exports.mergeObjects = mergeObjects;
/**
* Creates an (SG|X)ML node element.
*
* @param String name The node name
* @param Object attributes Optional attributes
* @return HTMLElement
*/
function node(name, attributes) {
var node = document.createElement(name);
for (var attrName in attributes) {
var value = attributes[attrName];
if (attributes.hasOwnProperty(attrName) && isString(attrName)) {
node.setAttribute(attrName, value);
}
}
return node;
}
exports.node = node;
/**
* Serializes a value using JSON.
*
* @param Mixed value
......@@ -265,13 +284,13 @@ exports.serialize = serialize;
* @param {function} superCtor Constructor function to inherit prototype from.
*/
exports.inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
......
......@@ -37,64 +37,52 @@ exports.create = function() {
*
*/
XUnitExporter = function() {
var node = function(name, attributes) {
var node = document.createElement(name);
for (var attrName in attributes) {
var value = attributes[attrName];
if (attributes.hasOwnProperty(attrName) && utils.isType(attrName, "string")) {
node.setAttribute(attrName, value);
}
}
return node;
};
var xml = node('testsuite');
xml.toString = function() {
this._xml = utils.node('testsuite');
this._xml.toString = function() {
return this.outerHTML; // ouch
};
};
exports.XUnitExporter = XUnitExporter;
/**
* Adds a successful test result
*
* @param String classname
* @param String name
*/
this.addSuccess = function(classname, name) {
xml.appendChild(node('testcase', {
classname: classname,
name: name
}));
};
/**
* Adds a failed test result
*
* @param String classname
* @param String name
* @param String message
* @param String type
*/
this.addFailure = function(classname, name, message, type) {
var fnode = node('testcase', {
classname: classname,
name: name
});
var failure = node('failure', {
type: type || "unknown"
});
failure.appendChild(document.createTextNode(message || "no message left"));
fnode.appendChild(failure);
xml.appendChild(fnode);
};
/**
* Adds a successful test result
*
* @param String classname
* @param String name
*/
XUnitExporter.prototype.addSuccess = function(classname, name) {
this._xml.appendChild(utils.node('testcase', {
classname: classname,
name: name
}));
};
/**
* Retrieves generated XML object - actually an HTMLElement.
*
* @return HTMLElement
*/
this.getXML = function() {
return xml;
};
/**
* Adds a failed test result
*
* @param String classname
* @param String name
* @param String message
* @param String type
*/
XUnitExporter.prototype.addFailure = function(classname, name, message, type) {
var fnode = utils.node('testcase', {
classname: classname,
name: name
});
var failure = utils.node('failure', {
type: type || "unknown"
});
failure.appendChild(document.createTextNode(message || "no message left"));
fnode.appendChild(failure);
this._xml.appendChild(fnode);
};
exports.XUnitExporter = XUnitExporter;
/**
* Retrieves generated XML object - actually an HTMLElement.
*
* @return HTMLElement
*/
XUnitExporter.prototype.getXML = function() {
return this._xml;
};
......