001/*
002 * Copyright (C) 2022 - 2024, 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 static org.assertj.core.api.Assertions.assertThat;
019
020import io.github.ascopes.jct.repr.LocationRepresentation;
021import javax.tools.JavaFileManager.Location;
022import org.apiguardian.api.API;
023import org.apiguardian.api.API.Status;
024import org.assertj.core.api.AbstractAssert;
025import org.assertj.core.api.AbstractStringAssert;
026import org.jspecify.annotations.Nullable;
027
028/**
029 * Assertions for an individual {@link Location location}.
030 *
031 * @author Ashley Scopes
032 * @since 0.0.1
033 */
034@API(since = "0.0.1", status = Status.STABLE)
035public final class LocationAssert extends AbstractAssert<LocationAssert, Location> {
036
037  /**
038   * Initialize this assertion type.
039   *
040   * @param value the value to assert on.
041   */
042  @SuppressWarnings("DataFlowIssue")
043  public LocationAssert(@Nullable Location value) {
044    super(value, LocationAssert.class);
045    info.useRepresentation(LocationRepresentation.getInstance());
046  }
047
048  /**
049   * Assert that the location is {@link Location#isModuleOrientedLocation() module-oriented}.
050   *
051   * @return this assertion object for further call chaining.
052   * @throws AssertionError if the location is null or if the location is not module-oriented.
053   */
054  public LocationAssert isModuleOrientedLocation() {
055    isNotNull();
056
057    if (!actual.isModuleOrientedLocation()) {
058      throw failure(
059          "Expected location %s to be module-oriented but it was not", actual.getName()
060      );
061    }
062
063    return this;
064  }
065
066  /**
067   * Assert that the location is not {@link Location#isModuleOrientedLocation() module-oriented}.
068   *
069   * @return this assertion object for further call chaining.
070   * @throws AssertionError if the location is null or if the location is module-oriented.
071   */
072  public LocationAssert isNotModuleOrientedLocation() {
073    isNotNull();
074
075    if (actual.isModuleOrientedLocation()) {
076      throw failure(
077          "Expected location %s to not be module-oriented but it was", actual.getName()
078      );
079    }
080
081    return this;
082  }
083
084  /**
085   * Assert that the location is an {@link Location#isOutputLocation() output location}.
086   *
087   * @return this assertion object for further call chaining.
088   * @throws AssertionError if the location is null or is not an output location.
089   */
090  public LocationAssert isOutputLocation() {
091    isNotNull();
092
093    if (!actual.isOutputLocation()) {
094      throw failure(
095          "Expected location %s to be an output location but it was not", actual.getName()
096      );
097    }
098
099    return this;
100  }
101
102  /**
103   * Assert that the location is not an {@link Location#isOutputLocation() output location}.
104   *
105   * @return this assertion object for further call chaining.
106   * @throws AssertionError if the location is null or is an output location.
107   */
108  public LocationAssert isNotOutputLocation() {
109    isNotNull();
110
111    if (actual.isOutputLocation()) {
112      throw failure(
113          "Expected location %s to not be an output location but it was", actual.getName()
114      );
115    }
116
117    return this;
118  }
119
120  /**
121   * Perform assertions on the {@link Location#getName name} of the location.
122   *
123   * @return the string assertions to perform.
124   * @throws AssertionError if the location is null.
125   */
126  public AbstractStringAssert<?> name() {
127    isNotNull();
128
129    return assertThat(actual.getName());
130  }
131}