This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wru.js
144 lines (119 loc) · 6.16 KB
/
wru.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var // wru library core
wru = {
timeout: TIMEOUT,
assert: function assert(description, result) {
// if no description provided, variables are shifted
// these are both valid wru.assert calls indeed
// wru.assert(truishValue);
// wru.assert("test description", truishValue);
if (arguments[LENGTH] == 1) {
result = description;
description = UNKNOWN;
}
// flag used in wru.async to verify at least
// one assertion was performed
called = TRUE;
// store the result in the right collection
push.call(result ? pass : fail, prefix + description);
// just to add a bit of sugar
return result;
},
async: function async(description, callback, timeout, p) {
var r, delay = timeout || wru.timeout || (wru.timeout = TIMEOUT);
// p is used as sentinel
// it defines the anonymous name
// if necessary and it's used to flag the timeout
p = ++waitForIt;
// if no description provided, variables are shifted
// these are all valid wru.async calls indeed, timeout is optional
// wru.async(function () { ... })
// wru.async("test description", function () { ... })
// wru.async(function () { ... }, timeout)
// wru.async("test description", function () { ... }, timeout)
if (typeof description == "function") {
delay = callback || wru.timeout;
callback = description;
description = "asynchronous test #" + p;
}
// if in *TIMEOUT* time nothing happens ...
timeout = setTimeout(function () {
// p is flagged as 0
p = 0;
// timeout is handled as failure, not error (could be the server)
push.call(fail, description);
// if there is no reason to waitForIt then is time to call Dary()
--waitForIt || (daryTimeout = setTimeout(Dary, 0));
},
// timeout can be specified
// this procedure ensure that it's
// a number and it's greater than 0
abs(delay) || wru.timeout
);
// the async function is a wrap of the passed callback
return function async() {
// if it's executed after the timeout nothing happens
// since the failure has been already notified
if (!p) return;
// called is always set as *TRUE* during any assertion
// this indicates if the callback made at least one assertion
// as example, in this case the callback could be called many time
// with different readyState ... however, only on readyState 4
// there is the assertion we are interested about, e.g.
//
// xhr.onreadystatechange = wru.async(function (){
// if (this.readyState == 4)
// wru.assert("content", this.responseText.length)
// ;
// });
//
// in above example called will be flagged as true
// only during last readyState call
called = FALSE;
// simply recycled "string" variable
// prefix will be internally used by assert during function execution
prefix = description + ": ";
// the original callback is called with proper *this* if specified
try {
r = callback.apply(this, arguments);
} catch(doooodeThisIsBAD) {
// if there is an Error
// the test is screwed up
// called has to be set as *TRUE* to invalidate the test
called = TRUE;
// message is "casted" to avoid IE host objects errors problem
// (or any other possible edge case)
push.call(fatal, prefix + doooodeThisIsBAD);
}
// prefix can be *EMPTY* string again now
prefix = EMPTY;
// a failure or at least an assertion
if (called) {
// timeout not necessary anymore
clearTimeout(timeout);
// if there is no reason to waitForIt then is time to call Dary()
--waitForIt || (daryTimeout = setTimeout(Dary, 0));
}
// return the eventual callback value
return r;
};
},
// wru.test({...test...})
// wru.test([{...test...}, {...test...}, ...])
// the {...test...} object should have a string name and a function test property
// optionally a function setup and a function teardown too
test: function test(list, after) {
// in case you need to do something after
wru.after = after || function () {};
// test may be called multiple times
// queue should simply concatenate other calls
queue = concat.apply(queue, [list]);
// if wru.random is true, the queue is ranodomized
// this is to make tests indipendent from each others
wru.random && sort.call(queue, messItUp);
// if there is no test to waitForIt
// Dary() has been called already
// we can procede with next test
// invoking isGonnaBeLegen()
waitForIt || isGonnaBeLegen();
}
},