Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,29 @@ public static ContainAllMatcher containingAll(Object... expected) {
}

/**
* Contain exact matcher
* Contain exactly matcher
* <pre>
* actual(collection).should(containExact(el1, el2, el3));
* </pre>
* @param expected list of values to check
* @param expected vararg list of values to check
* @return matcher instance
*/
public static ContainExactlyMatcher containExactly(Object... expected) {
return new ContainExactlyMatcher(Arrays.asList(expected));
}

/**
* Contain exactly matcher
* <pre>
* actual(collection).should(containExact(el1, el2, el3));
* </pre>
* @param expected list of values to check
* @return matcher instance
*/
public static ContainExactlyMatcher containExactly(Iterable<Object> expected) {
return new ContainExactlyMatcher(expected);
}

/**
* Greater than matcher
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
import org.testingisdocumenting.webtau.data.render.PrettyPrinter;
import org.testingisdocumenting.webtau.expectation.ExpectedValuesAware;
import org.testingisdocumenting.webtau.expectation.ValueMatcher;
import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator;
import org.testingisdocumenting.webtau.expectation.equality.CompareToResult;
import org.testingisdocumenting.webtau.expectation.equality.ValuePathLazyMessageList;
import org.testingisdocumenting.webtau.expectation.equality.ValuePathMessage;
import org.testingisdocumenting.webtau.expectation.equality.*;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.testingisdocumenting.webtau.WebTauCore.*;
import static org.testingisdocumenting.webtau.expectation.TokenizedReportUtils.*;

public class ContainExactlyMatcher implements ValueMatcher, ExpectedValuesAware, PrettyPrintable {
private final Collection<Object> expectedList;
private final Iterable<Object> expectedList;
private List<ValuePathWithValue<Object>> actualCopy;
private List<ValuePathWithValue<Object>> expectedCopy;

Expand All @@ -48,7 +46,7 @@ public class ContainExactlyMatcher implements ValueMatcher, ExpectedValuesAware,

private CompareToComparator comparator;

public ContainExactlyMatcher(Collection<Object> expected) {
public ContainExactlyMatcher(Iterable<Object> expected) {
expectedList = expected;
}

Expand All @@ -59,7 +57,7 @@ public ValueConverter valueConverter() {

@Override
public Stream<Object> expectedValues() {
return expectedList.stream();
return StreamSupport.stream(expectedList.spliterator(), false);
}

@Override
Expand Down Expand Up @@ -157,6 +155,9 @@ private boolean matches(CompareToComparator comparator, ValuePath actualPath, Ob
actualCopy = ValuePathWithValue.listFromIterable(actualPath, ((Iterable<Object>) actualIterable));
expectedCopy = ValuePathWithValue.listFromIterable(actualPath, expectedList);

CompareToHandler compareToHandlerPrevious = null;
CompareToHandler compareToHandlerToUse = null;

Iterator<ValuePathWithValue<Object>> expectedIt = expectedCopy.iterator();
while (expectedIt.hasNext()) {
ValuePathWithValue<Object> expected = expectedIt.next();
Expand All @@ -169,7 +170,20 @@ private boolean matches(CompareToComparator comparator, ValuePath actualPath, Ob
boolean found = false;
while (actualIt.hasNext()) {
ValuePathWithValue<Object> actual = actualIt.next();
CompareToResult compareToResult = comparator.compareUsingEqualOnly(actual.getPath(), actual.getValue(), expected.getValue());

// cache compare to handler to use
if (actual.getValue() != null && expected.getValue() != null) {
if (compareToHandlerPrevious == null) {
compareToHandlerPrevious = CompareToComparator.findCompareToEqualHandler(actual.getValue(), expected.getValue());
}

compareToHandlerToUse = compareToHandlerPrevious;
}

CompareToResult compareToResult = compareToHandlerToUse != null ?
comparator.compareUsingEqualOnly(compareToHandlerToUse, actual.getPath(), actual.getValue(), expected.getValue()):
comparator.compareUsingEqualOnly(actual.getPath(), actual.getValue(), expected.getValue());

if (compareToResult.isEqual()) {
actualIt.remove();
expectedIt.remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* Is NOT thread safe
*/
public class ValuePathLazyMessageList implements Iterable<ValuePathMessage> {
private static final int SIZE_LIMIT = 100;

private List<ValuePathMessage> messages;
private ValuePathMessage singleMessage;
private int size;
Expand All @@ -43,7 +45,7 @@ public void add(ValuePathMessage message) {
messages.add(message);
singleMessage = null;
}
} else {
} else if (size < SIZE_LIMIT) {
messages.add(message);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.testingisdocumenting.webtau.expectation.contain
import org.junit.Test
import org.testingisdocumenting.webtau.data.Person

import java.util.stream.IntStream

import static org.testingisdocumenting.webtau.WebTauCore.*

class ContainExactlyMatcherGroovyTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Add: speedup [containExactly](matchers/contain-exactly) by pre-caching compare to handler