Skip to content

Commit

Permalink
update test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
holyshared committed Dec 3, 2011
1 parent 6d0d862 commit 48bdd08
Show file tree
Hide file tree
Showing 7 changed files with 348 additions and 325 deletions.
85 changes: 29 additions & 56 deletions Source/CacheManager.js
Expand Up @@ -27,29 +27,23 @@ provides:

(function(global){

/**
* CacheManager
*/
var CacheManager = global.CacheManager = function (storage){

this.storage = storage;

}.implement({
var name = '';

if (Type.isString(storage) !== true){
throw new TypeError('');
}

/*
name = storage.capitalize() + 'Storage';

Example:
if (!CacheManager[name]){
throw new TypeError('');
}
this.storage = CacheManager[name];

var content = {
name: 'foo',
age: 28,
email: 'foo@example.com'
};
cacheManager.set('name=foo&age=28', content);
cacheManager.set('name=foo&age=28', content, 1 * 60 * 60 * 1000);
}.implement({

*/
set: function(key, content, limit){
var cacheTime = new Date(),
cacheContent = null;
Expand All @@ -67,17 +61,6 @@ var CacheManager = global.CacheManager = function (storage){
this.storage.setItem(key, cacheContent);
},

/*
Example:
var cache = cacheManager.get('name=foo&age=28');
if (cache.isLimit() === true) {
//remove a cache content
cache.destory();
}
*/
get: function(key){
var cache = null,
cacheContent = this.storage.getItem(key);
Expand All @@ -93,7 +76,15 @@ var CacheManager = global.CacheManager = function (storage){
});

return new CacheManager.CacheContent(cache);
}
},

remove: function(key){
this.storage.removeItem(key);
},

clear: function(){
this.storage.clear();
}

});

Expand Down Expand Up @@ -122,33 +113,12 @@ CacheManager.HashStorage = {
},
removeItem: function(key){
delete this._store[key];
},
clear: function(){
this._store = {};
}
};


/*
Example:
var content = CacheContent({
storage: storage,
key: 'name=foo&age=28',
limit: 1 * 60 * 60 * 1000 + new Date().getTime(),
content: {
foo: 'foo',
bar: 'bar'
}
});
content.getKey();
content.getStorage();
content.getLimit()
content.getContent();
content.isLimit();
content.destory();
*/

CacheManager.CacheContent = function (properties){

var instance = this, getter = null;
Expand All @@ -164,17 +134,20 @@ CacheManager.CacheContent = function (properties){
Object.merge(instance, {

isLimit: function(){
if (instance.limit > new Date().getTime()){
if (this.getLimit() > new Date().getTime()){
return false;
} else {
return true;
}
},

destroy: function(){
instance.storage.removeItem(instance.key);
}

var key = this.getKey(),
storage = this.getStorage();

storage.removeItem(key);
}

});

};
Expand Down
8 changes: 4 additions & 4 deletions Tests/jasmine/index.html
Expand Up @@ -6,18 +6,18 @@
<link rel="stylesheet" type="text/css" href="css/jasmine.css">
<script type="text/javascript" src="js/jasmine.js"></script>
<script type="text/javascript" src="js/jasmine-html.js"></script>
<script type="text/javascript" src="js/mootools-core-1.4.1.js"></script>

<script type="text/javascript" src="js/mootools-core-1.4.2.js"></script>
<script type="text/javascript" src="../../Source/CacheManager.js"></script>
<script type="text/javascript" src="spec/cache-content-spec.js"></script>
<script type="text/javascript" src="spec/cache-hash-storage-spec.js"></script>
<script type="text/javascript" src="spec/cache-session-storage-spec.js"></script>
<script type="text/javascript" src="spec/cache-local-storage-spec.js"></script>
</head>
<body>

<script type="text/javascript">
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
jasmine.getEnv().execute();
</script>

</body>
Expand Down
105 changes: 42 additions & 63 deletions Tests/jasmine/spec/cache-content-spec.js
@@ -1,82 +1,61 @@
describe("CacheContent", function() {
/*
var cache1,
cache2,
storage = {
_caches: {},
setItem: function(key, content){
this._caches[key] = content;
},
getItem: function(key){
if (!this._caches[key]){
return false;
}
return this._caches[key];
},
removeItem: function(key){
delete this._caches[key];
}
},
cacheKey = cache1.getKey(),
cacheLimit = 1 * 60 * 60 * 1000 + new Date().getTime(),
cacheContent = {
foo: 'foo',
bar: 'bar'
};
*/
beforeEach(function() {
/* cache1 = new CacheContent({
storage: storage,
key: 'name=foo&age=28',
limit: limit,
content: cacheContent
});

cache2 = new CacheContent({
storage: storage,
key: 'name=foo&age=28',
limit: new Date().getTime(),
content: cacheContent
it("storage is equal", function(){
var cache = new CacheManager.CacheContent({
storage: CacheManager.HashStorage
});
storage.setItem('name=foo&age=28', cache1);
*/
expect(cache.getStorage()).toEqual(CacheManager.HashStorage);
});

//
it("storage is equal", function() {
// expect(cache1.getStorage()).toEqual(storage);
expect(true).toEqual(true);
});
/*
it ("key is equal", function() {
expect(cache1.getKey()).toEqual('name=foo&age=28');
it("key is equal", function() {
var cache = new CacheManager.CacheContent({
key: 'foo'
});
expect(cache.getKey()).toEqual('foo');
});

it ("limit is equal", function() {
expect(cache1.getLimit()).toEqual(cacheLimit);
it("limit is equal", function() {
var cache = new CacheManager.CacheContent({
limit: 1000
});
expect(cache.getLimit()).toEqual(1000);
});

it ("content is equal", function() {
expect(cache1.getContent()).toEqual(content);
it("content is equal", function() {
var content = { name: 'foo' }
var cache = new CacheManager.CacheContent({
content: content
});
expect(cache.getContent()).toEqual(content);
});

it ("limit", function() {
expect(cache1.isLimit()).toEqual(false);
it("limit is valid", function() {
var cache = null;

cache = new CacheManager.CacheContent({
limit: new Date().getTime()
});
expect(cache.isLimit()).toEqual(true);

cache = new CacheManager.CacheContent({
limit: new Date().getTime() + (1 * 60 * 60 * 1000)
});
expect(cache.isLimit()).toEqual(false);
});

setTimeout(function(){
it("destroy", function() {

it ("limit is over", function() {
expect(cache2.isLimit()).toEqual(true);
var cache = null;

cache = new CacheManager.CacheContent({
key: 'foo',
storage: CacheManager.HashStorage,
limit: new Date().getTime() + (1 * 60 * 60 * 1000)
});
}, 100);

it ("destroy", function() {
cacheKey = cache1.getKey();
cache1.destroy();
expect(storage.getItem(cacheKey)).toEqual(false);
cache.destroy();

expect(CacheManager.HashStorage.getItem('foo')).toEqual(null);
});
*/

});

0 comments on commit 48bdd08

Please sign in to comment.