Browse Source

put tests together

master
AJ ONeal 13 years ago
parent
commit
b75038ca44
  1. 1
      .gitignore
  2. 27
      test/test.js
  3. 2
      test/test2.js
  4. 77
      tests/node_modules/json-storage/json-storage.js
  5. 17
      tests/node_modules/json-storage/package.json
  6. 54
      tests/node_modules/localStorage/localStorage.js
  7. 16
      tests/node_modules/localStorage/package.json

1
.gitignore

@ -0,0 +1 @@
node_modules

27
test/test.js

@ -0,0 +1,27 @@
(function () {
"use strict";
var localStorage = require('localStorage')
, JsonStorage = require('json-storage')
, db = JsonStorage(localStorage)
, assert = require('assert')
;
assert.equal(null, db.get('x'));
assert.deepEqual([], db.keys());
db.clear();
assert.equal(null, db.get('x'));
db.set('a', 'b');
assert.deepEqual(['a'], db.keys());
assert.equal('b', db.get('a'));
db.remove('a');
assert.deepEqual([], db.keys());
db.set('a', 'b');
db.clear();
assert.deepEqual([], db.keys());
console.log("Done! All tests pass.");
}());

2
tests/test.js → test/test2.js

@ -25,4 +25,6 @@
db.set('a', { 'a': [1] });
assert.deepEqual({'a': [1] }, db.get('a'));
console.log('[PASS] no assertions failed');
}());

77
tests/node_modules/json-storage/json-storage.js

@ -1,77 +0,0 @@
(function () {
"use strict";
var db;
function Stringify(obj) {
var str;
try {
str = JSON.stringify(obj);
} catch(e) {
str = "";
}
return str;
}
function Parse(str) {
var obj = null;
try {
obj = JSON.parse(str);
} catch(e) {}
return obj;
}
function JsonStorage(w3cStorage) {
this.db = w3cStorage;
this.keysAreDirty = true;
}
db = JsonStorage;
db.prototype.clear = function () {
this.keysAreDirty = true;
self.keys().forEach(function (uuid) {
this.remove(uuid);
}, this);
};
db.prototype.remove = function (uuid) {
this.keysAreDirty = true;
this.db.removeItem(uuid);
};
db.prototype.get = function (uuid) {
return Parse(this.db.getItem(uuid));
};
db.prototype.set = function (uuid, val) {
this.keysAreDirty = true;
return this.db.setItem(uuid, Stringify(val));
};
db.prototype.keys = function () {
var i;
if (!this.keysAreDirty) {
return this.keys;
}
this.keys = [];
for (i = 0; i < this.db.length; i += 1) {
this.keys.push(this.db.key(i));
}
this.keysAreDirty = false;
return this.keys.concat([]);
};
db.prototype.size = function () {
return this.db.length;
};
function create(w3cStorage) {
return new JsonStorage(w3cStorage);
}
module.exports = create;
}());

17
tests/node_modules/json-storage/package.json

@ -1,17 +0,0 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "json-storage",
"description": "A wrapper for storage engines which use the W3C Storage API",
"keywords": ["ender", "localStorage", "sessionStorage", "globalStorage", "Storage"],
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/json-storage-js.git"
},
"engines": {
"node": ">= v0.2.0"
},
"main": "json-storage.js",
"dependencies": {},
"devDependencies": {}
}

54
tests/node_modules/localStorage/localStorage.js

@ -1,54 +0,0 @@
// http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm
// NOTE:
// this varies from actual localStorage in some subtle ways
// also, there is no persistence
// TODO persist
(function () {
"use strict";
var db;
function LocalStorage() {
}
db = LocalStorage;
db.prototype.getItem = function (key) {
if (key in this) {
return String(this[key]);
}
return null;
};
db.prototype.setItem = function (key, val) {
this[key] = String(val);
};
db.prototype.removeItem = function (key) {
delete this[key];
};
db.prototype.clear = function () {
var self = this;
Object.keys(self).forEach(function (key) {
self[key] = undefined;
delete self[key];
});
};
db.prototype.key = function (i) {
i = i || 0;
return Object.keys(this)[i];
};
db.prototype.__defineGetter__('length', function () {
return Object.keys(this).length;
});
if (global.localStorage) {
module.exports = localStorage;
} else {
module.exports = new LocalStorage();
}
}());

16
tests/node_modules/localStorage/package.json

@ -1,16 +0,0 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "localStorage",
"description": "W3C localStorage for Node.JS",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/node-localStorage.git"
},
"engines": {
"node": ">= v0.2.0"
},
"main": "localStorage.js",
"dependencies": {},
"devDependencies": {}
}
Loading…
Cancel
Save