1 | package com.github.valid8j.classic; | |
2 | ||
3 | import com.github.valid8j.pcond.validator.Validator; | |
4 | ||
5 | import java.util.function.Predicate; | |
6 | ||
7 | /** | |
8 | * // @formatter:off | |
9 | * Use methods in this class with the ```assert``` statement. | |
10 | * | |
11 | * [source, java] | |
12 | * ---- | |
13 | * import static com.github.valid8j.pcond.core.functions.Predicates.isNotNull; | |
14 | * import static com.github.valid8j.pcond.classic.Assertions.* | |
15 | * | |
16 | * public class TestClass { | |
17 | * public static void aMethod(Object value) { | |
18 | * assert that(value, isNotNull()); | |
19 | * } | |
20 | * } | |
21 | * ---- | |
22 | * | |
23 | * This prints a much more readable error message than one you see for a usual assertion failure. | |
24 | * | |
25 | * ---- | |
26 | * Exception in thread "main" java.lang.AssertionError: Value:null violated: isNotNull | |
27 | * null -> isNotNull -> false | |
28 | * at com.github.valid8j.pcond.provider.impls.DefaultAssertionProvider.checkValue(DefaultAssertionProvider.java:70) | |
29 | * at com.github.valid8j.pcond.provider.AssertionProviderBase.checkInvariant(AssertionProviderBase.java:78) | |
30 | * at com.github.valid8j.pcond.classic.Assertions.that(Assertions.java:51) | |
31 | * ... | |
32 | * ---- | |
33 | * | |
34 | * To enable this feature, do not forget giving ```-ea``` VM option to enable assertions. | |
35 | * Otherwise, the evaluation doesn't happen completely and even printable predicates constructed the right side of the `assert` statement. | |
36 | * This means you can enable the feature during the development and disable it in the production so that you will not see no performance impact. | |
37 | * | |
38 | * // @formatter:on | |
39 | * | |
40 | */ | |
41 | public enum Assertions { | |
42 | ; | |
43 | /** | |
44 | * A method to be used for checking a value satisfies a given invariant condition. | |
45 | * This method is intended to be used in `assert` statements. | |
46 | * | |
47 | * [source,java] | |
48 | * .Example | |
49 | * ---- | |
50 | * public void aMethod(String var) { | |
51 | * assert that(var, isNotNull()); | |
52 | * } | |
53 | * ---- | |
54 | * | |
55 | * @param value A value to be checked. | |
56 | * @param predicate An invariant condition to check the {@code value}. | |
57 | * @param <T> The type of {@code value}. | |
58 | * @return {@code true}, if the condition given as {@code predicate} is satisfied. | |
59 | */ | |
60 | public static <T> boolean that(T value, Predicate<? super T> predicate) { | |
61 |
1
1. that : removed call to com/github/valid8j/pcond/validator/Validator::checkInvariant → KILLED |
Validator.instance().checkInvariant(value, predicate); |
62 |
2
1. that : replaced boolean return with true for com/github/valid8j/classic/Assertions::that → SURVIVED 2. that : replaced boolean return with false for com/github/valid8j/classic/Assertions::that → KILLED |
return trueValue(); |
63 | } | |
64 | ||
65 | /** | |
66 | * A method to be used for checking a value satisfies a given pre-condition. | |
67 | * This method is intended to be used in `assert` statements. | |
68 | * | |
69 | * [source,java] | |
70 | * .Example | |
71 | * ---- | |
72 | * public void aMethod(String var) { | |
73 | * assert precondition(var, isNotNull()); | |
74 | * } | |
75 | * ---- | |
76 | * | |
77 | * @param value A value to be checked. | |
78 | * @param predicate A pre-condition to check the {@code value}. | |
79 | * @param <T> The type of {@code value}. | |
80 | * @return {@code true}, if the condition given as {@code predicate} is satisfied. | |
81 | */ | |
82 | public static <T> boolean precondition(T value, Predicate<? super T> predicate) { | |
83 |
1
1. precondition : removed call to com/github/valid8j/pcond/validator/Validator::checkPrecondition → KILLED |
Validator.instance().checkPrecondition(value, predicate); |
84 |
2
1. precondition : replaced boolean return with true for com/github/valid8j/classic/Assertions::precondition → SURVIVED 2. precondition : replaced boolean return with false for com/github/valid8j/classic/Assertions::precondition → KILLED |
return trueValue(); |
85 | } | |
86 | ||
87 | /** | |
88 | * A method to be used for checking a value satisfies a given post-condition. | |
89 | * This method is intended to be used in `assert` statements. | |
90 | * | |
91 | * [source,java] | |
92 | * .Example | |
93 | * ---- | |
94 | * public void aMethod(String var) { | |
95 | * assert postcondition(var, isNotNull()); | |
96 | * } | |
97 | * ---- | |
98 | * | |
99 | * @param value A value to be checked. | |
100 | * @param predicate A post-condition to check the {@code value}. | |
101 | * @param <T> The type of {@code value}. | |
102 | * @return {@code true}, if the condition given as {@code predicate} is satisfied. | |
103 | */ | |
104 | public static <T> boolean postcondition(T value, Predicate<? super T> predicate) { | |
105 |
1
1. postcondition : removed call to com/github/valid8j/pcond/validator/Validator::checkPostcondition → KILLED |
Validator.instance().checkPostcondition(value, predicate); |
106 |
2
1. postcondition : replaced boolean return with true for com/github/valid8j/classic/Assertions::postcondition → SURVIVED 2. postcondition : replaced boolean return with false for com/github/valid8j/classic/Assertions::postcondition → KILLED |
return trueValue(); |
107 | } | |
108 | ||
109 | /** | |
110 | * This method always return {@code true}. | |
111 | * This method is defined in order not to let an IDE report a warning | |
112 | * Condition 'that(methodType, Predicates.isNotNull())' is always 'true' | |
113 | * for caller codes of methods in this class. | |
114 | * | |
115 | * @return {@code true} | |
116 | */ | |
117 | @SuppressWarnings("ConstantConditions") | |
118 | private static boolean trueValue() { | |
119 |
2
1. trueValue : replaced boolean return with true for com/github/valid8j/classic/Assertions::trueValue → SURVIVED 2. trueValue : negated conditional → KILLED |
return Assertions.class != null; |
120 | } | |
121 | } | |
Mutations | ||
61 |
1.1 |
|
62 |
1.1 2.2 |
|
83 |
1.1 |
|
84 |
1.1 2.2 |
|
105 |
1.1 |
|
106 |
1.1 2.2 |
|
119 |
1.1 2.2 |