Skip to content

Commit eaaf5db

Browse files
committed
added tests for precision
1 parent c00579c commit eaaf5db

9 files changed

+4097
-3176
lines changed

analysis_options.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ linter:
44
rules:
55
avoid_print: false
66
prefer_single_quotes: true
7+
prefer_const_declarations: true
8+
prefer_const_constructors: true
9+
prefer_const_constructors_in_immutables: true
10+
prefer_const_literals_to_create_immutables: true

lib/src/formats.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'package:mdmoney/mdmoney.dart';
22

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

66
/// Describes possible decimal separator formats.
77
enum DecimalSeparatorFormat {
@@ -143,7 +143,7 @@ enum MoneyFormat {
143143
precision ?? value.precision ?? value.currency.precision;
144144

145145
if (adjustedPrecision < 0) {
146-
throw NegativePrecisionException();
146+
throw const NegativePrecisionException();
147147
}
148148

149149
return value.toDecimal().toStringAsFixed(adjustedPrecision);

lib/src/money.dart

+23-14
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Money implements Comparable<Money> {
2424
int? precision,
2525
}) {
2626
if (precision != null && precision < 0) {
27-
throw NegativePrecisionException();
27+
throw const NegativePrecisionException();
2828
}
2929

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

4343
final centModifier = _centModifier(precision ?? currency.precision);
@@ -52,7 +52,7 @@ class Money implements Comparable<Money> {
5252
int? precision,
5353
}) {
5454
if (precision != null && precision < 0) {
55-
throw NegativePrecisionException();
55+
throw const NegativePrecisionException();
5656
}
5757

5858
final centModifier = _centModifier(precision ?? currency.precision);
@@ -72,7 +72,7 @@ class Money implements Comparable<Money> {
7272
int? precision,
7373
}) {
7474
if (precision != null && precision < 0) {
75-
throw NegativePrecisionException();
75+
throw const NegativePrecisionException();
7676
}
7777

7878
final currencyFromAmount =
@@ -289,15 +289,23 @@ class Money implements Comparable<Money> {
289289
throw const CurrencyMismatchException();
290290
}
291291

292-
return Money._(cents + other.cents, currency, precision);
292+
return Money.fromDecimal(
293+
toDecimal() + other.toDecimal(),
294+
currency,
295+
precision: precision,
296+
);
293297
}
294298

295299
Money operator -(Money other) {
296300
if (currency != other.currency) {
297301
throw const CurrencyMismatchException();
298302
}
299303

300-
return Money._(cents - other.cents, currency, precision);
304+
return Money.fromDecimal(
305+
toDecimal() - other.toDecimal(),
306+
currency,
307+
precision: precision,
308+
);
301309
}
302310

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

335-
return cents < other.cents;
343+
return toDecimal() < other.toDecimal();
336344
}
337345

338346
bool operator <=(Money other) {
339347
if (currency != other.currency) {
340348
throw const CurrencyMismatchException();
341349
}
342350

343-
return cents <= other.cents;
351+
return toDecimal() <= other.toDecimal();
344352
}
345353

346354
bool operator >(Money other) {
347355
if (currency != other.currency) {
348356
throw const CurrencyMismatchException();
349357
}
350358

351-
return cents > other.cents;
359+
return toDecimal() > other.toDecimal();
352360
}
353361

354362
bool operator >=(Money other) {
355363
if (currency != other.currency) {
356364
throw const CurrencyMismatchException();
357365
}
358366

359-
return cents >= other.cents;
367+
return toDecimal() >= other.toDecimal();
360368
}
361369

362370
@override
@@ -365,21 +373,22 @@ class Money implements Comparable<Money> {
365373
other is Money &&
366374
runtimeType == other.runtimeType &&
367375
cents == other.cents &&
368-
currency == other.currency;
376+
currency == other.currency &&
377+
precision == other.precision;
369378
}
370379

371380
@override
372-
int get hashCode => cents.hashCode ^ currency.hashCode;
381+
int get hashCode => cents.hashCode ^ currency.hashCode ^ precision.hashCode;
373382

