From 5f5ebbad909bccac385f7c454c187cc66f0b10cb Mon Sep 17 00:00:00 2001 From: Abhjit L Date: Wed, 23 Sep 2020 23:04:53 +0530 Subject: [PATCH 1/5] Started going through the book --- calculator_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/calculator_test.go b/calculator_test.go index e5b716a..1bd6acd 100644 --- a/calculator_test.go +++ b/calculator_test.go @@ -14,11 +14,11 @@ func TestAdd(t *testing.T) { } } -// func TestSubtract(t *testing.T) { -// t.Parallel() -// var want float64 = 2 -// got := calculator.Subtract(4, 2) -// if want != got { -// t.Errorf("want %f, got %f", want, got) -// } -// } +func TestSubtract(t *testing.T) { + t.Parallel() + var want float64 = 2 + got := calculator.Subtract(2, 4) + if want != got { + t.Errorf("want %f, got %f", want, got) + } +} From 3aec813bff44f880f03b45d54240525ccf627fbb Mon Sep 17 00:00:00 2001 From: Abhjit L Date: Thu, 24 Sep 2020 23:40:35 +0530 Subject: [PATCH 2/5] Continuing the journey of TDD --- calculator.go | 19 ++++++++++++++++++- calculator_test.go | 44 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/calculator.go b/calculator.go index d88fbc3..afb63b3 100644 --- a/calculator.go +++ b/calculator.go @@ -1,9 +1,11 @@ // Package calculator provides a library for simple calculations in Go. package calculator +import "errors" + // Add takes two numbers and returns the result of adding them together. func Add(a, b float64) float64 { -return a + b + return a + b } // Subtract takes two numbers and returns the result of subtracting the second @@ -11,3 +13,18 @@ return a + b func Subtract(a, b float64) float64 { return b - a } + +// Multiply takes two numbers and returns the result of multplying the second +// by the first. +func Multiply(a, b float64) float64 { + return b * a +} + +// Divide takes two numbers and returns the result of dividing the second +// by the first. +func Divide(a, b float64) (float64, error) { + if a != 0 { + return b / a, nil + } + return 0, errors.New("math: Divide by Zero") +} diff --git a/calculator_test.go b/calculator_test.go index 1bd6acd..5dce259 100644 --- a/calculator_test.go +++ b/calculator_test.go @@ -5,13 +5,26 @@ import ( "testing" ) +type testCase struct { + a float64 + b float64 + want float64 + tcName string +} + func TestAdd(t *testing.T) { t.Parallel() - var want float64 = 4 - got := calculator.Add(2, 2) - if want != got { - t.Errorf("want %f, got %f", want, got) + tCases := []testCase{ + {2, 2.030000, 4.030000, "some fractions1"}, + {6, 3.99, 9.99, "some fractions2"}, + {2, 2.03675, 4.03675, "some fractions3"}, + } + for _, tc := range tCases { + if got := calculator.Add(tc.a, tc.b); tc.want != got { + t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + } } + } func TestSubtract(t *testing.T) { @@ -22,3 +35,26 @@ func TestSubtract(t *testing.T) { t.Errorf("want %f, got %f", want, got) } } + +func TestMultiply(t *testing.T) { + t.Parallel() + var want float64 = 8 + got := calculator.Multiply(2, 4) + if want != got { + t.Errorf("want %f, got %f", want, got) + } +} + +func TestDivide(t *testing.T) { + t.Parallel() + tCases := []testCase{ + {2.030000, 20, 9.852216, "some fractions1"}, + {0, 3.99, 0, "some fractions2"}, + {2, 0, 0, "some fractions3"}, + } + for _, tc := range tCases { + if got := calculator.Add(tc.a, tc.b); tc.want != got { + t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + } + } +} From 5969134f7a8400fb740dd7ef91a776051ff24fb2 Mon Sep 17 00:00:00 2001 From: Abhjit L Date: Sat, 26 Sep 2020 08:13:59 +0530 Subject: [PATCH 3/5] Enhance calculator tests: Summary, expected failure, etc --- calculator_test.go | 71 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/calculator_test.go b/calculator_test.go index 5dce259..3ea5678 100644 --- a/calculator_test.go +++ b/calculator_test.go @@ -2,26 +2,37 @@ package calculator_test import ( "calculator" + "fmt" + "math/big" "testing" ) +const ( + SHLD_SUCCEED = 1 + SHLD_FAIL = 0 +) + type testCase struct { - a float64 - b float64 - want float64 - tcName string + a float64 + b float64 + want float64 + tcName string + tcExpStatus int } func TestAdd(t *testing.T) { t.Parallel() tCases := []testCase{ - {2, 2.030000, 4.030000, "some fractions1"}, - {6, 3.99, 9.99, "some fractions2"}, - {2, 2.03675, 4.03675, "some fractions3"}, + {2, 2.030000, 4.030000, "some fractions1", SHLD_SUCCEED}, + {6, 3.99, 9.99, "some fractions2", SHLD_SUCCEED}, + {2, 2.03675, 4.03675, "some fractions3", SHLD_SUCCEED}, } for _, tc := range tCases { - if got := calculator.Add(tc.a, tc.b); tc.want != got { - t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + got := calculator.Add(tc.a, tc.b) + var bgot = big.NewFloat(got) + var bwant = big.NewFloat(tc.want) + if result := bgot.Cmp(bwant); result != 0 { + t.Errorf("Add: Test: %s : want %f, got %f and Cmp gave %d ", tc.tcName, tc.want, got, result) } } @@ -48,13 +59,45 @@ func TestMultiply(t *testing.T) { func TestDivide(t *testing.T) { t.Parallel() tCases := []testCase{ - {2.030000, 20, 9.852216, "some fractions1"}, - {0, 3.99, 0, "some fractions2"}, - {2, 0, 0, "some fractions3"}, + {2.030000, 20, 9.852216, "Divide some fractions1", SHLD_SUCCEED}, + {0, 3.99, 0, "Divide some fractions2", SHLD_FAIL}, + {2, 0, 0, "Divide some fractions3", SHLD_SUCCEED}, } + cntTestExec, cntfuncErr, cntexpMismatch := 0, 0, 0 + cntNoOfTests := len(tCases) + defer printDivTestSummary(cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) + for _, tc := range tCases { - if got := calculator.Add(tc.a, tc.b); tc.want != got { - t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + funcErr := false + expMismatch := false + cntTestExec++ + + got, err := calculator.Divide(tc.a, tc.b) + if err != nil { + funcErr = true + cntfuncErr++ + t.Fatalf("Test: %s :: (%f / %f) : Divide by Zero", tc.tcName, tc.b, tc.a) + } else { + if tc.want != got { + expMismatch = true + cntexpMismatch++ + } + } + switch tc.tcExpStatus { + case SHLD_FAIL: // We have defined this test case to FAIL. Its err it it doesn't + if !(funcErr || expMismatch) { + t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + } + case SHLD_SUCCEED: // We have defined this test case to SUCCEED. Its err it it doesn't + if funcErr || expMismatch { + t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + } } } + fmt.Printf("SummaryDiv: TotalTests: %d: Executed: %d: Failed: %d: Succeeded: %d\n", cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) + +} + +func printDivTestSummary(cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) { + fmt.Printf("SummaryDiv: TotalTests: %d: Executed: %d: Failed: %d: Succeeded: %d\n", cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) } From 96bc1dd033c9747800972c793028fd38c914a5aa Mon Sep 17 00:00:00 2001 From: Abhjit L Date: Sat, 26 Sep 2020 19:20:14 +0530 Subject: [PATCH 4/5] Just in case @bitfield wants to take a look, on seeing my tweet --- calculator_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/calculator_test.go b/calculator_test.go index 3ea5678..4e0e171 100644 --- a/calculator_test.go +++ b/calculator_test.go @@ -23,9 +23,9 @@ type testCase struct { func TestAdd(t *testing.T) { t.Parallel() tCases := []testCase{ - {2, 2.030000, 4.030000, "some fractions1", SHLD_SUCCEED}, - {6, 3.99, 9.99, "some fractions2", SHLD_SUCCEED}, - {2, 2.03675, 4.03675, "some fractions3", SHLD_SUCCEED}, + {2, 2.030000, 4.030000, "add fractions test 1: exp_succ", SHLD_SUCCEED}, + {6, 3.99, 9.99, "add fractions test 2: exp_succ", SHLD_SUCCEED}, + {2, 2.03675, 4.03675, "add fractions test3: exp_succ", SHLD_SUCCEED}, } for _, tc := range tCases { got := calculator.Add(tc.a, tc.b) @@ -98,6 +98,6 @@ func TestDivide(t *testing.T) { } -func printDivTestSummary(cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) { +func printDivTestSummary(cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch int) { fmt.Printf("SummaryDiv: TotalTests: %d: Executed: %d: Failed: %d: Succeeded: %d\n", cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) } From 7a6329b7af37eaa7b49f6e9a91ac83caacf6e53c Mon Sep 17 00:00:00 2001 From: Abhjit L Date: Sat, 26 Sep 2020 22:40:56 +0530 Subject: [PATCH 5/5] prepare code for pull request --- calculator_test.go | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/calculator_test.go b/calculator_test.go index 4e0e171..f5783e1 100644 --- a/calculator_test.go +++ b/calculator_test.go @@ -8,8 +8,8 @@ import ( ) const ( - SHLD_SUCCEED = 1 - SHLD_FAIL = 0 + SHLD_SUCCEED = 1 // We expect the test to succeed. If it fails, its an error + SHLD_FAIL = 0 // We expect the test to fail. It it succeeds, its an error ) type testCase struct { @@ -59,12 +59,13 @@ func TestMultiply(t *testing.T) { func TestDivide(t *testing.T) { t.Parallel() tCases := []testCase{ - {2.030000, 20, 9.852216, "Divide some fractions1", SHLD_SUCCEED}, - {0, 3.99, 0, "Divide some fractions2", SHLD_FAIL}, - {2, 0, 0, "Divide some fractions3", SHLD_SUCCEED}, + {2.030000, 20, 9.852216, "Divide fractions 1", SHLD_SUCCEED}, + {2, 0, 0, "Divide fractions 3", SHLD_SUCCEED}, + {0, 3.99, 0, "Divide by 0", SHLD_FAIL}, } cntTestExec, cntfuncErr, cntexpMismatch := 0, 0, 0 cntNoOfTests := len(tCases) + // If I have deferred it, why is it running here, in the flow. How is it related to tests? defer printDivTestSummary(cntNoOfTests, cntTestExec, cntfuncErr, cntexpMismatch) for _, tc := range tCases { @@ -73,6 +74,7 @@ func TestDivide(t *testing.T) { cntTestExec++ got, err := calculator.Divide(tc.a, tc.b) + if err != nil { funcErr = true cntfuncErr++ @@ -84,13 +86,13 @@ func TestDivide(t *testing.T) { } } switch tc.tcExpStatus { - case SHLD_FAIL: // We have defined this test case to FAIL. Its err it it doesn't + case SHLD_FAIL: // We have defined this test case to FAIL. Its err if it doesn't if !(funcErr || expMismatch) { - t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + t.Errorf("DivTest: %s :: want %f, got %f ", tc.tcName, tc.want, got) } - case SHLD_SUCCEED: // We have defined this test case to SUCCEED. Its err it it doesn't + case SHLD_SUCCEED: // We have defined this test case to SUCCEED. Its err if it doesn't if funcErr || expMismatch { - t.Errorf("want %f, got %f :: Test: %s", tc.want, got, tc.tcName) + t.Errorf("DivTest: %s :: want %f, got %f ", tc.tcName, tc.want, got) } } }