Util functions, specs for Class & Util

This commit is contained in:
mourner 2010-09-02 16:14:25 +03:00
parent 2d455764c7
commit 95de45ed8c
5 changed files with 130 additions and 1 deletions

28
spec/runner.html Normal file
View File

@ -0,0 +1,28 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Jasmine Test Runner</title>
<link rel="stylesheet" type="text/css" href="../lib/jasmine/jasmine.css">
<script type="text/javascript" src="../lib/jasmine/jasmine.js"></script>
<script type="text/javascript" src="../lib/jasmine/jasmine-html.js"></script>
<!-- include source files here... -->
<script type="text/javascript" src="../src/env.js"></script>
<script type="text/javascript" src="../src/util/Util.js"></script>
<script type="text/javascript" src="../src/util/Class.js"></script>
<!-- include spec files here... -->
<script type="text/javascript" src="suites/UtilSpec.js"></script>
<script type="text/javascript" src="suites/ClassSpec.js"></script>
</head>
<body>
<script type="text/javascript">
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
</script>
</body>
</html>

27
spec/suites/ClassSpec.js Normal file
View File

@ -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();
});
});
});

48
spec/suites/UtilSpec.js Normal file
View File

@ -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);
});
});
});

View File

@ -2,9 +2,10 @@
* Class powers the OOP facilities of the library. * Class powers the OOP facilities of the library.
*/ */
L.Class = function() {}; L.Class = function() {};
L.Class.extend = function(props) { L.Class.extend = function(props) {
debugger;
var _super = this.prototype, statics; var _super = this.prototype, statics;
// instantiate class without calling constructor // instantiate class without calling constructor

25
src/util/Util.js Normal file
View File

@ -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);
};
};