@@ -7,17 +7,21 @@ namespace ts {
77 options ?: TranspileOptions ;
88 expectedOutput ?: string ;
99 expectedDiagnosticCodes ?: number [ ] ;
10+ expectedDiagnosticTexts ?: string [ ] ;
1011 }
1112
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 ) ;
1517 }
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" ) ;
2125 }
2226
2327 function test ( input : string , testSettings : TranspileTestSettings ) : void {
@@ -26,7 +30,7 @@ namespace ts {
2630 if ( ! transpileOptions . compilerOptions ) {
2731 transpileOptions . compilerOptions = { } ;
2832 }
29- if ( transpileOptions . compilerOptions . newLine === undefined ) { //
33+ if ( transpileOptions . compilerOptions . newLine === undefined ) {
3034 // use \r\n as default new line
3135 transpileOptions . compilerOptions . newLine = ts . NewLineKind . CarriageReturnLineFeed ;
3236 }
@@ -36,7 +40,7 @@ namespace ts {
3640 transpileOptions . reportDiagnostics = true ;
3741 const transpileModuleResult = transpileModule ( input , transpileOptions ) ;
3842
39- checkDiagnostics ( transpileModuleResult . diagnostics , testSettings . expectedDiagnosticCodes ) ;
43+ checkDiagnostics ( transpileModuleResult . diagnostics , testSettings . expectedDiagnosticCodes , testSettings . expectedDiagnosticTexts ) ;
4044
4145 if ( testSettings . expectedOutput !== undefined ) {
4246 assert . equal ( transpileModuleResult . outputText , testSettings . expectedOutput ) ;
@@ -45,7 +49,7 @@ namespace ts {
4549 if ( canUseOldTranspile ) {
4650 const diagnostics : Diagnostic [ ] = [ ] ;
4751 const transpileResult = transpile ( input , transpileOptions . compilerOptions , transpileOptions . fileName , diagnostics , transpileOptions . moduleName ) ;
48- checkDiagnostics ( diagnostics , testSettings . expectedDiagnosticCodes ) ;
52+ checkDiagnostics ( diagnostics , testSettings . expectedDiagnosticCodes , testSettings . expectedDiagnosticTexts ) ;
4953 if ( testSettings . expectedOutput ) {
5054 assert . equal ( transpileResult , testSettings . expectedOutput ) ;
5155 }
@@ -292,13 +296,37 @@ var x = 0;`,
292296 const output = `"use strict";\nvar a = 10;\n` ;
293297 test ( input , {
294298 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 }
297300 } ) ;
298301 } ) ;
299302
300303 it ( "Supports urls in file name" , ( ) => {
301304 test ( "var x" , { expectedOutput : `"use strict";\r\nvar x;\r\n` , options : { fileName : "http://somewhere/directory//directory2/file.ts" } } ) ;
302305 } ) ;
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+ } ) ;
303331 } ) ;
304332}
0 commit comments