package fx import ( "math" "testing" ) func TestConvert(t *testing.T) { r := &Rates{PerEUR: map[string]float64{"USD": 1.25, "GBP": 0.8}} cases := []struct { amount float64 from, to string want float64 } { {100, "EUR", "USD", 125}, {125, "USD", "EUR", 100}, {100, "GBP", "USD", 156.25}, {100, "GBP", "GBP", 100}, } for _, c := range cases { got, err := r.Convert(c.amount, c.from, c.to) if err != nil || math.Abs(got-c.want) > 1e-9 { t.Errorf("%v %s in %s = %v, %v; want %v", c.amount, c.from, c.to, got, err, c.want) } } if _, err := r.Convert(1, "JPY", "EUR"); err == nil { t.Error("converted a currency without a rate") } }