package org.yukung.tutorial.junit;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import org.junit.experimental.runners.Enclosed;
import org.junit.experimental.theories.DataPoint;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Enclosed.class)
public class CalculatorTest {

	@RunWith(Theories.class)
	public static class 乗算メソッドのパラメータ化テスト {

		@DataPoint
		public static Fixture DATA1 = new Fixture(3, 4, 12);
		@DataPoint
		public static Fixture DATA2 = new Fixture(5, 7, 35);

		@Theory
		public void multiplyで乗算結果が取得できること(Fixture fx) {
			String msg = fx.x + "*" + fx.y + "=" + fx.expected;
			Calculator calc = new Calculator();
			int expected = fx.expected;
			int actual = calc.multiply(fx.x, fx.y);
			assertThat(msg, actual, is(expected));
		}

		static class Fixture {
			int x, y, expected;

			Fixture(int x, int y, int expected) {
				this.x = x;
				this.y = y;
				this.expected = expected;
			}
		}

	}

	@RunWith(Theories.class)
	public static class 除算メソッドのパラメータ化テスト {
		@DataPoint
		public static Fixture DATA1 = new Fixture(3, 2, 1.5f);
		@DataPoint
		public static Fixture DATA2 = new Fixture(5, 0, 0.0f);

		@Theory
		public void devideで除算結果が取得できること(Fixture fx) {
			String msg = fx.x + "/" + fx.y + "=" + fx.expected;
			Calculator calc = new Calculator();
			float expected = fx.expected;
			try {
				float actual = calc.divide(fx.x, fx.y);
				assertThat(msg, actual, is(expected));
			} catch (IllegalArgumentException e) {
				assertThat(msg, e, is(IllegalArgumentException.class));
			}
		}

		static class Fixture {
			int x, y;
			float expected;

			Fixture(int x, int y, float expected) {
				this.x = x;
				this.y = y;
				this.expected = expected;
			}
		}
	}

}

@@ -203,16 +203,17 @@
 ataPoint
+s
 ;%0Aimport
@@ -458,17 +458,16 @@
 %E3%82%BF%E5%8C%96%E3%83%86%E3%82%B9%E3%83%88 %7B%0A
-%0A
 %09%09@DataP
@@ -462,32 +462,33 @@
 %E3%83%88 %7B%0A%09%09@DataPoint
+s
 %0A%09%09public static
@@ -491,32 +491,36 @@
 atic Fixture
+%5B%5D
  DATA
-1
+S
  =
+ %7B
  new Fixture
@@ -533,54 +533,9 @@
  12)
-;%0A%09%09@DataPoint%0A%09%09public static Fixture DATA2 =
+,
  new
@@ -552,16 +552,19 @@
 , 7, 35)
+, %7D
 ;%0A%0A%09%09@Th
@@ -1069,32 +1069,33 @@
 %E3%83%88 %7B%0A%09%09@DataPoint
+s
 %0A%09%09public static
@@ -1106,16 +1106,20 @@
 ture
+%5B%5D
  DATA
-1
+S
  =
+ %7B
  new
@@ -1142,54 +1142,9 @@
 .5f)
-;%0A%09%09@DataPoint%0A%09%09public static Fixture DATA2 =
+,
  new
@@ -1163,16 +1163,19 @@
 0, 0.0f)
+, %7D
 ;%0A%0A%09%09@Th

テストパラメータの与え方を @DataPoints アノテーションで一括して与えるように変更。