Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Created October 7, 2010 16:31
Show Gist options
  • Save MoriTanosuke/615405 to your computer and use it in GitHub Desktop.
Save MoriTanosuke/615405 to your computer and use it in GitHub Desktop.
package de.kopis.katas;
public class FizzBuzz {
public String generate(int number) {
String result = null;
if(number == 0) {
result = "0";
} else if(number % 3 == 0 && number % 5 == 0) {
result = "FizzBuzz";
} else if(number % 3 == 0) {
result = "Fizz";
} else if(number % 5 == 0) {
result = "Buzz";
} else {
result = String.valueOf(number);
}
if(result.contains("3")) {
result = "Fizz";
} else if(result.contains("5")) {
result = "Buzz";
}
return result;
}
}
package de.kopis.katas;
import static org.junit.Assert.*;
import mockit.integration.junit4.JMockit;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(JMockit.class)
public class FizzBuzzTest {
@Test
public void testValids() {
int[] numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 99, 180};
String[] expected = new String[] {"0", "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "Fizz", "14", "FizzBuzz", "Fizz", "FizzBuzz"};
FizzBuzz fb = new FizzBuzz();
int index = 0;
for (int number : numbers) {
assertEquals(expected[index++], fb.generate(number));
}
}
@Test
public void testNewRequirements() {
int[] numbers = {13, 43, 99, 25, 55, 185, 90};
String[] expected = new String[] {"Fizz", "Fizz", "Fizz", "Buzz", "Buzz", "Buzz", "FizzBuzz"};
FizzBuzz fb = new FizzBuzz();
int index = 0;
for (int number : numbers) {
assertEquals(expected[index++], fb.generate(number));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment