Skip to content

Instantly share code, notes, and snippets.

@danilat
Created December 14, 2010 22:44
Show Gist options
  • Save danilat/741261 to your computer and use it in GitHub Desktop.
Save danilat/741261 to your computer and use it in GitHub Desktop.
import groovy.util.GroovyTestCase
//múltiplos de 3 > "Fizz", múltiplos de 5 > "Buzz", múltiplos de 3 y 5 > "FizzBuzz"
class FizzBuzz extends GroovyTestCase{
void testSiNoCoincideDevuelveElNumero(){
assertEquals("1", fizzBuzz(1))
}
void testSiEsMultiploDe3DevuelveFizz(){
assertEquals("Fizz", fizzBuzz(9))
}
void testSiEsMultiploDe5DevuelveBuzz(){
assertEquals("Buzz", fizzBuzz(10))
}
void testSiEsMultiplode3Y5DevuelveFizzBuzz(){
assertEquals("FizzBuzz", fizzBuzz(45))
}
void testSiEsCeroDevuelveCero(){
assertEquals("0", fizzBuzz(0))
}
static final int FIZZ_NUMBER = 3
static final int BUZZ_NUMBER = 5
static final int FIZZBUZZ_NUMBER = FIZZ_NUMBER * BUZZ_NUMBER
String fizzBuzz(int value){
if(valueIsNotZeroAndDivisibleBy(value, FIZZBUZZ_NUMBER))
return "FizzBuzz"
if(valueIsNotZeroAndDivisibleBy(value, FIZZ_NUMBER))
return "Fizz"
if(valueIsNotZeroAndDivisibleBy(value, BUZZ_NUMBER))
return "Buzz"
return value.toString()
}
Boolean valueIsNotZeroAndDivisibleBy(int value, int divisor){
return (value!=0 && valueIsDivisibleBy(value, divisor))
}
Boolean valueIsDivisibleBy(int value, int divisor){
return (value % divisor == 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment