-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.js
106 lines (84 loc) · 2.45 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
94
95
96
97
98
99
100
101
102
103
104
105
106
var assert = require('assert');
var inherits = require('util').inherits;
var createFunction = require('./');
var invoke = require('./invoke');
function Type (name, length) {
if (typeof this !== 'function')
return createFunction(name, length, Type, arguments);
this.count = 0;
}
inherits(Type, Function);
Type.prototype[invoke] = function (val) {
this.count += val;
};
var t1 = new Type('nate', 6);
assert.ok(t1 instanceof Type);
assert.ok(t1 instanceof Function);
assert.ok(t1 instanceof Object);
assert.equal(t1.name, 'nate');
assert.equal(t1.length, 6);
assert.equal(t1.count, 0);
t1(5);
assert.equal(t1.count, 5);
var t2 = new Type('foo', 6);
assert.ok(t2 instanceof Type);
assert.ok(t2 instanceof Function);
assert.ok(t2 instanceof Object);
assert.equal(t2.name, 'foo');
assert.equal(t2.length, 6);
assert.equal(t2.count, 0);
t2(-1);
assert.equal(t2.count, -1);
function Subclass (init) {
if (typeof this !== 'function')
return createFunction('sub', 100, Subclass, arguments);
Type.call(this);
this.count = init;
}
inherits(Subclass, Type);
var s1 = new Subclass(6);
assert.ok(s1 instanceof Subclass);
assert.ok(s1 instanceof Type);
assert.ok(s1 instanceof Function);
assert.ok(s1 instanceof Object);
assert.equal(s1.name, 'sub');
assert.equal(s1.length, 100);
assert.equal(s1.count, 6);
s1(-1);
assert.equal(s1.count, 5);
// one off instance
var oneOff = createFunction('oneOff', 1);
var oneOffCalled = false;
assert.ok(oneOff instanceof Function);
assert.ok(oneOff instanceof Object);
assert.equal(oneOff.name, 'oneOff');
assert.equal(oneOff.length, 1);
assert.throws(function () {
oneOff();
});
oneOff[invoke] = function () {
oneOffCalled = true;
return [].slice.apply(arguments);
};
assert.ok(!oneOffCalled);
assert.deepEqual(oneOff('foo', 'bar'), [ 'foo', 'bar' ]);
assert.ok(oneOffCalled);
// unicode and otherwise invalid JS idenifiers may be used as the name
var snowman = createFunction('⌛', 30);
assert.equal(snowman.name, '⌛');
assert.equal(snowman.length, 30);
// `this` arg
var thisArg = createFunction();
thisArg[invoke] = function () {
return this;
};
assert.equal(thisArg(), thisArg);
assert.equal(thisArg()()(), thisArg());
assert.equal(thisArg.call(global), global);
assert.equal(thisArg.call(1).valueOf(), 1);
assert.equal(thisArg.call(true).valueOf(), true);
var that = { foo: 'bar' };
var thatArg = thisArg.bind(that);
assert.equal(thatArg(), that);
assert.equal(thatArg().foo, 'bar');
assert.equal(thatArg.call(global), that);