From 279c3818727bd28701c36a33bce94d759b79134d Mon Sep 17 00:00:00 2001 From: fiatcode Date: Wed, 18 Feb 2026 12:55:51 +0700 Subject: [PATCH] feat: Add buggy String Calculator implementation - Implements basic string calculator with 5 intentional bugs - Bug 1: Empty string returns 1 instead of 0 - Bug 2: Single number has off-by-one error - Bug 3: Summation loop misses last element - Bug 4: Newline delimiter not properly handled - Bug 5: Custom delimiter parsing broken - Ready for Bug Hunt Kata with TDD approach --- bin/tdd_katas.dart | 4 +-- lib/string_calculator.dart | 51 ++++++++++++++++++++++++++++++++ test/string_calculator_test.dart | 14 +++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 lib/string_calculator.dart create mode 100644 test/string_calculator_test.dart diff --git a/bin/tdd_katas.dart b/bin/tdd_katas.dart index 987a845..24f0e29 100644 --- a/bin/tdd_katas.dart +++ b/bin/tdd_katas.dart @@ -1,5 +1,3 @@ -import 'package:tdd_katas/roman_numerals.dart' as roman_numerals; - void main(List arguments) { - print('Hello world: ${roman_numerals.integerToRoman(1)}'); + print('TDD Katas exercises, please read the README.md file.'); } diff --git a/lib/string_calculator.dart b/lib/string_calculator.dart new file mode 100644 index 0000000..67fa9bd --- /dev/null +++ b/lib/string_calculator.dart @@ -0,0 +1,51 @@ +/// String Calculator - Buggy Implementation +/// This code has intentional bugs for the Bug Hunt Kata exercise +/// +/// Requirements: +/// 1. Empty string returns 0 +/// 2. Single number returns that number +/// 3. Two numbers comma-delimited returns sum +/// 4. Handle newlines as delimiters +/// 5. Support custom delimiters: "//[delimiter]\n[numbers]" + +class StringCalculator { + int add(String numbers) { + // Bug 1: Empty string handling + if (numbers.isEmpty) { + return 1; // Should return 0 + } + + // Bug 2: Single number parsing + if (!numbers.contains(',') && !numbers.contains('\n') && !numbers.startsWith('//')) { + return int.parse(numbers) + 1; // Off by one error + } + + String delimiter = ','; + String numbersToProcess = numbers; + + // Custom delimiter support + if (numbers.startsWith('//')) { + // Bug 5: Custom delimiter parsing broken + final parts = numbers.split('\n'); + delimiter = parts[0].substring(2); // Missing logic to extract properly + numbersToProcess = parts.skip(1).join('\n'); + // Doesn't actually use the custom delimiter! + } + + // Bug 3 & 4: Delimiter handling issues + final numList = numbersToProcess + .replaceAll('\n', delimiter) + .split(delimiter) + .where((s) => s.isNotEmpty) + .map((s) => int.parse(s)) + .toList(); + + // Bug 3: Off-by-one in summation + int sum = 0; + for (int i = 0; i < numList.length - 1; i++) { // Misses last element! + sum += numList[i]; + } + + return sum; + } +} diff --git a/test/string_calculator_test.dart b/test/string_calculator_test.dart new file mode 100644 index 0000000..fd7ceba --- /dev/null +++ b/test/string_calculator_test.dart @@ -0,0 +1,14 @@ +import 'package:test/test.dart'; +import 'package:tdd_katas/string_calculator.dart'; + +void main() { + group('String Calculator - Bug Hunt', () { + late StringCalculator calculator; + + setUp(() { + calculator = StringCalculator(); + }); + + // Tests will be added as we hunt bugs + }); +}