374383
@override
375384
int compareTo(Money other) {
376385
if (currency != other.currency) {
377386
throw const CurrencyMismatchException();
378387
}
379388

380-
if (cents < other.cents) {
389+
if (toDecimal() < other.toDecimal()) {
381390
return -1;
382-
} else if (cents > other.cents) {
391+
} else if (toDecimal() > other.toDecimal()) {
383392
return 1;
384393
} else {
385394
return 0;

test/constants.dart

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'package:mdmoney/mdmoney.dart';
2+
3+
final defaultCurrency = FiatCurrency.$default;
4+
final otherCurrency =
5+
FiatCurrency.values.firstWhere((c) => c != defaultCurrency);
6+
const maxFinite =
7+
// ignore: lines_longer_than_80_chars
8+
1797693134862315708145274237317043567980705675258449965989174768031572607800285387605895586327668781715404589535143824642343213268894641827684675467035375169860499105765512820762454900903893289440758685084551339423045832369032229481658085593321233482747978262041447231687381771809192998812504040261841248583.68;
9+
const maxFiniteCents =
10+
// ignore: lines_longer_than_80_chars
11+
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0;

test/formats_test.dart

+35-17
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,37 @@ void main() {
55
group('DecimalSeparatorFormat >', () {
66
group('Exceptions >', () {
77
test('empty', () {
8-
String actual() => RankFormat.none.format('');
8+
String actual() => DecimalSeparatorFormat.point.format('');
99
final expected = throwsA(const TypeMatcher<ArgumentError>());
1010
expect(actual, expected);
1111
});
1212
test('comma separated', () {
13-
String actual() => RankFormat.none.format('1234,5');
13+
String actual() => DecimalSeparatorFormat.point.format('1234,5');
1414
final expected = throwsA(const TypeMatcher<ArgumentError>());
1515
expect(actual, expected);
1616
});
1717
test('comma separated with too much fractionals', () {
18-
String actual() => RankFormat.none.format('1234,567');
18+
String actual() => DecimalSeparatorFormat.point.format('1234,567');
1919
final expected = throwsA(const TypeMatcher<ArgumentError>());
2020
expect(actual, expected);
2121
});
2222
test('integer with decimal point', () {
23-
String actual() => RankFormat.none.format('123.');
23+
String actual() => DecimalSeparatorFormat.point.format('123.');
2424
final expected = throwsA(const TypeMatcher<ArgumentError>());
2525
expect(actual, expected);
2626
});
2727
test('decimal point', () {
28-
String actual() => RankFormat.none.format('.');
28+
String actual() => DecimalSeparatorFormat.point.format('.');
2929
final expected = throwsA(const TypeMatcher<ArgumentError>());
3030
expect(actual, expected);
3131
});
3232
test('decimal point and fractionals', () {
33-
String actual() => RankFormat.none.format('.23');
33+
String actual() => DecimalSeparatorFormat.point.format('.23');
3434
final expected = throwsA(const TypeMatcher<ArgumentError>());
3535
expect(actual, expected);
3636
});
3737
test('decimal point and too much fractionals', () {
38-
String actual() => RankFormat.none.format('.234');
39-
final expected = throwsA(const TypeMatcher<ArgumentError>());
40-
expect(actual, expected);
41-
});
42-
test('too much fractionals', () {
43-
String actual() => RankFormat.none.format('1.234');
38+
String actual() => DecimalSeparatorFormat.point.format('.234');
4439
final expected = throwsA(const TypeMatcher<ArgumentError>());
4540
expect(actual, expected);
4641
});
@@ -150,11 +145,6 @@ void main() {
150145
final expected = throwsA(const TypeMatcher<ArgumentError>());
151146
expect(actual, expected);
152147
});
153-
test('too much fractionals', () {
154-
String actual() => RankFormat.none.format('1.234');
155-
final expected = throwsA(const TypeMatcher<ArgumentError>());
156-
expect(actual, expected);
157-
});
158148
});
159149

160150
group('None >', () {
@@ -236,6 +226,11 @@ void main() {
236226
const expected = '12345678.85';
237227
expect(actual, expected);
238228
});
229+
test('1234.857562', () {
230+
final actual = RankFormat.none.format('1234.857562');
231+
const expected = '1234.857562';
232+
expect(actual, expected);
233+
});
239234
});
240235
});
241236

@@ -318,6 +313,11 @@ void main() {
318313
const expected = '12 345 678.85';
319314
expect(actual, expected);
320315
});
316+
test('1234.857562', () {
317+
final actual = RankFormat.space.format('1234.857562');
318+
const expected = '1 234.857562';
319+
expect(actual, expected);
320+
});
321321
});
322322
});
323323
});
@@ -342,6 +342,12 @@ void main() {
342342
const expected = '1234';
343343
expect(actual, expected);
344344
});
345+
test('1234.56789, precision 4', () {
346+
final actual = MoneyFormat.integer.format(
347+
Money.fromDouble(1234.56789, FiatCurrency.$default, precision: 4));
348+
const expected = '1234';
349+
expect(actual, expected);
350+
});
345351
});
346352

347353
group('fixedDouble >', () {
@@ -363,6 +369,12 @@ void main() {
363369
const expected = '1234.56';
364370
expect(actual, expected);
365371
});
372+
test('1234.56789, precision 4', () {
373+
final actual = MoneyFormat.fixedDouble.format(
374+
Money.fromDouble(1234.56789, FiatCurrency.$default, precision: 4));
375+
const expected = '1234.5679';
376+
expect(actual, expected);
377+
});
366378
});
367379

368380
group('flexibleDouble >', () {
@@ -384,6 +396,12 @@ void main() {
384396
const expected = '1234.56';
385397
expect(actual, expected);
386398
});
399+
test('1234.567, precision 4', () {
400+
final actual = MoneyFormat.flexibleDouble.format(
401+
Money.fromDouble(1234.567, FiatCurrency.$default, precision: 4));
402+
const expected = '1234.567';
403+
expect(actual, expected);
404+
});
387405
});
388406
});
389407

0 commit comments

Comments
 (0)