From 681f4fa20337f2f773c89e1de8af596d76c8d052 Mon Sep 17 00:00:00 2001 From: Jacob McKenzie Date: Tue, 17 Sep 2024 10:30:06 -0600 Subject: [PATCH] Add 3 new modes of calculation --- calc/calc.go | 6 ++++++ calc/calc_test.go | 28 +++++++++++++++++++++++++++- main_test.go | 4 ++-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/calc/calc.go b/calc/calc.go index ae70b73..b92b212 100644 --- a/calc/calc.go +++ b/calc/calc.go @@ -5,7 +5,13 @@ type Calculator interface { } type Addition struct{} +type Subtraction struct{} +type Multiplication struct{} +type Division struct{} func (Addition) Calculate(a, b int) int { return a + b } +func (Subtraction) Calculate(a, b int) int { return a - b } +func (Multiplication) Calculate(a, b int) int { return a * b } +func (Division) Calculate(a, b int) int { return a / b } diff --git a/calc/calc_test.go b/calc/calc_test.go index cdb627c..48656e3 100644 --- a/calc/calc_test.go +++ b/calc/calc_test.go @@ -1,6 +1,8 @@ package calc -import "testing" +import ( + "testing" +) func TestAddition_Calculate(t *testing.T) { type args struct { @@ -36,6 +38,30 @@ func TestAddition(t *testing.T) { } +func TestSubtraction(t *testing.T) { + var calculator Subtraction + assertEqual(t, 0, calculator.Calculate(0, 0)) + assertEqual(t, 1, calculator.Calculate(0, -1)) + assertEqual(t, 2, calculator.Calculate(4, 2)) + assertEqual(t, -4, calculator.Calculate(0, 4)) +} + +func TestMultiplication(t *testing.T) { + var calculator Multiplication + assertEqual(t, 0, calculator.Calculate(0, 0)) + assertEqual(t, 0, calculator.Calculate(0, -1)) + assertEqual(t, 8, calculator.Calculate(4, 2)) + assertEqual(t, 0, calculator.Calculate(0, 4)) +} + +func TestDivision(t *testing.T) { + var calculator Division + assertEqual(t, 1, calculator.Calculate(1, 1)) + assertEqual(t, 0, calculator.Calculate(0, -1)) + assertEqual(t, 2, calculator.Calculate(4, 2)) + assertEqual(t, -4, calculator.Calculate(16, -4)) +} + func assertEqual(t *testing.T, expected, actual any) { if actual != expected { t.Helper() //so that the stack trace shows the actual test that failed diff --git a/main_test.go b/main_test.go index 334d339..95c03a0 100644 --- a/main_test.go +++ b/main_test.go @@ -6,8 +6,8 @@ import ( "reflect" "testing" - "calc-lib/calc" - "calc-lib/handler" + calc "calc-lib/calc" + handler "calc-lib/handler" ) func assertEqual(t *testing.T, expected, actual any) {