TDD Kata - The String Calculator

preview_player
Показать описание
1-An empty string returns zero
2-A single number returns the value
3-Two numbers, comma delimited, returns the sum
4-Two numbers, newline delimited, returns the sum
5-Three numbers, delimited either way, returns the sum
6-Negative numbers throw an exception
7-Numbers greater than 1000 are ignored
Рекомендации по теме
Комментарии
Автор

Good intro to TDD katas! Some things I would like to point out:

1) Per my understanding, the point of "minimum code to make the test pass" means even writing code that seems stupid. For example,

assertEquals(calculator.calculate("1, 2, 3"), 6);

To make this test pass, your calculate method should simply *hard-code* the return value to 6. This seems stupid at first, but it shows you that you don't have enough tests. You then add another test

public void
    assertEquals(calculator.calculate("1, 2, 4"), 7);
}

This will fail since you are hard-coding 6 as the return value. You can now write the real production code to make this pass.

Following this would have helped you catch other errors. For example, for the second requirement in the kata, where a single input should return itself; the following test will fail:

assertEquals(calculator.calculate("15"), 15);

This is because you are checking the length instead of checking that there are no new lines or commas.

2) I also think the third step of the red-green-refactor cycle is optional. If your code already looks good there is no need to refactor. This will prevent you from creating simple wrapper methods if they are not necessary.

3) What about negative tests. For example, what if the input string is an invalid number?

kiranrao
Автор

Zvučiš kao Španac dok pričaš engleski :)
U svakom slučaju, drago mi je da se neko sa naših područja bavi TDD-om i TDD katama ;)

kobac
Автор

is there reason for wrapping call of single method into another method? example: input.isEmpty() into private boolean isEmpty(String input) 

TheOstah
Автор

In your assertEquals() from the test method emptyStringReturnsZero(), should the expected value be the first parameter, rather than the second?

ilariacorda
Автор

The part 'if input.length()==1' is wrong, should be 'if numbers.length()==1', it won't work with numbers gretar than 10. Also this leads to posterior changes, or the full suppression of this part of the if, due to posterior implmentatios because of next requirements.

dalonsogz
Автор

Off course it's a good attempt to showcase how to do a proper TDD, yet I beleive the last step of TDD (refactoring) can be improved significantally in this example.

kambizarei
Автор

can u send me the code or link for the code??

kushagrachavan