X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FListAccessorTest.java;fp=src%2Ftest%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FListAccessorTest.java;h=0c63aca38c4374d4a3cd85e2ef9f6b5e9c2cdbe5;hb=b4c32fd82aa6c88397bdf37b3b9a3b2a6fab0c1d;hp=0000000000000000000000000000000000000000;hpb=cc18e5878bc71081da5137eb777d91821c3f4cce;p=Sone.git diff --git a/src/test/java/net/pterodactylus/sone/template/ListAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/ListAccessorTest.java new file mode 100644 index 0000000..0c63aca --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/ListAccessorTest.java @@ -0,0 +1,50 @@ +package net.pterodactylus.sone.template; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.Matchers.sameInstance; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.util.template.Accessor; + +import org.junit.Test; + +/** + * Unit test for {@link ListAccessorTest}. + * + * @author David ‘Bombe’ Roden + */ +public class ListAccessorTest { + + private final Accessor accessor = new ListAccessor(); + + @Test + public void gettingARandomElementFromAnEmptyListReturnsNull() { + assertThat(accessor.get(null, Collections.emptyList(), "random"), nullValue()); + } + + @Test + public void gettingARandomElementFromAListOfOneWillReturnTheOneElement() { + Object object = new Object(); + assertThat(accessor.get(null, Arrays.asList(object), "random"), sameInstance(object)); + } + + @Test + public void gettingRandomElementsFromAListTwoElementsWillReturnBothWithSomeProportion() { + Object first = new Object(); + Object second = new Object(); + List objects = Arrays.asList(first, second); + int gotFirst = 0; + for (int i = 0; i < 10000; i++) { + if (accessor.get(null, objects, "random") == first) { + gotFirst++; + } + } + assertThat(gotFirst, greaterThanOrEqualTo(4000)); + } + +}