This project is an assessment completed in Python. The assessment includes TDD principles(writing test cases prior to coding). Please contact me directly with any questions.
Before you begin, ensure you have met the following requirements:
Project is created with:
- Pre-commit Hooks (pre-commit)
- Testing (Pytest)
- Code formatting (Black and flake8)
- Type checking (mypy)
To use this project, follow these steps:
This project assumes you are using pipenv to manage virtual environments and project dependencies.
pipenv install --dev
pipenv run pre-commit install
pipenv install [package]
Use the --dev flag if needed.
make test
Tests will be automatically run prior to every commit.
π€ Aaron Beach
- Website: aaronbeach.dev
- Github: @aaron-beach
- LinkedIn: @arbeach/
This project uses the following license: MIT License
Copyright (c) 2020 Aaron Beach
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In addition to autoformatting the code via Black and flake8 for each commit, it does the following:
- Runs the unit tests
- Runs mypy for type checking
If you would like to disable this, edit this file.
- Try not to read ahead.
- Do one task at a time. The trick is to learn to work incrementally.
- Make sure you only test for correct inputs. there is no need to test for invalid inputs for this kata
- Test First!
-
In a test-first manner, create a simple class
class StringCalculator
with a methodpublic int Add(string numbers)
- The method can take 0, 1 or 2 numbers, and will return their sum
(for an empty string it will return 0)
for example
ββ == 0 , β1β == 1 , β1,2β == 3
- Start with the simplest test case of an empty string and move to one & two numbers
- Remember to solve things as simply as possible so that you force yourself to write tests you did not think about
- Remember to refactor after each passing test
- The method can take 0, 1 or 2 numbers, and will return their sum
(for an empty string it will return 0)
for example
-
Allow the Add method to handle an unknown amount of numbers
- accept unknown number of numbers
-
Allow the Add method to handle new lines between numbers (instead of commas).
-
the following input is ok:
β1\n2,3β == 6
-
the following is INVALID input so do not expect it :
β1,\nβ
(not need to write a test for it)
-
-
Support different delimiters: to change a delimiter, the beginning of the string will contain a separate line
- accept multiple delimiters
that looks like this:
β//[delimiter]\n[numbersβ¦]β
for example
β//;\n1;2β == 3
since the default delimiter is
β;β
.Note: All existing scenarios and tests should still be supported
- accept multiple delimiters
that looks like this:
-
Calling Add with a negative number will throw an exception βnegatives not allowedβ - and the negative that was passed.
- throw exception
- display msg and err
-
If there are multiple negatives, show all of them in the exception message
- throw exception
- display msg with multi negatives
-
Using TDD, Add a method to
StringCalculator
calledpublic int GetCalledCount()
that returns how many timesAdd()
was invoked. Remember - Start with a failing (or even non compiling) test.- add method
public int GetCalledCount()
- return count of
add()
- add method
-
(.NET Only) Using TDD, Add an event to the
StringCalculator
class namedpublic event Action<string, int> AddOccured
, that is triggered after everyAdd()
call.*Hint: Create the event declaration first: then write a failing test that listens to the event and proves it should have been triggered when calling Add(). *Hint 2: Example:
string giveninput = null;
sc.AddOccured += delegate(string input, int result)
{
giveninput = input;
};
-
Numbers bigger than 1000 should be ignored, for example:
2 + 1001 == 2
- ignore numbers above 1000
-
Delimiters can be of any length with the following format:
β//[delimiter]\nβ
- passed delimiter of any length
for example:
β//[***]\n1***2***3β == 6
- passed delimiter of any length
for example:
-
Allow multiple delimiters like this:
β//[delim1][delim2]\nβ
- passed multiple delimiters
for example
β//[*][%]\n1*2%3β == 6.
- passed multiple delimiters
for example
-
make sure you can also handle multiple delimiters with length longer than one char for example
β//[**][%%]\n1**2%%3β == 6.
- passed multiple delimiters of any length
For more info visit https://osherove.com or email roy@osherove.com