Skip to content

Commit a9a3e53

Browse files
author
Christoph Bühler
committed
add tests
1 parent 2165721 commit a9a3e53

File tree

3 files changed

+124
-3
lines changed

3 files changed

+124
-3
lines changed

src/code-generators/typescript-generators/accessorDeclaration.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ export function generateAccessorDeclaration(
1818
let definitionLine: string;
1919
if (accessor instanceof SetterDeclaration) {
2020
definitionLine = `${tabs}${accessor.visibility !== undefined ? getVisibilityText(accessor.visibility) + ' ' : ''}` +
21-
`set ${accessor.name}(value${accessor.type ? `: ${accessor.type}` : ''}) {`;
21+
`${accessor.isAbstract ? 'abstract ' : ''}` +
22+
`set ${accessor.name}(value${accessor.type ? `: ${accessor.type}` : ''})`;
2223
} else {
2324
definitionLine = `${tabs}${accessor.visibility !== undefined ? getVisibilityText(accessor.visibility) + ' ' : ''}` +
24-
`get ${accessor.name}()${accessor.type ? `: ${accessor.type}` : ''} {`;
25+
`${accessor.isAbstract ? 'abstract ' : ''}` +
26+
`get ${accessor.name}()${accessor.type ? `: ${accessor.type}` : ''}`;
2527
}
2628

27-
return `${definitionLine}
29+
if (accessor.isAbstract) {
30+
return `${definitionLine};`;
31+
}
32+
33+
return `${definitionLine} {
2834
${tabs}${tabs}throw new Error('Not implemented yet.');
2935
${tabs}}\n`;
3036
}

test/code-generators/TypescriptCodeGenerator.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { GetterDeclaration, SetterDeclaration } from '../../src/declarations/AccessorDeclaration';
12
import { TypescriptCodeGenerator } from '../../src/code-generators/TypescriptCodeGenerator';
23
import { TypescriptGenerationOptions } from '../../src/code-generators/TypescriptGenerationOptions';
34
import { ClassDeclaration } from '../../src/declarations';
@@ -83,6 +84,24 @@ describe('TypescriptCodeGenerator', () => {
8384
defaultImport,
8485
defaultWithNamed,
8586
defaultWithNamedMultiline,
87+
new GetterDeclaration('pubGetter', DeclarationVisibility.Public, 'string', false),
88+
new GetterDeclaration('protGetter', DeclarationVisibility.Protected, 'string', false),
89+
new GetterDeclaration('privGetter', DeclarationVisibility.Private, 'string', false),
90+
new GetterDeclaration('pubNoTypeGetter', DeclarationVisibility.Public, undefined, false),
91+
new GetterDeclaration('protNoTypeGetter', DeclarationVisibility.Protected, undefined, false),
92+
new GetterDeclaration('privNoTypeGetter', DeclarationVisibility.Private, undefined, false),
93+
new GetterDeclaration('pubAbsGetter', DeclarationVisibility.Public, 'number', true),
94+
new GetterDeclaration('protAbsGetter', DeclarationVisibility.Protected, 'number', true),
95+
new GetterDeclaration('privAbsGetter', DeclarationVisibility.Private, 'number', true),
96+
new SetterDeclaration('pubSetter', DeclarationVisibility.Public, 'string', false),
97+
new SetterDeclaration('protSetter', DeclarationVisibility.Protected, 'string', false),
98+
new SetterDeclaration('privSetter', DeclarationVisibility.Private, 'string', false),
99+
new SetterDeclaration('pubNoTypeSetter', DeclarationVisibility.Public, undefined, false),
100+
new SetterDeclaration('protNoTypeSetter', DeclarationVisibility.Protected, undefined, false),
101+
new SetterDeclaration('privNoTypeSetter', DeclarationVisibility.Private, undefined, false),
102+
new SetterDeclaration('pubAbsSetter', DeclarationVisibility.Public, 'number', true),
103+
new SetterDeclaration('protAbsSetter', DeclarationVisibility.Protected, 'number', true),
104+
new SetterDeclaration('privAbsSetter', DeclarationVisibility.Private, 'number', true),
86105
];
87106

88107
for (const generatable of generatables) {

test/code-generators/__snapshots__/TypescriptCodeGenerator.spec.ts.snap

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,54 @@
22

33
exports[`TypescriptCodeGenerator should generate the correct code for ExternalModuleImport 1`] = `"import externalAlias = require('externalModuleLib');"`;
44

5+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 1`] = `
6+
" public get pubGetter(): string {
7+
throw new Error('Not implemented yet.');
8+
}
9+
"
10+
`;
11+
12+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 2`] = `
13+
" protected get protGetter(): string {
14+
throw new Error('Not implemented yet.');
15+
}
16+
"
17+
`;
18+
19+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 3`] = `
20+
" private get privGetter(): string {
21+
throw new Error('Not implemented yet.');
22+
}
23+
"
24+
`;
25+
26+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 4`] = `
27+
" public get pubNoTypeGetter() {
28+
throw new Error('Not implemented yet.');
29+
}
30+
"
31+
`;
32+
33+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 5`] = `
34+
" protected get protNoTypeGetter() {
35+
throw new Error('Not implemented yet.');
36+
}
37+
"
38+
`;
39+
40+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 6`] = `
41+
" private get privNoTypeGetter() {
42+
throw new Error('Not implemented yet.');
43+
}
44+
"
45+
`;
46+
47+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 7`] = `" public abstract get pubAbsGetter(): number;"`;
48+
49+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 8`] = `" protected abstract get protAbsGetter(): number;"`;
50+
51+
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 9`] = `" private abstract get privAbsGetter(): number;"`;
52+
553
exports[`TypescriptCodeGenerator should generate the correct code for MethodDeclaration 1`] = `
654
" public myMethod(): void {
755
throw new Error('Not implemented yet.');
@@ -113,6 +161,54 @@ exports[`TypescriptCodeGenerator should generate the correct code for PropertyDe
113161
"
114162
`;
115163

164+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 1`] = `
165+
" public set pubSetter(value: string) {
166+
throw new Error('Not implemented yet.');
167+
}
168+
"
169+
`;
170+
171+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 2`] = `
172+
" protected set protSetter(value: string) {
173+
throw new Error('Not implemented yet.');
174+
}
175+
"
176+
`;
177+
178+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 3`] = `
179+
" private set privSetter(value: string) {
180+
throw new Error('Not implemented yet.');
181+
}
182+
"
183+
`;
184+
185+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 4`] = `
186+
" public set pubNoTypeSetter(value) {
187+
throw new Error('Not implemented yet.');
188+
}
189+
"
190+
`;
191+
192+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 5`] = `
193+
" protected set protNoTypeSetter(value) {
194+
throw new Error('Not implemented yet.');
195+
}
196+
"
197+
`;
198+
199+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 6`] = `
200+
" private set privNoTypeSetter(value) {
201+
throw new Error('Not implemented yet.');
202+
}
203+
"
204+
`;
205+
206+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 7`] = `" public abstract set pubAbsSetter(value: number);"`;
207+
208+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 8`] = `" protected abstract set protAbsSetter(value: number);"`;
209+
210+
exports[`TypescriptCodeGenerator should generate the correct code for SetterDeclaration 9`] = `" private abstract set privAbsSetter(value: number);"`;
211+
116212
exports[`TypescriptCodeGenerator should generate the correct code for StringImport 1`] = `"import 'stringLib';"`;
117213

118214
exports[`TypescriptCodeGenerator should generate the correct code for SymbolSpecifier 1`] = `"SymbolSpecifier"`;

0 commit comments

Comments
 (0)