-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
personnummer.dart
167 lines (143 loc) · 4.31 KB
/
personnummer.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import 'package:test/test.dart';
import 'package:personnummer/personnummer.dart';
import 'dart:io';
import 'dart:async';
import 'dart:convert';
Future<String> fetchUrlBodyAsString(String url) async {
var request = await HttpClient().getUrl(Uri.parse(url));
var response = await request.close();
return response.transform(utf8.decoder).join();
}
var availableListFormats = [
'long_format',
'short_format',
'separated_format',
'separated_long',
];
void main() async {
String listBody = await fetchUrlBodyAsString(
'https://raw.githubusercontent.com/personnummer/meta/master/testdata/list.json');
String interimBody = await fetchUrlBodyAsString(
'https://raw.githubusercontent.com/personnummer/meta/master/testdata/interim.json');
runTests(jsonDecode(listBody), jsonDecode(interimBody));
}
void runTests(dynamic list, dynamic interim) {
test('should validate personnummer with control digit', () {
for (var item in list) {
for (var format in availableListFormats) {
expect(item["valid"], Personnummer.valid(item[format]));
}
}
});
test('should format personnummer', () {
for (var item in list) {
if (!item['valid']) {
continue;
}
for (var format in availableListFormats) {
if (format != 'short_format') {
var p = Personnummer.parse(item[format]);
expect(item["separated_format"], p.format());
expect(item["long_format"], p.format(true));
}
}
}
});
test('should throw personnummer error', () {
for (var item in list) {
if (item["valid"]) {
continue;
}
for (var format in availableListFormats) {
try {
Personnummer.parse(item[format]);
expect(false, true);
} catch (e) {
expect(true, true);
}
}
}
});
test('should test personnummer sex', () {
for (var item in list) {
if (!item["valid"]) {
continue;
}
for (var format in availableListFormats) {
var p = Personnummer.parse(item[format]);
expect(item["isMale"], p.isMale());
expect(item["isFemale"], p.isFemale());
}
}
});
test('should test personnummer age', () {
for (var item in list) {
if (!item['valid']) {
continue;
}
for (var format in availableListFormats) {
if (format != 'short_format') {
var pin = item["separated_long"];
var year = int.parse(pin.substring(0, 4));
var month = int.parse(pin.substring(4, 6));
var day = int.parse(pin.substring(6, 8));
if (item["type"] == 'con') {
day = day - 60;
}
var date = DateTime(year, month, day);
Personnummer.dateTimeNow = date;
expect(0, Personnummer.parse(item[format]).getAge());
}
}
}
});
test('should test personnummer date', () {
for (var item in list) {
if (!item['valid']) {
continue;
}
for (var format in availableListFormats) {
if (format != 'short_format') {
var pin = item["separated_long"];
var year = int.parse(pin.substring(0, 4));
var month = int.parse(pin.substring(4, 6));
var day = int.parse(pin.substring(6, 8));
if (item["type"] == 'con') {
day = day - 60;
}
var date = DateTime(year, month, day);
// Personnummer.dateTimeNow = date;
expect(date, Personnummer.parse(item[format]).getDate());
}
}
}
});
test('should test interim numbers', () {
for (var item in interim) {
if (!item['valid']) {
continue;
}
for (var format in availableListFormats) {
if (format != 'short_format') {
var p = Personnummer.parse(item[format], allowInterimNumber: true);
expect(item['separated_format'], p.format());
expect(item['long_format'], p.format(true));
}
}
}
});
test('should test invalid interim numbers', () {
for (var item in interim) {
if (item['valid']) {
continue;
}
for (var format in availableListFormats) {
if (format != 'short_format') {
expect(
() => Personnummer.parse(item[format], allowInterimNumber: true),
throwsException);
}
}
}
});
}