Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@twada
Last active April 10, 2024 04:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save twada/d089745ebecd4d3a585c84496b2ad391 to your computer and use it in GitHub Desktop.
Save twada/d089745ebecd4d3a585c84496b2ad391 to your computer and use it in GitHub Desktop.
テスト駆動開発ライブコーディング時のコード
package tdd;
public class FizzBuzz {
public String convert(int num) {
if (num % 3 == 0) {
return "Fizz";
}
if (num % 5 == 0) {
return "Buzz";
}
return String.valueOf(num);
}
}
package tdd;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("Fizz Buzz 数列とその変換規則を扱う FizzBuzz クラス")
class FizzBuzz1Test {
private FizzBuzz fizzbuzz;
@BeforeEach
void 準備() {
fizzbuzz = new FizzBuzz();
}
@Nested
class convertメソッドは引数に与えられた整数を文字列に変換する {
@Nested
class _3の倍数のときは数の代わりにFizzに変換する {
@Test
void _3を渡すと文字列Fizzを返す() throws Exception {
assertEquals("Fizz", fizzbuzz.convert(3));
}
}
@Nested
class _5の倍数のときは数の代わりにBuzzに変換する {
@Test
void _5を渡すと文字列Buzzを返す() throws Exception {
assertEquals("Buzz", fizzbuzz.convert(5));
}
}
@Nested
class その他の数のときは数をそのまま文字列に変換する {
@Test
void _1を渡すと文字列1を返す() throws Exception {
assertEquals("1", fizzbuzz.convert(1));
}
}
}
}
package tdd;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
@DisplayName("Fizz Buzz 数列とその変換規則を扱う FizzBuzz クラス")
class FizzBuzz2Test {
private FizzBuzz fizzbuzz;
@BeforeEach
void 準備() {
fizzbuzz = new FizzBuzz();
}
@Nested
class convertメソッドは引数に与えられた整数を文字列に変換する {
@Nested
class _3の倍数のときは数の代わりにFizzに変換する {
@Test
void 同値類の中の最小の3の倍数3を渡すと文字列Fizzを返す() throws Exception {
assertEquals("Fizz", fizzbuzz.convert(3));
}
@Test
void 上限の境界値のひとつ内側の値であり同値類の中の最大の3の倍数99を渡すと文字列Fizzを返す() throws Exception {
assertEquals("Fizz", fizzbuzz.convert(99));
}
}
@Nested
class _5の倍数のときは数の代わりにBuzzに変換する {
@Test
void 同値類の中の最小の5の倍数5を渡すと文字列Buzzを返す() throws Exception {
assertEquals("Buzz", fizzbuzz.convert(5));
}
@Test
void 上限の境界値であり同値類の中の最大の5の倍数100を渡すと文字列Buzzを返す() throws Exception {
assertEquals("Buzz", fizzbuzz.convert(100));
}
}
@Nested
class _3と5両方の倍数のときは数の代わりにFizzBuzzに変換する {
@Test
void 同値類の中の最小の3と5の公倍数15を渡すと文字列FizzBuzzを返す() throws Exception {
assertEquals("FizzBuzz", fizzbuzz.convert(15));
}
}
@Nested
class その他の数のときは数をそのまま文字列に変換する {
@Test
void 下限の境界値1を渡すと文字列1を返す() throws Exception {
assertEquals("1", fizzbuzz.convert(1));
}
@Test
void 下限の境界値のひとつ内側の値2を渡すと文字列2を返す() throws Exception {
assertEquals("2", fizzbuzz.convert(2));
}
}
@Nested
class 仕様の範囲外の数であっても同じ変換規則が適用される {
@Test
void 上限の境界値のひとつ外側の値101を渡すと文字列101を返す() throws Exception {
assertEquals("101", fizzbuzz.convert(101));
}
@Test
void 下限の境界値のひとつ外側の値0は3と5両方の倍数でもあるので0を渡すと文字列FizzBuzzを返す() throws Exception {
assertEquals("FizzBuzz", fizzbuzz.convert(0));
}
}
}
}
package tdd;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
@DisplayName("Fizz Buzz 数列とその変換規則を扱う FizzBuzz クラス")
class FizzBuzz3Test {
private FizzBuzz fizzbuzz;
@BeforeEach
void 前準備() {
fizzbuzz = new FizzBuzz();
}
@Nested
class convertメソッドは数を文字列に変換する {
@ParameterizedTest(name = "{1}{0}を渡すと文字列「Fizz」を返すこと")
@CsvSource({
"3, '同値類の中の最小の3の倍数'",
"99, '上限の境界値のひとつ内側の値であり同値類の中の最大の3の倍数'"
})
void _3の倍数のときは数の代わりにFizzに変換する(int input, String description) {
assertEquals("Fizz", fizzbuzz.convert(input));
}
@ParameterizedTest(name = "{1}{0}を渡すと文字列「Buzz」を返すこと")
@CsvSource({
"5, '同値類の中の最小の5の倍数'",
"100, '上限の境界値であり同値類の中の最大の5の倍数'"
})
void _5の倍数のときは数の代わりにBuzzに変換する(int input, String description) {
assertEquals("Buzz", fizzbuzz.convert(input));
}
@ParameterizedTest(name = "{1}{0}を渡すと文字列「FizzBuzz」を返すこと")
@CsvSource({
"15, '同値類の中の最小の3と5の公倍数'"
})
void _3と5両方の倍数の場合には数の代わりにFizzBuzzに変換する(int input, String description) {
assertEquals("FizzBuzz", fizzbuzz.convert(input));
}
@ParameterizedTest(name = "{2}{0}を渡すと文字列「{1}」を返すこと")
@CsvSource({
"1, '1', '下限の境界値'",
"2, '2', '下限の境界値のひとつ内側の値'"
})
void その他の数のときはそのまま文字列に変換する(int input, String expected, String description) {
assertEquals(expected, fizzbuzz.convert(input));
}
@ParameterizedTest(name = "{2}{0}を渡すと文字列「{1}」を返すこと")
@CsvSource({
"0, 'FizzBuzz', '下限の境界値のひとつ外側の値0は3と5の公倍数でもあるので'",
"101, '101', '上限の境界値のひとつ外側の値'"
})
void 仕様の範囲外の数であっても同じ変換規則が適用される(int input, String expected, String description) {
assertEquals(expected, fizzbuzz.convert(input));
}
}
}
package tdd;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class FizzBuzzOldTest {
private FizzBuzz fizzbuzz;
@BeforeEach
void 準備() {
fizzbuzz = new FizzBuzz();
}
@Test
void _1を渡すと文字列1を返す() throws Exception {
assertEquals("1", fizzbuzz.convert(1));
}
@Test
void _2を渡すと文字列2を返す() throws Exception {
assertEquals("2", fizzbuzz.convert(2));
}
@Test
void _3を渡すと文字列Fizzを返す() throws Exception {
assertEquals("Fizz", fizzbuzz.convert(3));
}
@Test
void _5を渡すと文字列Buzzを返す() throws Exception {
assertEquals("Buzz", fizzbuzz.convert(5));
}
}
TODO
=====================
重要度: 高 テスト容易性: 高
- [x] 数を文字列に変換する
- [x] 1を渡すと文字列"1"を返す -> 仮実装
- [x] 2を渡すと文字列"2"を返す -> 三角測量
- [x] 3の倍数のときは数の代わりに「Fizz」に変換する
- [x] 3を渡すと文字列"Fizz"を返す -> 仮実装 -> 本実装
- [x] 5の倍数のときは数の代わりに「Buzz」に変換する
- [x] 5を渡すと文字列"Buzz"を返す -> 明白な実装
- [ ] 3と5両方の倍数のときは数の代わりに「FizzBuzz」に変換する
重要度: 中 テスト容易性: 中
- [ ] 1からnまでの数
重要度: 低 テスト容易性: 低
- [ ] 1から100までの数
- [ ] プリントする
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment