1717package com.palantir.javaformat.gradle
1818
1919import spock.lang.Specification
20+ import spock.lang.Unroll
2021
2122class ConfigureJavaFormatterXmlTest extends Specification {
2223
@@ -60,6 +61,7 @@ class ConfigureJavaFormatterXmlTest extends Specification {
6061 </component>
6162 </root>
6263 """ . stripIndent()
64+ public static final ArrayList<String > ACTIONS_ON_SAVE = [' Format' , ' Optimize' ]
6365
6466 void testConfigure_missingEntireBlock_added () {
6567 def node = new XmlParser (). parseText(MISSING_ENTIRE_BLOCK )
@@ -104,41 +106,43 @@ class ConfigureJavaFormatterXmlTest extends Specification {
104106 xmlToString(node) == EXPECTED
105107 }
106108
107- void ' adds FormatOnSave block where none exists' () {
109+ @Unroll
110+ void ' adds #action OnSave block where none exists' (action) {
108111 // language=xml
109112 def node = new XmlParser (). parseText '''
110113 <root>
111114 </root>
112115 ''' . stripIndent(true )
113116
114117 when :
115- ConfigureJavaFormatterXml . configureFormatOnSave(node)
116- def newXml = xmlToString(node). strip()
118+ ConfigureJavaFormatterXml . configureWorkspaceXml(node)
119+
120+ def newXml = xmlSubcomponentToString(node, " ${ action} OnSaveOptions" ). strip()
117121
118122 then :
119- // language=xml
120- def expected = '''
121- <root>
122- <component name="FormatOnSaveOptions">
123- <option name="myRunOnSave" value="true"/>
124- <option name="myAllFileTypesSelected" value="false"/>
125- <option name="mySelectedFileTypes">
126- <set>
127- <option value="JAVA"/>
128- </set>
129- </option>
130- </component>
131- </root>
132- ''' . stripIndent(true ). strip()
123+ def expected = """
124+ <component name="${ action} OnSaveOptions">
125+ <option name="myRunOnSave" value="true"/>
126+ <option name="myAllFileTypesSelected" value="false"/>
127+ <option name="mySelectedFileTypes">
128+ <set>
129+ <option value="JAVA"/>
130+ </set>
131+ </option>
132+ </component>
133+ """ . stripIndent(true ). strip()
133134
134135 newXml == expected
136+
137+ where :
138+ action << ACTIONS_ON_SAVE
135139 }
136140
137- void ' adds Java to existing FormatOnSave block ' () {
138- // language=xml
139- def node = new XmlParser (). parseText '''
141+ @Unroll
142+ void ' adds Java to existing #action OnSave block ' (action) {
143+ def node = new XmlParser (). parseText """
140144 <root>
141- <component name="FormatOnSaveOptions ">
145+ <component name="${ action } OnSaveOptions ">
142146 <option name="myRunOnSave" value="true"/>
143147 <option name="myAllFileTypesSelected" value="false"/>
144148 <option name="mySelectedFileTypes">
@@ -148,58 +152,62 @@ class ConfigureJavaFormatterXmlTest extends Specification {
148152 </option>
149153 </component>
150154 </root>
151- ''' . stripIndent(true )
155+ """ . stripIndent(true )
152156
153157 when :
154- ConfigureJavaFormatterXml . configureFormatOnSave (node)
155- def newXml = xmlToString (node) . strip( )
158+ ConfigureJavaFormatterXml . configureWorkspaceXml (node)
159+ def newXml = xmlSubcomponentToString (node, " ${ action } OnSaveOptions " )
156160
157161 then :
158- // language=xml
159- def expected = '''
160- <root>
161- <component name="FormatOnSaveOptions">
162- <option name="myRunOnSave" value="true"/>
163- <option name="myAllFileTypesSelected" value="false"/>
164- <option name="mySelectedFileTypes">
165- <set>
166- <option value="Go"/>
167- <option value="JAVA"/>
168- </set>
169- </option>
170- </component>
171- </root>
172- ''' . stripIndent(true ). strip()
162+ def expected = """
163+ <component name="${ action} OnSaveOptions">
164+ <option name="myRunOnSave" value="true"/>
165+ <option name="myAllFileTypesSelected" value="false"/>
166+ <option name="mySelectedFileTypes">
167+ <set>
168+ <option value="Go"/>
169+ <option value="JAVA"/>
170+ </set>
171+ </option>
172+ </component>
173+ """ . stripIndent(true ). strip()
173174
174175 newXml == expected
176+
177+ where :
178+ action << ACTIONS_ON_SAVE
175179 }
176180
177- void ' if all file types are already formatted on save, dont change anything ' () {
178- // language=xml
179- def node = new XmlParser (). parseText '''
181+ @Unroll
182+ void ' if all file types are already configured to #action on save, dont change anything ' () {
183+ def node = new XmlParser (). parseText """
180184 <root>
181- <component name="FormatOnSaveOptions ">
185+ <component name="${ action } OnSaveOptions ">
182186 <option name="myRunOnSave" value="true"/>
183187 <!-- if myAllFileTypesSelected does not exist, it defaults to true -->
184188 </component>
185189 </root>
186- ''' . stripIndent(true )
190+ """ . stripIndent(true )
187191
188192 when :
189- ConfigureJavaFormatterXml . configureFormatOnSave (node)
190- def newXml = xmlToString (node). strip()
193+ ConfigureJavaFormatterXml . configureWorkspaceXml (node)
194+ def newXml = xmlSubcomponentToString (node, " ${ action } OnSaveOptions " ). strip()
191195
192196 then :
193- // language=xml
194- def expected = '''
195- <root>
196- <component name="FormatOnSaveOptions">
197- <option name="myRunOnSave" value="true"/>
198- </component>
199- </root>
200- ''' . stripIndent(true ). strip()
197+ def expected = """
198+ <component name="${ action} OnSaveOptions">
199+ <option name="myRunOnSave" value="true"/>
200+ </component>
201+ """ . stripIndent(true ). strip()
201202
202203 newXml == expected
204+
205+ where :
206+ action << ACTIONS_ON_SAVE
207+ }
208+
209+ private static String xmlSubcomponentToString (Node node , String name ) {
210+ xmlToString(node. children(). find { it. @name == name }). strip()
203211 }
204212
205213 static String xmlToString (Node node ) {
0 commit comments