BiFunctionTesterImpl.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit.bifunction.impl;

  21. import java.util.function.BiFunction;
  22. import java.util.stream.Stream;
  23. import java.util.stream.Stream.Builder;

  24. import org.hamcrest.Matcher;

  25. import ch.powerunit.Parameter;
  26. import ch.powerunit.Parameters;
  27. import ch.powerunit.Test;
  28. import ch.powerunit.TestDelegator;
  29. import ch.powerunit.TestSuite;
  30. import ch.powerunit.bifunction.BiFunctionTester;

  31. /**
  32.  * @author borettim
  33.  *
  34.  */
  35. @TestDelegator
  36. public class BiFunctionTesterImpl<T, U, R> implements TestSuite {
  37.     @Parameters
  38.     public static <T, U, R> Stream<Object[]> getParameters(
  39.             BiFunctionTester<T, U, R> input) {
  40.         Builder<Object[]> b = Stream.builder();
  41.         for (int i = 0; i < input.getInput1().size(); i++) {
  42.             Matcher<? super R> result = input.getResult().get(i).get();
  43.             T param1 = input.getInput1().get(i).get();
  44.             U param2 = input.getInput2().get(i).get();
  45.             String name = input.getName().get(i).get();
  46.             if ("".equals(name)) {
  47.                 name = "Passing `" + param1 + "` and `" + param2 + "`"
  48.                         + " then " + result + " is expected";
  49.             }
  50.             b.add(new Object[] { name, param1, param2, result,
  51.                     input.getUnderTest() });
  52.         }
  53.         return b.build();
  54.     }

  55.     @Parameter(0)
  56.     public String name;

  57.     @Parameter(1)
  58.     public T input1;

  59.     @Parameter(2)
  60.     public U input2;

  61.     @Parameter(3)
  62.     public Matcher<? super R> expectedResult;

  63.     @Parameter(4)
  64.     public BiFunction<T, U, R> function;

  65.     @Test(name = "%1$s")
  66.     public void testFunction() {
  67.         assertThatBiFunction(function, input1, input2).is(expectedResult);
  68.     }
  69. }