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 java.util.EnumSet;
019import java.util.Set;
020import javax.tools.Diagnostic.Kind;
021import org.apiguardian.api.API;
022import org.apiguardian.api.API.Status;
023import org.jspecify.annotations.Nullable;
024
025/**
026 * Assertions for an individual diagnostic kind.
027 *
028 * @author Ashley Scopes
029 * @since 0.0.1
030 */
031@API(since = "0.0.1", status = Status.STABLE)
032public final class DiagnosticKindAssert
033    extends AbstractEnumAssert<DiagnosticKindAssert, Kind> {
034
035  /**
036   * Kinds that we consider to be types of warnings.
037   */
038  static final Set<Kind> WARNING_DIAGNOSTIC_KINDS = EnumSet.of(
039      Kind.WARNING,
040      Kind.MANDATORY_WARNING
041  );
042
043  /**
044   * Kinds that we consider to be types of error.
045   */
046  static final Set<Kind> ERROR_DIAGNOSTIC_KINDS = EnumSet.of(
047      Kind.ERROR
048  );
049
050  /**
051   * Kinds that we consider to be types of warning or errors.
052   */
053  static final Set<Kind> WARNING_AND_ERROR_DIAGNOSTIC_KINDS = EnumSet.of(
054      Kind.WARNING,
055      Kind.MANDATORY_WARNING,
056      Kind.ERROR
057  );
058
059  /**
060   * Initialize this assertion type.
061   *
062   * @param value the value to assert on.
063   */
064  public DiagnosticKindAssert(@Nullable Kind value) {
065    super(value, DiagnosticKindAssert.class);
066  }
067
068  /**
069   * Assert that the kind is {@link Kind#ERROR}.
070   *
071   * @return this assertion object.
072   * @throws AssertionError if this value is null, or the value is not {@link Kind#ERROR}.
073   */
074  public DiagnosticKindAssert isError() {
075    return isAnyOfElements(ERROR_DIAGNOSTIC_KINDS);
076  }
077
078  /**
079   * Assert that the kind is {@link Kind#ERROR}, {@link Kind#WARNING}, or
080   * {@link Kind#MANDATORY_WARNING}.
081   *
082   * @return this assertion object.
083   * @throws AssertionError if this value is null, or the value is not any of {@link Kind#ERROR},
084   *                        {@link Kind#WARNING}, or {@link Kind#MANDATORY_WARNING}.
085   */
086  public DiagnosticKindAssert isWarningOrError() {
087    return isAnyOfElements(WARNING_AND_ERROR_DIAGNOSTIC_KINDS);
088  }
089
090  /**
091   * Assert that the kind is either {@link Kind#WARNING} or {@link Kind#MANDATORY_WARNING}.
092   *
093   * @return this assertion object.
094   * @throws AssertionError if this value is null, or the value is not either {@link Kind#WARNING}
095   *                        or {@link Kind#MANDATORY_WARNING}.
096   */
097  public DiagnosticKindAssert isWarning() {
098    return isAnyOfElements(WARNING_DIAGNOSTIC_KINDS);
099  }
100
101  /**
102   * Assert that the kind is {@link Kind#WARNING}.
103   *
104   * @return this assertion object.
105   * @throws AssertionError if this value is null, or the value is not {@link Kind#WARNING}.
106   */
107  public DiagnosticKindAssert isCustomWarning() {
108    return isAnyOf(Kind.WARNING);
109  }
110
111  /**
112   * Assert that the kind is {@link Kind#MANDATORY_WARNING}.
113   *
114   * @return this assertion object.
115   * @throws AssertionError if this value is null, or the value is not
116   *                        {@link Kind#MANDATORY_WARNING}.
117   */
118  public DiagnosticKindAssert isMandatoryWarning() {
119    return isAnyOf(Kind.MANDATORY_WARNING);
120  }
121
122  /**
123   * Assert that the kind is {@link Kind#NOTE}.
124   *
125   * @return this assertion object.
126   * @throws AssertionError if this value is null, or the value is not {@link Kind#NOTE}.
127   */
128  public DiagnosticKindAssert isNote() {
129    return isAnyOf(Kind.NOTE);
130  }
131
132  /**
133   * Assert that the kind is {@link Kind#OTHER}.
134   *
135   * @return this assertion object.
136   * @throws AssertionError if this value is null, or the value is not {@link Kind#OTHER}.
137   */
138  public DiagnosticKindAssert isOther() {
139    return isAnyOf(Kind.OTHER);
140  }
141}