This commit is contained in:
Meutel 2017-07-09 11:15:16 +02:00
parent f3eada70a8
commit c08497b146

View File

@ -0,0 +1,61 @@
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()
}
}
}