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.core.Every.everyItem;
import static org.junit.Assume.assumeThat;

import java.math.BigInteger;
import java.util.List;

import com.pholser.junit.quickcheck.ForAll;
import com.pholser.junit.quickcheck.generator.InRange;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
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
{
    @Theory public void primeNumberIsItsOwnFactor(@ForAll @InRange(minInt = 2, maxInt = Integer.MAX_VALUE) Integer number) {
        assumeThat(number, isProbablySimple());

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

    @Theory public void productOfFactorsShouldBeEqualToNumber(@ForAll @InRange(minInt = 2, maxInt = Integer.MAX_VALUE) 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 = Integer.MAX_VALUE) Integer number) {
        List<Integer> factors = PrimeFactors.extract(number);
        assertThat(factors, everyItem(isProbablySimple()));
    }

    @Theory public void everyFactorShouldBeSimpleEspeciallyForSmallNumbers(@ForAll @InRange(minInt = 2, maxInt = 200) Integer number) {
        everyFactorShouldBeSimple(number);
    }

    private Matcher<Integer> isProbablySimple()
    {
        return new BaseMatcher<Integer>()
        {
            @Override
            public boolean matches(Object item)
            {
                return (item instanceof Integer) &&
                    (BigInteger.valueOf((Integer) item).isProbablePrime(5));
            }

            @Override
            public void describeTo(Description description)
            {
                description.appendText("prime number");
            }
        };
    }
}

@@ -43,116 +43,158 @@
 ort 
-static org.hamcrest.CoreMatchers.hasItem;%0Aimport static org.hamcrest.MatcherAssert.assertThat;%0Aimport static
+com.pholser.junit.quickcheck.ForAll;%0Aimport com.pholser.junit.quickcheck.generator.InRange;%0Aimport java.math.BigInteger;%0Aimport java.util.List;%0Aimport
  org
@@ -207,19 +207,19 @@
 est.
+Base
 Matcher
-s.is
 ;%0Aim
@@ -219,31 +219,24 @@
 her;%0Aimport 
-static 
 org.hamcrest
@@ -240,233 +240,177 @@
 est.
-core.Every.everyItem;%0Aimport static org.junit.Assume.assumeThat
+Description;%0Aimport org.hamcrest.Matcher
 ;%0A
-%0A
 import 
-java.math.BigInteger;%0Aimport java.util.List;%0A%0Aimport com.pholser.junit.quickcheck.ForAll;%0Aimport com.pholser.junit.quickcheck.generator.InRange;
+org.junit.contrib.theories.Theories;%0Aimport org.junit.contrib.theories.Theory;%0Aimport org.junit.runner.RunWith;%0A
 %0Aimport 
 org.
@@ -397,32 +397,39 @@
 unWith;%0A%0Aimport 
+static 
 org.hamcrest.Bas
@@ -421,35 +421,35 @@
 rg.hamcrest.
-Bas
+Cor
 eMatcher
 ;%0Aimport org
@@ -428,32 +428,41 @@
 rest.CoreMatcher
+s.hasItem
 ;%0Aimport org.ham
@@ -446,32 +446,39 @@
 hasItem;%0Aimport 
+static 
 org.hamcrest.Des
@@ -470,35 +470,48 @@
 rg.hamcrest.
-Description
+MatcherAssert.assertThat
 ;%0Aimport org
@@ -499,32 +499,39 @@
 ertThat;%0Aimport 
+static 
 org.hamcrest.Mat
@@ -526,32 +526,36 @@
 hamcrest.Matcher
+s.is
 ;%0Aimport org.jun
@@ -551,43 +551,48 @@
 ort 
-org.junit.contrib.theories.Theories
+static org.hamcrest.core.Every.everyItem
 ;%0Aim
@@ -588,32 +588,39 @@
 eryItem;%0Aimport 
+static 
 org.junit.contri
@@ -617,70 +617,169 @@
 nit.
-contrib.theories.Theory;%0Aimport org.junit.runner.RunWith;%0A%0A/**
+Assume.assumeThat;%0A%0A/**%0A * @deprecated This example uses %3Cb%3Eold version of JUnit-QuickCheck%3C/b%3E. Please take a look at the newer example, %7B@link LightSwitchTest%7D
 %0A * 
@@ -848,16 +848,28 @@
 .class)%0A
+@Deprecated%0A
 public c

Add deprecation warning