Skip to content

Commit 765be8a

Browse files
author
Christoph Bühler
committed
add tests for single / double quote
1 parent 4942619 commit 765be8a

File tree

2 files changed

+145
-10
lines changed

2 files changed

+145
-10
lines changed

test/code-generators/TypescriptCodeGenerator.spec.ts

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { GetterDeclaration, SetterDeclaration } from '../../src/declarations/AccessorDeclaration';
21
import { TypescriptCodeGenerator } from '../../src/code-generators/TypescriptCodeGenerator';
32
import { TypescriptGenerationOptions } from '../../src/code-generators/TypescriptGenerationOptions';
43
import { ClassDeclaration } from '../../src/declarations';
4+
import { GetterDeclaration, SetterDeclaration } from '../../src/declarations/AccessorDeclaration';
55
import { DeclarationVisibility } from '../../src/declarations/DeclarationVisibility';
66
import { MethodDeclaration } from '../../src/declarations/MethodDeclaration';
77
import { ParameterDeclaration } from '../../src/declarations/ParameterDeclaration';
@@ -59,7 +59,27 @@ describe('TypescriptCodeGenerator', () => {
5959
stringQuoteStyle: `'`,
6060
tabSize: 4,
6161
};
62+
const impOptions: TypescriptGenerationOptions = {
63+
eol: ';',
64+
multiLineTrailingComma: true,
65+
multiLineWrapThreshold: 125,
66+
spaceBraces: true,
67+
stringQuoteStyle: `"`,
68+
tabSize: 2,
69+
};
70+
const imports = [
71+
new ExternalModuleImport('externalModuleLib', 'externalAlias'),
72+
new StringImport('stringLib'),
73+
new NamespaceImport('namespaceLib', 'namespaceAlias'),
74+
namedImport,
75+
multiLineNamedImport,
76+
new NamedImport('emptyImport'),
77+
defaultImport,
78+
defaultWithNamed,
79+
defaultWithNamedMultiline,
80+
];
6281
const generatables = [
82+
...imports,
6383
new SymbolSpecifier('SymbolSpecifier'),
6484
new SymbolSpecifier('SymbolSpecifier', 'WithAlias'),
6585
new MethodDeclaration('myMethod', false, DeclarationVisibility.Public, 'void'),
@@ -75,15 +95,6 @@ describe('TypescriptCodeGenerator', () => {
7595
new PropertyDeclaration('prvProperty', DeclarationVisibility.Private, 'boolean'),
7696
new VariableDeclaration('myVar', false, false, 'string'),
7797
new VariableDeclaration('myConst', true, false, 'string'),
78-
new ExternalModuleImport('externalModuleLib', 'externalAlias'),
79-
new StringImport('stringLib'),
80-
new NamespaceImport('namespaceLib', 'namespaceAlias'),
81-
namedImport,
82-
multiLineNamedImport,
83-
new NamedImport('emptyImport'),
84-
defaultImport,
85-
defaultWithNamed,
86-
defaultWithNamedMultiline,
8798
new GetterDeclaration('pubGetter', DeclarationVisibility.Public, 'string', false),
8899
new GetterDeclaration('protGetter', DeclarationVisibility.Protected, 'string', false),
89100
new GetterDeclaration('privGetter', DeclarationVisibility.Private, 'string', false),
@@ -114,6 +125,22 @@ describe('TypescriptCodeGenerator', () => {
114125

115126
}
116127

128+
for (const imp of imports) {
129+
130+
it(`should generate the correct code for ${imp.constructor.name} with single quote`, () => {
131+
const generator = new TypescriptCodeGenerator(defaultOptions);
132+
133+
expect(generator.generate(imp)).toMatchSnapshot();
134+
});
135+
136+
it(`should generate the correct code for ${imp.constructor.name} with double quote`, () => {
137+
const generator = new TypescriptCodeGenerator(impOptions);
138+
139+
expect(generator.generate(imp)).toMatchSnapshot();
140+
});
141+
142+
}
143+
117144
it('should throw on non generatable element', () => {
118145
const generator = new TypescriptCodeGenerator(defaultOptions);
119146

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

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
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 ExternalModuleImport with double quote 1`] = `"import externalAlias = require(\\"externalModuleLib\\");"`;
6+
7+
exports[`TypescriptCodeGenerator should generate the correct code for ExternalModuleImport with single quote 1`] = `"import externalAlias = require('externalModuleLib');"`;
8+
59
exports[`TypescriptCodeGenerator should generate the correct code for GetterDeclaration 1`] = `
610
" public get pubGetter(): string {
711
throw new Error('Not implemented yet.');
@@ -140,8 +144,108 @@ exports[`TypescriptCodeGenerator should generate the correct code for NamedImpor
140144
} from 'defaultWithNamedMultilineImport';"
141145
`;
142146

147+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 1`] = `"import { spec1, spec2 as alias2 } from \\"namedLib\\";"`;
148+
149+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 2`] = `
150+
"import {
151+
spec1,
152+
spec10,
153+
spec11,
154+
spec12,
155+
spec13,
156+
spec14,
157+
spec15,
158+
spec2,
159+
spec3,
160+
spec4,
161+
spec5,
162+
spec6,
163+
spec7,
164+
spec8,
165+
spec9,
166+
} from \\"multiLineNamedLib\\";"
167+
`;
168+
169+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 3`] = `"import { } from \\"emptyImport\\";"`;
170+
171+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 4`] = `"import Default from \\"defaultImport\\";"`;
172+
173+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 5`] = `"import Default, { spec1, spec2 as alias2 } from \\"defaultWithNamedImport\\";"`;
174+
175+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with double quote 6`] = `
176+
"import Default, {
177+
spec1,
178+
spec10,
179+
spec11,
180+
spec12,
181+
spec13,
182+
spec14,
183+
spec15,
184+
spec2,
185+
spec3,
186+
spec4,
187+
spec5,
188+
spec6,
189+
spec7,
190+
spec8,
191+
spec9,
192+
} from \\"defaultWithNamedMultilineImport\\";"
193+
`;
194+
195+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 1`] = `"import { spec1, spec2 as alias2 } from 'namedLib';"`;
196+
197+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 2`] = `
198+
"import {
199+
spec1,
200+
spec10,
201+
spec11,
202+
spec12,
203+
spec13,
204+
spec14,
205+
spec15,
206+
spec2,
207+
spec3,
208+
spec4,
209+
spec5,
210+
spec6,
211+
spec7,
212+
spec8,
213+
spec9,
214+
} from 'multiLineNamedLib';"
215+
`;
216+
217+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 3`] = `"import { } from 'emptyImport';"`;
218+
219+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 4`] = `"import Default from 'defaultImport';"`;
220+
221+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 5`] = `"import Default, { spec1, spec2 as alias2 } from 'defaultWithNamedImport';"`;
222+
223+
exports[`TypescriptCodeGenerator should generate the correct code for NamedImport with single quote 6`] = `
224+
"import Default, {
225+
spec1,
226+
spec10,
227+
spec11,
228+
spec12,
229+
spec13,
230+
spec14,
231+
spec15,
232+
spec2,
233+
spec3,
234+
spec4,
235+
spec5,
236+
spec6,
237+
spec7,
238+
spec8,
239+
spec9,
240+
} from 'defaultWithNamedMultilineImport';"
241+
`;
242+
143243
exports[`TypescriptCodeGenerator should generate the correct code for NamespaceImport 1`] = `"import * as namespaceAlias from 'namespaceLib';"`;
144244

245+
exports[`TypescriptCodeGenerator should generate the correct code for NamespaceImport with double quote 1`] = `"import * as namespaceAlias from \\"namespaceLib\\";"`;
246+
247+
exports[`TypescriptCodeGenerator should generate the correct code for NamespaceImport with single quote 1`] = `"import * as namespaceAlias from 'namespaceLib';"`;
248+
145249
exports[`TypescriptCodeGenerator should generate the correct code for ParameterDeclaration 1`] = `"param"`;
146250

147251
exports[`TypescriptCodeGenerator should generate the correct code for ParameterDeclaration 2`] = `"stringParam: string"`;
@@ -211,6 +315,10 @@ exports[`TypescriptCodeGenerator should generate the correct code for SetterDecl
211315

212316
exports[`TypescriptCodeGenerator should generate the correct code for StringImport 1`] = `"import 'stringLib';"`;
213317

318+
exports[`TypescriptCodeGenerator should generate the correct code for StringImport with double quote 1`] = `"import \\"stringLib\\";"`;
319+
320+
exports[`TypescriptCodeGenerator should generate the correct code for StringImport with single quote 1`] = `"import 'stringLib';"`;
321+
214322
exports[`TypescriptCodeGenerator should generate the correct code for SymbolSpecifier 1`] = `"SymbolSpecifier"`;
215323

216324
exports[`TypescriptCodeGenerator should generate the correct code for SymbolSpecifier 2`] = `"SymbolSpecifier as WithAlias"`;

0 commit comments

Comments
 (0)