Skip to content

deepss1/tdd-assessment

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

String Calculator TDD Kata

Coverage Build Status Language

A production-grade implementation of the famous String Calculator Kata, demonstrating strict Test-Driven Development (TDD) practices and clean architecture principles.

🚀 Features

This calculator handles complex string parsing scenarios including:

  • Basic Arithmetic: Sums comma-separated integers.
  • Robust Parsing: Handles newlines (\n) and custom delimiters seamlessly.
  • Advanced Delimiters:
    • Custom single-character delimiters (e.g., //;\n1;2)
    • Multi-character delimiters (e.g., //[***]\n1***2***3)
    • Multiple different delimiters (e.g., //[*][%]\n1*2%3)
    • Multiple multi-character delimiters (e.g., //[foo][bar]\n1foo2bar3)
  • Safety:
    • Ignores empty entries caused by consecutive delimiters (1,,2 → 3).
    • Ignores numbers > 1000.
    • Throws informative exceptions for negative numbers, listing all offenders.
# Feature Example Output Status
1 Empty String add("") 0
2 Single Number add("1") 1
3 Multiple Numbers add("1,2,3") 6
4 Newline Delimiter add("1\n2,3") 6
5 Custom Delimiter add("//;\n1;2") 3
6 Negative Exception add("-1,2") Exception
7 All Negatives Listed add("-1,-2") Exception: -1, -2
8 Ignore > 1000 add("2,1001") 2
9 Multi-char Delimiter add("//[***]\n1***2") 3
10 Multiple Delimiters add("//[*][%]\n1*2%3") 6
11 Multi-char Multiple add("//[foo][bar]\n1foo2bar3") 6
12 Consecutive Delimiters add("1,,2") 3
13 Trailing Delimiters add("1,2,") 3
14 Boundary (1000) add("1000,1001,5") 1005
15 Invalid Input add("1,a,3") Exception

🛠️ Technical Approach

This project was built using the Red-Green-Refactor cycle.

Key Design Decisions

  1. Unified Regex Parsing: Instead of messy if-else chains for different delimiter types, the solution compiles all delimiters into a single RegExp. This makes the add method purely functional and declarative.
  2. Immutability: The code prioritizes final variables and pure functions to reduce side effects.
  3. Defensive Coding:
    • RegExp.escape() is used to prevent special characters (like * or +) from crashing the parser.
    • Boundary checks (e.g., inclusive vs exclusive limits) are explicitly tested.

🧪 Running Tests

This project uses the standard test package.

Run all tests:

dart test

Run with expanded output (to see individual test cases):

dart test -r expanded

✅ Test Results

Test Results

📂 Project Structure

string_calculator_kata/
├── lib/
│   ├── string_calculator_kata.dart  # Main logic (Parse -> Filter -> Reduce)
└── test/
│   └── string_calculator_kata_test.dart  # Comprehensive test suite
└── README.md

📝 Commit History

The git log of this repository serves as proof of the TDD process, featuring atomic commits tagged with test: [RED], feat: [GREEN], and refactor steps.

Implemented as part of the Incubyte TDD Assessment.

About

Production-grade String Calculator TDD Kata with 35 commits demonstrating perfect RED-GREEN-REFACTOR cycles. 100% test coverage, 15 features implemented.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages