diff --git a/.jscsrc b/.jscsrc index 817e9960..23bca019 100644 --- a/.jscsrc +++ b/.jscsrc @@ -1,11 +1,8 @@ { - "disallowDanglingUnderscores": { - "allExcept": ["_exception", "_obj" , "_private"] - }, "disallowEmptyBlocks": true, "disallowImplicitTypeConversion": [ "binary", - "boolean", + //"boolean", "numeric", "string" ], diff --git a/test/.jshintrc b/test/.jshintrc index efcaffe1..98c4b4b4 100644 --- a/test/.jshintrc +++ b/test/.jshintrc @@ -30,6 +30,7 @@ "mocha": false, "modulejs": false, "sinon": false, + "uniq": false, "util": false } } diff --git a/test/lib/uniq.js b/test/lib/uniq.js new file mode 100644 index 00000000..24c21c4c --- /dev/null +++ b/test/lib/uniq.js @@ -0,0 +1,48 @@ +/* uniq 0.3.1 - http://larsjung.de/uniq/ */ +(function (root, factory) { + 'use strict'; + + if (typeof module !== 'undefined') { + module.exports = factory(); + } else { + root.uniq = factory(); + } +}(this, function () { + 'use strict'; + + var PREFIX = 'UNIQ-'; + var SUFFIX = '-ID'; + var LENGTH = 4; + var ZERO_PAD = new Array(LENGTH + 1).join('0'); + var RE_ID = new RegExp('^' + PREFIX + '\\d{' + LENGTH + '}' + SUFFIX + '$'); + + var counter = 0; + + function id() { + + counter += 1; + return PREFIX + (ZERO_PAD + counter).substr(-LENGTH) + SUFFIX; + } + + function isId(sequence) { + + return RE_ID.test(sequence); + } + + function obj() { + + return {_uniq_id: id()}; + } + + function path(suffix) { + + return '/_uniq_path/' + id() + (suffix ? suffix : ''); + } + + return { + id: id, + isId: isId, + obj: obj, + path: path + }; +})); diff --git a/test/tests/integration/view.js b/test/tests/integration/view.js index da16bdf2..e9ca4def 100644 --- a/test/tests/integration/view.js +++ b/test/tests/integration/view.js @@ -9,8 +9,8 @@ describe('view', function () { this.storeKey = '_h5ai'; this.xConfig = { setup: { - PUBLIC_HREF: util.uniqPath('-PUBLIC/'), - ROOT_HREF: util.uniqPath('-ROOT/') + PUBLIC_HREF: uniq.path('-PUBLIC/'), + ROOT_HREF: uniq.path('-ROOT/') } }; }); diff --git a/test/tests/premisses.js b/test/tests/premisses.js index 3a914a8b..df226e2a 100644 --- a/test/tests/premisses.js +++ b/test/tests/premisses.js @@ -38,44 +38,10 @@ describe('premisses', function () { assert.strictEqual(util, window.util); }); - it('util.uniqId() works', function () { + it('uniq is global object', function () { - assert.isFunction(util.uniqId); - - var uid1 = parseInt(util.uniqId().replace(/\D/g, ''), 10); - - assert.isTrue(util.isUniqId(util.uniqId())); - assert.notEqual(util.uniqId(), util.uniqId()); - assert.notDeepEqual(util.uniqId(), util.uniqId()); - assert.notStrictEqual(util.uniqId(), util.uniqId()); - - var uid2 = parseInt(util.uniqId().replace(/\D/g, ''), 10); - assert.strictEqual(uid2, uid1 + 8); - }); - - it('util.uniqObj() works', function () { - - assert.isFunction(util.uniqId); - - assert.lengthOfKeys(util.uniqObj(), 1); - assert.isTrue(util.isUniqId(util.uniqObj().uniqId)); - assert.notEqual(util.uniqObj(), util.uniqObj()); - assert.notDeepEqual(util.uniqObj(), util.uniqObj()); - assert.notStrictEqual(util.uniqObj(), util.uniqObj()); - assert.notEqual(util.uniqObj().uniqId, util.uniqObj().uniqId); - assert.notDeepEqual(util.uniqObj().uniqId, util.uniqObj().uniqId); - assert.notStrictEqual(util.uniqObj().uniqId, util.uniqObj().uniqId); - }); - - it('util.uniqPath() works', function () { - - assert.isFunction(util.uniqPath); - - assert.notEqual(util.uniqPath(), util.uniqPath()); - assert.notDeepEqual(util.uniqPath(), util.uniqPath()); - assert.notStrictEqual(util.uniqPath(), util.uniqPath()); - assert.strictEqual(util.uniqPath('abc').substr(-3), 'abc'); - assert.strictEqual(util.uniqPath('xyz/').substr(-4), 'xyz/'); + assert.isPlainObject(util); + assert.strictEqual(util, window.util); }); it('assert.isPlainObject() works', function () { diff --git a/test/tests/unit/boot.js b/test/tests/unit/boot.js index 2bfda7e1..7ee4c0fa 100644 --- a/test/tests/unit/boot.js +++ b/test/tests/unit/boot.js @@ -10,7 +10,7 @@ describe('module \'' + ID + '\'', function () { this.definition = modulejs._private.definitions[ID]; - this.xConfig = util.uniqObj(); + this.xConfig = uniq.obj(); this.xDefine = sinon.stub(modulejs, 'define'); this.xRequire = sinon.stub(modulejs, 'require'); this.xServer = { diff --git a/test/tests/unit/config.js b/test/tests/unit/config.js index 18a166fc..2bfffb15 100644 --- a/test/tests/unit/config.js +++ b/test/tests/unit/config.js @@ -53,7 +53,7 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); assert.isPlainObject(instance); assert.lengthOfKeys(instance, 1); - assert.isTrue(util.isUniqId(instance.uniqId)); + assert.isTrue(uniq.isId(instance._uniq_id)); }); }); }); diff --git a/test/tests/unit/core/langs.js b/test/tests/unit/core/langs.js index 8946341c..47bda5bb 100644 --- a/test/tests/unit/core/langs.js +++ b/test/tests/unit/core/langs.js @@ -10,7 +10,7 @@ describe('module \'' + ID + '\'', function () { this.definition = modulejs._private.definitions[ID]; - this.xConfig = {langs: util.uniqObj()}; + this.xConfig = {langs: uniq.obj()}; this.applyFn = function () { return this.definition.fn(_, this.xConfig); diff --git a/test/tests/unit/core/location.js b/test/tests/unit/core/location.js index 5d1809e1..c4bd839b 100644 --- a/test/tests/unit/core/location.js +++ b/test/tests/unit/core/location.js @@ -242,7 +242,7 @@ describe('module \'' + ID + '\'', function () { var $el = $(''); var item = { - absHref: util.uniqId(), + absHref: uniq.id(), isManaged: false, isFolder: sinon.stub().returns(false) }; @@ -259,7 +259,7 @@ describe('module \'' + ID + '\'', function () { var $el = $(''); var item = { - absHref: util.uniqId(), + absHref: uniq.id(), isManaged: false, isFolder: sinon.stub().returns(true) }; @@ -276,7 +276,7 @@ describe('module \'' + ID + '\'', function () { var $el = $(''); var item = { - absHref: util.uniqId(), + absHref: uniq.id(), isManaged: true, isFolder: sinon.stub().returns(true) }; @@ -293,7 +293,7 @@ describe('module \'' + ID + '\'', function () { var $el = $(''); var item = { - absHref: util.uniqId(), + absHref: uniq.id(), isManaged: true, isFolder: sinon.stub().returns(true) }; diff --git a/test/tests/unit/core/resource.js b/test/tests/unit/core/resource.js index 79144636..c175a559 100644 --- a/test/tests/unit/core/resource.js +++ b/test/tests/unit/core/resource.js @@ -16,7 +16,7 @@ describe('module \'' + ID + '\'', function () { b: 'myTheme/b.jpg' } }; - this.xSettings = {publicHref: util.uniqPath('/publicHref/')}; + this.xSettings = {publicHref: uniq.path('/publicHref/')}; this.applyFn = function () { return this.definition.fn(_, this.xConfig, this.xSettings); @@ -62,7 +62,7 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); assert.isPlainObject(instance); - assert.lengthOf(_.keys(instance), 2); + assert.lengthOfKeys(instance, 2); }); }); diff --git a/test/tests/unit/core/server.js b/test/tests/unit/core/server.js index 532d2de2..badb0076 100644 --- a/test/tests/unit/core/server.js +++ b/test/tests/unit/core/server.js @@ -81,7 +81,7 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); assert.isPlainObject(instance); - assert.lengthOf(_.keys(instance), 2); + assert.lengthOfKeys(instance, 2); }); }); @@ -97,8 +97,8 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); - var xData = util.uniqObj(); - var xResult = util.uniqObj(); + var xData = uniq.obj(); + var xResult = uniq.obj(); var spy = sinon.spy(); var res = instance.request(xData, spy); @@ -124,7 +124,7 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); - var xData = util.uniqObj(); + var xData = uniq.obj(); var spy = sinon.spy(); var res = instance.request(xData, spy); @@ -160,8 +160,8 @@ describe('module \'' + ID + '\'', function () { var instance = this.applyFn(); var xData = { - a: util.uniqId(), - b: util.uniqId() + a: uniq.id(), + b: uniq.id() }; var res = instance.formRequest(xData); diff --git a/test/tests/unit/core/settings.js b/test/tests/unit/core/settings.js index c34a6061..bf5ca696 100644 --- a/test/tests/unit/core/settings.js +++ b/test/tests/unit/core/settings.js @@ -12,13 +12,13 @@ describe('module \'' + ID + '\'', function () { this.xConfig = { options: { - someOptions: util.uniqObj(), - otherOptions: util.uniqObj(), - more: util.uniqObj() + someOptions: uniq.obj(), + otherOptions: uniq.obj(), + more: uniq.obj() }, setup: { - PUBLIC_HREF: util.uniqId(), - ROOT_HREF: util.uniqId() + PUBLIC_HREF: uniq.id(), + ROOT_HREF: uniq.id() } }; this.applyFn = function () { diff --git a/test/tests/unit/main/info.js b/test/tests/unit/main/info.js index 36381fd6..a2025779 100644 --- a/test/tests/unit/main/info.js +++ b/test/tests/unit/main/info.js @@ -12,7 +12,7 @@ describe('module \'' + ID + '\'', function () { this.xConfig = { setup: { - VERSION: util.uniqId() + VERSION: uniq.id() }, options: {} }; @@ -136,7 +136,7 @@ describe('module \'' + ID + '\'', function () { it('login works', function () { - var pass = util.uniqId(); + var pass = uniq.id(); var expectedData = { action: 'login', pass: pass diff --git a/test/tests/unit/model/item.js b/test/tests/unit/model/item.js index 650b054a..07d1a832 100644 --- a/test/tests/unit/model/item.js +++ b/test/tests/unit/model/item.js @@ -10,19 +10,19 @@ describe('module \'' + ID + '\'', function () { this.definition = modulejs._private.definitions[ID]; - this.xRootName = util.uniqId(); + this.xRootName = uniq.id(); this.xTypes = { - getType: sinon.stub().returns(util.uniqId()) + getType: sinon.stub().returns(uniq.id()) }; - this.xEvent = util.uniqObj(); + this.xEvent = uniq.obj(); this.xSettings = { - rootHref: util.uniqPath('/' + this.xRootName + '/') + rootHref: uniq.path('/' + this.xRootName + '/') }; - this.xServer = util.uniqObj(); + this.xServer = uniq.obj(); this.xLocation = { forceEncoding: sinon.stub().returnsArg(0), - getDomain: sinon.stub().returns(util.uniqId()), - getAbsHref: sinon.stub().returns(util.uniqId()) + getDomain: sinon.stub().returns(uniq.id()), + getAbsHref: sinon.stub().returns(uniq.id()) }; this.applyFn = function () { diff --git a/test/tests/unit/view/sidebar.js b/test/tests/unit/view/sidebar.js index 62c21007..f7d1b591 100644 --- a/test/tests/unit/view/sidebar.js +++ b/test/tests/unit/view/sidebar.js @@ -13,8 +13,8 @@ describe('module \'' + ID + '\'', function () { this.xResource = { image: sinon.stub().throws('invalid image request') }; - this.xResource.image.withArgs('back').returns(util.uniqPath('-back.png')); - this.xResource.image.withArgs('sidebar').returns(util.uniqPath('-sidebar.png')); + this.xResource.image.withArgs('back').returns(uniq.path('-back.png')); + this.xResource.image.withArgs('sidebar').returns(uniq.path('-sidebar.png')); this.xStore = { get: sinon.stub().returns(false), put: sinon.stub() diff --git a/test/tests/unit/view/view.js b/test/tests/unit/view/view.js index 46137934..2009bf0e 100644 --- a/test/tests/unit/view/view.js +++ b/test/tests/unit/view/view.js @@ -16,14 +16,14 @@ describe('module \'' + ID + '\'', function () { }; this.xFormat = { setDefaultMetric: sinon.stub(), - formatDate: sinon.stub().returns(util.uniqId()), - formatSize: sinon.stub().returns(util.uniqId()) + formatDate: sinon.stub().returns(uniq.id()), + formatSize: sinon.stub().returns(uniq.id()) }; this.xLocation = { - setLink: sinon.stub().returns(util.uniqId()) + setLink: sinon.stub().returns(uniq.id()) }; this.xResource = { - icon: sinon.stub().returns(util.uniqId()) + icon: sinon.stub().returns(uniq.id()) }; this.xSettings = {view: { binaryPrefix: false, @@ -253,7 +253,7 @@ describe('module \'' + ID + '\'', function () { it('sets correct class to #view-hint', function () { - var key = util.uniqId(); + var key = uniq.id(); var instance = this.applyFn(); instance.setHint(key); assert.strictEqual($('#view-hint').attr('class'), 'l10n-' + key); @@ -261,7 +261,7 @@ describe('module \'' + ID + '\'', function () { it('removes all other classes from #view-hint', function () { - var key = util.uniqId(); + var key = uniq.id(); var instance = this.applyFn(); $('#view-hint').addClass('a'); instance.setHint(key); @@ -407,10 +407,10 @@ describe('module \'' + ID + '\'', function () { // return { // isFolder: sinon.stub().returns(false), - // label: util.uniqId(), + // label: uniq.id(), // time: 0, // size: 0, - // type: util.uniqId(), + // type: uniq.id(), // isManaged: false, // icon: null, // isCurrentParentFolder: sinon.stub().returns(false) diff --git a/test/tests/unit/view/viewmode.js b/test/tests/unit/view/viewmode.js index 5412ee89..b117f14d 100644 --- a/test/tests/unit/view/viewmode.js +++ b/test/tests/unit/view/viewmode.js @@ -15,7 +15,7 @@ describe('module \'' + ID + '\'', function () { pub: sinon.stub() }; this.xResource = { - image: sinon.stub().returns(util.uniqPath('-image.png')) + image: sinon.stub().returns(uniq.path('-image.png')) }; this.xSettings = {view: { modeToggle: false diff --git a/test/util/assert.js b/test/util/assert.js new file mode 100644 index 00000000..f5584543 --- /dev/null +++ b/test/util/assert.js @@ -0,0 +1,17 @@ +(function () { +'use strict'; + +var assert = chai.assert; + +assert.lengthOfKeys = function (val, count, msg) { + + var keyCount = _.keys(val).length; + assert(keyCount === count, msg ? msg : 'expected ' + val + ' to have ' + count + ' keys, but has ' + keyCount); +}; + +assert.isPlainObject = function (val, msg) { + + assert($.isPlainObject(val), msg ? msg : 'expected ' + val + ' to be a plain Object'); +}; + +}()); diff --git a/test/util/chai.isPlainObject.js b/test/util/chai.isPlainObject.js deleted file mode 100644 index 5c434a5f..00000000 --- a/test/util/chai.isPlainObject.js +++ /dev/null @@ -1,18 +0,0 @@ -(function () { -'use strict'; - -chai.Assertion.addChainableMethod('isPlainObject', function () { - - this.assert( - $.isPlainObject(this._obj), - 'expected ' + this._obj + ' to be a plain Object', - 'expected ' + this._obj + ' not to be a plain Object' - ); -}); - -chai.assert.isPlainObject = function (val, msg) { - - new chai.Assertion(val, msg).to.be.isPlainObject(); -}; - -}()); diff --git a/test/util/chai.lengthOfKeys.js b/test/util/chai.lengthOfKeys.js deleted file mode 100644 index 16d42477..00000000 --- a/test/util/chai.lengthOfKeys.js +++ /dev/null @@ -1,20 +0,0 @@ -(function () { -'use strict'; - -chai.Assertion.addChainableMethod('lengthOfKeys', function (count) { - - var keyCount = _.keys(this._obj).length; - - this.assert( - keyCount === count, - 'expected ' + this._obj + ' to have ' + count + ' keys, but has ' + keyCount, - 'expected ' + this._obj + ' not to have ' + count + ' keys, but has ' + keyCount - ); -}); - -chai.assert.lengthOfKeys = function (val, count, msg) { - - new chai.Assertion(val, msg).to.be.lengthOfKeys(count); -}; - -}()); diff --git a/test/util/modulejs.js b/test/util/modulejs.js index e4bcca05..32b03d7d 100644 --- a/test/util/modulejs.js +++ b/test/util/modulejs.js @@ -11,7 +11,7 @@ function clearModulejs() { function mockConfigModule() { - modulejs.define('config', window.util.uniqObj()); + modulejs.define('config', window.uniq.obj()); } window.util = window.util || {}; diff --git a/test/util/uniq.js b/test/util/uniq.js deleted file mode 100644 index c4363733..00000000 --- a/test/util/uniq.js +++ /dev/null @@ -1,39 +0,0 @@ -(function () { -'use strict'; - -var PREFIX = 'UNIQ-'; -var SUFFIX = '-ID'; -var LENGTH = 4; -var RE = new RegExp('^' + PREFIX + '\\d{' + LENGTH + '}' + SUFFIX + '$'); - -var counter = 0; - -function uniqId() { - - counter += 1; - return PREFIX + ('00000000' + counter).substr(-LENGTH) + SUFFIX; -} - -function isUniqId(uid) { - - return RE.test(uid); -} - -function uniqObj() { - - return {uniqId: uniqId()}; -} - -function uniqPath(suffix) { - - return '/some/path/' + uniqId() + (suffix ? suffix : ''); -} - - -window.util = window.util || {}; -window.util.uniqId = uniqId; -window.util.isUniqId = isUniqId; -window.util.uniqObj = uniqObj; -window.util.uniqPath = uniqPath; - -}());