@@ -7,17 +7,21 @@ namespace ts {
7
7
options ?: TranspileOptions ;
8
8
expectedOutput ?: string ;
9
9
expectedDiagnosticCodes ?: number [ ] ;
10
+ expectedDiagnosticTexts ?: string [ ] ;
10
11
}
11
12
12
- function checkDiagnostics ( diagnostics : Diagnostic [ ] , expectedDiagnosticCodes ?: number [ ] ) {
13
- if ( ! expectedDiagnosticCodes ) {
14
- return ;
13
+ function checkDiagnostics ( diagnostics : Diagnostic [ ] , expectedDiagnosticCodes : number [ ] = [ ] , expectedDiagnosticTexts ?: string [ ] ) {
14
+ const n = expectedDiagnosticCodes . length ;
15
+ if ( expectedDiagnosticTexts ) {
16
+ assert . equal ( n , expectedDiagnosticTexts . length ) ;
15
17
}
16
-
17
- for ( let i = 0 ; i < expectedDiagnosticCodes . length ; i ++ ) {
18
- assert . equal ( expectedDiagnosticCodes [ i ] , diagnostics [ i ] && diagnostics [ i ] . code , `Could not find expeced diagnostic.` ) ;
19
- }
20
- assert . equal ( diagnostics . length , expectedDiagnosticCodes . length , "Resuting diagnostics count does not match expected" ) ;
18
+ for ( let i = 0 ; i < n ; i ++ ) {
19
+ assert . equal ( expectedDiagnosticCodes [ i ] , diagnostics [ i ] && diagnostics [ i ] . code , `Could not find expected diagnostic.` ) ;
20
+ if ( expectedDiagnosticTexts ) {
21
+ assert . equal ( expectedDiagnosticTexts [ i ] , diagnostics [ i ] && diagnostics [ i ] . messageText ) ;
22
+ }
23
+ } ;
24
+ assert . equal ( diagnostics . length , n , "Resuting diagnostics count does not match expected" ) ;
21
25
}
22
26
23
27
function test ( input : string , testSettings : TranspileTestSettings ) : void {
@@ -26,7 +30,7 @@ namespace ts {
26
30
if ( ! transpileOptions . compilerOptions ) {
27
31
transpileOptions . compilerOptions = { } ;
28
32
}
29
- if ( transpileOptions . compilerOptions . newLine === undefined ) { //
33
+ if ( transpileOptions . compilerOptions . newLine === undefined ) {
30
34
// use \r\n as default new line
31
35
transpileOptions . compilerOptions . newLine = ts . NewLineKind . CarriageReturnLineFeed ;
32
36
}
@@ -36,7 +40,7 @@ namespace ts {
36
40
transpileOptions . reportDiagnostics = true ;
37
41
const transpileModuleResult = transpileModule ( input , transpileOptions ) ;
38
42
39
- checkDiagnostics ( transpileModuleResult . diagnostics , testSettings . expectedDiagnosticCodes ) ;
43
+ checkDiagnostics ( transpileModuleResult . diagnostics , testSettings . expectedDiagnosticCodes , testSettings . expectedDiagnosticTexts ) ;
40
44
41
45
if ( testSettings . expectedOutput !== undefined ) {
42
46
assert . equal ( transpileModuleResult . outputText , testSettings . expectedOutput ) ;
@@ -45,7 +49,7 @@ namespace ts {
45
49
if ( canUseOldTranspile ) {
46
50
const diagnostics : Diagnostic [ ] = [ ] ;
47
51
const transpileResult = transpile ( input , transpileOptions . compilerOptions , transpileOptions . fileName , diagnostics , transpileOptions . moduleName ) ;
48
- checkDiagnostics ( diagnostics , testSettings . expectedDiagnosticCodes ) ;
52
+ checkDiagnostics ( diagnostics , testSettings . expectedDiagnosticCodes , testSettings . expectedDiagnosticTexts ) ;
49
53
if ( testSettings . expectedOutput ) {
50
54
assert . equal ( transpileResult , testSettings . expectedOutput ) ;
51
55
}
@@ -292,13 +296,37 @@ var x = 0;`,
292
296
const output = `"use strict";\nvar a = 10;\n` ;
293
297
test ( input , {
294
298
expectedOutput : output ,
295
- options : { compilerOptions : { newLine : NewLineKind . LineFeed , module : ModuleKind . CommonJS } , fileName : "input.js" , reportDiagnostics : true } ,
296
- expectedDiagnosticCodes : [ ]
299
+ options : { compilerOptions : { newLine : NewLineKind . LineFeed , module : ModuleKind . CommonJS } , fileName : "input.js" , reportDiagnostics : true }
297
300
} ) ;
298
301
} ) ;
299
302
300
303
it ( "Supports urls in file name" , ( ) => {
301
304
test ( "var x" , { expectedOutput : `"use strict";\r\nvar x;\r\n` , options : { fileName : "http://somewhere/directory//directory2/file.ts" } } ) ;
302
305
} ) ;
306
+
307
+ describe ( "String values for enums" , ( ) => {
308
+ it ( "Accepts strings instead of enum values" , ( ) => {
309
+ test ( `export const x = 0` , {
310
+ options : {
311
+ compilerOptions : {
312
+ module : < ModuleKind > < any > "es6" ,
313
+ // Capitalization and spaces ignored
314
+ target : < ScriptTarget > < any > " Es6 "
315
+ }
316
+ } ,
317
+ expectedOutput : "export const x = 0;\r\n"
318
+ } ) ;
319
+ } ) ;
320
+
321
+ it ( "Fails on bad value" , ( ) => {
322
+ for ( const value in [ 123 , { } , "" ] ) {
323
+ test ( `` , {
324
+ options : { compilerOptions : { module : < ModuleKind > < any > value } } ,
325
+ expectedDiagnosticCodes : [ 6046 ] ,
326
+ expectedDiagnosticTexts : [ "Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015'" ]
327
+ } ) ;
328
+ }
329
+ } ) ;
330
+ } ) ;
303
331
} ) ;
304
332
}
0 commit comments