| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | (function () { | 
					
						
							|  |  |  |   "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var assert = require('assert') | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     , fs = require('fs') | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |     , Storage = require('../') | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     , dbPath = './db.json' | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  |     ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |   function runTest(storage) { | 
					
						
							| 
									
										
										
										
											2013-01-26 02:24:05 -07:00
										 |  |  |     // should not return prototype properties
 | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |     assert.strictEqual(null, storage.getItem('key')); | 
					
						
							| 
									
										
										
										
											2013-01-26 02:24:05 -07:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     assert.strictEqual(0, Object.keys(storage).length); | 
					
						
							|  |  |  |     assert.strictEqual(0, storage.length); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     // can't make assuptions about key positioning
 | 
					
						
							|  |  |  |     storage.setItem('a', 1); | 
					
						
							|  |  |  |     assert.strictEqual(storage.key(0), 'a'); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     storage.setItem('b', '2'); | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |     assert.strictEqual(storage.getItem('a'), 1); | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     assert.strictEqual(storage.getItem('b'), '2'); | 
					
						
							|  |  |  |     assert.strictEqual(storage.length, 2); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     assert.strictEqual(storage['c'], undefined); | 
					
						
							|  |  |  |     assert.strictEqual(storage.getItem('c'), null); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     storage.setItem('c'); | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |     assert.strictEqual(storage.getItem('c'), null); | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     assert.strictEqual(storage.length, 3); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     storage.removeItem('c'); | 
					
						
							|  |  |  |     assert.strictEqual(storage.getItem('c'), null); | 
					
						
							|  |  |  |     assert.strictEqual(storage.length, 2); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |     storage.clear(); | 
					
						
							|  |  |  |     assert.strictEqual(storage.getItem('a'), null); | 
					
						
							|  |  |  |     assert.strictEqual(storage.getItem('b'), null); | 
					
						
							|  |  |  |     assert.strictEqual(storage.length, 0); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function runAll() { | 
					
						
							| 
									
										
										
										
											2014-03-08 14:55:21 -07:00
										 |  |  |     var localStorage = new Storage(dbPath, { strict: false, ws: '  ' }) | 
					
						
							|  |  |  |       , sessionStorage = new Storage(null, { strict: false }) | 
					
						
							| 
									
										
										
										
											2012-08-16 13:15:28 -06:00
										 |  |  |       ; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     runTest(sessionStorage); | 
					
						
							|  |  |  |     runTest(localStorage); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     localStorage.setItem('a', 1); | 
					
						
							|  |  |  |     setTimeout(function () { | 
					
						
							|  |  |  |       assert.deepEqual({ a: 1 }, JSON.parse(fs.readFileSync(dbPath))); | 
					
						
							|  |  |  |       console.log('All tests passed'); | 
					
						
							|  |  |  |     }, 100); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   fs.unlink(dbPath, runAll); | 
					
						
							| 
									
										
										
										
											2011-07-22 13:21:23 -06:00
										 |  |  | }()); |