-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
75 lines (74 loc) · 2.41 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function () {
/*
Usage:
const assert = include("utils").assert;
assert.equal(expected, actual, {
message: "User '" + name + "' is not same:\n",
skip:["password"]
});
*/
function compare(left, right, args) {
if(args === undefined) {
args = {};
}
var errors = [];
var visited = [];
function compareObjects(path, left, right) {
if(left === null || right === null ||
typeof left !== 'object' || typeof right !== 'object') {
if(left !== right) {
errors.push("different '" + path + "': expected=" + left
+ " but actual=" + right);
}
return;
}
if(visited.indexOf(left) >= 0) {
return;
}
visited.push(left);
var keys = Object.keys(left);
if(args.skip) {
var skip = args.skip;
var filter;
if(typeof skip === 'function') {
filter = function(i) { return !skip(path + i) };
} else if(skip instanceof Array) {
filter = function(i) { return skip.indexOf(path + i) < 0 };
}
if(filter) {
keys = keys.filter(filter);
}
}
const isArray = left instanceof Array;
for(var i = 0; i < keys.length; ++i) {
var key = keys[i];
var expected = left[key];
var actual = right[key];
var prefix = path + (isArray? "[" + key + "]" : "." + key);
compareObjects(prefix, expected, actual);
}
}
compareObjects("", left, right);
if(errors.length > 0) {
console.assert(false, args.message || "", errors.join('\n'));
}
};
var utils = {assert:{}};
utils.assert.equal = compare;
utils.wait = function(func, arg) {
arg = arg || {};
let interval = arg.interval || 1;
let time = arg.time || 10;
let tries = Math.round(time / interval) || 1;
for(var i = 0; i < tries; ++i) {
let res = func();
if(res) {
return res;
}
java.lang.Thread.sleep(interval * 1000);
}
return null;
};
Object.freeze(utils);
return utils;
}