This repository contains an implementation of the String Calculator TDD Kata, written in Dart following Test-Driven Development (TDD) principles.
The objective of this exercise is to demonstrate software craftsmanship by focusing on:
- Clean and readable code
- Small, well-defined functions
- Comprehensive unit tests
- Incremental development driven by tests
dart flutter tdd test-driven-development kata string-calculator unit-testing clean-code incubyte
Create a calculator with a method:
int add(String numbers)The method takes a string containing numbers separated by delimiters and returns their sum, while following a specific set of rules described below. The implementation follows the Red-Green-Refactor TDD cycle, where each feature is developed incrementally:
| Phase | Description |
|---|---|
| RED | Write a failing test first |
| GREEN | Write minimal code to make the test pass |
| REFACTOR | Clean up the code while keeping tests green |
- Confidence: Every line of code is covered by tests
- Design: Tests drive better API design
- Documentation: Tests serve as living documentation
- Maintainability: Refactoring is safe with comprehensive test coverage
| # | Feature | Description | Example |
|---|---|---|---|
| 1 | Empty String | Returns 0 for empty input |
add("") → 0 |
| 2 | Single Number | Returns the number itself | add("1") → 1 |
| 3 | Two Numbers | Sum of comma-separated numbers | add("1,2") → 3 |
| 4 | Multiple Numbers | Handles any amount of numbers | add("1,2,3,4,5") → 15 |
| 5 | Newline Delimiter | Supports \n as delimiter |
add("1\n2,3") → 6 |
| 6 | Custom Delimiter | Supports //[delimiter]\n syntax |
add("//;\n1;2") → 3 |
| 7 | Multi-char Delimiter | Supports //[***]\n syntax |
add("//[***]\n1***2") → 3 |
| 8 | Multiple Delimiters | Supports //[*][&]\n syntax |
add("//[*][%]\n1*2&3") → 6 |
| 9 | Input Validation | Handles empty parts gracefully | add("1,,3") → 4 |
| 10 | Negative Numbers | Throws exception listing all negatives | add("-1,2,-3") → Exception |
import 'package:string_calculator/string_calculator.dart';
void main(List<String> arguments) {
final calculator = StringCalculator();
// Basic usage
print(calculator.add('')); // 0
print(calculator.add('1')); // 1
print(calculator.add('1,2')); // 3
print(calculator.add('1,2,3,4,5')); // 15
// Newline delimiter
print(calculator.add('1\n2,3')); // 6
// Custom delimiter
print(calculator.add('//;\n1;2')); // 3
// Multi-character delimiter
print(calculator.add('//[***]\n1***2***3')); // 6
// Multiple delimiters
print(calculator.add('//[*][&]\n1*2&3')); // 6
// Input validation - empty parts ignored
print(calculator.add('1,,3')); // 4
// Negative numbers throw exception
try {
calculator.add('-1,2,-3');
} catch (e) {
print(e); // Exception: negative numbers not allowed -1,-3
}
}# Run all tests
dart test
# Run tests with verbose output
dart test --reporter expanded
# Run with coverage
dart test --coverage00:00 +0: loading /Users/avishal/Desktop/personal/tdd_string_calculator/test/tdd_string_calculator_test.dart
00:00 +0: returns 0 for empty string
00:00 +1: returns number itself for single number
00:00 +2: returns sum of two comma separated numbers
00:00 +3: handles any amount of numbers
00:00 +4: handles new lines between numbers
00:00 +5: supports custom delimiter
00:00 +6: supports multi-character delimiter
00:00 +7: supports multiple delimiters
00:00 +8: supports multiple multi-character delimiters
00:00 +9: throws exception for single negative number
00:00 +10: throws exception listing all negative numbers
00:00 +11: throws exception for invalid input
00:00 +12: ignores empty parts between delimiters
00:00 +13: All tests passed!
- Dart SDK: ^3.9.0
# Clone the repository
git clone https://github.com/Aniket76/string_calculator.git
# Navigate to project
cd string_calculator
# Get dependencies
dart pub get
# Run tests
dart test --reporter expandedEach feature follows the RED-GREEN-REFACTOR pattern. View the complete commit history:
git log --onelinef85b6b4 RED: test two comma-separated numbers sum
b38344c GREEN: split by comma and sum numbers
714a6e2 REFACTOR: created functions for better handling
3cf7841 RED: handles any amount of numbers
1a44564 GREEN: already works with existing implementation
...
Total: 31 commits demonstrating incremental TDD development.
Aniket Vishal
This project is open source and available under the MIT License.
