|
| 1 | +/* |
| 2 | + * Copyright 2015-2025 the original author or authors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials are |
| 5 | + * made available under the terms of the Eclipse Public License v2.0 which |
| 6 | + * accompanies this distribution and is available at |
| 7 | + * |
| 8 | + * https://www.eclipse.org/legal/epl-v20.html |
| 9 | + */ |
| 10 | + |
| 11 | +package org.junit.jupiter.api.condition; |
| 12 | + |
| 13 | +import static org.assertj.core.api.Assertions.assertThat; |
| 14 | + |
| 15 | +import java.lang.annotation.Retention; |
| 16 | +import java.lang.annotation.RetentionPolicy; |
| 17 | + |
| 18 | +import org.jspecify.annotations.Nullable; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.extension.ConditionEvaluationResult; |
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.NullSource; |
| 23 | +import org.junit.jupiter.params.provider.ValueSource; |
| 24 | + |
| 25 | +/** |
| 26 | + * Unit tests for {@link ConditionEvaluationResult}. |
| 27 | + * |
| 28 | + * @since 6.0 |
| 29 | + */ |
| 30 | +class ConditionEvaluationResultTests { |
| 31 | + |
| 32 | + @Test |
| 33 | + void enabledWithReason() { |
| 34 | + var result = ConditionEvaluationResult.enabled("reason"); |
| 35 | + |
| 36 | + assertThat(result.isDisabled()).isFalse(); |
| 37 | + assertThat(result.getReason()).contains("reason"); |
| 38 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = true, reason = 'reason']"); |
| 39 | + } |
| 40 | + |
| 41 | + @EmptyReasonsTest |
| 42 | + void enabledWithoutReason(@Nullable String reason) { |
| 43 | + var result = ConditionEvaluationResult.enabled(reason); |
| 44 | + |
| 45 | + assertThat(result.isDisabled()).isFalse(); |
| 46 | + assertThat(result.getReason()).isEmpty(); |
| 47 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = true, reason = '<unknown>']"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void disabledWithDefaultReason() { |
| 52 | + var result = ConditionEvaluationResult.disabled("default"); |
| 53 | + |
| 54 | + assertThat(result.isDisabled()).isTrue(); |
| 55 | + assertThat(result.getReason()).contains("default"); |
| 56 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']"); |
| 57 | + } |
| 58 | + |
| 59 | + @EmptyReasonsTest |
| 60 | + void disabledWithoutDefaultReason(@Nullable String reason) { |
| 61 | + var result = ConditionEvaluationResult.disabled(reason); |
| 62 | + |
| 63 | + assertThat(result.isDisabled()).isTrue(); |
| 64 | + assertThat(result.getReason()).isEmpty(); |
| 65 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']"); |
| 66 | + } |
| 67 | + |
| 68 | + @EmptyReasonsTest |
| 69 | + void disabledWithDefaultReasonButWithoutCustomReason(@Nullable String customReason) { |
| 70 | + var result = ConditionEvaluationResult.disabled("default", customReason); |
| 71 | + |
| 72 | + assertThat(result.isDisabled()).isTrue(); |
| 73 | + assertThat(result.getReason()).contains("default"); |
| 74 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default']"); |
| 75 | + } |
| 76 | + |
| 77 | + @EmptyReasonsTest |
| 78 | + void disabledWithoutDefaultReasonButWithCustomReason(@Nullable String reason) { |
| 79 | + var result = ConditionEvaluationResult.disabled(reason, "custom"); |
| 80 | + |
| 81 | + assertThat(result.isDisabled()).isTrue(); |
| 82 | + assertThat(result.getReason()).contains("custom"); |
| 83 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'custom']"); |
| 84 | + } |
| 85 | + |
| 86 | + @EmptyReasonsTest |
| 87 | + void disabledWithoutDefaultReasonOrCustomReason(@Nullable String reason) { |
| 88 | + // We intentionally use the reason as both the default and custom reason. |
| 89 | + var result = ConditionEvaluationResult.disabled(reason, reason); |
| 90 | + |
| 91 | + assertThat(result.isDisabled()).isTrue(); |
| 92 | + assertThat(result.getReason()).isEmpty(); |
| 93 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = '<unknown>']"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void disabledWithDefaultReasonAndCustomReason() { |
| 98 | + var result = ConditionEvaluationResult.disabled("default", "custom"); |
| 99 | + |
| 100 | + assertThat(result.isDisabled()).isTrue(); |
| 101 | + assertThat(result.getReason()).contains("default ==> custom"); |
| 102 | + assertThat(result).asString().isEqualTo("ConditionEvaluationResult [enabled = false, reason = 'default ==> custom']"); |
| 103 | + } |
| 104 | + |
| 105 | + @Retention(RetentionPolicy.RUNTIME) |
| 106 | + @ParameterizedTest |
| 107 | + @NullSource |
| 108 | + @ValueSource(strings = { "", " ", " ", "\t", "\n" }) |
| 109 | + @interface EmptyReasonsTest { |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments