@@ -2,198 +2,181 @@ import * as util from "../util";
22import { TSTLErrors } from "../../src/TSTLErrors" ;
33
44test ( "Class decorator with no parameters" , ( ) => {
5- const source = `
6- function SetBool<T extends { new(...args: any[]): {} }>(constructor: T) {
7- return class extends constructor {
8- decoratorBool = true;
5+ util . fn `
6+ function SetBool<T extends { new(...args: any[]): {} }>(constructor: T) {
7+ return class extends constructor {
8+ decoratorBool = true;
9+ }
910 }
10- }
11-
12- @SetBool
13- class TestClass {
14- public decoratorBool = false;
15- }
1611
17- const classInstance = new TestClass();
18- return classInstance.decoratorBool;
19- ` ;
12+ @SetBool
13+ class TestClass {
14+ public decoratorBool = false;
15+ }
2016
21- const result = util . transpileAndExecute ( source ) ;
22- expect ( result ) . toBe ( true ) ;
17+ const classInstance = new TestClass();
18+ return classInstance.decoratorBool;
19+ ` . expectToMatchJsResult ( ) ;
2320} ) ;
2421
2522test ( "Class decorator with parameters" , ( ) => {
26- const source = `
27- function SetNum(numArg: number) {
28- return <T extends new(...args: any[]) => {}>(constructor: T) => {
29- return class extends constructor {
30- decoratorNum = numArg;
23+ util . fn `
24+ function SetNum(numArg: number) {
25+ return <T extends new(...args: any[]) => {}>(constructor: T) => {
26+ return class extends constructor {
27+ decoratorNum = numArg;
28+ };
3129 };
32- };
33- }
34-
35- @SetNum(420)
36- class TestClass {
37- public decoratorNum;
38- }
30+ }
3931
40- const classInstance = new TestClass();
41- return classInstance.decoratorNum;
42- ` ;
32+ @SetNum(420)
33+ class TestClass {
34+ public decoratorNum;
35+ }
4336
44- const result = util . transpileAndExecute ( source ) ;
45- expect ( result ) . toBe ( 420 ) ;
37+ const classInstance = new TestClass();
38+ return classInstance.decoratorNum;
39+ ` . expectToMatchJsResult ( ) ;
4640} ) ;
4741
4842test ( "Class decorator with variable parameters" , ( ) => {
49- const source = `
50- function SetNumbers(...numArgs: number[]) {
51- return <T extends new(...args: any[]) => {}>(constructor: T) => {
52- return class extends constructor {
53- decoratorNums = new Set<number>(numArgs);
43+ util . fn `
44+ function SetNumbers(...numArgs: number[]) {
45+ return <T extends new(...args: any[]) => {}>(constructor: T) => {
46+ return class extends constructor {
47+ decoratorNums = new Set<number>(numArgs);
48+ };
5449 };
55- };
56- }
57-
58- @SetNumbers(120, 30, 54)
59- class TestClass {
60- public decoratorNums;
61- }
62-
63- const classInstance = new TestClass();
64- let sum = 0;
65- for (const value of classInstance.decoratorNums) {
66- sum += value;
67- }
68- return sum;
69- ` ;
50+ }
51+
52+ @SetNumbers(120, 30, 54)
53+ class TestClass {
54+ public decoratorNums;
55+ }
7056
71- const result = util . transpileAndExecute ( source ) ;
72- expect ( result ) . toBe ( 204 ) ;
57+ const classInstance = new TestClass();
58+ let sum = 0;
59+ for (const value of classInstance.decoratorNums) {
60+ sum += value;
61+ }
62+ return sum;
63+ ` . expectToMatchJsResult ( ) ;
7364} ) ;
7465
7566test ( "Multiple class decorators" , ( ) => {
76- const source = `
77- function SetTen<T extends { new(...args: any[]): {} }>(constructor: T) {
78- return class extends constructor {
79- decoratorTen = 10;
67+ util . fn `
68+ function SetTen<T extends { new(...args: any[]): {} }>(constructor: T) {
69+ return class extends constructor {
70+ decoratorTen = 10;
71+ }
8072 }
81- }
8273
83- function SetNum(numArg: number) {
84- return <T extends new(...args: any[]) => {}>(constructor: T) => {
85- return class extends constructor {
86- decoratorNum = numArg;
74+ function SetNum(numArg: number) {
75+ return <T extends new(...args: any[]) => {}>(constructor: T) => {
76+ return class extends constructor {
77+ decoratorNum = numArg;
78+ };
8779 };
88- };
89- }
90-
91- @SetTen
92- @SetNum(410)
93- class TestClass {
94- public decoratorTen;
95- public decoratorNum;
96- }
97-
98- const classInstance = new TestClass();
99- return classInstance.decoratorNum + classInstance.decoratorTen;
100- ` ;
80+ }
81+
82+ @SetTen
83+ @SetNum(410)
84+ class TestClass {
85+ public decoratorTen;
86+ public decoratorNum;
87+ }
10188
102- const result = util . transpileAndExecute ( source ) ;
103- expect ( result ) . toBe ( 420 ) ;
89+ const classInstance = new TestClass();
90+ return classInstance.decoratorNum + classInstance.decoratorTen;
91+ ` . expectToMatchJsResult ( ) ;
10492} ) ;
10593
10694test ( "Class decorator with inheritance" , ( ) => {
107- const source = `
108- function SetTen<T extends { new(...args: any[]): {} }>(constructor: T) {
109- return class extends constructor {
110- decoratorTen = 10;
95+ util . fn `
96+ function SetTen<T extends { new(...args: any[]): {} }>(constructor: T) {
97+ return class extends constructor {
98+ decoratorTen = 10;
99+ }
111100 }
112- }
113101
114- function SetNum(numArg: number) {
115- return <T extends new(...args: any[]) => {}>(constructor: T) => {
116- return class extends constructor {
117- decoratorNum = numArg;
102+ function SetNum(numArg: number) {
103+ return <T extends new(...args: any[]) => {}>(constructor: T) => {
104+ return class extends constructor {
105+ decoratorNum = numArg;
106+ };
118107 };
119- };
120- }
121-
122- class TestClass {
123- public decoratorTen = 0;
124- public decoratorNum = 0;
125- }
108+ }
126109
127- @SetTen
128- @SetNum(410)
129- class SubTestClass extends TestClass {}
110+ class TestClass {
111+ public decoratorTen = 0;
112+ public decoratorNum = 0;
113+ }
130114
131- const classInstance = new SubTestClass();
132- return classInstance.decoratorNum + classInstance.decoratorTen;
133- ` ;
115+ @SetTen
116+ @SetNum(410)
117+ class SubTestClass extends TestClass {}
134118
135- const result = util . transpileAndExecute ( source ) ;
136- expect ( result ) . toBe ( 420 ) ;
119+ const classInstance = new SubTestClass();
120+ return classInstance.decoratorNum + classInstance.decoratorTen;
121+ ` . expectToMatchJsResult ( ) ;
137122} ) ;
138123
139124test ( "Class decorators are applied in order and executed in reverse order" , ( ) => {
140- const source = `
141- const order = [];
142-
143- function SetString(stringArg: string) {
144- order.push("eval " + stringArg);
145- return <T extends new (...args: any[]) => {}>(constructor: T) => {
146- order.push("execute " + stringArg);
147- return class extends constructor {
148- decoratorString = stringArg;
125+ util . fn `
126+ const order = [];
127+
128+ function SetString(stringArg: string) {
129+ order.push("eval " + stringArg);
130+ return <T extends new (...args: any[]) => {}>(constructor: T) => {
131+ order.push("execute " + stringArg);
132+ return class extends constructor {
133+ decoratorString = stringArg;
134+ };
149135 };
150- };
151- }
152-
153- @SetString("fox")
154- @SetString("jumped")
155- @SetString("over dog")
156- class TestClass {
157- public static decoratorString = "";
158- }
159-
160- const inst = new TestClass();
161- return order.join(" ");
162- ` ;
136+ }
163137
164- const result = util . transpileAndExecute ( source ) ;
165- expect ( result ) . toBe ( "eval fox eval jumped eval over dog execute over dog execute jumped execute fox" ) ;
138+ @SetString("fox")
139+ @SetString("jumped")
140+ @SetString("over dog")
141+ class TestClass {
142+ public static decoratorString = "";
143+ }
144+
145+ const inst = new TestClass();
146+ return order.join(" ");
147+ ` . expectToMatchJsResult ( ) ;
166148} ) ;
167149
168150test ( "Throws error if decorator function has void context" , ( ) => {
169151 const source = `
170- function SetBool<T extends { new(...args: any[]): {} }>(this: void, constructor: T) {
171- return class extends constructor {
172- decoratorBool = true;
152+ function SetBool<T extends { new(...args: any[]): {} }>(this: void, constructor: T) {
153+ return class extends constructor {
154+ decoratorBool = true;
155+ }
173156 }
174- }
175157
176- @SetBool
177- class TestClass {
178- public decoratorBool = false;
179- }
158+ @SetBool
159+ class TestClass {
160+ public decoratorBool = false;
161+ }
180162
181- const classInstance = new TestClass();
182- return classInstance.decoratorBool;
163+ const classInstance = new TestClass();
164+ return classInstance.decoratorBool;
183165 ` ;
184166
185167 expect ( ( ) => util . transpileAndExecute ( source ) ) . toThrowExactError ( TSTLErrors . InvalidDecoratorContext ( util . nodeStub ) ) ;
186168} ) ;
187169
188170test ( "Exported class decorator" , ( ) => {
189- const code = `
171+ util . mod `
190172 function decorator<T extends any>(c: T): T {
191173 c.bar = "foobar";
192174 return c;
193175 }
194176
195177 @decorator
196- export class Foo {}` ;
197-
198- expect ( util . transpileExecuteAndReturnExport ( code , "Foo.bar" ) ) . toBe ( "foobar" ) ;
178+ export class Foo {}
179+ `
180+ . export ( "Foo.bar" )
181+ . expectToMatchJsResult ( ) ;
199182} ) ;
0 commit comments