refactored xunit.js module
Showing
2 changed files
with
36 additions
and
29 deletions
... | @@ -236,6 +236,25 @@ function mergeObjects(obj1, obj2) { | ... | @@ -236,6 +236,25 @@ function mergeObjects(obj1, obj2) { |
236 | exports.mergeObjects = mergeObjects; | 236 | exports.mergeObjects = mergeObjects; |
237 | 237 | ||
238 | /** | 238 | /** |
239 | * Creates an (SG|X)ML node element. | ||
240 | * | ||
241 | * @param String name The node name | ||
242 | * @param Object attributes Optional attributes | ||
243 | * @return HTMLElement | ||
244 | */ | ||
245 | function node(name, attributes) { | ||
246 | var node = document.createElement(name); | ||
247 | for (var attrName in attributes) { | ||
248 | var value = attributes[attrName]; | ||
249 | if (attributes.hasOwnProperty(attrName) && isString(attrName)) { | ||
250 | node.setAttribute(attrName, value); | ||
251 | } | ||
252 | } | ||
253 | return node; | ||
254 | } | ||
255 | exports.node = node; | ||
256 | |||
257 | /** | ||
239 | * Serializes a value using JSON. | 258 | * Serializes a value using JSON. |
240 | * | 259 | * |
241 | * @param Mixed value | 260 | * @param Mixed value | ... | ... |
... | @@ -37,36 +37,27 @@ exports.create = function() { | ... | @@ -37,36 +37,27 @@ exports.create = function() { |
37 | * | 37 | * |
38 | */ | 38 | */ |
39 | XUnitExporter = function() { | 39 | XUnitExporter = function() { |
40 | var node = function(name, attributes) { | 40 | this._xml = utils.node('testsuite'); |
41 | var node = document.createElement(name); | 41 | this._xml.toString = function() { |
42 | for (var attrName in attributes) { | ||
43 | var value = attributes[attrName]; | ||
44 | if (attributes.hasOwnProperty(attrName) && utils.isType(attrName, "string")) { | ||
45 | node.setAttribute(attrName, value); | ||
46 | } | ||
47 | } | ||
48 | return node; | ||
49 | }; | ||
50 | |||
51 | var xml = node('testsuite'); | ||
52 | xml.toString = function() { | ||
53 | return this.outerHTML; // ouch | 42 | return this.outerHTML; // ouch |
54 | }; | 43 | }; |
44 | }; | ||
45 | exports.XUnitExporter = XUnitExporter; | ||
55 | 46 | ||
56 | /** | 47 | /** |
57 | * Adds a successful test result | 48 | * Adds a successful test result |
58 | * | 49 | * |
59 | * @param String classname | 50 | * @param String classname |
60 | * @param String name | 51 | * @param String name |
61 | */ | 52 | */ |
62 | this.addSuccess = function(classname, name) { | 53 | XUnitExporter.prototype.addSuccess = function(classname, name) { |
63 | xml.appendChild(node('testcase', { | 54 | this._xml.appendChild(utils.node('testcase', { |
64 | classname: classname, | 55 | classname: classname, |
65 | name: name | 56 | name: name |
66 | })); | 57 | })); |
67 | }; | 58 | }; |
68 | 59 | ||
69 | /** | 60 | /** |
70 | * Adds a failed test result | 61 | * Adds a failed test result |
71 | * | 62 | * |
72 | * @param String classname | 63 | * @param String classname |
... | @@ -74,27 +65,24 @@ XUnitExporter = function() { | ... | @@ -74,27 +65,24 @@ XUnitExporter = function() { |
74 | * @param String message | 65 | * @param String message |
75 | * @param String type | 66 | * @param String type |
76 | */ | 67 | */ |
77 | this.addFailure = function(classname, name, message, type) { | 68 | XUnitExporter.prototype.addFailure = function(classname, name, message, type) { |
78 | var fnode = node('testcase', { | 69 | var fnode = utils.node('testcase', { |
79 | classname: classname, | 70 | classname: classname, |
80 | name: name | 71 | name: name |
81 | }); | 72 | }); |
82 | var failure = node('failure', { | 73 | var failure = utils.node('failure', { |
83 | type: type || "unknown" | 74 | type: type || "unknown" |
84 | }); | 75 | }); |
85 | failure.appendChild(document.createTextNode(message || "no message left")); | 76 | failure.appendChild(document.createTextNode(message || "no message left")); |
86 | fnode.appendChild(failure); | 77 | fnode.appendChild(failure); |
87 | xml.appendChild(fnode); | 78 | this._xml.appendChild(fnode); |
88 | }; | 79 | }; |
89 | 80 | ||
90 | /** | 81 | /** |
91 | * Retrieves generated XML object - actually an HTMLElement. | 82 | * Retrieves generated XML object - actually an HTMLElement. |
92 | * | 83 | * |
93 | * @return HTMLElement | 84 | * @return HTMLElement |
94 | */ | 85 | */ |
95 | this.getXML = function() { | 86 | XUnitExporter.prototype.getXML = function() { |
96 | return xml; | 87 | return this._xml; |
97 | }; | ||
98 | }; | 88 | }; |
99 | |||
100 | exports.XUnitExporter = XUnitExporter; | ... | ... |
-
Please register or sign in to post a comment