| 1 | package com.github.dakusui.thincrest; | |
| 2 | ||
| 3 | import com.github.dakusui.thincrest_pcond.fluent.ListHolder; | |
| 4 | import com.github.dakusui.thincrest_pcond.fluent.Statement; | |
| 5 | import com.github.dakusui.thincrest_pcond.validator.Validator; | |
| 6 | ||
| 7 | import java.util.Arrays; | |
| 8 | import java.util.List; | |
| 9 | import java.util.function.Function; | |
| 10 | import java.util.function.Predicate; | |
| 11 | import java.util.function.Supplier; | |
| 12 | ||
| 13 | import static java.util.stream.Collectors.toList; | |
| 14 | ||
| 15 | /** | |
| 16 | * An entry-point class for test assertions. | |
| 17 | * You can use the methods to replace the usages of the methods the same names of the `Hamcrest`. | |
| 18 | * | |
| 19 | * Instead of the `Matcher` s of `Hamcrest`, you can use just simple functions and predicates. | |
| 20 | * | |
| 21 | * There are pre-defined printable functions and predicates in the {@link com.github.dakusui.thincrest_pcond.forms.Functions} | |
| 22 | * class and {@link com.github.dakusui.thincrest_pcond.forms.Predicates} class. | |
| 23 | * | |
| 24 | * You can build your matchers by composing them using methods in the Java's | |
| 25 | * {@link Function} and {@link Predicate} such as {@link Function#andThen(Function)}, | |
| 26 | * {@link Function#compose(Function)}, {@link Predicate#and(Predicate)}, {@link Predicate#or(Predicate)}, | |
| 27 | * and {@link Predicate#negate()}. | |
| 28 | * | |
| 29 | * To create your own function or predicate that renders a human-readable message, | |
| 30 | * you can use the `function` and `predicate` methods defined in the `Printables` | |
| 31 | * class. | |
| 32 | * | |
| 33 | * {@link com.github.dakusui.thincrest_pcond.forms.Predicates#transform(Function)} is a method | |
| 34 | * to build a predicate, which first transforms a value into a type that the `pcond` offers | |
| 35 | * a rich support, such as {@link String}, {@link java.util.List}, {@link Comparable}, etc., | |
| 36 | * and the applies predicates to the transformed value. | |
| 37 | * With this approach, you will not need to create your own matchers, but just create | |
| 38 | * a function that transform your class into easily verifiable types for your verification. | |
| 39 | * | |
| 40 | * Each method, which ends with `Statement` or `all`, in this class accepts {@link Statement} objects. | |
| 41 | * To create a {@link Statement} object, you can call static methods in {@link Statement} itself. | |
| 42 | * class such as {@link Statement#booleanValue(Boolean)}, {@link Statement#stringValue(String)}, | |
| 43 | * etc. | |
| 44 | * | |
| 45 | * @see com.github.dakusui.thincrest_pcond.forms.Predicates | |
| 46 | * @see com.github.dakusui.thincrest_pcond.forms.Functions | |
| 47 | * @see com.github.dakusui.thincrest_pcond.forms.Predicates#transform(Function) | |
| 48 | * @see com.github.dakusui.thincrest_pcond.forms.Printables#predicate(String, Predicate) | |
| 49 | * @see com.github.dakusui.thincrest_pcond.forms.Printables#function(String, Function) | |
| 50 | * @see com.github.dakusui.thincrest_pcond.forms.Printables#predicate(Supplier, Predicate) | |
| 51 | * @see com.github.dakusui.thincrest_pcond.forms.Printables#function(Supplier, Function) | |
| 52 | */ | |
| 53 | public enum TestAssertions { | |
| 54 | ||
| 55 | ; | |
| 56 | ||
| 57 | /** | |
| 58 | * A method to check a given `value` satisfies a condition `predicate`, to be verified by the test. | |
| 59 | * If it is not satisfied, the test should fail. | |
| 60 | * | |
| 61 | * @param value The value to be checked. | |
| 62 | * @param predicate A condition to check the `value`. | |
| 63 | * @param <T> The type of the `value`. | |
| 64 | */ | |
| 65 | public static <T> void assertThat(T value, Predicate<? super T> predicate) { | |
| 66 |
1
1. assertThat : removed call to com/github/dakusui/thincrest_pcond/validator/Validator::assertThat → KILLED |
Validator.instance().assertThat(value, predicate); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * A method to check a given `value` satisfies a condition `predicate`, which is required by the *test's design* to execute it. | |
| 71 | * If it is not satisfied, that means, the value violates an assumption of the test, therefore the test should be ignored, not fail. | |
| 72 | * If you are using *JUnit4*, an `AssumptionViolatedException` should be thrown. | |
| 73 | * | |
| 74 | * @param value The value to be checked. | |
| 75 | * @param predicate A condition to check the `value`. | |
| 76 | * @param <T> The type of the `value`. | |
| 77 | */ | |
| 78 | public static <T> void assumeThat(T value, Predicate<? super T> predicate) { | |
| 79 |
1
1. assumeThat : removed call to com/github/dakusui/thincrest_pcond/validator/Validator::assumeThat → KILLED |
Validator.instance().assumeThat(value, predicate); |
| 80 | } | |
| 81 | | |
| 82 | /** | |
| 83 | * Fluent version of {@link TestAssertions#assertThat(Object, Predicate)}. | |
| 84 | * | |
| 85 | * @param statement A statement to be verified | |
| 86 | * @param <T> The type of the value to be verified which a given statement holds. | |
| 87 | */ | |
| 88 | public static <T> void assertStatement(Statement<T> statement) { | |
| 89 |
1
1. assertStatement : removed call to com/github/dakusui/thincrest/TestAssertions::assertThat → KILLED |
TestAssertions.assertThat(statement.statementValue(), statement.statementPredicate()); |
| 90 | } | |
| 91 | | |
| 92 | /** | |
| 93 | * Fluent version of {@link TestAssertions#assertThat(Object, Predicate)}. | |
| 94 | * Use this method when you need to verify multiple values. | |
| 95 | * | |
| 96 | * You can use {@link TestAssertions#assertStatement(Statement)}, if you have only one statement to be verified, for readability's sake. | |
| 97 | * | |
| 98 | * @param statements Statements to be verified | |
| 99 | * @see TestAssertions#assertStatement(Statement) | |
| 100 | */ | |
| 101 | public static void assertAll(Statement<?>... statements) { | |
| 102 | List<?> values = Arrays.stream(statements).map(Statement::statementValue).collect(toList()); | |
| 103 |
1
1. assertAll : removed call to com/github/dakusui/thincrest/TestAssertions::assertThat → KILLED |
TestAssertions.assertThat(ListHolder.fromList(values), Statement.createPredicateForAllOf(statements)); |
| 104 | } | |
| 105 | | |
| 106 | /** | |
| 107 | * Fluent version of {@link TestAssertions#assumeThat(Object, Predicate)}. | |
| 108 | * | |
| 109 | * @param statement A statement to be verified | |
| 110 | */ | |
| 111 | public static <T> void assumeStatement(Statement<T> statement) { | |
| 112 |
1
1. assumeStatement : removed call to com/github/dakusui/thincrest/TestAssertions::assumeThat → KILLED |
TestAssertions.assumeThat(statement.statementValue(), statement.statementPredicate()); |
| 113 | } | |
| 114 | | |
| 115 | /** | |
| 116 | * Fluent version of {@link TestAssertions#assumeThat(Object, Predicate)}. | |
| 117 | * Use this method when you need to verify multiple values. | |
| 118 | * | |
| 119 | * You can use {@link TestAssertions#assumeStatement(Statement)}}, if you have only one statement to be verified, for readability's sake. | |
| 120 | * | |
| 121 | * @param statements Statements to be verified | |
| 122 | * @see TestAssertions#assumeStatement(Statement) | |
| 123 | */ | |
| 124 | public static void assumeAll(Statement<?>... statements) { | |
| 125 | List<?> values = Arrays.stream(statements).map(Statement::statementValue).collect(toList()); | |
| 126 |
1
1. assumeAll : removed call to com/github/dakusui/thincrest/TestAssertions::assumeThat → KILLED |
TestAssertions.assumeThat(ListHolder.fromList(values), Statement.createPredicateForAllOf(statements)); |
| 127 | } | |
| 128 | } | |
Mutations | ||
| 66 |
1.1 |
|
| 79 |
1.1 |
|
| 89 |
1.1 |
|
| 103 |
1.1 |
|
| 112 |
1.1 |
|
| 126 |
1.1 |