Last active
March 31, 2018 06:22
-
-
Save shuyuhey/3499a7ad00d3af52c46cfce80db4e3dd to your computer and use it in GitHub Desktop.
tddyyχ_2018-03-31
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FizzBuzz { | |
public static String answer(int number) { | |
if (number % 15 == 0 ) { | |
return "FizzBuzz"; | |
} else if (number % 3 == 0 ) { | |
return "Fizz"; | |
} else if (number % 5 == 0) { | |
return "Buzz"; | |
} | |
return String.valueOf(number); | |
} | |
public static void print(String text) { | |
System.out.println(text); | |
} | |
public static void spike(int number) { | |
for (int i = 1; i <= number; i++) { | |
print(answer(i)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.junit.Assert.*; | |
import java.io.*; | |
public class FizzBuzzTest { | |
private ByteArrayOutputStream _baos; | |
private PrintStream _out; | |
@Before public void setUp() { | |
_baos = new ByteArrayOutputStream(); | |
_out = System.out; | |
System.setOut( | |
new PrintStream( | |
new BufferedOutputStream(_baos) | |
) | |
); | |
} | |
@After public void tearDown() { | |
System.setOut(_out); | |
} | |
@Test | |
public void _3の時にFizzが返る() { | |
String expected = "Fizz"; | |
String actual = FizzBuzz.answer(3); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _6の時にFizzが返る() { | |
String expected = "Fizz"; | |
String actual = FizzBuzz.answer(6); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _5の時にBuzzが返る() { | |
String expected = "Buzz"; | |
String actual = FizzBuzz.answer(5); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _10の時にBuzzが返る() { | |
String expected = "Buzz"; | |
String actual = FizzBuzz.answer(10); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _1のとき文字列の1が返る() { | |
String expected = "1"; | |
String actual = FizzBuzz.answer(1); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _2のとき文字列の2が返る() { | |
String expected = "2"; | |
String actual = FizzBuzz.answer(2); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _15の時にFizzBuzzが返る() { | |
String expected = "FizzBuzz"; | |
String actual = FizzBuzz.answer(15); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _与えられた文字列を出力する() { | |
FizzBuzz.print("Buzz"); | |
System.out.flush(); | |
String expected = "Buzz\n"; | |
String actual = _baos.toString(); | |
assertEquals(expected, actual); | |
} | |
@Test | |
public void _1から渡された数までFizzBuzzにわたすとよきように文字列を出力する() { | |
FizzBuzz.spike(4); | |
System.out.flush(); | |
String expected = "1\n2\nFizz\n4\n"; | |
String actual = _baos.toString(); | |
assertEquals(expected, actual); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment