Skip to content
This repository has been archived by the owner on Mar 23, 2024. It is now read-only.

Commit

Permalink
Chai: WIP
Browse files Browse the repository at this point in the history
Final fixes
  • Loading branch information
mdevils committed Oct 7, 2015
1 parent 653a711 commit c30f38e
Show file tree
Hide file tree
Showing 180 changed files with 5,347 additions and 5,029 deletions.
8 changes: 6 additions & 2 deletions lib/js-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ JsFile.prototype = {
}

if (options && options.includeComments) {
return this._tokens[index];
return this._tokens[index] || null;
}

do {
Expand All @@ -350,6 +350,8 @@ JsFile.prototype = {
return this._tokens[index];
}
} while (--index >= 0);

return null;
},

/**
Expand All @@ -368,7 +370,7 @@ JsFile.prototype = {
}

if (options && options.includeComments) {
return this._tokens[index];
return this._tokens[index] || null;
}

do {
Expand All @@ -380,6 +382,8 @@ JsFile.prototype = {
return this._tokens[index];
}
} while (++index < this._tokens.length);

return null;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-capitalized-comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ module.exports.prototype = {
var firstToken = file.getFirstNodeToken(comment);
var otherToken = file.getPrevToken(firstToken, { includeComments: true });

return otherToken.loc.start.line === firstToken.loc.start.line;
return otherToken ? otherToken.loc.start.line === firstToken.loc.start.line : false;
},

check: function(file, errors) {
Expand Down
7 changes: 4 additions & 3 deletions lib/string-checker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var Configuration = require('./config/configuration');

var MAX_FIX_ATTEMPTS = 5;

function getErrorMessage(rule, e) {
function getInternalErrorMessage(rule, e) {
return 'Error running rule ' + rule + ': ' +
'This is an issue with JSCS and not your codebase.\n' +
'Please file an issue (with the stack trace below) at: ' +
Expand Down Expand Up @@ -137,7 +137,7 @@ StringChecker.prototype = {

} catch (e) {
error.fixed = undefined;
errors.add(getErrorMessage(error.rule, e), 1, 0);
errors.add(getInternalErrorMessage(error.rule, e), 1, 0);
}
}
});
Expand All @@ -164,7 +164,8 @@ StringChecker.prototype = {
try {
rule.check(file, errors);
} catch (e) {
errors.add(getErrorMessage(rule.getOptionName(), e.stack), 1, 0);
errors.setCurrentRule('internalError');
errors.add(getInternalErrorMessage(rule.getOptionName(), e.stack), 1, 0);
}
}, this);

Expand Down
2 changes: 1 addition & 1 deletion lib/token-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ TokenAssert.prototype.noTrailingSpaces = function(options) {
}
}

if (typeof targetToken !== null && !fixed) {
if (targetToken !== null && !fixed) {
var eolCount = targetToken.loc.start.line - startLineNumber + 1;
var targetIndent = '';
var targetLine = lines[targetToken.loc.start.line - 1];
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@
"estraverse": "^4.1.0",
"exit": "~0.1.2",
"glob": "^5.0.1",
"htmlparser2": "3.8.3",
"js-yaml": "~3.4.0",
"jscs-jsdoc": "1.2.0",
"lodash": "~3.10.0",
"jsonlint": "~1.6.2",
"lodash": "~3.10.0",
"minimatch": "~2.0.1",
"natural-compare": "~1.2.2",
"pathval": "~0.1.1",
Expand All @@ -86,18 +87,19 @@
"to-single-quotes": "^1.0.2",
"vow": "~0.4.8",
"vow-fs": "~0.3.4",
"xmlbuilder": "^2.6.1",
"htmlparser2": "3.8.3"
"xmlbuilder": "^2.6.1"
},
"devDependencies": {
"browserify": "^11.0.0",
"chai": "^3.3.0",
"coveralls": "~2.11.2",
"has-ansi": "~1.0.1",
"jshint": "~2.8.0",
"mocha": "^2.2.0",
"regenerate": "~1.2.1",
"rewire": "^2.3.1",
"sinon": "^1.13.0",
"sinon-chai": "^2.8.0",
"unicode-7.0.0": "~0.1.5",
"unit-coverage": "^3.4.0",
"xml2js": "~0.4.4"
Expand Down
153 changes: 153 additions & 0 deletions test/chai-extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
var chai = require('chai');

chai.use(require('sinon-chai'));

chai.use(function(chai, utils) {
/**
* Error assertion for `Errors` instances.
*/
chai.Assertion.addChainableMethod('error', function(message) {
var list = getErrorList(this);
var allErrors = buildErrorReport(this);

if (message) {
if (utils.flag(this, 'contains')) {
return this.assert(
list.some(function(error) {
return error.message === message;
}),
'Expected to contain "' + message + '"' + allErrors,
'Expected not to contain "' + message + '"' + allErrors
);
} else {
this.assert(
list.length === 1,
'1 error expected, but ' + list.length + ' found' + allErrors,
'Unexpected to have 1 error' + allErrors
);
return this.assert(
list[0].message === message,
'Expected "' + list[0].message + '" to equal "' + message + '"' + allErrors,
'Expected "' + list[0].message + '" not to equal "' + message + '"' + allErrors
);
}
}
}, function() {
if (utils.flag(this, 'one')) {
var allErrors = buildErrorReport(this);

var list = getErrorList(this);

return this.assert(
list.length === 1,
'Expected to have 1 error, but ' + list.length + ' found' + allErrors,
'Expected not to have 1 error' + allErrors
);
}
});

