|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 - 2023, the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.ascopes.jct.tests.unit.assertions; |
| 17 | + |
| 18 | +import io.github.ascopes.jct.assertions.TraceDiagnosticListAssert; |
| 19 | +import org.junit.jupiter.api.DisplayName; |
| 20 | +import org.junit.jupiter.api.Nested; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import javax.tools.Diagnostic.Kind; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static io.github.ascopes.jct.tests.helpers.Fixtures.someTraceDiagnostic; |
| 26 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 27 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 28 | + |
| 29 | +/** |
| 30 | + * {@link TraceDiagnosticListAssert} tests. |
| 31 | + * |
| 32 | + * @author Ashley Scopes |
| 33 | + */ |
| 34 | +@DisplayName("TraceDiagnosticListAssert tests") |
| 35 | +class TraceDiagnosticListAssertTest { |
| 36 | + |
| 37 | + @DisplayName("TraceDiagnosticListAssert#errors tests") |
| 38 | + @Nested |
| 39 | + class ErrorsTest { |
| 40 | + |
| 41 | + @DisplayName(".errors() fails if the list is null") |
| 42 | + @Test |
| 43 | + void failsIfListIsNull() { |
| 44 | + // Given |
| 45 | + var assertions = new TraceDiagnosticListAssert(null); |
| 46 | + |
| 47 | + // Then |
| 48 | + assertThatThrownBy(assertions::errors) |
| 49 | + .isInstanceOf(AssertionError.class); |
| 50 | + } |
| 51 | + |
| 52 | + @DisplayName(".errors() returns assertions on just the errors") |
| 53 | + @Test |
| 54 | + void returnsAssertionsOnJustErrors() { |
| 55 | + // Given |
| 56 | + var error1 = someTraceDiagnostic(Kind.ERROR); |
| 57 | + var error2 = someTraceDiagnostic(Kind.ERROR); |
| 58 | + var error3 = someTraceDiagnostic(Kind.ERROR); |
| 59 | + |
| 60 | + var list = List.of( |
| 61 | + error1, |
| 62 | + someTraceDiagnostic(Kind.WARNING), |
| 63 | + someTraceDiagnostic(Kind.MANDATORY_WARNING), |
| 64 | + error2, |
| 65 | + someTraceDiagnostic(Kind.NOTE), |
| 66 | + someTraceDiagnostic(Kind.OTHER), |
| 67 | + someTraceDiagnostic(Kind.WARNING), |
| 68 | + someTraceDiagnostic(Kind.MANDATORY_WARNING), |
| 69 | + error3 |
| 70 | + ); |
| 71 | + |
| 72 | + var assertions = new TraceDiagnosticListAssert(list); |
| 73 | + |
| 74 | + // Then |
| 75 | + assertThatNoException() |
| 76 | + .isThrownBy(() -> assertions.errors().containsExactly(error1, error2, error3)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @DisplayName("TraceDiagnosticListAssert#warnings tests") |
| 81 | + @Nested |
| 82 | + class WarningsTest { |
| 83 | + |
| 84 | + @DisplayName(".warnings() fails if the list is null") |
| 85 | + @Test |
| 86 | + void failsIfListIsNull() { |
| 87 | + // Given |
| 88 | + var assertions = new TraceDiagnosticListAssert(null); |
| 89 | + |
| 90 | + // Then |
| 91 | + assertThatThrownBy(assertions::warnings) |
| 92 | + .isInstanceOf(AssertionError.class); |
| 93 | + } |
| 94 | + |
| 95 | + @DisplayName(".warnings() returns assertions on just the warnings") |
| 96 | + @Test |
| 97 | + void returnsAssertionsOnJustWarnings() { |
| 98 | + // Given |
| 99 | + var warning1 = someTraceDiagnostic(Kind.WARNING); |
| 100 | + var warning2 = someTraceDiagnostic(Kind.WARNING); |
| 101 | + var warning3 = someTraceDiagnostic(Kind.MANDATORY_WARNING); |
| 102 | + var warning4 = someTraceDiagnostic(Kind.MANDATORY_WARNING); |
| 103 | + var warning5 = someTraceDiagnostic(Kind.WARNING); |
| 104 | + |
| 105 | + var list = List.of( |
| 106 | + someTraceDiagnostic(Kind.ERROR), |
| 107 | + someTraceDiagnostic(Kind.ERROR), |
| 108 | + warning1, |
| 109 | + warning2, |
| 110 | + someTraceDiagnostic(Kind.OTHER), |
| 111 | + someTraceDiagnostic(Kind.NOTE), |
| 112 | + warning3, |
| 113 | + someTraceDiagnostic(Kind.NOTE), |
| 114 | + warning4, |
| 115 | + someTraceDiagnostic(Kind.ERROR), |
| 116 | + warning5 |
| 117 | + ); |
| 118 | + |
| 119 | + var assertions = new TraceDiagnosticListAssert(list); |
| 120 | + |
| 121 | + // Then |
| 122 | + |
| 123 | + assertThatNoException() |
| 124 | + .isThrownBy(() -> assertions.warnings() |
| 125 | + .containsExactly(warning1, warning2, warning3, warning4, warning5)); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @DisplayName("TraceDiagnosticListAssert#customWarnings tests") |
| 130 | + @Nested |
| 131 | + class CustomWarningsTest { |
| 132 | + |
| 133 | + @DisplayName(".customWarnings() fails if the list is null") |
| 134 | + @Test |
| 135 | + void failsIfListIsNull() { |
| 136 | + // Given |
| 137 | + var assertions = new TraceDiagnosticListAssert(null); |
| 138 | + |
| 139 | + // Then |
| 140 | + assertThatThrownBy(assertions::warnings) |
| 141 | + .isInstanceOf(AssertionError.class); |
| 142 | + } |
| 143 | + |
| 144 | + @DisplayName(".customWarnings() returns assertions on just the custom warnings") |
| 145 | + @Test |
| 146 | + void returnsAssertionsOnJustCustomWarnings() { |
| 147 | + // Given |
| 148 | + var warning1 = someTraceDiagnostic(Kind.WARNING); |
| 149 | + var warning2 = someTraceDiagnostic(Kind.WARNING); |
| 150 | + var warning3 = someTraceDiagnostic(Kind.WARNING); |
| 151 | + |
| 152 | + var list = List.of( |
| 153 | + someTraceDiagnostic(Kind.ERROR), |
| 154 | + someTraceDiagnostic(Kind.ERROR), |
| 155 | + someTraceDiagnostic(Kind.MANDATORY_WARNING), |
| 156 | + warning1, |
| 157 | + someTraceDiagnostic(Kind.ERROR), |
| 158 | + someTraceDiagnostic(Kind.OTHER), |
| 159 | + warning2, |
| 160 | + warning3, |
| 161 | + someTraceDiagnostic(Kind.NOTE), |
| 162 | + someTraceDiagnostic(Kind.MANDATORY_WARNING), |
| 163 | + someTraceDiagnostic(Kind.ERROR) |
| 164 | + ); |
| 165 | + |
| 166 | + var assertions = new TraceDiagnosticListAssert(list); |
| 167 | + |
| 168 | + // Then |
| 169 | + |
| 170 | + assertThatNoException() |
| 171 | + .isThrownBy(() -> assertions.customWarnings() |
| 172 | + .containsExactly(warning1, warning2, warning3)); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + @DisplayName("TraceDiagnosticListAssert#mandatoryWarnings tests") |
| 178 | + @Nested |
| 179 | + class MandatoryWarningsTest { |
| 180 | + |
| 181 | + @DisplayName(".mandatoryWarnings() fails if the list is null") |
| 182 | + @Test |
| 183 | + void failsIfListIsNull() { |
| 184 | + // Given |
| 185 | + var assertions = new TraceDiagnosticListAssert(null); |
| 186 | + |
| 187 | + // Then |
| 188 | + assertThatThrownBy(assertions::warnings) |
| 189 | + .isInstanceOf(AssertionError.class); |
| 190 | + } |
| 191 | + |
| 192 | + @DisplayName(".mandatoryWarnings() returns assertions on just the mandatory warnings") |
| 193 | + @Test |
| 194 | + void returnsAssertionsOnJustMandatoryWarnings() { |
| 195 | + // Given |
| 196 | + var warning1 = someTraceDiagnostic(Kind.MANDATORY_WARNING); |
| 197 | + var warning2 = someTraceDiagnostic(Kind.MANDATORY_WARNING); |
| 198 | + var warning3 = someTraceDiagnostic(Kind.MANDATORY_WARNING); |
| 199 | + |
| 200 | + var list = List.of( |
| 201 | + someTraceDiagnostic(Kind.ERROR), |
| 202 | + someTraceDiagnostic(Kind.ERROR), |
| 203 | + someTraceDiagnostic(Kind.WARNING), |
| 204 | + warning1, |
| 205 | + someTraceDiagnostic(Kind.ERROR), |
| 206 | + someTraceDiagnostic(Kind.OTHER), |
| 207 | + warning2, |
| 208 | + warning3, |
| 209 | + someTraceDiagnostic(Kind.NOTE), |
| 210 | + someTraceDiagnostic(Kind.WARNING), |
| 211 | + someTraceDiagnostic(Kind.ERROR) |
| 212 | + ); |
| 213 | + |
| 214 | + var assertions = new TraceDiagnosticListAssert(list); |
| 215 | + |
| 216 | + // Then |
| 217 | + |
| 218 | + assertThatNoException() |
| 219 | + .isThrownBy(() -> assertions.mandatoryWarnings() |
| 220 | + .containsExactly(warning1, warning2, warning3)); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments