/** * Return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. * * @param actualAsList the list to look sequence in * @param sequence the sequence to look for * @param startingIndex the index of actual list at which we start looking for sequence. * @return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. */ private boolean containsSequenceAtGivenIndex(List<?> actualAsList, Object[] sequence, int startingIndex) { // check that, starting from given index, actualAsList has enough remaining elements to contain sequence if (actualAsList.size() - startingIndex < sequence.length) return false; for (int i = 0; i < sequence.length; i++) { if (!areEqual(actualAsList.get(startingIndex + i), sequence[i])) return false; } return true; }
/** * Return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. * * @param actualAsList the list to look sequence in * @param sequence the sequence to look for * @param startingIndex the index of actual list at which we start looking for sequence. * @return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. */ private boolean containsSequenceAtGivenIndex(List<?> actualAsList, Object[] sequence, int startingIndex) { // check that, starting from given index, actualAsList has enough remaining elements to contain sequence if (actualAsList.size() - startingIndex < sequence.length) return false; for (int i = 0; i < sequence.length; i++) { if (!areEqual(actualAsList.get(startingIndex + i), sequence[i])) return false; } return true; }
/** * Verifies that the given <code>{@link Iterable}</code> contains the given subsequence of objects (possibly with * other values between them). * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param subsequence the subsequence of objects to look for. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws NullPointerException if the given sequence is {@code null}. * @throws IllegalArgumentException if the given subsequence is empty. * @throws AssertionError if the given {@code Iterable} does not contain the given subsequence of objects. */ public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, subsequence)) return; Iterator<?> actualIterator = actual.iterator(); int subsequenceIndex = 0; while (actualIterator.hasNext() && subsequenceIndex < subsequence.length) { Object actualNext = actualIterator.next(); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) subsequenceIndex++; } if (subsequenceIndex < subsequence.length) throw actualDoesNotContainSubsequence(info, actual, subsequence); }
/** * Verifies that the given <code>{@link Iterable}</code> contains the given subsequence of objects (possibly with * other values between them). * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param subsequence the subsequence of objects to look for. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws NullPointerException if the given sequence is {@code null}. * @throws IllegalArgumentException if the given subsequence is empty. * @throws AssertionError if the given {@code Iterable} does not contain the given subsequence of objects. */ public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, subsequence)) return; Iterator<?> actualIterator = actual.iterator(); int subsequenceIndex = 0; while (actualIterator.hasNext() && subsequenceIndex < subsequence.length) { Object actualNext = actualIterator.next(); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) subsequenceIndex++; } if (subsequenceIndex < subsequence.length) throw actualDoesNotContainSubsequence(info, actual, subsequence); }
Object actualNext = actualAsList.get(index); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) { if (subsequenceIndex == 0) subsequenceStartIndex = index; subsequenceIndex++;
/** * Verifies that the given {@code Iterable} starts with the given sequence of objects, without any other objects * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it * also verifies that the first element in the sequence is also the first element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not start with the given sequence of objects. */ public void assertStartsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int i = 0; for (Object actualCurrentElement : actual) { if (i >= sequence.length) break; if (areEqual(actualCurrentElement, sequence[i++])) continue; throw actualDoesNotStartWithSequence(info, actual, sequence); } if (sequence.length > i) { // sequence has more elements than actual throw actualDoesNotStartWithSequence(info, actual, sequence); } }
/** * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also * verifies that the last element in the sequence is also the last element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects. */ public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { checkNotNullIterables(info, actual, sequence); int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequence.length) throw actualDoesNotEndWithSequence(info, actual, sequence); int start = sizeOfActual - sequence.length; int sequenceIndex = 0, indexOfActual = 0; for (Object actualElement : actual) { if (indexOfActual++ < start) continue; if (areEqual(actualElement, sequence[sequenceIndex++])) continue; throw actualDoesNotEndWithSequence(info, actual, sequence); } }
Object actualNext = actualAsList.get(index); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) { if (subsequenceIndex == 0) subsequenceStartIndex = index; subsequenceIndex++;
/** * Verifies that the given {@code Iterable} starts with the given sequence of objects, without any other objects * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it * also verifies that the first element in the sequence is also the first element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not start with the given sequence of objects. */ public void assertStartsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int i = 0; for (Object actualCurrentElement : actual) { if (i >= sequence.length) break; if (areEqual(actualCurrentElement, sequence[i++])) continue; throw actualDoesNotStartWithSequence(info, actual, sequence); } if (sequence.length > i) { // sequence has more elements than actual throw actualDoesNotStartWithSequence(info, actual, sequence); } }
/** * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also * verifies that the last element in the sequence is also the last element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects. */ public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { checkNotNullIterables(info, actual, sequence); int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequence.length) throw actualDoesNotEndWithSequence(info, actual, sequence); int start = sizeOfActual - sequence.length; int sequenceIndex = 0, indexOfActual = 0; for (Object actualElement : actual) { if (indexOfActual++ < start) continue; if (areEqual(actualElement, sequence[sequenceIndex++])) continue; throw actualDoesNotEndWithSequence(info, actual, sequence); } }
if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, elementsDifferAtIndex(elementFromActual, values[i], i, comparisonStrategy));
if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, elementsDifferAtIndex(elementFromActual, values[i], i, comparisonStrategy));
/** * Return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. * * @param actualAsList the list to look sequance in * @param sequence the sequence to look for * @param startingIndex the index of actual list at which we start looking for sequence. * @return true if actualAsList contains exactly the given sequence at given starting index, false otherwise. */ private boolean containsSequenceAtGivenIndex(List<?> actualAsList, Object[] sequence, int startingIndex) { // check that, starting from given index, actualAsList has enough remaining elements to contain sequence if (actualAsList.size() - startingIndex < sequence.length) { return false; } for (int i = 0; i < sequence.length; i++) { if (!areEqual(actualAsList.get(startingIndex + i), sequence[i])) { return false; } } return true; }
/** * Verifies that the given <code>{@link Iterable}</code> contains the given subsequence of objects (possibly with * other values between them). * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param subsequence the subsequence of objects to look for. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws NullPointerException if the given sequence is {@code null}. * @throws IllegalArgumentException if the given subsequence is empty. * @throws AssertionError if the given {@code Iterable} does not contain the given subsequence of objects. */ public void assertContainsSubsequence(AssertionInfo info, Iterable<?> actual, Object[] subsequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, subsequence)) { return; } Iterator<?> actualIterator = actual.iterator(); int subsequenceIndex = 0; while (actualIterator.hasNext() && subsequenceIndex < subsequence.length) { Object actualNext = actualIterator.next(); Object subsequenceNext = subsequence[subsequenceIndex]; if (areEqual(actualNext, subsequenceNext)) { subsequenceIndex++; } } if (subsequenceIndex < subsequence.length) { throw actualDoesNotContainSubsequence(info, actual, subsequence); } }
/** * Verifies that the given {@code Iterable} starts with the given sequence of objects, without any other objects * between them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it * also verifies that the first element in the sequence is also the first element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not start with the given sequence of objects. */ public void assertStartsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int i = 0; for (Object actualCurrentElement : actual) { if (i >= sequence.length) break; if (areEqual(actualCurrentElement, sequence[i++])) continue; throw actualDoesNotStartWithSequence(info, actual, sequence); } if (sequence.length > i) { // sequence has more elements than actual throw actualDoesNotStartWithSequence(info, actual, sequence); } }
/** * Verifies that the given {@code Iterable} ends with the given sequence of objects, without any other objects between * them. Similar to <code>{@link #assertContainsSequence(AssertionInfo, Iterable, Object[])}</code>, but it also * verifies that the last element in the sequence is also the last element of the given {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param sequence the sequence of objects to look for. * @throws NullPointerException if the given argument is {@code null}. * @throws IllegalArgumentException if the given argument is an empty array. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not end with the given sequence of objects. */ public void assertEndsWith(AssertionInfo info, Iterable<?> actual, Object[] sequence) { if (commonCheckThatIterableAssertionSucceeds(info, actual, sequence)) return; int sizeOfActual = sizeOf(actual); if (sizeOfActual < sequence.length) { throw actualDoesNotEndWithSequence(info, actual, sequence); } int start = sizeOfActual - sequence.length; int sequenceIndex = 0, indexOfActual = 0; for (Object actualElement : actual) { if (indexOfActual++ < start) continue; if (areEqual(actualElement, sequence[sequenceIndex++])) continue; throw actualDoesNotEndWithSequence(info, actual, sequence); } }
/** * Asserts that the given {@code Iterable} contains exactly the given values and nothing else, <b>in order</b>. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param values the values that are expected to be in the given {@code Iterable} in order. * @throws NullPointerException if the array of values is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given * {@code Iterable} contains values that are not in the given array, in order. */ public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object[] values) { checkIsNotNull(values); assertHasSameSizeAs(info, actual, values); // include check that actual is not null Set<Object> notExpected = setFromIterable(actual); Set<Object> notFound = containsOnly(notExpected, values); if (notExpected.isEmpty() && notFound.isEmpty()) { // actual and values have the same elements but are they in the same order. int i = 0; for (Object elementFromActual : actual) { if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, shouldContainExactly(elementFromActual, values[i], i, comparisonStrategy)); } i++; } return; } throw failures.failure(info, shouldContainExactly(actual, values, notFound, notExpected, comparisonStrategy)); }