The following is a TDD Kata- an exercise in coding, refactoring and test-first
You can look here at osherove or see the steps described here.
This code is written step by step. You can check code at each commit. Only main points are covered here.
- Create a simple String calculator with a method signature:
int Add(string numbers)
- The method can take up to two numbers, separated by commas, and will return their sum.
(for an empty string it will return 0)
- For example
“”
or“1”
or“1,2”
as inputs.
- For example
- Add method can also handle an unknown amount of numbers
- Add method handles new lines between numbers.
- the following input is ok:
“1\n2,3”
(will equal 6) - the following input is NOT ok:
“1,\n”
(not need to prove it - just clarifying)
- the following input is ok:
- Support for different delimiters
- to change a delimiter, the beginning of the string will contain a separate line that looks like this:
“//[delimiter]\n[numbers…]”
for example“//;\n1;2”
should return three where the default delimiter is‘;’
- the first line is optional. all existing scenarios should still be supported
- to change a delimiter, the beginning of the string will contain a separate line that looks like this:
- Calling Add with a negative number will throw an exception "negatives not allowed" - and the negative that was passed.
- if there are multiple negatives, show all of them in the exception message.
- Numbers bigger than 1000 should be ignored, so adding
2 + 1001
= 2 - Delimiters can be of any length with the following format:
“//[delimiter]\n”
- for example:
“//[***]\n1***2***3”
should return 6
- for example:
- Allow multiple delimiters like this:
“//[delim1][delim2]\n”
for example“//[*][%]\n1*2%3”
should return 6. - Make sure to handle multiple delimiters with length longer than one char
-
Clone the repository
-
Run the test file which is in following directory:
tdd-string-calculator/src/com/ajinz/test/
You should get following output:
Java Version: java 16.0.2
Testing Library: TestNG