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