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
This commit is contained in:
parent
3e737dfef3
commit
279c381872
3 changed files with 66 additions and 3 deletions
|
|
@ -1,5 +1,3 @@
|
||||||
import 'package:tdd_katas/roman_numerals.dart' as roman_numerals;
|
|
||||||
|
|
||||||
void main(List<String> arguments) {
|
void main(List<String> arguments) {
|
||||||
print('Hello world: ${roman_numerals.integerToRoman(1)}');
|
print('TDD Katas exercises, please read the README.md file.');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
51
lib/string_calculator.dart
Normal file
51
lib/string_calculator.dart
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
test/string_calculator_test.dart
Normal file
14
test/string_calculator_test.dart
Normal file
|
|
@ -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
|
||||||
|
});
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue