-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
93 lines (72 loc) · 2.24 KB
/
test.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
const assert = require('assert');
const bindCache = require('.');
// TEST HELPERS
function run(...args) {
return args.map(arg => arg());
}
let passedAssertions = 0;
let failedAssertions = 0;
function checkAssertion(assertion) {
try {
assert(assertion);
passedAssertions++;
} catch(e) {
console.error(e);
failedAssertions++;
}
}
function reportTestResults() {
console.log(`${passedAssertions} assertions passed.`);
console.log(`${failedAssertions} assertions failed.`);
if (failedAssertions) {
process.exit(1);
}
}
// TEST DEFINITIONS
function runTests() {
let instance;
function getBarResult(myArg, mySecondArg) {
return myArg + mySecondArg;
}
const argument1 = 'hey';
const argument2 = 'ho';
const result1 = 'myresult';
const result2 = getBarResult(argument1, argument2);
class MyClass {
constructor() {
instance = this;
this.bind = bindCache(this);
const boundFoo = this.bind(this.foo);
const boundBar = this.bind(this.bar, argument1, argument2);
// check that function calls execute successfully
const [fooResult, barResult] = run(boundFoo, boundBar);
checkAssertion(fooResult === result1);
checkAssertion(barResult === result2);
// check that calls to bind with the same set of function+arguments
// return the same bound function
checkAssertion(this.bind(this.foo) === boundFoo);
checkAssertion(this.bind(this.bar, argument1, argument2) === boundBar);
// check that different functions don't return the bound function
checkAssertion(boundFoo !== boundBar);
// check that the same function with different arguments returns
// a different bound function
checkAssertion(this.bind(this.foo, argument1) !== boundFoo);
checkAssertion(this.bind(this.bar, argument1) !== boundBar);
checkAssertion(this.bind(this.bar) !== boundBar);
}
foo() {
checkAssertion(this === instance);
return result1;
}
bar(myArg, mySecondArg) {
checkAssertion(this === instance);
checkAssertion(myArg === argument1);
checkAssertion(mySecondArg === argument2);
return getBarResult(myArg, mySecondArg);
}
}
new MyClass();
}
// RUN TESTS
runTests();
reportTestResults();