2014-03-11 06:26:19 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var utils = source("utils");
|
2014-02-28 06:52:25 +08:00
|
|
|
|
|
|
|
describe("Utils", function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
describe("Monkey-patches", function() {
|
|
|
|
describe("Number", function() {
|
|
|
|
describe("#seconds", function() {
|
|
|
|
it("allows for expressing time in seconds", function() {
|
|
|
|
expect((5).seconds()).to.be.eql(5000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#second", function() {
|
|
|
|
it("allows for expressing time in seconds", function() {
|
|
|
|
expect((1).second()).to.be.eql(1000);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#fromScale", function() {
|
|
|
|
it("converts a value from one scale to 0-1 scale", function() {
|
|
|
|
expect((5).fromScale(0, 10)).to.be.eql(0.5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("converts floats", function() {
|
|
|
|
expect(2.5.fromScale(0, 10)).to.be.eql(0.25);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#toScale", function() {
|
|
|
|
it("converts a value from 0-1 scale to another", function() {
|
|
|
|
expect((0.5).toScale(0, 10)).to.be.eql(5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("converts to floats", function() {
|
|
|
|
expect(0.25.toScale(0, 10)).to.be.eql(2.5);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("can be chained with #fromScale", function() {
|
|
|
|
var num = (5).fromScale(0, 20).toScale(0, 10);
|
|
|
|
expect(num).to.be.eql(2.5);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#every", function() {
|
|
|
|
before(function() {
|
|
|
|
this.clock = sinon.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function() {
|
|
|
|
this.clock.restore();
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
it("sets a function to be called every time an interval passes", function() {
|
|
|
|
var func = spy();
|
|
|
|
utils.every(10, func);
|
|
|
|
this.clock.tick(25);
|
|
|
|
expect(func).to.be.calledTwice;
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
2014-03-11 06:26:19 +08:00
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
describe("#after", function() {
|
|
|
|
before(function() {
|
|
|
|
this.clock = sinon.useFakeTimers();
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
after(function() {
|
|
|
|
this.clock.restore();
|
2013-10-25 05:25:42 +08:00
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
it("sets a function to be called after time an interval passes", function() {
|
|
|
|
var func = spy();
|
|
|
|
utils.after(10, func);
|
|
|
|
this.clock.tick(15);
|
|
|
|
expect(func).to.be.called;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("constantly", function() {
|
|
|
|
before(function() {
|
|
|
|
stub(global, 'every').returns(0);
|
2013-10-29 03:07:56 +08:00
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
after(function() {
|
|
|
|
global.every.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("schedules a task to run continuously with #every", function() {
|
|
|
|
var func = function() {};
|
|
|
|
utils.constantly(func);
|
|
|
|
|
|
|
|
expect(global.every).to.be.calledWith(0, func);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#subclass", function() {
|
|
|
|
var BaseClass = (function() {
|
|
|
|
function BaseClass(opts) {
|
|
|
|
this.greeting = opts.greeting;
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseClass.prototype.sayHi = function() {
|
|
|
|
return "Hi!";
|
|
|
|
};
|
|
|
|
|
|
|
|
return BaseClass
|
|
|
|
})();
|
|
|
|
|
|
|
|
var SubClass = (function(klass) {
|
2014-06-06 03:11:37 +08:00
|
|
|
utils.subclass(SubClass, klass);
|
2014-03-11 06:26:19 +08:00
|
|
|
|
|
|
|
function SubClass(opts) {
|
|
|
|
SubClass.__super__.constructor.apply(this, arguments);
|
|
|
|
};
|
|
|
|
|
|
|
|
return SubClass;
|
|
|
|
})(BaseClass);
|
|
|
|
|
|
|
|
it("adds inheritance to Javascript classes", function() {
|
|
|
|
var sub = new SubClass({greeting: "Hello World"});
|
|
|
|
expect(sub.greeting).to.be.eql("Hello World");
|
|
|
|
expect(sub.sayHi()).to.be.eql("Hi!");
|
2014-02-20 07:52:47 +08:00
|
|
|
});
|
2013-10-25 05:25:42 +08:00
|
|
|
});
|
|
|
|
|
2014-02-28 06:52:25 +08:00
|
|
|
describe("#proxyFunctionsToObject", function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
var methods = ['asString', 'toString', 'returnString'];
|
2014-02-28 09:39:02 +08:00
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
var ProxyClass = (function() {
|
2014-02-28 06:52:25 +08:00
|
|
|
function ProxyClass() {}
|
|
|
|
|
|
|
|
ProxyClass.prototype.asString = function() {
|
|
|
|
return "[object ProxyClass]";
|
|
|
|
};
|
|
|
|
|
|
|
|
ProxyClass.prototype.toString = function() {
|
|
|
|
return "[object ProxyClass]";
|
|
|
|
};
|
|
|
|
|
|
|
|
ProxyClass.prototype.returnString = function(string) {
|
|
|
|
return string;
|
|
|
|
};
|
|
|
|
|
|
|
|
return ProxyClass;
|
|
|
|
})();
|
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
var TestClass = (function() {
|
2014-02-28 06:52:25 +08:00
|
|
|
function TestClass() {
|
|
|
|
this.self = this;
|
|
|
|
this.testInstance = new ProxyClass;
|
2014-06-06 03:11:37 +08:00
|
|
|
utils.proxyFunctionsToObject(methods, this.testInstance, this.self, true);
|
2014-02-28 06:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return TestClass;
|
|
|
|
})();
|
|
|
|
|
2014-03-11 06:26:19 +08:00
|
|
|
var testclass = new TestClass();
|
|
|
|
|
2014-02-28 06:52:25 +08:00
|
|
|
it('can alias methods', function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
expect(testclass.asString()).to.be.eql("[object ProxyClass]");
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can alias existing methods if forced to', function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
expect(testclass.toString()).to.be.eql("[object ProxyClass]");
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can alias methods with arguments', function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
expect(testclass.returnString).to.be.a('function');
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("#proxyTestStubs", function() {
|
|
|
|
it("proxies methods to an object's commandList", function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
var methods = ["hello", "goodbye"],
|
|
|
|
base = { commandList: [] };
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
utils.proxyTestStubs(methods, base);
|
2014-02-28 06:52:25 +08:00
|
|
|
expect(base.commandList).to.be.eql(methods);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns the object methods have been proxied to", function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
var methods = ["hello", "goodbye"],
|
|
|
|
base = { commandList: [] };
|
|
|
|
|
2014-06-06 03:11:37 +08:00
|
|
|
expect(utils.proxyTestStubs(methods, base)).to.be.eql(base);
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|
|
|
|
});
|
2014-03-01 02:31:10 +08:00
|
|
|
|
|
|
|
describe("#bind", function() {
|
2014-03-11 06:26:19 +08:00
|
|
|
var me = { hello: "Hello World" },
|
|
|
|
proxy = { boundMethod: function() { return this.hello; } };
|
2014-03-01 02:31:10 +08:00
|
|
|
|
|
|
|
it("binds the 'this' scope for the method", function() {
|
|
|
|
proxy.boundMethod = function() { return this.hello; };
|
2014-06-06 03:11:37 +08:00
|
|
|
proxy.boundMethod = utils.bind(proxy.boundMethod, me);
|
2014-03-01 02:31:10 +08:00
|
|
|
|
|
|
|
expect(proxy.boundMethod()).to.eql("Hello World");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("passes arguments along to bound functions", function() {
|
|
|
|
proxy.boundMethod = function(hello, world) { return [hello, world]; };
|
2014-06-06 03:11:37 +08:00
|
|
|
proxy.boundMethod = utils.bind(proxy.boundMethod, me);
|
2014-03-01 02:31:10 +08:00
|
|
|
|
|
|
|
expect(proxy.boundMethod("Hello", "World")).to.eql(["Hello", "World"]);
|
|
|
|
})
|
|
|
|
});
|
2014-02-28 06:52:25 +08:00
|
|
|
});
|