/** * Starts a method chain for a check in which the actual elements (i.e. the elements of the {@link * Iterable} under test) are compared to expected elements using the given {@link Correspondence}. * The actual elements must be of type {@code A}, the expected elements must be of type {@code E}. * The check is actually executed by continuing the method chain. For example: * * <pre>{@code * assertThat(actualIterable).comparingElementsUsing(correspondence).contains(expected); * }</pre> * * where {@code actualIterable} is an {@code Iterable<A>} (or, more generally, an {@code * Iterable<? extends A>}), {@code correspondence} is a {@code Correspondence<A, E>}, and {@code * expected} is an {@code E}. * * <p>Any of the methods on the returned object may throw {@link ClassCastException} if they * encounter an actual element that is not of type {@code A}. * * <p>Note that the {@code IterableOfProtosSubject} is designed to save you from having to write * your own {@link Correspondence}. The configuration methods, such as {@link * #ignoringRepeatedFieldOrder()} will construct a {@link Correspondence} under the hood which * performs protobuf comparisons with {@link #ignoringRepeatedFieldOrder()}. */ public <A, E> IterableSubject.UsingCorrespondence<A, E> comparingElementsUsing( Correspondence<A, E> correspondence) { return delegate().comparingElementsUsing(correspondence); }
/** Variant of testIntendedSuccess_truthCorrespondence that uses chained Correspondences. */ @Test public void testIntendedSuccess_truthChainedCorrespondence() { Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); i.putExtra("ignoreextra", ""); i.setData(Uri.parse("http://robolectric.org")); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(i); Intent alsoSentIntentButNotMatching = new Intent(Intent.ACTION_VIEW); alsoSentIntentButNotMatching.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(alsoSentIntentButNotMatching); Intent expectedIntent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse("http://robolectric.org")); assertThat(getIntents()).comparingElementsUsing(all(action(), data())).contains(expectedIntent); }
/** * Fails if the map does not contain exactly the keys in the given multimap, mapping to values * that correspond to the values of the given multimap. * * <p>A subsequent call to {@link Ordered#inOrder} may be made if the caller wishes to verify * that the two Multimaps iterate fully in the same order. That is, their key sets iterate in * the same order, and the corresponding value collections for each key iterate in the same * order. */ @CanIgnoreReturnValue public <K, V extends E> Ordered containsExactlyEntriesIn(Multimap<K, V> expectedMultimap) { // Note: The non-fuzzy MultimapSubject.containsExactlyEntriesIn has a custom implementation // and produces somewhat better failure messages simply asserting about the iterables of // entries would: it formats the expected values as k=[v1, v2] rather than k=v1, k=v2; and in // the case where inOrder() fails it says the keys and/or the values for some keys are out of // order. We don't bother with that here. It would be nice, but it would be a lot of added // complexity for little gain. return check() .about(iterableEntries()) .that(actual().entries()) .comparingElementsUsing(new EntryCorrespondence<K, A, V>(correspondence)) .containsExactlyElementsIn(expectedMultimap.entries()); }
/** * Variant of testIntendedSuccess that uses truth APIs. * * <p>This is a more flexible/lenient variant of {@link #testIntendedSuccess_truth} that handles * cases where other intents might have been sent. */ @Test public void testIntendedSuccess_truthCorrespondence() { Intent i = new Intent(); i.setAction(Intent.ACTION_VIEW); i.putExtra("ignoreextra", ""); i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(i); Intent alsoSentIntentButDontCare = new Intent(Intent.ACTION_MAIN); alsoSentIntentButDontCare.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(alsoSentIntentButDontCare); assertThat(getIntents()) .comparingElementsUsing(action()) .contains(new Intent(Intent.ACTION_VIEW)); }
@Test public void getAvailableRestoreSets_shouldCallbackToRestoreSetsAvailable() { RestoreSession restoreSession = backupManager.beginRestoreSession(); int result = restoreSession.getAvailableRestoreSets(restoreObserver); assertThat(result).isEqualTo(BackupManager.SUCCESS); Robolectric.flushForegroundThreadScheduler(); ArgumentCaptor<RestoreSet[]> restoreSetArg = ArgumentCaptor.forClass(RestoreSet[].class); verify(restoreObserver).restoreSetsAvailable(restoreSetArg.capture()); RestoreSet[] restoreSets = restoreSetArg.getValue(); assertThat(restoreSets).hasLength(2); assertThat(restoreSets) .asList() .comparingElementsUsing(fieldCorrespondence("token")) .containsExactly(123L, 456L); }
/** * Starts a method chain for a check in which the actual elements (i.e. the elements of the {@link * Iterable} under test) are compared to expected elements using the given {@link Correspondence}. * The actual elements must be of type {@code A}, the expected elements must be of type {@code E}. * The check is actually executed by continuing the method chain. For example: * * <pre>{@code * assertThat(actualIterable).comparingElementsUsing(correspondence).contains(expected); * }</pre> * * where {@code actualIterable} is an {@code Iterable<A>} (or, more generally, an {@code * Iterable<? extends A>}), {@code correspondence} is a {@code Correspondence<A, E>}, and {@code * expected} is an {@code E}. * * <p>Any of the methods on the returned object may throw {@link ClassCastException} if they * encounter an actual element that is not of type {@code A}. * * <p>Note that the {@code IterableOfProtosSubject} is designed to save you from having to write * your own {@link Correspondence}. The configuration methods, such as {@link * #ignoringRepeatedFieldOrder()} will construct a {@link Correspondence} under the hood which * performs protobuf comparisons with {@link #ignoringRepeatedFieldOrder()}. */ public <A, E> IterableSubject.UsingCorrespondence<A, E> comparingElementsUsing( Correspondence<A, E> correspondence) { return delegate().comparingElementsUsing(correspondence); }