Created
October 5, 2015 05:50
-
-
Save maderaka/7b51cfcb26dc21fc2316 to your computer and use it in GitHub Desktop.
Automated Testing in Golang
This file contains 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 characters
package librarian | |
import ( | |
"time" | |
) | |
const ( | |
FINE_DAY = 15 | |
FINE_MONTH = 500 | |
FINE_YEAR = 10000 | |
NO_FINE = 0 | |
) | |
func CountFine(aD, eD time.Time) int { | |
fine := NO_FINE | |
// Actual Date | |
adY := aD.Year() | |
adM := int(aD.Month()) | |
adD := aD.Day() | |
// Expected Date | |
edY := eD.Year() | |
edM := int(eD.Month()) | |
edD := eD.Day() | |
if adY > edY { | |
fine = FINE_YEAR | |
} else if adM > edM && adY == edY { | |
months := (adM - edM) | |
if months > 0 { | |
fine = months * FINE_MONTH | |
} | |
} else if adM == edM && adY == edY { | |
days := (adD - edD) | |
if days > 0 { | |
fine = days * FINE_DAY | |
} | |
} | |
return fine | |
} |
This file contains 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 characters
package librarian | |
import ( | |
"fmt" | |
"testing" | |
"time" | |
) | |
type test struct { | |
aD time.Time | |
eD time.Time | |
fine int | |
} | |
var tests = []test{ | |
{date(2015, time.June, 9), date(2015, time.June, 6), 45}, | |
{date(2015, time.June, 10), date(2015, time.June, 6), 60}, | |
{date(2015, time.July, 10), date(2015, time.June, 6), 500}, | |
{date(2015, time.August, 9), date(2015, time.June, 10), 1000}, | |
{date(2015, time.October, 10), date(2015, time.June, 6), 2000}, | |
{date(2016, time.July, 10), date(2015, time.June, 6), 10000}, | |
{date(2017, time.June, 9), date(2015, time.June, 6), 10000}, | |
{date(2015, time.November, 10), date(2015, time.June, 6), 2500}, | |
{date(2016, time.August, 10), date(2015, time.June, 6), 10000}, | |
{date(2015, time.June, 1), date(2015, time.June, 6), 0}, | |
} | |
func date(y int, m time.Month, d int) time.Time { | |
return time.Date(y, m, d, 0, 0, 0, 0, time.UTC) | |
} | |
func toString(d time.Time) string { | |
return fmt.Sprintf( | |
"%d-%s-%d", | |
d.Year(), | |
d.Month(), | |
d.Day(), | |
) | |
} | |
func TestCountFine(t *testing.T) { | |
for _, test := range tests { | |
fine := CountFine(test.aD, test.eD) | |
if fine != test.fine { | |
t.Error( | |
"For `actual date` :", toString(test.aD), | |
"and `expected date` :", toString(test.eD), | |
"expected", test.fine, | |
"got", fine, | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment