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