From c08497b146d436f7209e64859c60aed0fbf00ccc Mon Sep 17 00:00:00 2001 From: Meutel Date: Sun, 9 Jul 2017 11:15:16 +0200 Subject: [PATCH] Tests --- converter/converter_test.go | 61 +++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 converter/converter_test.go 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() + } + + } + +}