go-examples/converter/converter_test.go
2017-07-09 11:56:01 +02:00

67 lines
1.1 KiB
Go

package converter
import (
"strings"
"testing"
)
func TestConvert(t *testing.T) {
result, err := Convert("50aa", "km")
if err == nil {
t.Log("should be error")
t.Fail()
}
result, err = Convert("aami", "km")
if err == nil {
t.Log("should be error")
t.Fail()
}
result, err = Convert("50mi", "aa")
if err == nil {
t.Log("should be error")
t.Fail()
}
result, err = Convert("50mi", "km")
if err != nil {
t.Log("error should be nil")
t.Fail()
}
if !strings.HasSuffix(result, "km") {
t.Log("wrong unit")
t.Fail()
}
convTests := []struct{ input, unit1, unit2 string }{
{"50.00", "mi", "km"},
{"50.00", "ft", "m"},
}
for _, c := range convTests {
input := c.input + c.unit1
conv1, err := Convert(input, c.unit2)
if err != nil {
t.Log("error should be nil")
t.Fail()
}
conv2, err := Convert(conv1, c.unit1)
if err != nil {
t.Log("error should be nil")
t.Fail()
}
if input != conv2 {
t.Log("Conversion error", input, conv2)
t.Fail()
}
}
}
func BenchmarkConvert(b *testing.B) {
for i := 0; i < b.N; i++ {
Convert("50.0mi", "km")
}
}