-
Notifications
You must be signed in to change notification settings - Fork 7
/
mixin.test.ts
86 lines (74 loc) · 2.18 KB
/
mixin.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
import { ok, strictEqual } from "node:assert";
import mixin from "./mixin.ts";
import { hasOwn } from "./object.ts";
import _try from "./try.ts";
describe("mixin", () => {
class A {
get name() {
return this.constructor.name;
}
str() {
return "";
}
}
class B {
num() {
return 0;
}
}
class C extends B {
bool() {
return false;
}
}
class Foo<T> {
constructor(readonly ver: T) { }
echo(text: string) {
return text;
}
}
class Bar extends mixin<typeof Foo<string>, [A, C]>(Foo, A, C) {
show(str: string) {
return str;
}
}
const Bar2 = mixin(Bar, {
log(text: string) {
console.log(text);
}
});
it("class constructor", () => {
const bar = new Bar("v0.1");
ok(typeof bar.bool === "function");
ok(typeof bar.echo === "function");
ok(typeof bar.num === "function");
ok(typeof bar.show === "function");
ok(typeof bar.str === "function");
strictEqual(bar.ver, "v0.1");
ok(!hasOwn(bar, "name"));
strictEqual(Bar.length, 0);
strictEqual(bar.name, "Bar");
const proto = Object.getPrototypeOf(bar);
strictEqual(proto, Bar.prototype);
ok(typeof proto.bool === "function");
ok(typeof proto.echo === "function");
ok(typeof proto.num === "function");
ok(typeof proto.show === "function");
ok(typeof proto.str === "function");
});
it("object literal", () => {
const bar2 = new Bar2("v0.1");
ok(bar2 instanceof Bar);
ok(typeof bar2.bool === "function");
ok(typeof bar2.echo === "function");
ok(typeof bar2.num === "function");
ok(typeof bar2.show === "function");
ok(typeof bar2.str === "function");
ok(typeof bar2.log === "function");
strictEqual(bar2.ver, "v0.1");
ok(!hasOwn(bar2, "name"));
strictEqual(Bar2.length, 0);
const [err] = _try(() => strictEqual(bar2.name, ""));
err && strictEqual(bar2.name, "Bar"); // old v8
});
});