Skip to content

Instantly share code, notes, and snippets.

@sbastn
Created September 2, 2012 15:11
Show Gist options
  • Save sbastn/3600293 to your computer and use it in GitHub Desktop.
Save sbastn/3600293 to your computer and use it in GitHub Desktop.
holatdd: numeros romanos. Video: http://holatdd.com/videos/numeros-romanos
package com.learning;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class RomanNumeralsTest {
interface RomanNumber {
int toNumeral();
int value();
}
class NextEmptyNumber implements RomanNumber {
@Override
public int toNumeral() {
return 0;
}
@Override
public int value() {
return 0;
}
}
class V implements RomanNumber {
private RomanNumber nextNumber;
public V(RomanNumber nextNumber) {
this.nextNumber = nextNumber;
}
public V() {
this(new NextEmptyNumber());
}
@Override
public int toNumeral() {
if (nextNumber.value() == 5) {
throw new IllegalArgumentException("VV is not valid");
}
return nextNumber.value() + value();
}
@Override
public int value() {
return 5;
}
}
class I implements RomanNumber {
private RomanNumber nextNumber;
public I(RomanNumber nextNumber) {
this.nextNumber = nextNumber;
}
public I() {
this(new NextEmptyNumber());
}
public int toNumeral() {
if (nextNumber.value() == 5) {
return nextNumber.toNumeral() - value();
}
return nextNumber.toNumeral() + value();
}
public int value() {
return 1;
}
}
@Test
public void I_is_1() throws Exception {
assertEquals(1, new I().toNumeral());
}
@Test
public void II_is_2() throws Exception {
assertEquals(2, new I(new I()).toNumeral());
}
@Test
public void III_is_3() throws Exception {
assertEquals(3, new I(new I(new I())).toNumeral());
}
@Test
public void IV_is_4() throws Exception {
assertEquals(4, new I(new V()).toNumeral());
}
@Test
public void VI_is_6() throws Exception {
assertEquals(6, new V(new I()).toNumeral());
}
@Test(expected = IllegalArgumentException.class)
public void VV_is_invalid() throws Exception {
new V(new V()).toNumeral();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment