1 | var tinytest = require('tinytest')
|
2 |
|
3 | tinytest.hijackConsoleLog()
|
4 |
|
5 | var { createStore } = require('../src/store-engine')
|
6 | var { each } = require('../src/util')
|
7 | var storages = require('../storages/all')
|
8 | var allPlugins = require('../plugins/all')
|
9 | var allPluginTests = require('../plugins/all_tests')
|
10 |
|
11 | module.exports = {
|
12 | output:null,
|
13 | outputError:null,
|
14 | runTests: runTests,
|
15 | failed:false
|
16 | }
|
17 |
|
18 | function runTests() {
|
19 | setupEngineTests()
|
20 | each(storages, function(storage) {
|
21 | test.group(storage.name, function() {
|
22 | if (!_checkEnabled(storage)) {
|
23 | test.skip('disabled')
|
24 | }
|
25 | test('Storage tests', function() {
|
26 | var store = createStore(storage)
|
27 | runStorageTests(store)
|
28 | })
|
29 | each(allPluginTests, function(pluginTest, pluginName) {
|
30 | var plugin = allPlugins[pluginName]
|
31 | test.group('plugin: '+pluginName, function() {
|
32 | var store = createStore(storage, plugin)
|
33 | pluginTest.setup(store)
|
34 | })
|
35 | })
|
36 | })
|
37 | })
|
38 |
|
39 | require('./bugs/all')
|
40 |
|
41 | tinytest.runTests({
|
42 | failFast: false
|
43 | })
|
44 | }
|
45 |
|
46 | function _checkEnabled(storage) {
|
47 | if (!storage) {
|
48 | print('Skip unsupported storage:', storage.name)
|
49 | return false
|
50 | }
|
51 | var store = createStore([storage])
|
52 | if (!store.enabled) {
|
53 | print('Skip disabled storage:', storage.name)
|
54 | return false
|
55 | }
|
56 | return true
|
57 | }
|
58 |
|
59 | function setupEngineTests(store) {
|
60 | test('Addon super_fn args', function() {
|
61 | function underlyingPlugin() {
|
62 | return {
|
63 | set: function(super_fn, key, val, customArg1, customArg2) {
|
64 | assert(key == 'key'+'appended')
|
65 | assert(val == 'val')
|
66 | assert(customArg1 == 'overridden-customArg1')
|
67 | assert(customArg2 == 'customArg2')
|
68 | calls++
|
69 | }
|
70 | }
|
71 | }
|
72 | function overlyingPlugin() {
|
73 | return {
|
74 | set: function(super_fn, key, val) {
|
75 | super_fn(key+'appended', val, 'overridden-customArg1')
|
76 | calls++
|
77 | }
|
78 | }
|
79 | }
|
80 |
|
81 | var store = createStore(storages.memoryStorage, [underlyingPlugin, overlyingPlugin])
|
82 | var calls = 0
|
83 | store.set('key', 'val', 'customArg1', 'customArg2')
|
84 | assert(calls == 2)
|
85 | })
|
86 | }
|
87 |
|
88 | function runStorageTests(store) {
|
89 | assert(store.enabled && store.enabled, "store should be enabled")
|
90 | store.clearAll()
|
91 |
|
92 | store.get('unsetValue')
|
93 |
|
94 | store.set('foo', 'bar')
|
95 | assert(store.get('foo') == 'bar', "stored key 'foo' not equal to stored value 'bar'")
|
96 |
|
97 | store.remove('foo')
|
98 | assert(store.get('foo') === undefined, "removed key 'foo' not undefined")
|
99 |
|
100 | assert(store.get('foo') === undefined, "key 'foo' exists when it shouldn't")
|
101 | assert(store.set('foo','value') == 'value', "store#set returns the stored value")
|
102 | assert(store.get('foo') !== undefined, "key 'foo' doesn't exist when it should")
|
103 |
|
104 | store.set('foo', 'bar1')
|
105 | store.set('foo', 'bar2')
|
106 | assert(store.get('foo') == 'bar2', "key 'foo' is not equal to second value set 'bar2'")
|
107 |
|
108 | store.set('foo', 'bar')
|
109 | store.set('bar', 'foo')
|
110 | store.remove('foo')
|
111 | assert(store.get('foo') === undefined, "key 'foo' exists when it shouldn't")
|
112 | assert(store.get('bar') == 'foo', "removing key 'foo' also removed key 'bar'")
|
113 |
|
114 | store.set('foo', 'bar')
|
115 | store.set('bar', 'foo')
|
116 | store.clearAll()
|
117 | assert(store.get('foo') === undefined && store.get('bar') === undefined, "keys foo and bar not cleared after store cleared")
|
118 |
|
119 | assert(store.get('defaultVal', 123) == 123, "store.get should return default value")
|
120 |
|
121 | store.set('foo', { name: 'marcus', arr: [1,2,3] })
|
122 | assert(typeof store.get('foo') == 'object', "type of stored object 'foo' is not 'object'")
|
123 | assert(store.get('foo') instanceof Object, "stored object 'foo' is not an instance of Object")
|
124 | assert(store.get('foo').name == 'marcus', "property 'name' of stored object 'foo' is not 'marcus'")
|
125 | assert(store.get('foo').arr instanceof Array, "Array property 'arr' of stored object 'foo' is not an instance of Array")
|
126 | assert(store.get('foo').arr.length == 3, "The length of Array property 'arr' stored on object 'foo' is not 3")
|
127 |
|
128 | store.remove('circularReference')
|
129 | var circularOne = {}
|
130 | var circularTwo = { one:circularOne }
|
131 | circularOne.two = circularTwo
|
132 | var threw = false
|
133 | try { store.set('circularReference', circularOne) }
|
134 | catch(e) { threw = true }
|
135 | assert(threw, "storing object with circular reference did not throw")
|
136 | assert(!store.get('circularReference'), "attempting to store object with circular reference which should have faile affected store state")
|
137 |
|
138 |
|
139 |
|
140 | var promoteValues = {
|
141 | 'int' : 42,
|
142 | 'bool' : true,
|
143 | 'float' : 3.141592653,
|
144 | 'string' : "Don't Panic",
|
145 | 'odd_string' : "{ZYX'} abc:;::)))"
|
146 | }
|
147 | for (var key in promoteValues) {
|
148 | store._storage.resolved.write(key, promoteValues[key])
|
149 | assert(store.get(key) == promoteValues[key], key+" was not correctly promoted to valid JSON")
|
150 | store.remove(key)
|
151 | }
|
152 | store.clearAll()
|
153 | var count = 0
|
154 | store.each(function() {
|
155 | count += 1
|
156 | })
|
157 | assert(count === 0)
|
158 | }
|