|
| 1 | +/* |
| 2 | + * Copyright 2023 webtau maintainers |
| 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 | + |
| 17 | +package org.testingisdocumenting.webtau.expectation.contain; |
| 18 | + |
| 19 | +import org.testingisdocumenting.webtau.data.ValuePath; |
| 20 | +import org.testingisdocumenting.webtau.data.ValuePathWithValue; |
| 21 | +import org.testingisdocumenting.webtau.data.converters.ValueConverter; |
| 22 | +import org.testingisdocumenting.webtau.data.render.PrettyPrintable; |
| 23 | +import org.testingisdocumenting.webtau.data.render.PrettyPrinter; |
| 24 | +import org.testingisdocumenting.webtau.expectation.ExpectedValuesAware; |
| 25 | +import org.testingisdocumenting.webtau.expectation.ValueMatcher; |
| 26 | +import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator; |
| 27 | +import org.testingisdocumenting.webtau.reporter.TokenizedMessage; |
| 28 | + |
| 29 | +import java.util.*; |
| 30 | +import java.util.stream.Collectors; |
| 31 | +import java.util.stream.Stream; |
| 32 | + |
| 33 | +import static org.testingisdocumenting.webtau.WebTauCore.*; |
| 34 | + |
| 35 | +public class ContainExactlyMatcher implements ValueMatcher, ExpectedValuesAware, PrettyPrintable { |
| 36 | + private final Collection<Object> expectedList; |
| 37 | + private List<ValuePathWithValue<Object>> actualCopy; |
| 38 | + private List<ValuePathWithValue<Object>> expectedCopy; |
| 39 | + |
| 40 | + private CompareToComparator comparator; |
| 41 | + |
| 42 | + public ContainExactlyMatcher(Collection<Object> expected) { |
| 43 | + expectedList = expected; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public ValueConverter valueConverter() { |
| 48 | + return comparator == null ? ValueConverter.EMPTY : comparator.createValueConverter(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Stream<Object> expectedValues() { |
| 53 | + return expectedList.stream(); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public Set<ValuePath> mismatchedPaths() { |
| 58 | + return actualCopy.stream().map(ValuePathWithValue::getPath).collect(Collectors.toSet()); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public TokenizedMessage matchingTokenizedMessage(ValuePath actualPath, Object actual) { |
| 63 | + comparator = CompareToComparator.comparator(CompareToComparator.AssertionMode.EQUAL); |
| 64 | + return tokenizedMessage().matcher("to contain exactly").valueFirstLinesOnly(expectedList); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public TokenizedMessage matchedTokenizedMessage(ValuePath actualPath, Object actual) { |
| 69 | + return tokenizedMessage().matcher("contains exactly").valueFirstLinesOnly(expectedList); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public TokenizedMessage mismatchedTokenizedMessage(ValuePath actualPath, Object actual) { |
| 74 | + TokenizedMessage messageTokens = tokenizedMessage(); |
| 75 | + if (!expectedCopy.isEmpty()) { |
| 76 | + messageTokens = messageTokens.error("no matches found for").colon().value( |
| 77 | + expectedCopy.stream().map(ValuePathWithValue::getValue).toList()); |
| 78 | + } |
| 79 | + |
| 80 | + if (!actualCopy.isEmpty()) { |
| 81 | + if (!messageTokens.isEmpty()) { |
| 82 | + messageTokens = messageTokens.newLine(); |
| 83 | + } |
| 84 | + messageTokens = messageTokens.error("unexpected elements").colon().value( |
| 85 | + actualCopy.stream().map(ValuePathWithValue::getValue).toList()); |
| 86 | + } |
| 87 | + |
| 88 | + return messageTokens; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean matches(ValuePath actualPath, Object actualIterable) { |
| 93 | + return matches(comparator, actualPath, actualIterable); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public TokenizedMessage negativeMatchingTokenizedMessage(ValuePath actualPath, Object actual) { |
| 98 | + comparator = CompareToComparator.comparator(CompareToComparator.AssertionMode.NOT_EQUAL); |
| 99 | + return tokenizedMessage().matcher("to not contain exactly").valueFirstLinesOnly(expectedList); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public TokenizedMessage negativeMatchedTokenizedMessage(ValuePath actualPath, Object actual) { |
| 104 | + return tokenizedMessage().matcher("not contains exactly").valueFirstLinesOnly(expectedList); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public TokenizedMessage negativeMismatchedTokenizedMessage(ValuePath actualPath, Object actual) { |
| 109 | + return tokenizedMessage().matcher("contains exactly").value(expectedList); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public boolean negativeMatches(ValuePath actualPath, Object actualIterable) { |
| 114 | + return !matches(comparator, actualPath, actualIterable); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void prettyPrint(PrettyPrinter printer) { |
| 119 | + printer.printDelimiter("<"); |
| 120 | + printer.print(PrettyPrinter.CLASSIFIER_COLOR, "containExactly "); |
| 121 | + printer.printObject(expectedList); |
| 122 | + printer.printDelimiter(">"); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String toString() { |
| 127 | + return PrettyPrinter.renderAsTextWithoutColors(this); |
| 128 | + } |
| 129 | + |
| 130 | + @SuppressWarnings("unchecked") |
| 131 | + private boolean matches(CompareToComparator comparator, ValuePath actualPath, Object actualIterable) { |
| 132 | + if (!(actualIterable instanceof Iterable)) { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + actualCopy = ValuePathWithValue.listFromIterable(actualPath, ((Iterable<Object>) actualIterable)); |
| 137 | + expectedCopy = ValuePathWithValue.listFromIterable(actualPath, expectedList); |
| 138 | + |
| 139 | + Iterator<ValuePathWithValue<Object>> expectedIt = expectedCopy.iterator(); |
| 140 | + while (expectedIt.hasNext()) { |
| 141 | + ValuePathWithValue<Object> expected = expectedIt.next(); |
| 142 | + Iterator<ValuePathWithValue<Object>> actualIt = actualCopy.iterator(); |
| 143 | + while (actualIt.hasNext()) { |
| 144 | + ValuePathWithValue<Object> actual = actualIt.next(); |
| 145 | + boolean isEqual = comparator.compareIsEqual(actual.getPath(), actual.getValue(), expected.getValue()); |
| 146 | + if (isEqual) { |
| 147 | + actualIt.remove(); |
| 148 | + expectedIt.remove(); |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + return actualCopy.isEmpty() && expectedCopy.isEmpty(); |
| 155 | + } |
| 156 | +} |
0 commit comments