utils/conversions_test.go

61 lines
1.4 KiB
Go

package utils
import "testing"
func TestConvertIntToStringAndAddFrontSpacing(t *testing.T) {
data := map[int64]string{
1: " 1",
12: " 12",
123: " 123",
1234: " 1234",
12345: " 12345",
123456: " 123456",
1234567: " 1234567",
12345678: "12345678",
123456789: "123456789",
}
for d, expectedResult := range data {
result := ConvertIntToStringAndAddFrontSpacing(d, 8)
if result != expectedResult {
t.Errorf("\ngot: %q\nwanted: %q\nfor: %q", result, expectedResult, d)
}
}
}
func TestConvertStringToFloatOrZeroOnError(t *testing.T) {
data := map[string]float64{
"-13.37": -13.37,
"-10": -10.0,
"0": 0.0,
"10": 10.0,
"13.37": 13.37,
"Son Goku": 0.0,
"0x1f346": 0.0,
}
for d, expectedResult := range data {
result := ConvertStringToFloatOrZeroOnError(d)
if result != expectedResult {
t.Errorf("\ngot: %.2f\nwanted: %.2f\nfor: %q", result, expectedResult, d)
}
}
}
func TestConvertStringToIntOrZeroOnError(t *testing.T) {
data := map[string]int64{
"-13": -13,
"0": 0,
"13": 13,
"Son Goku": 0,
"1f346": 0,
}
for d, expectedResult := range data {
result := ConvertStringToIntOrZeroOnError(d)
if result != expectedResult {
t.Errorf("\ngot: %d\nwanted: %d\nfor: %q", result, expectedResult, d)
}
}
}