-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.mjs
More file actions
108 lines (92 loc) · 2.13 KB
/
test.mjs
File metadata and controls
108 lines (92 loc) · 2.13 KB
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
/**
* Object extension tools.
* @module
* @see module:tools
* @see module:reactive
* @see module:observe
*/
import {
noop,
pipe
} from "./operation.mjs"
import {
good,
DEBUG,
ASSERT_T,
} from "./debug.mjs"
import observable from "./observe.mjs"
import reactive from "./reactive.mjs"
DEBUG(true)
var COUNTER = 0
export const states = {
FAILED: "FAILED",
PASSED: "PASSED"
}
export class Test {
constructor(description, test = pipe, ...opt) {
good(description, String)
good(test, Function)
this.id = ++COUNTER
this.options = opt;
this.description = description;
this.test = test;
this.bindable("result")
}
run(...args) {
var promise = new Promise(resolver.bind(
this,
args,
finish.bind(this, states.PASSED, args),
finish.bind(this, states.FAILED, args)
));
promise.finally((x)=> this.fire("complete", x))
return promise
}
async die(...args) {
const fail = new Test("", this.test)
COUNTER--
fail.print = noop
this.test = ASSERT_T
try {
const result = await fail.run(...args)
this.print = this.print.bind(this, `die: ${result}`)
return await this.run(fail.state == "FAILED");
} finally {
this.print = this.constructor.prototype.print
this.test = fail.test
}
}
}
Test.prototype.print = consolePrinter;
observable.call(Test.prototype)
reactive.call(Test.prototype)
export default Test;
async function resolver(args, ok, ko, resolve) {
try {
return resolve(ok(await this.test.apply(this, args)))
} catch(e) {
return resolve(ko(e))
}
}
function finish(state, args, result) {
this.state = state;
this.fire(state, this.result = result)
this.print.bind(this, args) `${state}`
return result;
}
function consolePrinter(...args) {
var color = "red"
var status = args[args.length - 1]
if(status == states.PASSED) {
color = "green"
}
args.pop()
args.pop()
var par = args[args.length - 1]
args.pop()
var logmsg = args.length? ' [' + args.join() + ']' : ''
console[status == states.PASSED ? 'log' : 'error'](
`${this.id}) %c${status}%c -> ${this.description}${logmsg}%c (${par}):`,
`color:${color}`, "color:initial;font-style: oblique", "color:initial", this.result
)
}