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