package com.github.ahitrin.jqexample;

import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isIn;
import static org.hamcrest.core.Every.everyItem;
import static org.junit.Assume.assumeThat;

import java.util.Arrays;
import java.util.List;

import com.pholser.junit.quickcheck.ForAll;
import com.pholser.junit.quickcheck.generator.InRange;
import org.junit.contrib.theories.Theories;
import org.junit.contrib.theories.Theory;
import org.junit.runner.RunWith;

/**
 * @author Andrey Hitrin
 * @since 25.09.14
 */
@RunWith(Theories.class)
public class PrimeFactorsTest
{
    private final List<Integer> firstPrimeNumbers = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47);

    @Theory public void primeNumberIsItsOwnFactor(@ForAll @InRange(minInt = 2, maxInt = 500) Integer number) {
        assumeThat(number, isIn(firstPrimeNumbers));

        List<Integer> factors = PrimeFactors.extract(number);
        assertThat(factors, hasItem(number));
    }

    @Theory public void productOfFactorsShouldBeEqualToNumber(@ForAll @InRange(minInt = 2, maxInt = 500) Integer number) {
        List<Integer> factors = PrimeFactors.extract(number);
        Integer product = 1;
        for (Integer factor: factors)
            product = product * factor;
        assertThat(product, is(number));
    }

    @Theory public void everyFactorShouldBeSimple(@ForAll @InRange(minInt = 2, maxInt = 500) Integer number) {
        List<Integer> factors = PrimeFactors.extract(number);
        assertThat(factors, everyItem(isIn(firstPrimeNumbers)));
    }
}

@@ -178,50 +178,8 @@
 is;%0A
-import static org.hamcrest.Matchers.isIn;%0A
 impo
@@ -219,24 +219,24 @@
 .everyItem;%0A
+
 import stati
@@ -283,19 +283,23 @@
 ava.
-util.Arrays
+math.BigInteger
 ;%0Aim
@@ -419,16 +419,111 @@
 nRange;%0A
+import org.hamcrest.BaseMatcher;%0Aimport org.hamcrest.Description;%0Aimport org.hamcrest.Matcher;%0A
 import o
@@ -747,132 +747,8 @@
 t%0A%7B%0A
-    private final List%3CInteger%3E firstPrimeNumbers = Arrays.asList(2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47);%0A%0A
     
@@ -883,36 +883,31 @@
 mber, is
-In(firstPrimeNumbers
+ProbablySimple(
 ));%0A%0A   
@@ -1575,31 +1575,544 @@
 m(is
-In(firstPrimeNumbers)))
+ProbablySimple()));%0A    %7D%0A%0A    private Matcher%3CInteger%3E isProbablySimple()%0A    %7B%0A        return new BaseMatcher%3CInteger%3E()%0A        %7B%0A            @Override%0A            public boolean matches(Object item)%0A            %7B%0A                return (item instanceof Integer) &&%0A                    (BigInteger.valueOf((Integer) item).isProbablePrime(5));%0A            %7D%0A%0A            @Override%0A            public void describeTo(Description description)%0A            %7B%0A                description.appendText(%22prime number%22);%0A            %7D%0A        %7D
 ;%0A  

Use custom matcher to filter prime numbers