dev
dart 110 lines 4.31 KB
Raw
1 import 'package:cw_core/parse_fixed.dart';
2 import 'package:flutter_test/flutter_test.dart';
3
4 void main() {
5 group('parseFixed', () {
6 group('parseFixed, positive', () {
7 test('should parse 1.000001 as 1000001',
8 () => expect(parseFixed("1.000001", 6), BigInt.from(1000001)));
9
10 test('should parse 1 as 1000000', () => expect(parseFixed("1", 6), BigInt.from(1000000)));
11
12 test('should parse 1. as 1000000', () => expect(parseFixed("1.", 6), BigInt.from(1000000)));
13
14 test('should parse 1.1 as 1100000', () => expect(parseFixed("1.1", 6), BigInt.from(1100000)));
15
16 test('should parse 01.1 as 1100000',
17 () => expect(parseFixed("01.1", 6), BigInt.from(1100000)));
18
19 test('should parse 1100000 as 11000000',
20 () => expect(parseFixed("1100000", 1), BigInt.from(11000000)));
21 });
22
23 group('parseFixed, negative', () {
24 test('should parse -1.000001 as -1000001',
25 () => expect(parseFixed("-1.000001", 6), BigInt.from(-1000001)));
26
27 test('should parse -1 as 1000000', () => expect(parseFixed("-1", 6), BigInt.from(-1000000)));
28 });
29
30 group('parseFixed, no leading 0', () {
31 test('should parse .000001 as 1', () => expect(parseFixed(".000001", 6), BigInt.from(1)));
32
33 test('should parse .00002 as 20', () => expect(parseFixed(".00002", 6), BigInt.from(20)));
34
35 test('should parse -.00002 as -20', () => expect(parseFixed("-.00002", 6), BigInt.from(-20)));
36 });
37
38 group('parseFixed, failing', () {
39 test('should fail to parse .000.0010, too many fractional digits',
40 () => expect(() => parseFixed(".000.0010", 6), throwsFormatException));
41
42 test('should fail to parse .0000010, fractional component exceeds decimals',
43 () => expect(() => parseFixed(".0000010", 6), throwsFormatException));
44
45 test('should fail to parse `.`, missing value',
46 () => expect(() => parseFixed(".", 6), throwsFormatException));
47 });
48
49 group("parseFixed, zero decimal currency", () {
50 test("should parse 5 as 5", () => expect(parseFixed("5", 0), BigInt.from(5)));
51
52 test("should parse 0 as 0", () => expect(parseFixed("0", 0), BigInt.from(0)));
53
54 test("should parse 1. as 1", () => expect(parseFixed("1.", 0), BigInt.from(1)));
55
56 test("should parse -3 as -3", () => expect(parseFixed("-3", 0), BigInt.from(-3)));
57
58 test("should fail to parse 5.5, fractional component exceeds decimals",
59 () => expect(() => parseFixed("5.5", 0), throwsFormatException));
60 });
61 });
62
63 group('tryParseFixed', () {
64 group('tryParseFixed, positive', () {
65 test('should parse 1.000001 as 1000001',
66 () => expect(tryParseFixed("1.000001", 6), BigInt.from(1000001)));
67
68 test('should parse 1 as 1000000', () => expect(tryParseFixed("1", 6), BigInt.from(1000000)));
69
70 test(
71 'should parse 1. as 1000000', () => expect(tryParseFixed("1.", 6), BigInt.from(1000000)));
72
73 test('should parse 1.1 as 1100000',
74 () => expect(tryParseFixed("1.1", 6), BigInt.from(1100000)));
75
76 test('should parse 01.1 as 1100000',
77 () => expect(tryParseFixed("01.1", 6), BigInt.from(1100000)));
78
79 test('should parse 1100000 as 11000000',
80 () => expect(tryParseFixed("1100000", 1), BigInt.from(11000000)));
81 });
82
83 group('tryParseFixed, negative', () {
84 test('should parse -1.000001 as -1000001',
85 () => expect(tryParseFixed("-1.000001", 6), BigInt.from(-1000001)));
86
87 test('should parse -1 as 1000000',
88 () => expect(tryParseFixed("-1", 6), BigInt.from(-1000000)));
89 });
90
91 group('tryParseFixed, no leading 0', () {
92 test('should parse .000001 as 1', () => expect(tryParseFixed(".000001", 6), BigInt.from(1)));
93
94 test('should parse .00002 as 20', () => expect(tryParseFixed(".00002", 6), BigInt.from(20)));
95
96 test('should parse -.00002 as -20',
97 () => expect(tryParseFixed("-.00002", 6), BigInt.from(-20)));
98 });
99
100 group('tryParseFixed, return `null`', () {
101 test('should parse .000.0010 as null, too many fractional digits',
102 () => expect(tryParseFixed(".000.0010", 6), isNull));
103
104 test('should parse .0000010 as null, fractional component exceeds decimals',
105 () => expect(tryParseFixed(".0000010", 6), isNull));
106
107 test('should parse . as `null`, missing value', () => expect(tryParseFixed(".", 6), isNull));
108 });
109 });
110 }