package parser import "testing" func TestParseAmount(t *testing.T) { cases := []struct { in string decimal string thousands string digits int want int64 }{ {"45.20", ".", "", 2, 4520}, {"-45.20", ".", "", 2, -4520}, {"45,20", ",", ".", 2, 4520}, {"1.234,56", ",", ".", 2, 123456}, {"1,234.56", ".", ",", 2, 123456}, {"1 234.56", ".", " ", 2, 123456}, {"45.20 EUR", ".", "", 2, 4520}, {"BGN 45.20", ".", "", 2, 4520}, {"45,20-", ",", "", 2, -4520}, {"(45.20)", ".", "", 2, -4520}, {"+45.20", ".", "", 2, 4520}, {"0.00", ".", "", 2, 0}, {".50", ".", "", 2, 50}, {"45", ".", "", 2, 4500}, {"45.2", ".", "", 2, 4520}, {"45.205", ".", "", 2, 4521}, // round half away from zero {"45.204", ".", "", 2, 4520}, // round down {"-45.205", ".", "", 2, -4521}, {"1234", ".", "", 0, 1234}, // zero-digit currency {"1234.6", ".", "", 0, 1235}, {"1.234567", ".", "", 3, 1235}, {"−45.20", ".", "", 2, -4520}, // U+2212 minus } for _, c := range cases { got, err := ParseAmount(c.in, c.decimal, c.thousands, c.digits) if err != nil { t.Errorf("ParseAmount(%q) error: %v", c.in, err) continue } if got != c.want { t.Errorf("ParseAmount(%q, decimal=%q, digits=%d) = %d, want %d", c.in, c.decimal, c.digits, got, c.want) } } } func TestParseAmountErrors(t *testing.T) { for _, in := range []string{"", " ", "abc", "-", "."} { if v, err := ParseAmount(in, ".", "", 2); err == nil { t.Errorf("ParseAmount(%q) = %d, want error", in, v) } } }