diff --git a/converter/converter_test.go b/converter/converter_test.go new file mode 100644 index 0000000..5661a7e --- /dev/null +++ b/converter/converter_test.go @@ -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() + } + + } + +}