001/* 002 * Copyright (C) 2022 - 2023, the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package io.github.ascopes.jct.assertions; 017 018import io.github.ascopes.jct.compilers.JctCompilation; 019import io.github.ascopes.jct.containers.ModuleContainerGroup; 020import io.github.ascopes.jct.containers.OutputContainerGroup; 021import io.github.ascopes.jct.containers.PackageContainerGroup; 022import io.github.ascopes.jct.diagnostics.TraceDiagnostic; 023import io.github.ascopes.jct.filemanagers.PathFileObject; 024import io.github.ascopes.jct.utils.UtilityClass; 025import java.util.List; 026import javax.tools.Diagnostic; 027import javax.tools.JavaFileManager.Location; 028import javax.tools.JavaFileObject; 029import org.apiguardian.api.API; 030import org.apiguardian.api.API.Status; 031import org.jspecify.annotations.Nullable; 032 033/** 034 * Helper class to provide fluent creation of assertions for compilations. 035 * 036 * @author Ashley Scopes 037 * @since 0.0.1 038 */ 039@API(since = "0.0.1", status = Status.STABLE) 040public final class JctAssertions extends UtilityClass { 041 042 private JctAssertions() { 043 // Disallow initialisation. 044 } 045 046 /** 047 * Perform an assertion on a compilation. 048 * 049 * <p>This is a shorthand alias for {@link #assertThatCompilation(JctCompilation)}. If you are 050 * using AssertJ assertions in your tests with static imports, you may wish to use that instead to 051 * prevent name conflicts. 052 * 053 * @param compilation the compilation to assert on. 054 * @return the assertion. 055 */ 056 public static JctCompilationAssert assertThat(@Nullable JctCompilation compilation) { 057 return assertThatCompilation(compilation); 058 } 059 060 /** 061 * Perform an assertion on a module container group. 062 * 063 * <p>This is a shorthand alias for {@link #assertThatContainerGroup(ModuleContainerGroup)}. If 064 * you are using AssertJ assertions in your tests with static imports, you may wish to use that 065 * instead to prevent name conflicts. 066 * 067 * @param moduleContainerGroup the module container group to assert on. 068 * @return the assertion. 069 */ 070 public static ModuleContainerGroupAssert assertThat( 071 @Nullable ModuleContainerGroup moduleContainerGroup 072 ) { 073 return assertThatContainerGroup(moduleContainerGroup); 074 } 075 076 /** 077 * Perform an assertion on an output container group. 078 * 079 * <p>This is a shorthand alias for {@link #assertThatContainerGroup(OutputContainerGroup)}. If 080 * you are using AssertJ assertions in your tests with static imports, you may wish to use that 081 * instead to prevent name conflicts. 082 * 083 * @param outputContainerGroup the output container group to assert on. 084 * @return the assertion. 085 */ 086 public static OutputContainerGroupAssert assertThat( 087 @Nullable OutputContainerGroup outputContainerGroup 088 ) { 089 return assertThatContainerGroup(outputContainerGroup); 090 } 091 092 /** 093 * Perform an assertion on a package container group. 094 * 095 * <p>This is a shorthand alias for {@link #assertThatContainerGroup(PackageContainerGroup)}. If 096 * you are using AssertJ assertions in your tests with static imports, you may wish to use that 097 * instead to prevent name conflicts. 098 * 099 * @param packageContainerGroup the package container group to assert on. 100 * @return the assertion. 101 */ 102 public static PackageContainerGroupAssert assertThat( 103 @Nullable PackageContainerGroup packageContainerGroup 104 ) { 105 return assertThatContainerGroup(packageContainerGroup); 106 } 107 108 /** 109 * Perform an assertion on a diagnostic. 110 * 111 * <p>This is a shorthand alias for {@link #assertThatDiagnostic(TraceDiagnostic)}. If 112 * you are using AssertJ assertions in your tests with static imports, you may wish to use that 113 * instead to prevent name conflicts. 114 * 115 * @param diagnostic the diagnostic to assert on. 116 * @return the assertion. 117 */ 118 public static TraceDiagnosticAssert assertThat( 119 @Nullable TraceDiagnostic<? extends JavaFileObject> diagnostic 120 ) { 121 return assertThatDiagnostic(diagnostic); 122 } 123 124 /** 125 * Perform an assertion on a Java file object. 126 * 127 * <p>This is a shorthand alias for {@link #assertThatFileObject(JavaFileObject)}. If you are 128 * using AssertJ assertions in your tests with static imports, you may wish to use that instead to 129 * prevent name conflicts. 130 * 131 * @param fileObject the file object to assert on. 132 * @return the assertion. 133 */ 134 public static JavaFileObjectAssert assertThat(@Nullable JavaFileObject fileObject) { 135 return assertThatFileObject(fileObject); 136 } 137 138 /** 139 * Perform an assertion on a Path-based Java file object. 140 * 141 * <p>This is a shorthand alias for {@link #assertThatFileObject(PathFileObject)}. If you are 142 * using AssertJ assertions in your tests with static imports, you may wish to use that instead to 143 * prevent name conflicts. 144 * 145 * @param fileObject the file object to assert on. 146 * @return the assertion. 147 */ 148 public static PathFileObjectAssert assertThat(@Nullable PathFileObject fileObject) { 149 return assertThatFileObject(fileObject); 150 } 151 152 /** 153 * Perform an assertion on a diagnostic kind. 154 * 155 * <p>This is a shorthand alias for {@link #assertThatKind(Diagnostic.Kind)}. If you are using 156 * AssertJ assertions in your tests with static imports, you may wish to use that instead to 157 * prevent name conflicts. 158 * 159 * @param kind the diagnostic kind to assert on. 160 * @return the assertion. 161 */ 162 public static DiagnosticKindAssert assertThat(Diagnostic.@Nullable Kind kind) { 163 return assertThatKind(kind); 164 } 165 166 /** 167 * Perform an assertion on a Java file object kind. 168 * 169 * <p>This is a shorthand alias for {@link #assertThatKind(JavaFileObject.Kind)}. If you are 170 * using AssertJ assertions in your tests with static imports, you may wish to use that instead to 171 * prevent name conflicts. 172 * 173 * @param kind the Java file object kind to assert on. 174 * @return the assertion. 175 */ 176 public static JavaFileObjectKindAssert assertThat(JavaFileObject.@Nullable Kind kind) { 177 return assertThatKind(kind); 178 } 179 180 /** 181 * Perform an assertion on a location. 182 * 183 * <p>This is a shorthand alias for {@link #assertThatLocation(Location)}. If you are using 184 * AssertJ assertions in your tests with static imports, you may wish to use that instead to 185 * prevent name conflicts. 186 * 187 * @param location the location to assert on. 188 * @return the assertion. 189 */ 190 public static LocationAssert assertThat(@Nullable Location location) { 191 return assertThatLocation(location); 192 } 193 194 /** 195 * Perform an assertion on a compilation. 196 * 197 * @param compilation the compilation to assert on. 198 * @return the assertion. 199 */ 200 public static JctCompilationAssert assertThatCompilation(@Nullable JctCompilation compilation) { 201 return new JctCompilationAssert(compilation); 202 } 203 204 /** 205 * Perform an assertion on a module container group. 206 * 207 * @param moduleContainerGroup the module container group to assert on. 208 * @return the assertion. 209 */ 210 public static ModuleContainerGroupAssert assertThatContainerGroup( 211 @Nullable ModuleContainerGroup moduleContainerGroup 212 ) { 213 return new ModuleContainerGroupAssert(moduleContainerGroup); 214 } 215 216 /** 217 * Perform an assertion on an output container group. 218 * 219 * @param outputContainerGroup the output container group to assert on. 220 * @return the assertion. 221 */ 222 public static OutputContainerGroupAssert assertThatContainerGroup( 223 @Nullable OutputContainerGroup outputContainerGroup 224 ) { 225 return new OutputContainerGroupAssert(outputContainerGroup); 226 } 227 228 /** 229 * Perform an assertion on a package container group. 230 * 231 * @param packageContainerGroup the package container group to assert on. 232 * @return the assertion. 233 */ 234 public static PackageContainerGroupAssert assertThatContainerGroup( 235 @Nullable PackageContainerGroup packageContainerGroup 236 ) { 237 return new PackageContainerGroupAssert(packageContainerGroup); 238 } 239 240 /** 241 * Perform an assertion on a diagnostic. 242 * 243 * @param diagnostic the diagnostic to assert on. 244 * @return the assertion. 245 */ 246 public static TraceDiagnosticAssert assertThatDiagnostic( 247 @Nullable TraceDiagnostic<? extends JavaFileObject> diagnostic 248 ) { 249 return new TraceDiagnosticAssert(diagnostic); 250 } 251 252 /** 253 * Perform an assertion on a list of diagnostics. 254 * 255 * @param diagnostics the diagnostics to assert on. 256 * @return the assertion. 257 */ 258 public static TraceDiagnosticListAssert assertThatDiagnostics( 259 @Nullable List<@Nullable ? extends TraceDiagnostic<? extends JavaFileObject>> diagnostics 260 ) { 261 return new TraceDiagnosticListAssert(diagnostics); 262 } 263 264 /** 265 * Perform an assertion on a Java file object. 266 * 267 * @param fileObject the file object to assert on. 268 * @return the assertion. 269 */ 270 public static JavaFileObjectAssert assertThatFileObject(@Nullable JavaFileObject fileObject) { 271 return new JavaFileObjectAssert(fileObject); 272 } 273 274 /** 275 * Perform an assertion on a Path-based Java file object. 276 * 277 * @param fileObject the file object to assert on. 278 * @return the assertion. 279 */ 280 public static PathFileObjectAssert assertThatFileObject(@Nullable PathFileObject fileObject) { 281 return new PathFileObjectAssert(fileObject); 282 } 283 284 /** 285 * Perform an assertion on a diagnostic kind. 286 * 287 * @param kind the diagnostic kind to assert on. 288 * @return the assertion. 289 */ 290 public static DiagnosticKindAssert assertThatKind(Diagnostic.@Nullable Kind kind) { 291 return new DiagnosticKindAssert(kind); 292 } 293 294 /** 295 * Perform an assertion on a Java file object kind. 296 * 297 * @param kind the Java file object kind to assert on. 298 * @return the assertion. 299 */ 300 public static JavaFileObjectKindAssert assertThatKind(JavaFileObject.@Nullable Kind kind) { 301 return new JavaFileObjectKindAssert(kind); 302 } 303 304 /** 305 * Perform an assertion on a location. 306 * 307 * @param location the location to assert on. 308 * @return the assertion. 309 */ 310 public static LocationAssert assertThatLocation(@Nullable Location location) { 311 return new LocationAssert(location); 312 } 313} 314