Debian Codegraph @

src/testRunner/unittests/evaluation/autoAccessors.ts

raw
1 import * as evaluator from "../../_namespaces/evaluator";
2 import * as ts from "../../_namespaces/ts";
3
4 describe("unittests:: evaluation:: autoAccessors", () => {
5 const editions = [
6 { name: "es2022", target: ts.ScriptTarget.ES2022 },
7 { name: "es2015", target: ts.ScriptTarget.ES2015 },
8 ];
9 for (const { name, target } of editions) {
10 describe(name, () => {
11 it("generates accessor pair", async () => {
12 const { C } = evaluator.evaluateTypeScript(`
13 export class C {
14 accessor x;
15 }
16 `, { target });
17
18 const desc = Object.getOwnPropertyDescriptor(C.prototype, "x");
19 assert.isDefined(desc);
20 assert.isFunction(desc!.get);
21 assert.isFunction(desc!.set);
22 });
23
24 it("storage is private", async () => {
25 const { C } = evaluator.evaluateTypeScript(`
26 export class C {
27 accessor x;
28 }
29 `, { target });
30
31 const desc = Object.getOwnPropertyDescriptor(C.prototype, "x");
32 const obj = Object.create(C.prototype);
33 assert.throws(() => desc!.get!.call(obj), /private/);
34 });
35
36 it("getter and setter wrap same field", async () => {
37 const { C } = evaluator.evaluateTypeScript(`
38 export class C {
39 accessor x;
40 }
41 `, { target });
42 const obj = new C();
43 obj.x = 1;
44 assert.equal(obj.x, 1);
45
46 obj.x = 2;
47 assert.equal(obj.x, 2);
48 });
49
50 it("supports initializer", async () => {
51 const { C } = evaluator.evaluateTypeScript(`
52 export class C {
53 accessor x = 1;
54 }
55 `, { target });
56 const obj = new C();
57 assert.equal(obj.x, 1);
58 });
59
60 it("legacy decorator can intercept getter/setter", async () => {
61 const { actions, C } = evaluator.evaluateTypeScript(`
62 function dec(target, key, descriptor) {
63 const { get, set } = descriptor;
64 actions.push({ kind: "decorate", target, key });
65 descriptor.get = function() {
66 actions.push({ kind: "get", this: this });
67 return get.call(this);
68 };
69 descriptor.set = function(value) {
70 actions.push({ kind: "set", this: this, value });
71 set.call(this, value);
72 };
73 }
74 export const actions = [];
75 export class C {
76 @dec
77 accessor x;
78 }
79 `, { target, experimentalDecorators: true });
80
81 assert.deepEqual(actions, [
82 { kind: "decorate", target: C.prototype, key: "x" }
83 ]);
84 const obj = new C();
85 obj.x = 1;
86 assert.equal(obj.x, 1);
87 assert.deepEqual(actions, [
88 { kind: "decorate", target: C.prototype, key: "x" },
89 { kind: "set", this: obj, value: 1 },
90 { kind: "get", this: obj },
91 ]);
92 });
93
94 it("legacy decorator cannot intercept initializer", async () => {
95 const { actions, C } = evaluator.evaluateTypeScript(`
96 function dec(target, key, descriptor) {
97 const { get, set } = descriptor;
98 descriptor.set = function(value) {
99 actions.push({ kind: "set", this: this, value });
100 set.call(this, value);
101 };
102 }
103 export const actions = [];
104 export class C {
105 @dec
106 accessor x = 1;
107 }
108 `, { target, experimentalDecorators: true });
109
110 const obj = new C();
111 assert.equal(obj.x, 1);
112 assert.deepEqual(actions, []);
113 });
114 });
115 }
116 });