A simple calculator implementation in Go developed using Test-Driven Development (TDD) methodology. This project demonstrates the complete TDD cycle with comprehensive test coverage and CI/CD pipeline setup.
- Addition: Add two integers
- Subtraction: Subtract two integers
- Multiplication: Multiply two integers
- Division: Divide two integers with proper error handling for division by zero
calc/
├── internal/
│ └── calc/
│ └── calc.go # Calculator implementation
├── test/
│ └── calc_test.go # Test suite
├── .github/
│ └── workflows/
│ └── ci.yml # CI/CD pipeline
├── Makefile # Build automation
├── go.mod # Go module definition
├── NARRATIVE.md # Detailed TDD process documentation
└── README.md # This file
- Go 1.21 or later
- Git
- Make (optional, for using Makefile targets)
import "github.com/dytsou/calc/internal/calc"
// Addition
result := calc.Add(2, 3) // Returns 5
// Subtraction
result := calc.Subtract(5, 3) // Returns 2
// Multiplication
result := calc.Multiply(2, 3) // Returns 6
// Division (returns float64 and error)
result, err := calc.Divide(6, 3) // Returns 2.0, nil
result, err := calc.Divide(5, 0) // Returns 0.0, error# Run the interactive calculator demo
make run
# Or run directly
go run main.goThe calculator will prompt you to enter an expression in the format: a op b where:
aandbare integersopis one of:+,-,*,/
Example:
Calculator
Enter a op(+,-,*,/) b
10 + 5
Result: 15
# Run all tests
make test
# Run tests with verbose output
go test -v ./test/...
# Run tests with coverage
make coverage
# Run all checks (test + lint)
make allmake run- Run the interactive calculator demomake test- Run the test suitemake coverage- Generate HTML coverage reportmake lint- Run golangci-lint static analysismake all- Run all checks (test + lint)
This project was developed using Test-Driven Development (TDD) with the following approach:
- Write Failing Test - Create test cases for new functionality
- Implement Function - Write minimal code to make tests pass
- Refactor - Improve code quality while keeping tests green
- Commit - Each TDD cycle committed separately
- Cycle 1: Add function with comprehensive test cases
- Cycle 2: Subtract function with edge case testing
- Cycle 3: Multiply function with large number testing
- Cycle 4: Divide function with error handling for division by zero
See NARRATIVE.md for detailed documentation of each TDD cycle.
This project includes a GitHub Actions CI/CD pipeline that:
- Tests: Runs test suite on multiple Go versions (1.21, 1.22)
- Coverage: Generates and displays test coverage reports
- Linting: Performs static analysis using golangci-lint
- Automated testing on push and pull requests
- Multi-version Go testing (1.21, 1.22)
- Code coverage reporting
- Static analysis with golangci-lint
- Build status badges
The test suite includes:
- Normal Cases: Standard arithmetic operations with positive and negative numbers
- Edge Cases: Operations with zero values, large numbers
- Error Cases: Division by zero error handling
- Table-Driven Tests: Comprehensive test coverage using Go's idiomatic testing pattern
The Divide function implements proper error handling:
result, err := calc.Divide(a, b)
if err != nil {
// Handle division by zero error
log.Printf("Error: %v", err)
return
}
// Use result safelyThe project maintains a clean git history showing the TDD progression:
- Each TDD cycle is committed separately
- Clear commit messages describing the TDD step
- Easy to follow the red-green-refactor cycle
- Fork the repository
- Create a feature branch
- Follow TDD methodology for new features
- Ensure all tests pass
- Submit a pull request
This project is part of a software testing course assignment and is for educational purposes.