Skip to content

Commit

Permalink
Merge pull request #1204 from naokiy/strict_assert_error
Browse files Browse the repository at this point in the history
More strict error assertion
  • Loading branch information
tommy351 committed Apr 17, 2015
2 parents 56bfc43 + 3e84730 commit 64fdc3a
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 49 deletions.
2 changes: 1 addition & 1 deletion lib/models/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = function(ctx){
parent: parent || {$exists: false}
});

if (cat && cat._id === data._id){
if (cat){
throw new Error('Category `' + name + '` has already existed!');
}
});
Expand Down
2 changes: 1 addition & 1 deletion lib/models/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = function(ctx){
var Tag = ctx.model('Tag');
var tag = Tag.findOne({name: name});

if (tag && tag._id === data._id){
if (tag){
throw new Error('Tag `' + name + '` has already existed!');
}
});
Expand Down
22 changes: 19 additions & 3 deletions test/scripts/box/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var fs = require('hexo-fs');
var Promise = require('bluebird');
var crypto = require('crypto');
var util = require('hexo-util');
var sinon = require('sinon');
var Pattern = util.Pattern;
var testUtil = require('../../util');

Expand Down Expand Up @@ -82,12 +83,17 @@ describe('Box', function(){

it('addProcessor() - no fn', function(){
var box = newBox();
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'fn must be a function');
});

try {
box.addProcessor('test');
} catch (err){
err.should.have.property('message', 'fn must be a function');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('_loadFiles() - create', function(){
Expand Down Expand Up @@ -430,11 +436,16 @@ describe('Box', function(){

it.skip('watch() - watcher has started', function(callback){
var box = newBox();
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'Watcher has already started.');
});

box.watch().then(function(){
box.watch().catch(function(err){
err.should.have.property('message', 'Watcher has already started.');
errorCallback(err);
box.unwatch();
}).finally(function() {
errorCallback.calledOnce.should.be.false;
callback();
});
});
Expand Down Expand Up @@ -482,12 +493,17 @@ describe('Box', function(){

it('unwatch() - watcher not started', function(){
var box = newBox();
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'Watcher hasn\'t started yet.');
});

try {
box.unwatch();
} catch (err){
err.should.have.property('message', 'Watcher hasn\'t started yet.');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it.skip('isWatching()', function(){
Expand Down
14 changes: 12 additions & 2 deletions test/scripts/extend/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,32 @@ describe('Filter', function(){

it('unregister() - type is required', function(){
var f = new Filter();
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'type is required');
});

try {
f.unregister();
} catch (err){
err.should.have.property('message', 'type is required');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('unregister() - fn must be a function', function(){
var f = new Filter();
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'fn must be a function');
});

try {
f.unregister('test');
} catch (err){
err.should.have.property('message', 'fn must be a function');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('list()', function(){
Expand Down
17 changes: 15 additions & 2 deletions test/scripts/extend/tag.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var should = require('chai').should();
var sinon = require('sinon');
var Promise = require('bluebird');

describe('Tag', function(){
Expand Down Expand Up @@ -123,19 +124,31 @@ describe('Tag', function(){
});

it('register() - name is required', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'name is required');
});

try {
tag.register();
} catch (err){
err.should.have.property('message', 'name is required');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('register() - fn must be a function', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'fn must be a function');
});

try {
tag.register('test');
} catch (err){
err.should.have.property('message', 'fn must be a function');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('render() - context', function(){
Expand Down
7 changes: 6 additions & 1 deletion test/scripts/filters/new_post_path.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var should = require('chai').should();
var sinon = require('sinon');
var pathFn = require('path');
var moment = require('moment');
var Promise = require('bluebird');
Expand Down Expand Up @@ -170,8 +171,12 @@ describe('new_post_path', function(){
});

it('data is required', function(){
return newPostPath().catch(function(err){
var errorCallback = sinon.spy(function(err){
err.should.have.property('message', 'Either data.path or data.slug is required!');
});

return newPostPath().catch(errorCallback).finally(function() {
errorCallback.calledOnce.should.be.true;
});
});
});
9 changes: 8 additions & 1 deletion test/scripts/helpers/partial.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var should = require('chai').should();
var sinon = require('sinon');
var pathFn = require('path');
var fs = require('hexo-fs');
var Promise = require('bluebird');
Expand Down Expand Up @@ -80,10 +81,16 @@ describe('partial', function(){
});

it('name must be a string', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'name must be a string!');
});

try {
partial();
} catch (err){
err.should.have.property('message', 'name must be a string!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});
});
6 changes: 5 additions & 1 deletion test/scripts/hexo/hexo.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ describe('Hexo', function(){
});

it('call() - console not registered', function(){
return hexo.call('nothing').catch(function(err){
var errorCallback = sinon.spy(function(err){
err.should.have.property('message', 'Console `nothing` has not been registered yet!');
});

return hexo.call('nothing').catch(errorCallback).finally(function(){
errorCallback.calledOnce.should.be.true;
});
});

it('init()', function(){
Expand Down
33 changes: 29 additions & 4 deletions test/scripts/hexo/locals.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
'use strict';

var should = require('chai').should();
var sinon = require('sinon');

describe('Locals', function(){
var Locals = require('../../../lib/hexo/locals');
var locals = new Locals();

it('get() - name must be a string', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'name must be a string!');
});

try {
locals.get();
} catch (err){
err.should.have.property('message', 'name must be a string!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('set() - function', function(){
Expand All @@ -32,19 +39,31 @@ describe('Locals', function(){
});

it('set() - name must be a string', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'name must be a string!');
});

try {
locals.set();
} catch (err){
err.should.have.property('message', 'name must be a string!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('set() - value is required', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'value is required!');
});

try {
locals.set('test');
} catch (err){
err.should.have.property('message', 'value is required!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('remove()', function(){
Expand All @@ -57,11 +76,17 @@ describe('Locals', function(){
});

it('remove() - name must be a string', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'name must be a string!');
});

try {
locals.remove();
} catch (err){
err.should.have.property('message', 'name must be a string!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('toObject()', function(){
Expand Down
14 changes: 12 additions & 2 deletions test/scripts/hexo/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,12 @@ describe('Render', function(){
});

it('render() - no path and text', function(){
return hexo.render.render().catch(function(err){
var errorCallback = sinon.spy(function(err){
err.should.have.property('message', 'No input file or string!');
})

return hexo.render.render().catch(errorCallback).finally(function() {
errorCallback.calledOnce.should.be.true;
});
});

Expand Down Expand Up @@ -220,11 +224,17 @@ describe('Render', function(){
});

it('renderSync() - no path and text', function(){
var errorCallback = sinon.spy(function(err) {
err.should.have.property('message', 'No input file or string!');
});

try {
hexo.render.renderSync();
} catch (err){
err.should.have.property('message', 'No input file or string!');
errorCallback(err);
}

errorCallback.calledOnce.should.be.true;
});

it('renderSync() - options', function(){
Expand Down
Loading

0 comments on commit 64fdc3a

Please sign in to comment.