Last active
December 6, 2021 09:06
-
-
Save denisvmedia/f4405faf77440e726028d267d05a25d5 to your computer and use it in GitHub Desktop.
Revisions
-
denisvmedia revised this gist
Dec 6, 2021 . 2 changed files with 6 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,15 @@ package math import "errors" var ErrCantDivideByZero = errors.New("can't divide by zero") // divide cannot be tested directly func divide(x, y float32) float32 { return x / y } // Divide is exported and will be tested func Divide(x, y int) (int, error) { if y == 0 { return 0, ErrCantDivideByZero This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,11 @@ package math_test // _test suffix is used to prevent private members direct tests import ( "log" "os" "testing" . "mynamespace/math" // dot import to use public symbols without a package name ) func TestDivide(t *testing.T) { @@ -59,6 +59,7 @@ func TestDivide(t *testing.T) { } } // inits tests globally (package wide) func TestMain(m *testing.M) { log.Print("setup all") code := m.Run() -
denisvmedia revised this gist
Dec 3, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -50,10 +50,10 @@ func TestDivide(t *testing.T) { res, err := Divide(tc.x, tc.y) if err != tc.err { t.Errorf("error expected %v, got %v", tc.err, err) } if res != tc.res { t.Errorf("result expected %d, got %d", tc.res, res) } }) } -
denisvmedia revised this gist
Dec 3, 2021 . 1 changed file with 9 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,16 @@ package mult import "errors" var ErrCantDivideByZero = errors.New("can't divide by zero") func divide(x, y float32) float32 { return x / y } func Divide(x, y int) (int, error) { if y == 0 { return 0, ErrCantDivideByZero } return int(divide(float32(x), float32(y))), nil } -
denisvmedia created this gist
Dec 3, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ package mult func divide(x, y float32) float32 { if y == 0 { panic("can't divide by zero") // otherise it returns +/-Inf } return x / y } func Divide(x, y int) int { return int(divide(float32(x), float32(y))) } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,67 @@ package mult_test import ( "log" "os" "testing" . "kasten.io/k10/mult" // dot import to use public symbols without a package name ) func TestDivide(t *testing.T) { cases := []struct { name string x int y int res int err error }{ { name: "integer dividable without reminder", x: 4, y: 2, res: 2, }, { name: "integer dividable with reminder", x: 3, y: 2, res: 1, }, { name: "divide by zero", x: 4, y: 0, err: ErrCantDivideByZero, }, { name: "Intentionally broken test case", x: 0, y: 0, res: 1, err: nil, }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { log.Print("before test") defer log.Print("after test") res, err := Divide(tc.x, tc.y) if err != tc.err { t.Errorf("error expected %v", tc.err) } if res != tc.res { t.Errorf("expected %d, got %d", tc.res, res) } }) } } func TestMain(m *testing.M) { log.Print("setup all") code := m.Run() log.Print("teardown all") os.Exit(code) }