Skip to content

Commit

Permalink
added tests for precision
Browse files Browse the repository at this point in the history
  • Loading branch information
OlehMarch committed Oct 24, 2024
1 parent c00579c commit eaaf5db
Show file tree
Hide file tree
Showing 9 changed files with 4,097 additions and 3,176 deletions.
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ linter:
rules:
avoid_print: false
prefer_single_quotes: true
prefer_const_declarations: true
prefer_const_constructors: true
prefer_const_constructors_in_immutables: true
prefer_const_literals_to_create_immutables: true
6 changes: 3 additions & 3 deletions lib/src/formats.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:mdmoney/mdmoney.dart';

final _doubleRegex = RegExp(r'^(-?)(0|([1-9][0-9]*))(\.[0-9]{1,2})?$');
final _doubleRankedRegex = RegExp(r'^(-?)(0|([1-9][0-9 ]*))(\.[0-9]{1,2})?$');
final _doubleRegex = RegExp(r'^(-?)(0|([1-9][0-9]*))(\.[0-9]{1,})?$');
final _doubleRankedRegex = RegExp(r'^(-?)(0|([1-9][0-9 ]*))(\.[0-9]{1,})?$');

/// Describes possible decimal separator formats.
enum DecimalSeparatorFormat {
Expand Down Expand Up @@ -143,7 +143,7 @@ enum MoneyFormat {
precision ?? value.precision ?? value.currency.precision;

if (adjustedPrecision < 0) {
throw NegativePrecisionException();
throw const NegativePrecisionException();
}

return value.toDecimal().toStringAsFixed(adjustedPrecision);
Expand Down
37 changes: 23 additions & 14 deletions lib/src/money.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Money implements Comparable<Money> {
int? precision,
}) {
if (precision != null && precision < 0) {
throw NegativePrecisionException();
throw const NegativePrecisionException();
}

return Money._(BigInt.from(cents), currency, precision);
Expand All @@ -37,7 +37,7 @@ class Money implements Comparable<Money> {
int? precision,
}) {
if (precision != null && precision < 0) {
throw NegativePrecisionException();
throw const NegativePrecisionException();
}

final centModifier = _centModifier(precision ?? currency.precision);
Expand All @@ -52,7 +52,7 @@ class Money implements Comparable<Money> {
int? precision,
}) {
if (precision != null && precision < 0) {
throw NegativePrecisionException();
throw const NegativePrecisionException();
}

final centModifier = _centModifier(precision ?? currency.precision);
Expand All @@ -72,7 +72,7 @@ class Money implements Comparable<Money> {
int? precision,
}) {
if (precision != null && precision < 0) {
throw NegativePrecisionException();
throw const NegativePrecisionException();
}

final currencyFromAmount =
Expand Down Expand Up @@ -289,15 +289,23 @@ class Money implements Comparable<Money> {
throw const CurrencyMismatchException();
}

return Money._(cents + other.cents, currency, precision);
return Money.fromDecimal(
toDecimal() + other.toDecimal(),
currency,
precision: precision,
);
}

Money operator -(Money other) {
if (currency != other.currency) {
throw const CurrencyMismatchException();
}

return Money._(cents - other.cents, currency, precision);
return Money.fromDecimal(
toDecimal() - other.toDecimal(),
currency,
precision: precision,
);
}

// ! Possibly change from double to dynamic to support double and Decimal
Expand Down Expand Up @@ -332,31 +340,31 @@ class Money implements Comparable<Money> {
throw const CurrencyMismatchException();
}

return cents < other.cents;
return toDecimal() < other.toDecimal();
}

bool operator <=(Money other) {
if (currency != other.currency) {
throw const CurrencyMismatchException();
}

return cents <= other.cents;
return toDecimal() <= other.toDecimal();
}

bool operator >(Money other) {
if (currency != other.currency) {
throw const CurrencyMismatchException();
}

return cents > other.cents;
return toDecimal() > other.toDecimal();
}

bool operator >=(Money other) {
if (currency != other.currency) {
throw const CurrencyMismatchException();
}

return cents >= other.cents;
return toDecimal() >= other.toDecimal();
}

@override
Expand All @@ -365,21 +373,22 @@ class Money implements Comparable<Money> {
other is Money &&
runtimeType == other.runtimeType &&
cents == other.cents &&
currency == other.currency;
currency == other.currency &&
precision == other.precision;
}

