From 95de45ed8c08a2d52fd17f552178e3ae4f2b9020 Mon Sep 17 00:00:00 2001 From: mourner Date: Thu, 2 Sep 2010 16:14:25 +0300 Subject: [PATCH] Util functions, specs for Class & Util --- spec/runner.html | 28 +++++++++++++++++++++++ spec/suites/ClassSpec.js | 27 ++++++++++++++++++++++ spec/suites/UtilSpec.js | 48 ++++++++++++++++++++++++++++++++++++++++ src/util/Class.js | 3 ++- src/util/Util.js | 25 +++++++++++++++++++++ 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 spec/runner.html create mode 100644 spec/suites/ClassSpec.js create mode 100644 spec/suites/UtilSpec.js create mode 100644 src/util/Util.js diff --git a/spec/runner.html b/spec/runner.html new file mode 100644 index 00000000..758c1236 --- /dev/null +++ b/spec/runner.html @@ -0,0 +1,28 @@ + + + + Jasmine Test Runner + + + + + + + + + + + + + + + + + + + + diff --git a/spec/suites/ClassSpec.js b/spec/suites/ClassSpec.js new file mode 100644 index 00000000..506fda56 --- /dev/null +++ b/spec/suites/ClassSpec.js @@ -0,0 +1,27 @@ +describe("Class", function() { + + describe("#extend", function() { + var Klass; + + it("should create a class with the given constructor & properties", function() { + var constructor = jasmine.createSpy('constructor'), + method = jasmine.createSpy('method'); + + Klass = L.Class.extend({ + initialize: constructor, + foo: 5, + bar: method + }); + + var a = new Klass(); + + expect(constructor).toHaveBeenCalled(); + expect(a.foo).toEqual(5); + + a.bar(); + + expect(method).toHaveBeenCalled(); + }); + + }); +}); \ No newline at end of file diff --git a/spec/suites/UtilSpec.js b/spec/suites/UtilSpec.js new file mode 100644 index 00000000..cadf79bb --- /dev/null +++ b/spec/suites/UtilSpec.js @@ -0,0 +1,48 @@ +describe('L.Util', function() { + + describe('#extend', function() { + var a; + + beforeEach(function() { + a = { + foo: 5, + bar: 'asd' + }; + }); + + it('should extend the first argument with the properties of the second', function() { + L.Util.extend(a, { + bar: 7, + baz: 3 + }); + + expect(a).toEqual({ + foo: 5, + bar: 7, + baz: 3 + }); + }); + + it('should work with more than 2 arguments', function() { + L.Util.extend(a, {bar: 7}, {baz: 3}); + + expect(a).toEqual({ + foo: 5, + bar: 7, + baz: 3 + }); + }); + }); + + describe('#bind', function() { + it('should return the given function with the given context', function() { + var fn = function() { + return this; + }; + + var fn2 = L.Util.bind(fn, 5); + + expect(fn2()).toEqual(5); + }); + }); +}); \ No newline at end of file diff --git a/src/util/Class.js b/src/util/Class.js index e49b9cdd..80326aae 100644 --- a/src/util/Class.js +++ b/src/util/Class.js @@ -2,9 +2,10 @@ * Class powers the OOP facilities of the library. */ -L.Class = function() {}; +L.Class = function() {}; L.Class.extend = function(props) { + debugger; var _super = this.prototype, statics; // instantiate class without calling constructor diff --git a/src/util/Util.js b/src/util/Util.js new file mode 100644 index 00000000..df3232d3 --- /dev/null +++ b/src/util/Util.js @@ -0,0 +1,25 @@ +/* + L.Util is a namespace for various utility functions. +*/ + +L.Util = {}; + +L.Util.extend = function(dest) { // merge src properties into dest + var sources = Array.prototype.slice.call(arguments, 1), + src; + for (var j = 0, len = sources.length; j < len; j++) { + src = sources[j] || {}; + for (var i in src) { + if (src.hasOwnProperty(i)) { + dest[i] = src[i]; + } + } + } + return dest; +}; + +L.Util.bind = function(fn, obj) { + return function() { + return fn.apply(obj, arguments); + }; +}; \ No newline at end of file