11import * as assert from "assert" ;
22import * as vs from "vscode" ;
3- import { activate , currentDoc , currentEditor , documentEol , emptyExcludedFile , emptyFileInExcludedFolder , openFile , positionOf , setTestContent } from "../../helpers" ;
3+ import { platformEol } from "../../../src/debug/utils" ;
4+ import { activate , currentDoc , currentEditor , emptyExcludedFile , emptyFileInExcludedFolder , openFile , positionOf , setConfigForTest , setTestContent } from "../../helpers" ;
45
56const formattingOptions : vs . FormattingOptions = { tabSize : 2 , insertSpaces : true } ;
67
78describe ( "dart_formatting_edit_provider" , ( ) => {
89
910 beforeEach ( "activate" , ( ) => activate ( ) ) ;
1011
12+ const unformattedContent = ` main ( ) { }${ platformEol } ` ;
13+ const formattedContent = `main() {}${ platformEol } ` ;
14+
1115 async function formatDocument ( expectResult = true ) : Promise < void > {
1216 const formatResult = await ( vs . commands . executeCommand ( "vscode.executeFormatDocumentProvider" , currentDoc ( ) . uri , formattingOptions ) as Thenable < vs . TextEdit [ ] > ) ;
1317 if ( expectResult ) {
@@ -28,29 +32,51 @@ describe("dart_formatting_edit_provider", () => {
2832 }
2933
3034 it ( "formats the document" , async ( ) => {
31- await setTestContent ( " main ( ) { }" ) ;
35+ await setTestContent ( unformattedContent ) ;
3236 await formatDocument ( ) ;
33- assert . equal ( currentDoc ( ) . getText ( ) , `main() {}${ documentEol } ` ) ;
37+ assert . equal ( currentDoc ( ) . getText ( ) , formattedContent ) ;
38+ } ) ;
39+
40+ it ( "formats the document on save" , async ( ) => {
41+ await setTestContent ( unformattedContent ) ;
42+ await currentDoc ( ) . save ( ) ;
43+ assert . equal ( currentDoc ( ) . getText ( ) , formattedContent ) ;
44+ } ) ;
45+
46+ it ( "does not format the document if disabled" , async ( ) => {
47+ await setConfigForTest ( "dart" , "enableSdkFormatter" , false ) ;
48+ await setTestContent ( unformattedContent ) ;
49+ await currentDoc ( ) . save ( ) ;
50+ assert . equal ( currentDoc ( ) . getText ( ) , unformattedContent ) ;
51+ } ) ;
52+
53+
54+ it ( "formats the document if re-enabled" , async ( ) => {
55+ await setConfigForTest ( "dart" , "enableSdkFormatter" , false ) ;
56+ await setConfigForTest ( "dart" , "enableSdkFormatter" , true ) ;
57+ await setTestContent ( unformattedContent ) ;
58+ await currentDoc ( ) . save ( ) ;
59+ assert . equal ( currentDoc ( ) . getText ( ) , formattedContent ) ;
3460 } ) ;
3561
3662 it ( "formats the code when typing a }" , async ( ) => {
3763 // Currently we just format the whole doc on format as out formatter doesn't support ranges.
38- await setTestContent ( " main ( ) { }" ) ;
64+ await setTestContent ( unformattedContent ) ;
3965 await formatOnType ( "{ ^" , "}" ) ;
40- assert . equal ( currentDoc ( ) . getText ( ) , `main() {} ${ documentEol } ` ) ;
66+ assert . equal ( currentDoc ( ) . getText ( ) , formattedContent ) ;
4167 } ) ;
4268
4369 it ( "does not format an excluded file" , async ( ) => {
4470 await openFile ( emptyExcludedFile ) ;
45- await setTestContent ( " main ( ) { }" ) ;
71+ await setTestContent ( unformattedContent ) ;
4672 await formatDocument ( false ) ;
47- assert . equal ( currentDoc ( ) . getText ( ) , " main ( ) { }" ) ;
73+ assert . equal ( currentDoc ( ) . getText ( ) , unformattedContent ) ;
4874 } ) ;
4975
5076 it ( "does not format a file in an excluded folder" , async ( ) => {
5177 await openFile ( emptyFileInExcludedFolder ) ;
52- await setTestContent ( " main ( ) { }" ) ;
78+ await setTestContent ( unformattedContent ) ;
5379 await formatDocument ( false ) ;
54- assert . equal ( currentDoc ( ) . getText ( ) , " main ( ) { }" ) ;
80+ assert . equal ( currentDoc ( ) . getText ( ) , unformattedContent ) ;
5581 } ) ;
5682} ) ;
0 commit comments