@override
int get hashCode => cents.hashCode ^ currency.hashCode;
int get hashCode => cents.hashCode ^ currency.hashCode ^ precision.hashCode;

@override
int compareTo(Money other) {
if (currency != other.currency) {
throw const CurrencyMismatchException();
}

if (cents < other.cents) {
if (toDecimal() < other.toDecimal()) {
return -1;
} else if (cents > other.cents) {
} else if (toDecimal() > other.toDecimal()) {
return 1;
} else {
return 0;
Expand Down
11 changes: 11 additions & 0 deletions test/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:mdmoney/mdmoney.dart';

final defaultCurrency = FiatCurrency.$default;
final otherCurrency =
FiatCurrency.values.firstWhere((c) => c != defaultCurrency);
const maxFinite =
// ignore: lines_longer_than_80_chars
1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583.68;
const maxFiniteCents =
// ignore: lines_longer_than_80_chars
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;
52 changes: 35 additions & 17 deletions test/formats_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,37 @@ void main() {
group('DecimalSeparatorFormat >', () {
group('Exceptions >', () {
test('empty', () {
String actual() => RankFormat.none.format('');
String actual() => DecimalSeparatorFormat.point.format('');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('comma separated', () {
String actual() => RankFormat.none.format('1234,5');
String actual() => DecimalSeparatorFormat.point.format('1234,5');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('comma separated with too much fractionals', () {
String actual() => RankFormat.none.format('1234,567');
String actual() => DecimalSeparatorFormat.point.format('1234,567');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('integer with decimal point', () {
String actual() => RankFormat.none.format('123.');
String actual() => DecimalSeparatorFormat.point.format('123.');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('decimal point', () {
String actual() => RankFormat.none.format('.');
String actual() => DecimalSeparatorFormat.point.format('.');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('decimal point and fractionals', () {
String actual() => RankFormat.none.format('.23');
String actual() => DecimalSeparatorFormat.point.format('.23');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('decimal point and too much fractionals', () {
String actual() => RankFormat.none.format('.234');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('too much fractionals', () {
String actual() => RankFormat.none.format('1.234');
String actual() => DecimalSeparatorFormat.point.format('.234');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
Expand Down Expand Up @@ -150,11 +145,6 @@ void main() {
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
test('too much fractionals', () {
String actual() => RankFormat.none.format('1.234');
final expected = throwsA(const TypeMatcher<ArgumentError>());
expect(actual, expected);
});
});

group('None >', () {
Expand Down Expand Up @@ -236,6 +226,11 @@ void main() {
const expected = '12345678.85';
expect(actual, expected);
});
test('1234.857562', () {
final actual = RankFormat.none.format('1234.857562');
const expected = '1234.857562';
expect(actual, expected);
});
});
});

Expand Down Expand Up @@ -318,6 +313,11 @@ void main() {
const expected = '12 345 678.85';
expect(actual, expected);
});
test('1234.857562', () {
final actual = RankFormat.space.format('1234.857562');
const expected = '1 234.857562';
expect(actual, expected);
});
});
});
});
Expand All @@ -342,6 +342,12 @@ void main() {
const expected = '1234';
expect(actual, expected);
});
test('1234.56789, precision 4', () {
final actual = MoneyFormat.integer.format(
Money.fromDouble(1234.56789, FiatCurrency.$default, precision: 4));
const expected = '1234';
expect(actual, expected);
});
});

group('fixedDouble >', () {
Expand All @@ -363,6 +369,12 @@ void main() {
const expected = '1234.56';
expect(actual, expected);
});
test('1234.56789, precision 4', () {
final actual = MoneyFormat.fixedDouble.format(
Money.fromDouble(1234.56789, FiatCurrency.$default, precision: 4));
const expected = '1234.5679';
expect(actual, expected);
});
});

group('flexibleDouble >', () {
Expand All @@ -384,6 +396,12 @@ void main() {
const expected = '1234.56';
expect(actual, expected);
});
test('1234.567, precision 4', () {
final actual = MoneyFormat.flexibleDouble.format(
Money.fromDouble(1234.567, FiatCurrency.$default, precision: 4));
const expected = '1234.567';
expect(actual, expected);
});
});
});

Expand Down
Loading

0 comments on commit eaaf5db

Please sign in to comment.