/**
* Rule name assertion for `Errors` instances.
*/
chai.Assertion.addChainableMethod('from', function(ruleName) {
var list = getErrorList(this);
var ruleNames = list.map(function(error) {
return error.rule;
});

var matches = ruleNames.every(function(errorRuleName) {
return errorRuleName === ruleName;
});

var allErrors = buildErrorReport(this);

return this.assert(
(utils.flag(this, 'no') ? !matches : matches),
'Expected error rules "' + ruleNames.join(', ') + '" to equal "' + ruleName + '"' + allErrors,
'Expected error rules "' + ruleNames.join(', ') + '" not to equal "' + ruleName + '"' + allErrors
);
});

/**
* Rule name assertion for `Errors` instances.
*/
chai.Assertion.addChainableMethod('errors',
function() {
var list = getErrorList(this);
var allErrors = buildErrorReport(this);
var messages = list.map(function(error) {
return error.message;
});
return this.assert(
utils.flag(this, 'no') ? list.length === 0 : list.length !== 0,
'Expected not to have errors, but "' + messages.join(', ') + '" found' + allErrors,
'Expected to have some errors' + allErrors
);
}
);

/**
* Error count property for `Errors` instances.
*/
chai.Assertion.addProperty('count', function() {
return new chai.Assertion(getErrorList(this).length);
});

/**
* Error count property for `Errors` instances.
*/
chai.Assertion.addProperty('no', function() {
utils.flag(this, 'no', true);
});

/**
* Error assertion for `Errors` instances.
*/
chai.Assertion.addProperty('one', function() {
utils.flag(this, 'one', true);
});

chai.Assertion.addProperty('validation', function() {
utils.flag(this, 'validation', true);
});

chai.Assertion.addProperty('parse', function() {
utils.flag(this, 'parse', true);
});

function getErrorList(context) {
var errors = utils.flag(context, 'object');
var list = errors.getErrorList();
if (utils.flag(context, 'validation')) {
list = list.filter(isValidationError);
}
if (utils.flag(context, 'parse')) {
list = list.filter(isParseError);
}
return list;
}

function buildErrorReport(context) {
var errorReport = utils.flag(context, '_errorReport');
if (!errorReport) {
errorReport = '\nAll errors:\n' +
utils.flag(context, 'object').getErrorList().map(function(error) {
return ' - ' + error.rule + ': ' + error.message;
}).join('\n');
utils.flag(context, '_errorReport', errorReport);
}
return errorReport;
}

function isValidationError(error) {
return !isParseError(error) && !isInternalError(error);
}

function isParseError(error) {
return error.rule === 'parseError';
}

function isInternalError(error) {
return error.rule === 'internalError';
}
});
20 changes: 10 additions & 10 deletions test/lib/assertHelpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var assert = require('assert');
var expect = require('chai').expect;
var Checker = require('../../lib/checker');

var AssertHelpers = {
Expand All @@ -14,14 +14,14 @@ var AssertHelpers = {
* @param {Number} [options.errors='1'] the expected number of errors when checking input
*/
reportAndFix: function(options) {
assert.equal(typeof(options), 'object');
assert.equal(typeof(options.name), 'string');
assert.equal(typeof(options.rules), 'object');
assert.equal(typeof(options.input), 'string');
assert.equal(typeof(options.output), 'string');
expect(options).to.be.a('object');
expect(options.name).to.be.a('string');
expect(options.rules).to.be.a('object');
expect(options.input).to.be.a('string');
expect(options.output).to.be.a('string');

if (options.errors !== undefined) {
assert.equal(typeof(options.errors), 'number');
expect(options.errors).to.be.a('number');
}

options.errors = options.errors === undefined ? 1 : options.errors;
Expand All @@ -36,13 +36,13 @@ var AssertHelpers = {
});

it('report', function() {
assert(checker.checkString(options.input).getErrorCount() === options.errors);
expect(checker.checkString(options.input)).to.have.error.count.equal(options.errors);
});

it('fix', function() {
var result = checker.fixString(options.input);
assert(result.errors.isEmpty());
assert.equal(result.output, options.output);
expect(result.errors).to.have.no.errors();
expect(result.output).to.equal(options.output);
});
}

Expand Down
2 changes: 2 additions & 0 deletions test/mocha.opts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
test/chai-extensions.js
test/specs/**/*.js
-t 3000
-R progress
-u bdd
Loading

0 comments on commit c30f38e

Please sign in to comment.