added extra Amount functionality + tests

This commit is contained in:
julian 2023-03-30 10:49:07 -06:00
parent 61894c9e8e
commit 8452758d17
2 changed files with 54 additions and 0 deletions

View file

@ -12,6 +12,12 @@ class Amount implements Equatable {
}) : assert(fractionDigits >= 0),
_value = rawValue;
/// special zero case with [fractionDigits] set to 0
static Amount get zero => Amount(
rawValue: BigInt.zero,
fractionDigits: 0,
);
/// truncate double value to [fractionDigits] places
Amount.fromDouble(double amount, {required this.fractionDigits})
: assert(fractionDigits >= 0),
@ -66,6 +72,17 @@ class Amount implements Equatable {
);
}
// ===========================================================================
// ======= operators =========================================================
bool operator >(Amount other) => raw > other.raw;
bool operator <(Amount other) => raw < other.raw;
bool operator >=(Amount other) => raw >= other.raw;
bool operator <=(Amount other) => raw <= other.raw;
// ===========================================================================
// ======= Overrides =========================================================

View file

@ -78,4 +78,41 @@ void main() {
}
expect(didThrow, true);
});
group("operators", () {
final one = Amount(rawValue: BigInt.one, fractionDigits: 0);
final two = Amount(rawValue: BigInt.two, fractionDigits: 0);
test(">", () {
expect(one > two, false);
expect(one > one, false);
expect(two > two, false);
expect(two > one, true);
});
test("<", () {
expect(one < two, true);
expect(one < one, false);
expect(two < two, false);
expect(two < one, false);
});
test(">=", () {
expect(one >= two, false);
expect(one >= one, true);
expect(two >= two, true);
expect(two >= one, true);
});
test("<=", () {
expect(one <= two, true);
expect(one <= one, true);
expect(two <= two, true);
expect(two <= one, false);
});
});
}