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  public LocationAssert(@Nullable Location value) {
043    super(value, LocationAssert.class);
044    info.useRepresentation(LocationRepresentation.getInstance());
045  }
046
047  /**
048   * Assert that the location is {@link Location#isModuleOrientedLocation() module-oriented}.
049   *
050   * @return this assertion object for further call chaining.
051   * @throws AssertionError if the location is null or if the location is not module-oriented.
052   */
053  public LocationAssert isModuleOrientedLocation() {
054    isNotNull();
055
056    if (!actual.isModuleOrientedLocation()) {
057      throw failure(
058          "Expected location %s to be module-oriented but it was not", actual.getName()
059      );
060    }
061
062    return this;
063  }
064
065  /**
066   * Assert that the location is not {@link Location#isModuleOrientedLocation() module-oriented}.
067   *
068   * @return this assertion object for further call chaining.
069   * @throws AssertionError if the location is null or if the location is module-oriented.
070   */
071  public LocationAssert isNotModuleOrientedLocation() {
072    isNotNull();
073
074    if (actual.isModuleOrientedLocation()) {
075      throw failure(
076          "Expected location %s to not be module-oriented but it was", actual.getName()
077      );
078    }
079
080    return this;
081  }
082
083  /**
084   * Assert that the location is an {@link Location#isOutputLocation() output location}.
085   *
086   * @return this assertion object for further call chaining.
087   * @throws AssertionError if the location is null or is not an output location.
088   */
089  public LocationAssert isOutputLocation() {
090    isNotNull();
091
092    if (!actual.isOutputLocation()) {
093      throw failure(
094          "Expected location %s to be an output location but it was not", actual.getName()
095      );
096    }
097
098    return this;
099  }
100
101  /**
102   * Assert that the location is not an {@link Location#isOutputLocation() output location}.
103   *
104   * @return this assertion object for further call chaining.
105   * @throws AssertionError if the location is null or is an output location.
106   */
107  public LocationAssert isNotOutputLocation() {
108    isNotNull();
109
110    if (actual.isOutputLocation()) {
111      throw failure(
112          "Expected location %s to not be an output location but it was", actual.getName()
113      );
114    }
115
116    return this;
117  }
118
119  /**
120   * Perform assertions on the {@link Location#getName name} of the location.
121   *
122   * @return the string assertions to perform.
123   * @throws AssertionError if the location is null.
124   */
125  public AbstractStringAssert<?> name() {
126    isNotNull();
127
128    return assertThat(actual.getName());
129  }
130}