Files

39 lines
1.1 KiB
Go

package ledger
import "testing"
func TestParseAmountCanonicalizesExactValues(t *testing.T) {
for input, expected := range map[string]string{
"0": "0",
"1": "1",
"1.2300": "1.23",
"-0.000000000000000001": "-0.000000000000000001",
"99999999999999999999": "99999999999999999999",
} {
amount, err := ParseAmount(input)
if err != nil {
t.Fatalf("ParseAmount(%q): %v", input, err)
}
if got := amount.String(); got != expected {
t.Fatalf("ParseAmount(%q) = %q, want %q", input, got, expected)
}
}
}
func TestParseAmountRejectsInvalidValues(t *testing.T) {
for _, input := range []string{"", "+1", "01", ".1", "1.", "1e2", "1.0000000000000000001", "123456789012345678901234567890123456789"} {
if _, err := ParseAmount(input); err == nil {
t.Fatalf("ParseAmount(%q) succeeded", input)
}
}
}
func TestAmountAdditionIsExact(t *testing.T) {
one, _ := ParseAmount("0.1")
two, _ := ParseAmount("0.2")
minusThree, _ := ParseAmount("-0.3")
if got := one.Add(two).Add(minusThree); !got.IsZero() {
t.Fatalf("expected exact zero, got %s", got.String())
}
}