Moving from the test, the implementation and the re-factoring we get the following results
package ch.ge.vaadin
import org.junit.Test
import org.junit.Before
import org.junit.After
import com.vaadin.ui.Button
import static org.mockito.Mockito.*
class VaadinCalculatorTest {
private VaadinCalculator calculator
@Before void before() {
calculator = new VaadinCalculator()
calculator.init()
}
@After void after() {
calculator.close()
}
@Test void shouldDisplay2IfIPressButton2() {
def event = mock(Button.ClickEvent)
def button = mock(Button)
when(button.getCaption()).thenReturn("2")
when(event.button).thenReturn(button)
calculator.buttonClick(event)
assert calculator.stored == 0.0
assert calculator.current == 2.0
}
@Test void shouldDisplay2IfIPressButton2AndPlus() {
def event = mock(Button.ClickEvent)
def button = mock(Button)
when(button.getCaption()).thenReturn("2")
when(event.button).thenReturn(button)
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("+")
calculator.buttonClick(event)
assert calculator.current == 0.0
assert calculator.stored == 2.0
}
@Test void shouldDisplay4IfIPressButton2AndPlusAnd4() {
def event = mock(Button.ClickEvent)
def button = mock(Button)
when(button.getCaption()).thenReturn("2")
when(event.button).thenReturn(button)
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("+")
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("4")
calculator.buttonClick(event)
assert calculator.current == 4.0
assert calculator.stored == 2.0
}
@Test void shouldDisplay6IfIPressButton2AndPlusAnd4AndEqual() {
def event = mock(Button.ClickEvent)
def button = mock(Button)
when(button.getCaption()).thenReturn("2")
when(event.button).thenReturn(button)
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("+")
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("4")
calculator.buttonClick(event)
when(button.getCaption()).thenReturn("=")
calculator.buttonClick(event)
assert calculator.current == 0.0
assert calculator.stored == 6.0
}
}
And the implementation
package ch.ge.vaadin
import com.vaadin.Application
import com.vaadin.ui.Button
import org.apache.commons.lang.CharUtils
class VaadinCalculator extends Application implements Button.ClickListener{
static char TWO = 2
static char FOUR = 4
static char OPERATOR_PLUS = '+'
static char OPERATOR_EQUAL = '='
double current = 0.0
double stored = 0.0
char lastOperationRequested
@Override
def void init() {
}
def void buttonClick(Button.ClickEvent clickEvent) {
def button = clickEvent.button
def requestedOperation = button.caption.charAt(0)
calculate(requestedOperation)
}
def calculate(char requestedOperation){
if(CharUtils.isAsciiNumeric(requestedOperation)){
current *= 10
current += CharUtils.toIntValue(requestedOperation)
return current;
}
if(lastOperationRequested == '+'){
stored += current
}else {
stored = current
}
current = 0.0;
lastOperationRequested = requestedOperation;
if (requestedOperation == 'C') {
stored = 0.0;
}
return stored;
}
}
If we launch the CalculatorSpech ATDD class, it should be green now
No comments:
Post a Comment