Permalink
Browse files

* added doc

* added Report type check for formatter
  • Loading branch information...
1 parent 320c8fd commit d596eae35d8b165e2d2146c106d65734c60cad2d Julien Polo committed Nov 17, 2010
Showing with 58 additions and 13 deletions.
  1. +49 −11 lib/lint/formatter.js
  2. +1 −1 test/lint/formatter/xml_test.js
  3. +8 −1 test/lint/formatter_test.js
View
@@ -3,6 +3,17 @@
* Imports
*/
+/*******************************************************************************
+ * Formatter class
+ *
+ * Usage:
+ *
+ * <pre>
+ * var formatter = new Formatter({type: 'json'});
+ *
+ * formatter.formater( report)//report is instance of Report
+ * </pre>
+ ******************************************************************************/
/**
* Formatter constructor
*
@@ -20,8 +31,9 @@ Formatter.NORMAL = 'normal';
Formatter.FULL = 'full';
/**
+ * Return the adapter from configuration
*
- * @return Base
+ * @return {Base}
*/
Formatter.prototype._getAdapter = function () {
if (!this._adapter) {
@@ -65,13 +77,21 @@ Formatter.prototype.configure = function (options) {
};
/**
+ * Return a formatted text
*
* @return {string}
*/
-Formatter.prototype.format = function (data) {
- return this._getAdapter().format(data);
+Formatter.prototype.format = function (report) {
+ return this._getAdapter().format(report);
};
+/*******************************************************************************
+ * Base class
+ *
+ * Usage:
+ *
+ * > inherits from this class to create a new reporter
+ ******************************************************************************/
/**
* Base constructor
*
@@ -141,21 +161,25 @@ Base.prototype.configure = function (options) {
* @param {string} mode
* @return {string}
*/
-Base.prototype.format = function (data, mode) {
- if (data === undefined) {
- throw new Error('data should not be undefined');
+Base.prototype.format = function (report, mode) {
+ if (report === undefined) {
+ throw new Error('`report` should not be undefined');
+ }
+
+ if (!(report instanceof Report)) {
+ throw new Error('`report` should be an instance of Report.');
}
mode = mode || this.mode;
- data = data || {};
+ report = report || {};
switch (mode) {
case Formatter.SIMPLE:
- return this._formatSimple(data);
+ return this._formatSimple(report);
case Formatter.NORMAL:
- return this._formatNormal(data);
+ return this._formatNormal(report);
case Formatter.FULL:
- return this._formatFull(data);
+ return this._formatFull(report);
default:
throw new Error('type "' + mode + '" does not exist');
}
@@ -199,7 +223,21 @@ Base.prototype._guessEol = function () {
};
-
+/*******************************************************************************
+ * Report class
+ *
+ * Usage:
+ *
+ * <pre>
+ * var report = new Report();
+ * report.addFile('foo.js', [
+ * //... some errors
+ * ]);
+ * report.addFile('bar.js', [
+ * //... some other errors
+ * ]);
+ * </pre>
+ ******************************************************************************/
/**
* Report constructor
*
@@ -82,7 +82,7 @@ var FormatterTest = vows.describe('XML Formatter class').addBatch({
},
'should return format empty content' : function (topic) {
var expected = '<?xml version="1.0" encoding="UTF-8" ?>\n\r<jslint>\n\r</jslint>\n\r';
- assert.equal(topic.format([]), expected);
+ assert.equal(topic.format(createReportEmpty()), expected);
},
'should return pretty xml formatted content' : function (topic) {
var expected = '<?xml version="1.0" encoding="UTF-8" ?>\n\r<jslint>\n\r\t<file name="foo">\n\r\t\t<issue line="12" char="5" evidence="e" reason="r" />\n\r\t</file>\n\r</jslint>\n\r';
@@ -2,6 +2,7 @@
var vows = require('vows');
var assert = require('assert');
var formatter = require('../../lib/lint/formatter');
+var Report = require('../../lib/lint/formatter').Report;
function createFormatter(options) {
return new formatter.Formatter(options);
@@ -11,6 +12,12 @@ function createBase(options) {
return new formatter.Base(options);
}
+function createReportEmpty() {
+ var report;
+ report = new Report();
+ return report;
+}
+
var FormatterTest = vows.describe('Formatter class').addBatch({
"constructor()" : {
topic : function (item) {
@@ -31,7 +38,7 @@ var FormatterTest = vows.describe('Formatter class').addBatch({
'should not throw error for types cli, json, etc' : function (topic) {
['cli', 'json', 'xml', 'textmate'].forEach(function (type) {
assert.doesNotThrow(function () {
- topic.format([]);
+ topic.format(createReportEmpty());
});
});
}

0 comments on commit d596eae

Please sign in to comment.