Put insert and require into own util module; Fix running tests using grunt-contrib-qunit

This commit is contained in:
Martin Kleinschrodt 2013-11-11 18:23:31 +01:00
parent a070cbeffd
commit abfc4a01a9
3 changed files with 38 additions and 32 deletions

View File

@ -1,15 +1,4 @@
define(["safe/crypto"], function(crypto) {
function insert(arr, rec, i) {
i = i || 0;
return arr.slice(0, i).concat(rec).concat(arr.slice(i));
}
function remove(arr, from, to) {
from = Math.max(from, 0);
to = Math.max(from, to || 0);
return arr.slice(0, from).concat(arr.slice(to + 1));
}
define(["safe/crypto", "safe/util"], function(crypto, util) {
var record = Object.create({}, {
name: {
enumerable: true,
@ -61,7 +50,7 @@ define(["safe/crypto"], function(crypto) {
},
add: {
value: function(rec, at) {
this.records = insert(this.records, rec, at);
this.records = util.insert(this.records, rec, at);
}
},
remove: {
@ -74,7 +63,7 @@ define(["safe/crypto"], function(crypto) {
},
removeAt: {
value: function(from, to) {
remove(this.records, from, to);
util.remove(this.records, from, to);
}
}
});
@ -95,10 +84,6 @@ define(["safe/crypto"], function(crypto) {
});
return {
_private: {
insert: insert,
remove: remove
},
record: record,
collection: collection,
store: store

17
src/util.js Normal file
View File

@ -0,0 +1,17 @@
define(function() {
function insert(arr, rec, i) {
i = i || 0;
return arr.slice(0, i).concat(rec).concat(arr.slice(i));
}
function remove(arr, from, to) {
from = Math.max(from, 0);
to = Math.max(from, to || 0);
return arr.slice(0, from).concat(arr.slice(to + 1));
}
return {
insert: insert,
remove: remove
};
});

View File

@ -1,4 +1,6 @@
require(["safe/crypto", "safe/model"], function(crypto, model) {
QUnit.config.autostart = false;
require(["safe/crypto", "safe/util", "safe/model"], function(crypto, util, model) {
module("safe/crypto");
test("key generation", function() {
@ -42,49 +44,51 @@ require(["safe/crypto", "safe/model"], function(crypto, model) {
equal(dec, pt);
});
module("safe/model");
module("safe/util");
test("insert (private)", function() {
test("insert", function() {
// Insert single element at the correct position
var a = model._private.insert([0, 1, 2, 3, 4, 5], "a", 2);
var a = util.insert([0, 1, 2, 3, 4, 5], "a", 2);
deepEqual(a, [0, 1, "a", 2, 3, 4, 5]);
// Insert mutliple elements at the correct position
var b = model._private.insert([0, 1, 2, 3, 4, 5], ["hello", "world"], 3);
var b = util.insert([0, 1, 2, 3, 4, 5], ["hello", "world"], 3);
deepEqual(b, [0, 1, 2, "hello", "world", 3, 4, 5]);
// For negative indexes, count from the end backwards
var c = model._private.insert([0, 1, 2, 3, 4, 5], "a", -2);
var c = util.insert([0, 1, 2, 3, 4, 5], "a", -2);
deepEqual(c, [0, 1, 2, 3, "a", 4, 5]);
// Index should default to 0
var d = model._private.insert([0, 1, 2, 3, 4, 5], "a");
var d = util.insert([0, 1, 2, 3, 4, 5], "a");
deepEqual(d, ["a", 0, 1, 2, 3, 4, 5]);
// An out-of-range index should result in the value being inserted at the end
var e = model._private.insert([0, 1, 2, 3, 4, 5], "a", 9);
var e = util.insert([0, 1, 2, 3, 4, 5], "a", 9);
deepEqual(e, [0, 1, 2, 3, 4, 5, "a"]);
});
test("remove (private)", function() {
test("remove", function() {
// Remove single element
var a = model._private.remove(["a", "b", "c", "d", "e"], 3);
var a = util.remove(["a", "b", "c", "d", "e"], 3);
deepEqual(a, ["a", "b", "c", "e"]);
// Remove a range of elements
var b = model._private.remove(["a", "b", "c", "d", "e"], 1, 3);
var b = util.remove(["a", "b", "c", "d", "e"], 1, 3);
deepEqual(b, ["a", "e"]);
// If upper bound is smaller then lower bound, ignore it
var c = model._private.remove(["a", "b", "c", "d", "e"], 1, -1);
var c = util.remove(["a", "b", "c", "d", "e"], 1, -1);
deepEqual(c, ["a", "c", "d", "e"]);
// If upper bound is bigger than the length of the list, remove everything up to the end
var d = model._private.remove(["a", "b", "c", "d", "e"], 1, 10);
var d = util.remove(["a", "b", "c", "d", "e"], 1, 10);
deepEqual(d, ["a"]);
// If lower bound is out-of-range, return a simple copy
var e = model._private.remove(["a", "b", "c", "d", "e"], 10);
var e = util.remove(["a", "b", "c", "d", "e"], 10);
deepEqual(e, ["a", "b", "c", "d", "e"]);
});
QUnit.start();
});