-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.ts
191 lines (161 loc) · 5.81 KB
/
test.ts
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, it } from "mocha";
import * as assert from "assert";
import * as magic from ".";
class Car {
name?: string;
wheels?: number;
windows?: number;
constructor(wheels: number = 0) {
if (wheels)
this.wheels = wheels;
}
__get(prop: string) {
// @ts-ignore
return (prop in this && this[prop] !== undefined)
// @ts-ignore
? this[prop]
// @ts-ignore
: (prop == "name" ? this.constructor.name + " Instance" : null);
}
__set(prop: string, value: any) {
// @ts-ignore
this[prop] = prop == "name" ? value + " Instance" : value;
}
__has(prop: string) {
return prop.slice(0, 2) != "__" && (prop in this || prop == "name");
}
__delete(prop: string) {
if (prop.slice(0, 2) == "__" || prop == "wheels") return;
// @ts-ignore
delete this[prop];
}
__invoke() {
return "invoking Car as a function";
}
test(str: string) {
return str;
}
}
class Car2 {
static __invoke() {
return `invoking ${this.name} as a function`;
}
}
describe("applying magic methods to class", () => {
it("should generate a proxy class looks exactly like the original one", () => {
let _Car = magic.applyMagic(Car);
assert.strictEqual(_Car.name, Car.name);
assert.strictEqual(_Car.length, Car.length);
assert.strictEqual(_Car.toString(), Car.toString());
assert.strictEqual(_Car.prototype instanceof Car, true);
var car = new _Car(4);
assert.strictEqual(car instanceof Car, true);
assert.strictEqual(car.wheels, 4);
assert.strictEqual(car.test("Hello, World!"), "Hello, World!");
});
it("should apply __get method as expected", () => {
let _Car = magic.applyMagic(Car);
var car = new _Car;
car.wheels = 4;
assert.strictEqual(car.wheels, 4);
assert.strictEqual(car.name, "Car Instance");
});
it("should apply __set method as expected", () => {
let _Car = magic.applyMagic(Car);
var car = new _Car;
car.wheels = 4;
car.name = "MyCar";
assert.strictEqual(car.wheels, 4);
assert.strictEqual(car.name, "MyCar Instance");
});
it("should apply __has method as expected", () => {
let _Car = magic.applyMagic(Car);
var car = new _Car;
car.wheels = 4;
assert.strictEqual("wheels" in car, true);
assert.strictEqual("name" in car, true);
assert.strictEqual("__has" in car, false);
});
it("should apply __delete method as expected", () => {
let _Car = magic.applyMagic(Car);
var car = new _Car;
car.wheels = 4;
delete car.wheels;
assert.strictEqual("wheels" in car, true);
assert.strictEqual(car.wheels, 4);
});
it("should apply __invoke method as expected", () => {
let _Car = magic.applyMagic(Car);
// @ts-ignore
assert.strictEqual(_Car(), "invoking Car as a function");
});
it("should apply static __invoke method as expected", () => {
let _Car2 = magic.applyMagic(Car2);
// @ts-ignore
assert.strictEqual(_Car2(), "invoking Car2 as a function");
});
});
describe("class inheritance of magical class", () => {
let _Car = magic.applyMagic(Car);
it("should define an ES6 class extends the magical class as expected", () => {
class Auto extends _Car { }
let classStr = Auto.toString();
assert.strictEqual(Auto.name, "Auto");
assert.strictEqual(Auto.length, 0);
assert.strictEqual(Auto.toString(), classStr);
var auto = new Auto(4);
assert.strictEqual(auto instanceof Car, true);
assert.deepStrictEqual(auto.name, "Auto Instance");
assert.deepStrictEqual(auto.wheels, 4);
assert.deepStrictEqual(auto.windows, null);
auto.name = "MyAuto";
auto.windows = 4;
assert.strictEqual(auto.name, "MyAuto Instance");
assert.strictEqual(auto.windows, 4);
assert.strictEqual(auto.test("Hello, World!"), "Hello, World!");
});
it("should define an ES5 class extends the magical class as expected", () => {
function Auto() {
return Reflect.construct(_Car, arguments, new.target);
}
let classStr = Auto.toString();
Object.setPrototypeOf(Auto, _Car);
Object.setPrototypeOf(Auto.prototype, _Car.prototype);
assert.strictEqual(Auto.name, "Auto");
assert.strictEqual(Auto.length, 0);
assert.strictEqual(Auto.toString(), classStr);
// @ts-ignore
var auto = new Auto(4);
assert.strictEqual(auto instanceof Car, true);
assert.deepStrictEqual(auto.name, "Auto Instance");
assert.deepStrictEqual(auto.wheels, 4);
assert.strictEqual(auto.windows, null);
auto.name = "MyAuto";
auto.windows = 4;
assert.strictEqual(auto.name, "MyAuto Instance");
assert.strictEqual(auto.windows, 4);
assert.strictEqual(auto.test("Hello, World!"), "Hello, World!");
});
});
describe("apply magic functions on objects other than class", () => {
it("should apply magic functions on an object as expected", () => {
let obj = magic.applyMagic({
__get(prop: string) {
return "bar";
}
});
// @ts-ignore
assert.strictEqual(obj.foo, "bar");
});
it("should apply magic functions on a function as expected", () => {
let fn = new Function();
// @ts-ignore
fn["__get"] = (prop: string) => {
// @ts-ignore
return prop === "name" ? "fn" : prop in fn ? fn[prop] : void 0;
};
// @ts-ignore
fn = magic.applyMagic(fn, true);
assert.strictEqual(fn.name, "fn");
});
});