1+ import { Expect , Test , TestCase } from "alsatian" ;
2+ import { TSHelper as tsHelper } from "../../src/TSHelper" ;
3+
14import * as ts from "typescript" ;
5+ import * as util from "../src/util" ;
26
3- import { Expect , FocusTest , IgnoreTest , Test , TestCase } from "alsatian" ;
4- import { TSHelper as tsEx } from "../../src/TSHelper" ;
7+ import { DecoratorKind } from "../../src/Decorator" ;
58
69enum TestEnum {
710 testA = 1 ,
@@ -15,8 +18,8 @@ export class TSHelperTests {
1518 @TestCase ( - 1 , "unknown" )
1619 @TestCase ( TestEnum . testA | TestEnum . testB , "unknown" )
1720 @Test ( "EnumName" )
18- public testEnumName ( inp , expected ) {
19- const result = tsEx . enumName ( inp , TestEnum ) ;
21+ public testEnumName ( inp , expected ) : void {
22+ const result = tsHelper . enumName ( inp , TestEnum ) ;
2023
2124 Expect ( result ) . toEqual ( expected ) ;
2225 }
@@ -26,14 +29,133 @@ export class TSHelperTests {
2629 @TestCase ( TestEnum . testA | TestEnum . testC , [ "testA" , "testC" ] )
2730 @TestCase ( TestEnum . testA | TestEnum . testB | TestEnum . testC , [ "testA" , "testB" , "testC" ] )
2831 @Test ( "EnumNames" )
29- public testEnumNames ( inp , expected ) {
30- const result = tsEx . enumNames ( inp , TestEnum ) ;
32+ public testEnumNames ( inp , expected ) : void {
33+ const result = tsHelper . enumNames ( inp , TestEnum ) ;
3134
3235 Expect ( result ) . toEqual ( expected ) ;
3336 }
3437
3538 @Test ( "IsFileModuleNull" )
36- public isFileModuleNull ( ) {
37- Expect ( tsEx . isFileModule ( null ) ) . toEqual ( false ) ;
39+ public isFileModuleNull ( ) : void {
40+ Expect ( tsHelper . isFileModule ( null ) ) . toEqual ( false ) ;
41+ }
42+
43+ @Test ( "GetCustomDecorators single" )
44+ public GetCustomDecoratorsSingle ( ) : void {
45+ const source = `/** !CompileMembersOnly */
46+ enum TestEnum {
47+ val1 = 0,
48+ val2 = 2,
49+ val3,
50+ val4 = "bye",
51+ }
52+
53+ const a = TestEnum.val1;` ;
54+
55+ const [ sourceFile , typeChecker ] = util . parseTypeScript ( source ) ;
56+ const identifier = util . findFirstChild ( sourceFile , ts . isIdentifier ) ;
57+ const enumType = typeChecker . getTypeAtLocation ( identifier ) ;
58+
59+ const decorators = tsHelper . getCustomDecorators ( enumType , typeChecker ) ;
60+
61+ Expect ( decorators . size ) . toBe ( 1 ) ;
62+ Expect ( decorators . has ( DecoratorKind . CompileMembersOnly ) ) . toBeTruthy ( ) ;
63+ }
64+
65+ @Test ( "GetCustomDecorators multiple" )
66+ public GetCustomDecoratorsMultiple ( ) : void {
67+ const source = `/** !CompileMembersOnly
68+ * !Phantom */
69+ enum TestEnum {
70+ val1 = 0,
71+ val2 = 2,
72+ val3,
73+ val4 = "bye",
74+ }
75+
76+ const a = TestEnum.val1;` ;
77+
78+ const [ sourceFile , typeChecker ] = util . parseTypeScript ( source ) ;
79+ const identifier = util . findFirstChild ( sourceFile , ts . isIdentifier ) ;
80+ const enumType = typeChecker . getTypeAtLocation ( identifier ) ;
81+
82+ const decorators = tsHelper . getCustomDecorators ( enumType , typeChecker ) ;
83+
84+ Expect ( decorators . size ) . toBe ( 2 ) ;
85+ Expect ( decorators . has ( DecoratorKind . CompileMembersOnly ) ) . toBeTruthy ( ) ;
86+ Expect ( decorators . has ( DecoratorKind . Phantom ) ) . toBeTruthy ( ) ;
87+ }
88+
89+ @Test ( "GetCustomDecorators single jsdoc" )
90+ public GetCustomDecoratorsSingleJSDoc ( ) : void {
91+ const source = `/** @CompileMembersOnly */
92+ enum TestEnum {
93+ val1 = 0,
94+ val2 = 2,
95+ val3,
96+ val4 = "bye",
97+ }
98+
99+ const a = TestEnum.val1;` ;
100+
101+ const [ sourceFile , typeChecker ] = util . parseTypeScript ( source ) ;
102+ const identifier = util . findFirstChild ( sourceFile , ts . isIdentifier ) ;
103+ const enumType = typeChecker . getTypeAtLocation ( identifier ) ;
104+
105+ const decorators = tsHelper . getCustomDecorators ( enumType , typeChecker ) ;
106+
107+ Expect ( decorators . size ) . toBe ( 1 ) ;
108+ Expect ( decorators . has ( DecoratorKind . CompileMembersOnly ) ) . toBeTruthy ( ) ;
109+ }
110+
111+ @Test ( "GetCustomDecorators multiple jsdoc" )
112+ public GetCustomDecoratorsMultipleJSDoc ( ) : void {
113+ const source = `/** @Phantom
114+ * @CompileMembersOnly */
115+ enum TestEnum {
116+ val1 = 0,
117+ val2 = 2,
118+ val3,
119+ val4 = "bye",
120+ }
121+
122+ const a = TestEnum.val1;` ;
123+
124+ const [ sourceFile , typeChecker ] = util . parseTypeScript ( source ) ;
125+ const identifier = util . findFirstChild ( sourceFile , ts . isIdentifier ) ;
126+ const enumType = typeChecker . getTypeAtLocation ( identifier ) ;
127+
128+ const decorators = tsHelper . getCustomDecorators ( enumType , typeChecker ) ;
129+
130+ Expect ( decorators . size ) . toBe ( 2 ) ;
131+ Expect ( decorators . has ( DecoratorKind . Phantom ) ) . toBeTruthy ( ) ;
132+ Expect ( decorators . has ( DecoratorKind . CompileMembersOnly ) ) . toBeTruthy ( ) ;
133+ }
134+
135+ @Test ( "GetCustomDecorators multiple default jsdoc" )
136+ public GetCustomDecoratorsMultipleDefaultJSDoc ( ) : void {
137+ const source = `/**
138+ * @description
139+ * @param abc def
140+ * @Phantom
141+ * @CompileMembersOnly */
142+ enum TestEnum {
143+ val1 = 0,
144+ val2 = 2,
145+ val3,
146+ val4 = "bye",
147+ }
148+
149+ const a = TestEnum.val1;` ;
150+
151+ const [ sourceFile , typeChecker ] = util . parseTypeScript ( source ) ;
152+ const identifier = util . findFirstChild ( sourceFile , ts . isIdentifier ) ;
153+ const enumType = typeChecker . getTypeAtLocation ( identifier ) ;
154+
155+ const decorators = tsHelper . getCustomDecorators ( enumType , typeChecker ) ;
156+
157+ Expect ( decorators . size ) . toBe ( 2 ) ;
158+ Expect ( decorators . has ( DecoratorKind . Phantom ) ) . toBeTruthy ( ) ;
159+ Expect ( decorators . has ( DecoratorKind . CompileMembersOnly ) ) . toBeTruthy ( ) ;
38160 }
39161}
0 commit comments