Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
TDD Workshop makotan & masaomoc
package tddworkshop;
/**
* Created by mochizukimasao on 2017/08/01.
*
* @author mochizukimasao
* @since version
*/
public class ClosedRange {
private int lowerEndpoint;
private int upperEndpoint;
public ClosedRange(int lowerEndpoint, int upperEndpoint) {
if(upperEndpoint < lowerEndpoint) {
throw new IllegalArgumentException();
}
this.lowerEndpoint = lowerEndpoint;
this.upperEndpoint = upperEndpoint;
}
public int getLowerEndpoint() {
return lowerEndpoint;
}
public int getUpperEndpoint() {
return upperEndpoint;
}
@Override
public String toString() {
return String.format("[%s,%s]",getLowerEndpoint(),getUpperEndpoint());
}
public boolean contains(int i) {
if (getUpperEndpoint() < i || i < getLowerEndpoint()) {
return false;
}
return true;
}
@Override
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (!(object instanceof ClosedRange)) {
return false;
}
ClosedRange closedRange = (ClosedRange)object;
if(closedRange.getLowerEndpoint() == getLowerEndpoint() &&
closedRange.getUpperEndpoint() == getUpperEndpoint()) {
return true;
}
return false;
}
public static boolean compare(ClosedRange source, ClosedRange target) {
if(source == null || target == null) {
return false;
}
if(target.getLowerEndpoint() < source.getLowerEndpoint()) {
return false;
}
if(source.getUpperEndpoint() < target.getUpperEndpoint()) {
return false;
}
return true;
}
}
package tddworkshop;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by mochizukimasao on 2017/08/01.
*
* @author mochizukimasao
* @since version
*/
@RunWith(Enclosed.class)
public class ClosedRangeTest {
public static class 閉区間の前提 {
private ClosedRange sut;
@Before
public void 前準備() throws Exception {
sut = new ClosedRange(1,2);
}
@Test
public void 閉区間オブジェクトが作成できること() throws Exception {
assertThat(sut).isInstanceOf(ClosedRange.class);
}
@Test
public void 下端点を持っていること() throws Exception {
assertThat(sut.getLowerEndpoint()).isEqualTo(1);
}
@Test
public void 上端点を持っていること() throws Exception {
assertThat(sut.getUpperEndpoint()).isEqualTo(2);
}
@Test
public void 文字列として表現できること() throws Exception {
assertThat(sut.toString()).isEqualTo("[1,2]");
}
@Test(expected = IllegalArgumentException.class)
public void 上端点より下端点が大きい閉区間を作ることはできないこと() throws Exception {
ClosedRange invalidClosedRange = new ClosedRange(3, 2);
}
@Test
public void 上端点と下端点が等しい閉区間を作ることができること() throws Exception {
ClosedRange validClosedRange = new ClosedRange(2, 2);
}
}
public static class 指定した整数を含むかどうかを判定することができる {
private ClosedRange sut;
private static final int LOWER_ENDPOINT = 2;
private static final int UPPER_ENDPOINT = 10;
@Before
public void 前準備() throws Exception {
sut = new ClosedRange(LOWER_ENDPOINT,UPPER_ENDPOINT);
}
@Test
public void 下端点よりも下の値を渡した時にfalseを返すこと() throws Exception {
assertThat(sut.contains(LOWER_ENDPOINT - 1)).isFalse();
}
@Test
public void 下端点と同じ値を渡した時にtrueを返すこと() throws Exception {
assertThat(sut.contains(LOWER_ENDPOINT)).isTrue();
}
@Test
public void 下端点と上端点の間の値を渡した時にtrueを返すこと() throws Exception {
int middle = (LOWER_ENDPOINT + UPPER_ENDPOINT) / 2;
assertThat(sut.contains(middle)).isTrue();
}
@Test
public void 上端点と同じ値を渡した時にtrueを返すこと() throws Exception {
assertThat(sut.contains(UPPER_ENDPOINT)).isTrue();
}
@Test
public void 上端点よりも大きい値を渡した時falseを返すこと() throws Exception {
assertThat(sut.contains(UPPER_ENDPOINT + 1)).isFalse();
}
}
public static class 別の閉区間と等価かどうかを判定することができる {
private ClosedRange sut;
private static final int LOWER_ENDPOINT = 2;
private static final int UPPER_ENDPOINT = 10;
@Before
public void 前準備() throws Exception {
sut = new ClosedRange(LOWER_ENDPOINT, UPPER_ENDPOINT);
}
@Test
public void 下端点と上端点が同じ閉区間を渡した時にtrueを返すこと() throws Exception {
// setup
ClosedRange target = new ClosedRange(LOWER_ENDPOINT, UPPER_ENDPOINT);
assertThat(sut.equals(target)).isTrue();
}
@Test
public void 下端点が異なる閉区間を渡した時falseを返すこと() throws Exception {
// setup
ClosedRange target = new ClosedRange(LOWER_ENDPOINT + 1, UPPER_ENDPOINT);
assertThat(sut.equals(target)).isFalse();
}
@Test
public void 上端点が異なる閉区間を渡した時falseを返すこと() throws Exception {
// setup
ClosedRange target = new ClosedRange(LOWER_ENDPOINT, UPPER_ENDPOINT + 1);
assertThat(sut.equals(target)).isFalse();
}
@Test
public void equalsの引数がnullの時falseを返す() throws Exception {
assertThat(sut.equals(null)).isFalse();
}
@Test
public void equalsの引数がClosedRangeでない時にfalseを返す() throws Exception {
assertThat(sut.equals("string")).isFalse();
}
}
public static class 完全に含まれるかどうかを判定することができる {
private ClosedRange sut;
private static final int LOWER_ENDPOINT = 2;
private static final int UPPER_ENDPOINT = 10;
@Before
public void 前準備() throws Exception {
sut = new ClosedRange(LOWER_ENDPOINT, UPPER_ENDPOINT);
}
@Test
public void 完全に含まれる場合はtrueを返す() throws Exception {
ClosedRange target = new ClosedRange(LOWER_ENDPOINT + 1, UPPER_ENDPOINT - 1);
assertThat(ClosedRange.compare(sut, target)).isTrue();
}
@Test
public void 下端点が元のオブジェクトより小さい場合falseを返す() throws Exception {
ClosedRange target = new ClosedRange(LOWER_ENDPOINT - 1, UPPER_ENDPOINT);
assertThat(ClosedRange.compare(sut, target)).isFalse();
}
@Test
public void 元のオブジェクトがnullの場合はfalseを返す() throws Exception {
ClosedRange target = new ClosedRange(LOWER_ENDPOINT, UPPER_ENDPOINT + 1);
assertThat(ClosedRange.compare(null, target)).isFalse();
}
@Test
public void 比較対象のオブジェクトnullの場合はfalseを返す() throws Exception {
assertThat(ClosedRange.compare(sut, null)).isFalse();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.