Skip to content

Commit 59ad4ab

Browse files
committed
Make container group assert methods fail eagerly if the location does not exist
- JctCompilationAssert#packageGroup(Location) now throws an AssertionError if the location did not have any associated group with it. - JctCompilationAssert#moduleGroup(Location) now throws an AssertionError if the location did not have any associated group with it. - JctCompilationAssert#outputGroup(Location) now throws an AssertionError if the location did not have any associated group with it. - Improved error messages in JctCompilationAssert.
1 parent fc263bb commit 59ad4ab

File tree

1 file changed

+53
-24
lines changed

1 file changed

+53
-24
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/assertions/JctCompilationAssert.java

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static java.util.stream.Collectors.toUnmodifiableList;
2020

2121
import io.github.ascopes.jct.compilers.JctCompilation;
22+
import io.github.ascopes.jct.containers.ContainerGroup;
2223
import io.github.ascopes.jct.repr.TraceDiagnosticListRepresentation;
2324
import io.github.ascopes.jct.utils.StringUtils;
2425
import java.util.Collection;
@@ -31,8 +32,6 @@
3132
import org.assertj.core.api.AbstractAssert;
3233
import org.assertj.core.api.AbstractListAssert;
3334
import org.assertj.core.api.AbstractStringAssert;
34-
import org.assertj.core.api.AssertFactory;
35-
import org.assertj.core.api.Assertions;
3635
import org.assertj.core.api.FactoryBasedNavigableListAssert;
3736
import org.assertj.core.api.StringAssert;
3837
import org.jspecify.annotations.Nullable;
@@ -156,9 +155,11 @@ public TraceDiagnosticListAssert diagnostics() {
156155
*
157156
* @param location the location to configure.
158157
* @return the assertions to perform.
159-
* @throws AssertionError if the compilation was null.
158+
* @throws AssertionError if the compilation was null, or no group for the location
159+
* was found.
160160
* @throws IllegalArgumentException if the location was
161-
* {@link Location#isModuleOrientedLocation() module-oriented}.
161+
* {@link Location#isModuleOrientedLocation() module-oriented}
162+
* or {@link Location#isOutputLocation() an output location}.
162163
* @throws NullPointerException if the provided location object is null.
163164
*/
164165
public PackageContainerGroupAssert packageGroup(Location location) {
@@ -170,11 +171,17 @@ public PackageContainerGroupAssert packageGroup(Location location) {
170171
);
171172
}
172173

174+
if (location.isOutputLocation()) {
175+
throw new IllegalArgumentException(
176+
"Expected location " + location + " to not be an output location"
177+
);
178+
}
179+
173180
isNotNull();
174181

175-
return new PackageContainerGroupAssert(
176-
actual.getFileManager().getPackageContainerGroup(location)
177-
);
182+
var group = actual.getFileManager().getPackageContainerGroup(location);
183+
assertLocationExists(location, group);
184+
return new PackageContainerGroupAssert(group);
178185
}
179186

180187
/**
@@ -184,7 +191,8 @@ public PackageContainerGroupAssert packageGroup(Location location) {
184191
*
185192
* @param location the location to configure.
186193
* @return the assertions to perform.
187-
* @throws AssertionError if the compilation was null.
194+
* @throws AssertionError if the compilation was null, or no group for the location
195+
* was found.
188196
* @throws IllegalArgumentException if the location is not
189197
* {@link Location#isModuleOrientedLocation() module-oriented}.
190198
* @throws NullPointerException if the provided location object is null.
@@ -194,15 +202,21 @@ public ModuleContainerGroupAssert moduleGroup(Location location) {
194202

195203
if (!location.isModuleOrientedLocation()) {
196204
throw new IllegalArgumentException(
197-
"Expected location " + location + " to be module-oriented"
205+
"Expected location " + location.getName() + " to be module-oriented"
206+
);
207+
}
208+
209+
if (location.isOutputLocation()) {
210+
throw new IllegalArgumentException(
211+
"Expected location " + location.getName() + " to not be an output location"
198212
);
199213
}
200214

201215
isNotNull();
202216

203-
return new ModuleContainerGroupAssert(
204-
actual.getFileManager().getModuleContainerGroup(location)
205-
);
217+
var group = actual.getFileManager().getModuleContainerGroup(location);
218+
assertLocationExists(location, group);
219+
return new ModuleContainerGroupAssert(group);
206220
}
207221

208222
/**
@@ -212,7 +226,8 @@ public ModuleContainerGroupAssert moduleGroup(Location location) {
212226
*
213227
* @param location the location to configure.
214228
* @return the assertions to perform.
215-
* @throws AssertionError if the compilation was null.
229+
* @throws AssertionError if the compilation was null, or no group for the location
230+
* was found.
216231
* @throws IllegalArgumentException if the location is not
217232
* {@link Location#isOutputLocation() an output location}.
218233
* @throws NullPointerException if the provided location object is null.
@@ -222,15 +237,15 @@ public OutputContainerGroupAssert outputGroup(Location location) {
222237

223238
if (!location.isOutputLocation()) {
224239
throw new IllegalArgumentException(
225-
"Expected location " + location + " to be an output location"
240+
"Expected location " + location.getName() + " to be an output location"
226241
);
227242
}
228243

229244
isNotNull();
230245

231-
return new OutputContainerGroupAssert(
232-
actual.getFileManager().getOutputContainerGroup(location)
233-
);
246+
var group = actual.getFileManager().getOutputContainerGroup(location);
247+
assertLocationExists(location, group);
248+
return new OutputContainerGroupAssert(group);
234249
}
235250

236251
/**
@@ -239,7 +254,8 @@ public OutputContainerGroupAssert outputGroup(Location location) {
239254
* <p>If not configured, the value being asserted on will be {@code null} in value.
240255
*
241256
* @return the assertions to perform on the class outputs.
242-
* @throws AssertionError if the compilation is null.
257+
* @throws AssertionError if the compilation was null, or no group for the location
258+
* was found.
243259
*/
244260
public OutputContainerGroupAssert classOutput() {
245261
return outputGroup(StandardLocation.CLASS_OUTPUT);
@@ -251,7 +267,8 @@ public OutputContainerGroupAssert classOutput() {
251267
* <p>If not configured, the value being asserted on will be {@code null} in value.
252268
*
253269
* @return the assertions to perform on the source outputs.
254-
* @throws AssertionError if the compilation is null.
270+
* @throws AssertionError if the compilation was null, or no group for the location
271+
* was found.
255272
*/
256273
public OutputContainerGroupAssert sourceOutput() {
257274
return outputGroup(StandardLocation.SOURCE_OUTPUT);
@@ -263,12 +280,14 @@ public OutputContainerGroupAssert sourceOutput() {
263280
* <p>If not configured, the value being asserted on will be {@code null} in value.
264281
*
265282
* @return the assertions to perform on the header outputs.
266-
* @throws AssertionError if the compilation is null.
283+
* @throws AssertionError if the compilation was null, or no group for the location
284+
* was found.
267285
* @deprecated this method is rarely needed, so is being removed in v1.0.0. Use
268286
* {@link #outputGroup(Location)}, passing {@link StandardLocation#NATIVE_HEADER_OUTPUT} as the
269287
* first parameter instead.
270288
*/
271289
@Deprecated(since = "0.6.0", forRemoval = true)
290+
@SuppressWarnings("DeprecatedIsStillUsed")
272291
public OutputContainerGroupAssert generatedHeaders() {
273292
return outputGroup(StandardLocation.NATIVE_HEADER_OUTPUT);
274293
}
@@ -279,7 +298,8 @@ public OutputContainerGroupAssert generatedHeaders() {
279298
* <p>If not configured, the value being asserted on will be {@code null} in value.
280299
*
281300
* @return the assertions to perform on the class path.
282-
* @throws AssertionError if the compilation is null.
301+
* @throws AssertionError if the compilation was null, or no group for the location
302+
* was found.
283303
*/
284304
public PackageContainerGroupAssert classPath() {
285305
return packageGroup(StandardLocation.CLASS_PATH);
@@ -291,7 +311,8 @@ public PackageContainerGroupAssert classPath() {
291311
* <p>If not configured, the value being asserted on will be {@code null} in value.
292312
*
293313
* @return the assertions to perform on the source path.
294-
* @throws AssertionError if the compilation is null.
314+
* @throws AssertionError if the compilation was null, or no group for the location
315+
* was found.
295316
*/
296317
public PackageContainerGroupAssert sourcePath() {
297318
return packageGroup(StandardLocation.SOURCE_PATH);
@@ -303,7 +324,8 @@ public PackageContainerGroupAssert sourcePath() {
303324
* <p>If not configured, the value being asserted on will be {@code null} in value.
304325
*
305326
* @return the assertions to perform on the source path.
306-
* @throws AssertionError if the compilation is null.
327+
* @throws AssertionError if the compilation was null, or no group for the location
328+
* was found.
307329
*/
308330
public ModuleContainerGroupAssert moduleSourcePath() {
309331
return moduleGroup(StandardLocation.MODULE_SOURCE_PATH);
@@ -315,7 +337,8 @@ public ModuleContainerGroupAssert moduleSourcePath() {
315337
* <p>If not configured, the value being asserted on will be {@code null} in value.
316338
*
317339
* @return the assertions to perform on the module path.
318-
* @throws AssertionError if the compilation is null.
340+
* @throws AssertionError if the compilation was null, or no group for the location
341+
* was found.
319342
*/
320343
public ModuleContainerGroupAssert modulePath() {
321344
return moduleGroup(StandardLocation.MODULE_PATH);
@@ -348,4 +371,10 @@ private void failWithDiagnostics(
348371
failWithMessage(fullMessage);
349372
}
350373
}
374+
375+
private void assertLocationExists(Location location, ContainerGroup group) {
376+
if (group == null) {
377+
throw new AssertionError("No location named " + location.getName() + " exists");
378+
}
379+
}
351380
}

0 commit comments

Comments
 (0)