package com.github.rkumsher.collection;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.*;

import java.util.Collection;
import java.util.Collections;

import org.junit.Test;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

public class IterableUtilsTest {

  @Test
  public void randomFrom_WithEmptyCollection_ThrowsIllegalArgumentException() {
    try {
      IterableUtils.randomFrom(Collections.emptyList());
      fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(), is("Iterable cannot be empty"));
    }
  }

  @Test
  public void randomFrom_WithSingletonCollection_ReturnsOnlyElement() {
    Object onlyElement = new Object();
    Iterable<Object> singletonCollection = Collections.singleton(onlyElement);
    assertThat(IterableUtils.randomFrom(singletonCollection), sameInstance(onlyElement));
  }

  @Test
  public void randomFrom_ReturnsElementFromGivenCollection() {
    Collection<Object> collection = Lists.newArrayList(new Object(), new Object());
    assertThat(IterableUtils.randomFrom(collection), isIn(collection));
  }

  @Test
  public void randomFrom_WhenCollectionOnlyContainsExcludes_ThrowsIllegalArgumentException() {
    Collection<String> collection = Lists.newArrayList("1", "2", "2", "3");
    try {
      IterableUtils.randomFrom(collection, "1", "2", "3");
      fail("Should have thrown an IllegalArgumentException");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(), is("Iterable only consists of the given excludes"));
    }
  }

  @Test
  public void randomFrom_WithExcludes_ReturnsElementFromCollectionNotInExcludes() {
    Collection<String> collection = Lists.newArrayList("1", "2", "2", "3");
    assertThat(IterableUtils.randomFrom(collection, "1", "2"), is("3"));
  }

  @Test
  public void randomFrom_WithExcludes_WorksWithImmutableCollection() {
    Collection<String> immutableCollection =
        ImmutableList.<String>builder().add("1").add("2").build();
    assertThat(IterableUtils.randomFrom(immutableCollection, "1"), is("2"));
  }
}

@@ -2241,11 +2241,411 @@
 %22));%0A  %7D
+%0A%0A  @Test%0A  public void randomFrom_WithExcludesAndEmptyCollection_ThrowsIllegalArgumentException() %7B%0A    try %7B%0A      String%5B%5D excludes = %7B %221%22, %222%22 %7D;%0A      IterableUtils.randomFrom(Collections.emptyList(), excludes);%0A      fail(%22Should have thrown an IllegalArgumentException%22);%0A    %7D catch (IllegalArgumentException ex) %7B%0A      assertThat(ex.getMessage(), is(%22Iterable cannot be empty%22));%0A    %7D%0A  %7D
 %0A%7D%0A

Add test for randomFrom with excludes and empty collection