Skip to content

Commit

Permalink
Expanded test coverage and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Download committed Sep 2, 2015
1 parent d2b1047 commit 4088c3b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ var memoryStorage = new MemoryStorage('my-app');

## Use it
```javascript
memoryStorage.setItem('My string', 'Hello MemoryStorage!');
memoryStorage.setItem('My object', JSON.stringify({my: 'object'}));
memoryStorage.setItem('myString', 'Hello MemoryStorage!');
memoryStorage.myObject = JSON.stringify({my: 'object'}));
alert(memoryStorage.getItem('My string')); // alerts 'Hello MemoryStorage!'
alert(memoryStorage['My string']); // alerts 'Hello MemoryStorage!'
alert(memoryStorage.length); // alerts '2'
alert(memoryStorage.key(1)); // alerts 'My object'
memoryStorage.removeItem('My string');
Expand All @@ -39,4 +40,22 @@ memoryStorage.clear();
alert(memoryStorage.length); // alerts '0'
```

## Beyond the Web Storage API
MemoryStorage is type-agnosic; it doesn't care about the type of data you store.
If you want to remain within the Web Storage API, you should only read and write strings,
however if you want you can store other types just as well:
```javascript
memoryStorage.myObject = {my: 'object'};
alert(memoryStorage.myObject.my); // alerts 'object'
var tree = {
nested: {
objects: {
working: 'Sure!'
}
}
}
memoryStorage.setItem('tree', tree);
alert(memoryStorage.tree.nested.objects.working); // alerts 'Sure!'
```


3 changes: 1 addition & 2 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
<body>
<a style="font-size:xx-large;" href="test-memorystorage.html">Restart</a>
<h1>MemoryStorage Tests</h1>
<h3>W3C Web Storage API Compliance Test</h3>
<a href="test.js">Test code</a>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="test.js"></script>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
Expand Down
27 changes: 27 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,34 @@ QUnit.test("W3C Web Storage API Compliance Test", function( assert ) {
assert.ok(store.getItem('test0')===undefined, "no values in cleared store");
});

QUnit.test("Multiple Instances Test", function( assert ) {
var store1 = new MemoryStorage('local');
var store2 = new MemoryStorage('local');
store1.clear();
store1.setItem('test0', 'data0');
assert.ok(store2.getItem('test0') === store1.getItem('test0'), "Item added to store1 is also visible in store2");
store1['test0'] = 'changed';
assert.ok((store2.getItem('test0') === store1.getItem('test0')) && store1.getItem('test0') === 'changed', "Item changed in store1 is also changed in store2");
store1['test1'] = 'data1';
assert.ok(store2.length === store1.length, "Store lengths remain consistent");
for (var i=0; i<store1.length; i++) {
assert.ok(store1.key(i) === store2.key(i), 'Order of keys is consistent across stores');
assert.ok(store1[store1.key(i)] === store2.getItem(store2.key(i)), 'Order and contents of values are consistent across stores');
}
store1.clear();
assert.ok(store2.length===0, "Clearing store1 also clears store2");
});


QUnit.test("Beyond W3C API Test", function( assert ) {
var store = new MemoryStorage('local');
store.clear();
store.my = {object: 'Yes!'};
assert.ok(typeof store.getItem('my') === 'object', 'Object returned with getItem when object was stored');
assert.ok(store.getItem('my').object === 'Yes!', 'Contents of objects survive storing/retrieving');
store.tree = {nested: {objects: {works: 'Sure!'}}};
assert.ok(store.tree.nested.objects.works === 'Sure!', 'Deep nested trees stored correctly');
store.clear();
});


0 comments on commit 4088c3b

Please sign in to comment.