Permalink
Browse files

* added assert patch shortcut for LINT validation

  • Loading branch information...
1 parent 9f06476 commit ae6f5c7d2dfb7c17f70a893707bffe81e41faca7 Julien Polo committed Nov 29, 2010
Showing with 199 additions and 32 deletions.
  1. +22 −0 lib/assert/extension.js
  2. +2 −31 lib/lint/index.js
  3. +31 −0 lib/lint/parser.js
  4. +3 −0 resource/fixture/invalid_test.js
  5. +60 −0 test/assert/extension_test.js
  6. +81 −1 test/lint/parser_test.js
View
@@ -0,0 +1,22 @@
+var assert = require('assert');
+var lint = require('../lint/parser');
+
+if (! assert.validateLint) {
+ assert.validateLint = function (actual, message) {
+ var result = lint.isValid(actual);
+
+ if (!result) {
+ assert.fail(actual, true, message || "{actual} does not validate LINT", "===", assert.validateLint);
+ }
+ };
+}
+
+if (! assert.validateLintFile) {
+ assert.validateLintFile = function (actual, message) {
+ var result = lint.isValidFileSync(actual);
+
+ if (!result) {
+ assert.fail(result, true, message || "{actual} file does not validate LINT", "===", assert.validateLint);
+ }
+ };
+}
View
@@ -4,39 +4,10 @@
*/
var formatter = require('./formatter');
var parser = require('./parser');
-var fs = require('fs');
-function isValid(content, options) {
- var parser = new parser.Parser(options);
- return parser.update(content).isValid();
-}
+//Apply assert patch
+require('../assert/extension');
-function isValidFile(filePath, options, callback) {
- fs.readFile(filePath, function (error, data) {
- var parser, result;
-
- if (error) {
- if (callback) {
- callback(error);
- }
- } else {
- parser = new parser.Parser(options);
- result = parser.update(data).isValid();
- if (callback) {
- callback(undefined, result);
- }
- }
- });
-}
-
-function isValidFileSync(filePath, options) {
- var data, parser, result;
- data = fs.readFileSync(filePath);
- parser = new parser.Parser(options);
- result = parser.update(data).isValid();
-
- return result;
-}
exports.Formatter = formatter.Formatter;
exports.Launcher = parser.Launcher;
View
@@ -198,3 +198,34 @@ Parser.prototype.getReport = function () {
* Exports
*/
exports.Parser = Parser;
+exports.isValid = function isValid(content, options) {
+ var parser = new Parser(options);
+ return parser.update(content).isValid();
+};
+
+exports.isValidFile = function isValidFile(filePath, options, callback) {
+ fs.readFile(filePath, function (error, data) {
+ var parser, result;
+
+ if (error) {
+ if (callback) {
+ callback(error);
+ }
+ } else {
+ parser = new Parser(options);
+ result = parser.update(data).isValid();
+ if (callback) {
+ callback(undefined, result);
+ }
+ }
+ });
+};
+
+exports.isValidFileSync = function isValidFileSync(filePath, options) {
+ var data, parser, result;
+ data = fs.readFileSync(filePath);
+ parser = new Parser(options);
+ result = parser.update(data).isValid();
+
+ return result;
+};
@@ -0,0 +1,3 @@
+object = {
+ bar: 'foo'
+}
@@ -0,0 +1,60 @@
+var vows = require('vows');
+require('../../lib/assert/extension');
+
+var fs = require('fs');
+var path = require('path');
+var assert = require('assert');
+
+var FIXTURE_PATH = fs.realpathSync(path.join(path.dirname(path.dirname(__dirname)), 'resource', 'fixture'));
+
+var AssertExtensionTest = vows.describe('assert module').addBatch({
+ "validateLint()" : {
+ topic : function (item) {
+ return assert.validateLint;
+ },
+ 'should return a function' : function (topic) {
+ assert.isFunction(topic);
+ },
+ 'should throw an error if wrong javascript is passed' : function (topic) {
+ assert.throws(function () {
+ topic("fdslfjsdlkj;");
+ });
+ },
+ 'should not throw an error if wrong javascript is passed' : function (topic) {
+ assert.doesNotThrow(function () {
+ topic("foo = 'bar';");
+ });
+ }
+ },
+ "validateLintFile()" : {
+ topic : function (item) {
+ return assert.validateLintFile;
+ },
+ 'should return a function' : function (topic) {
+ assert.isFunction(topic);
+ },
+ 'should throw an error if file does not exist' : function (topic) {
+ assert.throws(function () {
+ topic('nonexistent.js');
+ });
+ },
+ 'should throw an error if file does not contains valid javascript' : function (topic) {
+ var fixtureFile = path.join(FIXTURE_PATH, 'invalid_test.js');
+
+ assert.doesNotThrow(function () {
+ fs.realpathSync(fixtureFile);
+ });
+ assert.throws(function () {
+ topic(fixtureFile);
+ });
+ },
+ 'should not throw an error if wrong javascript is passed' : function (topic) {
+ var fixtureFile = path.join(FIXTURE_PATH, 'valid_test.js');
+ assert.doesNotThrow(function () {
+ topic(fixtureFile);
+ });
+ }
+ }
+});
+
+exports.AssertExtensionTest = AssertExtensionTest;
View
@@ -1,8 +1,13 @@
/*jslint indent:4 */
var vows = require('vows');
var assert = require('assert');
+var fs = require('fs');
+var path = require('path');
+
var parser = require('../../lib/lint/parser');
+var FIXTURE_PATH = fs.realpathSync(path.join(path.dirname(path.dirname(__dirname)), 'resource', 'fixture'));
+
function createParser(options) {
return new parser.Parser(options);
}
@@ -92,4 +97,79 @@ var ParserTest = vows.describe('Parser class').addBatch({
});
-exports.ParserTest = ParserTest;
+var ParserModuleTest = vows.describe('parser module').addBatch({
+ "isValid()" : {
+ topic : function (item) {
+ return parser.isValid;
+ },
+ 'should return true if empty' : function (topic) {
+ assert.equal(topic(''), true);
+ },
+ 'should return true if valid javascript (simple)' : function (topic) {
+ assert.equal(topic('var foo = "bar";'), true);
+ assert.equal(topic('var foo = "baz";'), true);
+ },
+ 'should return false if invalid javascript (simple)' : function (topic) {
+ assert.equal(topic('var foo = "bar"'), false);//(missing semicolon)
+ }
+ },
+ "isValidFile()" : {
+ topic : function (item) {
+ var report = {};
+ var self = this;
+
+
+
+ parser.isValidFile('nonexistent.js', null, function (error, result) {
+ report.nonExistent = {error: error, result: result};
+
+ var validFile = path.join(FIXTURE_PATH, 'valid_test.js');
+ parser.isValidFile(validFile, null, function (error, result) {
+ report.validFile = {error: error, result: result};
+
+ var invalidFile = path.join(FIXTURE_PATH, 'invalid_test.js');
+ parser.isValidFile(invalidFile, null, function (error, result) {
+ report.invalidFile = {error: error, result: result};
+
+ self.callback(null, report);
+ });
+ });
+ });
+
+ },
+ 'should set error in callback if file does not exist' : function (topic) {
+ assert.notEqual(topic.nonExistent.error, undefined);
+ },
+ 'should return true if valid javascript file' : function (topic) {
+ assert.isUndefined(topic.validFile.error);
+ assert.equal(topic.validFile.result, true);
+ },
+ 'should return false if invalid javascript file' : function (topic) {
+ assert.isUndefined(topic.invalidFile.error);
+ assert.equal(topic.invalidFile.result, false);
+ }
+ },
+ "isValidFileSync()" : {
+ topic : function (item) {
+ return parser.isValidFileSync;
+ },
+ 'should throw an error if file does not exist' : function (topic) {
+ assert.throws(function () {
+ topic('nonexistent.js');
+ });
+ },
+ 'should return true if valid javascript file' : function (topic) {
+ var fixtureFile = path.join(FIXTURE_PATH, 'valid_test.js');
+ assert.equal(topic(fixtureFile), true);
+ assert.equal(topic(fixtureFile), true);
+ },
+ 'should return false if invalid javascript file' : function (topic) {
+ var fixtureFile = path.join(FIXTURE_PATH, 'invalid_test.js');
+ assert.equal(topic(fixtureFile), false);
+ }
+ }
+});
+
+
+exports.ParserTest = ParserTest;
+exports.ParserModuleTest = ParserModuleTest;

0 comments on commit ae6f5c7

Please sign in to comment.