mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2024-12-22 19:39:22 +00:00
added extra Amount functionality + tests
This commit is contained in:
parent
61894c9e8e
commit
8452758d17
2 changed files with 54 additions and 0 deletions
|
@ -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 =========================================================
|
||||
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue