Commit cec78774 authored by Tristan Cavelier's avatar Tristan Cavelier

jio tests tools moved to util.js and fakestorage.js, some method name changed

parent 10a9ba26
/*jslint indent: 2, maxlen: 80, nomen: true */
/*global define, exports, window, jIO */
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
module(exports);
}
if (typeof window === 'object') {
window.fake_storage = {};
module(window.fake_storage);
}
}(['exports'], function (exports) {
"use strict";
var fakestorage = {};
function FakeStorage(spec) {
this._id = spec.id;
if (typeof this._id !== 'string' || this._id.length <= 0) {
throw new TypeError(
"Initialization error: wrong id"
);
}
}
FakeStorage.createNamespace = function (
that,
method,
command,
param,
options
) {
fakestorage[that._id + '/' + method] = {
param: param,
options: options,
success: function () {
var res = command.success.apply(command, arguments);
delete fakestorage[that._id + '/' + method];
return res;
},
error: function () {
var res = command.error.apply(command, arguments);
delete fakestorage[that._id + '/' + method];
return res;
},
retry: function () {
var res = command.retry.apply(command, arguments);
delete fakestorage[that._id + '/' + method];
return res;
},
notify: function () {
return command.notify.apply(command, arguments);
},
storage: function () {
return command.storage.apply(command, arguments);
},
end: function () {
return command.end.apply(command, arguments);
},
commit: function () {
return command.commit.apply(command, arguments);
},
free: function () {
delete fakestorage[that._id + '/' + method];
}
};
};
FakeStorage.makeMethod = function (method) {
return function (command, param, options) {
FakeStorage.createNamespace(this, method, command, param, options);
};
};
FakeStorage.prototype.post = FakeStorage.makeMethod('post');
FakeStorage.prototype.put = FakeStorage.makeMethod('put');
FakeStorage.prototype.get = FakeStorage.makeMethod('get');
FakeStorage.prototype.remove = FakeStorage.makeMethod('remove');
FakeStorage.prototype.putAttachment = FakeStorage.makeMethod('putAttachment');
FakeStorage.prototype.getAttachment = FakeStorage.makeMethod('getAttachment');
FakeStorage.prototype.removeAttachment =
FakeStorage.makeMethod('removeAttachment');
FakeStorage.prototype.check = FakeStorage.makeMethod('check');
FakeStorage.prototype.repair = FakeStorage.makeMethod('repair');
FakeStorage.prototype.allDocs = FakeStorage.makeMethod('allDocs');
jIO.addStorage('fake', FakeStorage);
exports.commands = fakestorage;
}));
This diff is collapsed.
/*jslint indent: 2, maxlen: 80 */
/*global define, exports, window, localStorage, ok, deepEqual, sinon */
(function (dependencies, module) {
"use strict";
if (typeof define === 'function' && define.amd) {
return define(dependencies, module);
}
if (typeof exports === 'object') {
module(exports);
}
if (typeof window === 'object') {
window.test_util = {};
module(window.test_util);
}
}(['exports'], function (exports) {
"use strict";
//////////////////////////////////////////////////////////////////////////////
// Tools
/**
* Test if the string is an Uuid
*
* @param {String} uuid The string to test
* @return {Boolean} true if is uuid, else false
*/
function isUuid(uuid) {
var x = "[0-9a-fA-F]";
if (typeof uuid !== "string") {
return false;
}
return (uuid.match(
"^" + x + "{8}-" + x + "{4}-" +
x + "{4}-" + x + "{4}-" + x + "{12}$"
) === null ? false : true);
}
exports.isUuid = isUuid;
/**
* A useful tool to set/get json object into the localStorage
*/
exports.jsonlocalstorage = {
clear: function () {
return localStorage.clear();
},
getItem: function (item) {
var value = localStorage.getItem(item);
return value === null ? null : JSON.parse(value);
},
setItem: function (item, value) {
return localStorage.setItem(item, JSON.stringify(value));
},
removeItem: function (item) {
return localStorage.removeItem(item);
}
};
//////////////////////////////////////////////////////////////////////////////
// Deprecated
function spyJioCallback(result_type, value, message) {
return function (err, response) {
var val;
switch (result_type) {
case 'value':
val = err || response;
break;
case 'status':
val = (err || {}).status;
break;
case 'jobstatus':
val = (err ? 'fail' : 'done');
break;
default:
ok(false, "Unknown case " + result_type);
break;
}
deepEqual(val, value, message);
};
}
exports.spyJioCallback = spyJioCallback;
function ospy(o, result_type, value, message, function_name) {
function_name = function_name || 'f';
o[function_name] = function (err, response) {
var val;
switch (result_type) {
case 'value':
val = err || response;
break;
case 'status':
val = (err || {}).status;
break;
case 'jobstatus':
val = (err ? 'fail' : 'done');
break;
default:
ok(false, "Unknown case " + result_type);
break;
}
deepEqual(val, value, message);
};
sinon.spy(o, function_name);
}
exports.ospy = ospy;
function otick(o, a, b) {
var tick = 1, function_name = 'f';
if (typeof a === 'number' && !isNaN(a)) {
tick = a;
a = b;
}
if (typeof a === 'string') {
function_name = a;
}
o.clock.tick(tick);
if (!o[function_name].calledOnce) {
if (o[function_name].called) {
ok(false, 'too much results');
} else {
ok(false, 'no response');
}
}
}
exports.otick = otick;
}));
......@@ -11,6 +11,8 @@
<script src="../lib/sinon/sinon.js"></script>
<script src="../lib/sinon/sinon-qunit.js"></script>
<script src="../jio.js"></script>
<script src="jio/util.js"></script>
<script src="jio/fakestorage.js"></script>
<script src="jio/tests.js"></script>
</body>
</html>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment