1- // ignore_for_file: deprecated_member_use_from_same_package
21import 'package:deep_pick/deep_pick.dart' ;
32import 'package:test/test.dart' ;
43
54void main () {
65 group ('Pick' , () {
7- test ('pick from null returns null Pick with full location' , () {
6+ test ('null pick carries full location' , () {
87 final p = pick (null , 'some' , 'path' );
98 expect (p.path, ['some' , 'path' ]);
109 expect (p.value, null );
1110 });
1211
1312 test ('toString() prints value and path' , () {
1413 expect (
15- Pick ('a' , path: ['b' , 0 ]).toString (), 'Pick(value=a, path=[b, 0])' );
14+ Pick ('a' , path: ['b' , 0 ]).toString (),
15+ 'Pick(value=a, path=[b, 0])' ,
16+ );
1617 });
1718
1819 test (
@@ -22,12 +23,10 @@ void main() {
2223 'set' : {'a' , 'b' , 'c' },
2324 };
2425 expect (
25- () => pick (data, 'set' , 0 ),
26- throwsA (isA <PickException >().having (
27- (e) => e.toString (),
28- 'toString' ,
29- allOf (
30- contains ('[set]' ), contains ('Set' ), contains ('index (0)' )))));
26+ () => pick (data, 'set' , 0 ),
27+ throwsA (isA <PickException >().having ((e) => e.toString (), 'toString' ,
28+ allOf (contains ('[set]' ), contains ('Set' ), contains ('index (0)' )))),
29+ );
3130 });
3231
3332 test ('call()' , () {
@@ -40,7 +39,7 @@ void main() {
4039 expect (first.value, {'name' : 'John Snow' });
4140
4241 // pick further
43- expect (first.call ('name' ).required (). asString (), 'John Snow' );
42+ expect (first.call ('name' ).asStringOrThrow (), 'John Snow' );
4443 });
4544
4645 test ('pick deeper than data structure returns null pick' , () {
@@ -69,24 +68,28 @@ void main() {
6968 expect (p.isAbsent (), isFalse);
7069 expect (p.missingValueAtIndex, null );
7170 });
71+
7272 test ('is not absent but null' , () {
7373 final p = pick (null );
7474 expect (p.value, isNull);
7575 expect (p.isAbsent (), isFalse);
7676 expect (p.missingValueAtIndex, null );
7777 });
78+
7879 test ('is not absent but null further down' , () {
7980 final p = pick ({'a' : null }, 'a' );
8081 expect (p.value, isNull);
8182 expect (p.isAbsent (), isFalse);
8283 expect (p.missingValueAtIndex, null );
8384 });
85+
8486 test ('is not absent, not null' , () {
8587 final p = pick ({'a' , 1 }, 'b' );
8688 expect (p.value, isNull);
8789 expect (p.isAbsent (), isTrue);
8890 expect (p.missingValueAtIndex, 0 );
8991 });
92+
9093 test ('is not absent, not null, further down' , () {
9194 final json = {
9295 'a' : {'b' : 1 }
@@ -99,189 +102,6 @@ void main() {
99102 });
100103 });
101104
102- group ('parsing' , () {
103- test ('asStringOrNull()' , () {
104- expect (pick ('adam' ).asStringOrNull (), 'adam' );
105- expect (pick (1 ).asStringOrNull (), '1' );
106- expect (pick (DateTime (2000 )).asStringOrNull (), '2000-01-01 00:00:00.000' );
107- expect (nullPick ().asStringOrNull (), isNull);
108- });
109-
110- test ('asMapOrNull()' , () {
111- expect (pick ({'ab' : 'cd' }).asMapOrNull (), {'ab' : 'cd' });
112- expect (pick (1 ).asMapOrNull (), isNull);
113- expect (nullPick ().asMapOrNull (), isNull);
114- });
115-
116- test ('asMapOrNull() reports errors correctly' , () {
117- final dynamic data = {
118- 'a' : {'some' : 'value' }
119- };
120-
121- try {
122- final parsed = pick (data).asMapOrNull <String , bool >();
123- fail ('casted map without verifying the types. '
124- 'Expected Map<String, bool> but was ${parsed .runtimeType }' );
125- // ignore: avoid_catching_errors
126- } on TypeError catch (e) {
127- expect (
128- e,
129- const TypeMatcher <TypeError >().having (
130- (e) => e.toString (),
131- 'message' ,
132- stringContainsInOrder (
133- ['<String, String>' , 'is not a subtype of type' , 'bool' ]),
134- ),
135- );
136- // ignore: avoid_catching_errors, deprecated_member_use
137- } on CastError catch (e) {
138- // backwards compatibility for Dart 2.7
139- // CastError was replaced with TypeError in Dart 2.8
140- expect (
141- e,
142- // ignore: deprecated_member_use
143- const TypeMatcher <CastError >().having (
144- (e) => e.toString (),
145- 'message' ,
146- stringContainsInOrder (
147- ['<String, String>' , 'is not a subtype of type' , 'bool' ]),
148- ),
149- );
150- }
151- });
152-
153- test ('asMapOrEmpty()' , () {
154- expect (pick ({'ab' : 'cd' }).asMapOrEmpty (), {'ab' : 'cd' });
155- expect (pick ('a' ).asMapOrEmpty (), {});
156- expect (nullPick ().asMapOrEmpty (), {});
157- });
158-
159- test ('asMapOrEmpty() reports errors correctly' , () {
160- final dynamic data = {
161- 'a' : {'some' : 'value' }
162- };
163-
164- try {
165- final parsed = pick (data).asMapOrEmpty <String , bool >();
166- fail ('casted map without verifying the types. '
167- 'Expected Map<String, bool> but was ${parsed .runtimeType }' );
168- // ignore: avoid_catching_errors
169- } on TypeError catch (e) {
170- expect (
171- e,
172- const TypeMatcher <TypeError >().having (
173- (e) => e.toString (),
174- 'message' ,
175- stringContainsInOrder (
176- ['<String, String>' , 'is not a subtype of type' , 'bool' ]),
177- ),
178- );
179- // ignore: avoid_catching_errors, deprecated_member_use
180- } on CastError catch (e) {
181- // backwards compatibility for Dart 2.7
182- // CastError was replaced with TypeError in Dart 2.8
183- expect (
184- e,
185- // ignore: deprecated_member_use
186- const TypeMatcher <CastError >().having (
187- (e) => e.toString (),
188- 'message' ,
189- stringContainsInOrder (
190- ['<String, String>' , 'is not a subtype of type' , 'bool' ]),
191- ),
192- );
193- }
194- });
195-
196- test ('asListOrNull()' , () {
197- expect (pick ([1 , 2 , 3 ]).asListOrNull <int >(), [1 , 2 , 3 ]);
198- expect (pick ('john' ).asListOrNull <int >(), isNull);
199- expect (nullPick ().asListOrNull <int >(), isNull);
200- });
201-
202- test ('asListOrEmpty()' , () {
203- expect (pick ([1 , 2 , 3 ]).asListOrEmpty <int >(), [1 , 2 , 3 ]);
204- expect (pick ('a' ).asListOrEmpty <int >(), []);
205- expect (nullPick ().asListOrEmpty <int >(), []);
206- });
207-
208- test ('asIntOrNull()' , () {
209- expect (pick (1 ).asIntOrNull (), 1 );
210- expect (pick ('a' ).asIntOrNull (), isNull);
211- expect (nullPick ().asIntOrNull (), isNull);
212- });
213-
214- test ('asDoubleOrNull()' , () {
215- expect (pick (1 ).asDoubleOrNull (), 1.0 );
216- expect (pick (2.0 ).asDoubleOrNull (), 2.0 );
217- expect (pick ('3.0' ).asDoubleOrNull (), 3.0 );
218- expect (pick ('a' ).asDoubleOrNull (), isNull);
219- expect (nullPick ().asDoubleOrNull (), isNull);
220- });
221-
222- test ('asDateTimeOrNull()' , () {
223- expect (pick ('2012-02-27 13:27:00,123456z' ).asDateTimeOrNull (),
224- DateTime .utc (2012 , 2 , 27 , 13 , 27 , 0 , 123 , 456 ));
225- expect (pick (DateTime .utc (2020 )).asDateTimeOrNull (), DateTime .utc (2020 ));
226- expect (pick ('1' ).asDateTimeOrNull (), isNull);
227- expect (pick ('Bubblegum' ).asDateTimeOrNull (), isNull);
228- expect (nullPick ().asDateTimeOrNull (), isNull);
229- });
230-
231- test ('asListOrEmpty(Pick -> T)' , () {
232- final data = [
233- {'name' : 'John Snow' },
234- {'name' : 'Daenerys Targaryen' },
235- ];
236- expect (pick (data).asListOrEmpty ((it) => Person .fromJson (it.asMap ())), [
237- Person (name: 'John Snow' ),
238- Person (name: 'Daenerys Targaryen' ),
239- ]);
240- expect (pick ([]).asList ((pick) => Person .fromJson (pick.asMap ())), []);
241- expect (nullPick ().asListOrEmpty ((pick) => Person .fromJson (pick.asMap ())),
242- []);
243- });
244-
245- test ('asListOrEmpty(Pick -> T) reports item parsing errors' , () {
246- final data = [
247- {'name' : 'John Snow' },
248- {'asdf' : 'Daenerys Targaryen' }, // <-- wrong key
249- ];
250- expect (
251- () =>
252- pick (data).asListOrEmpty ((pick) => Person .fromJson (pick.asMap ())),
253- throwsA (isA <PickException >().having ((e) => e.message, 'message' ,
254- contains ('required value at location `name` is absent' ))));
255- });
256-
257- test ('asListOrNull(Pick -> T)' , () {
258- final data = [
259- {'name' : 'John Snow' },
260- {'name' : 'Daenerys Targaryen' },
261- ];
262- expect (pick (data).asListOrNull ((pick) => Person .fromJson (pick.asMap ())), [
263- Person (name: 'John Snow' ),
264- Person (name: 'Daenerys Targaryen' ),
265- ]);
266- expect (
267- pick ([]).asListOrNull ((pick) => Person .fromJson (pick.asMap ())), []);
268- expect (nullPick ().asListOrNull ((pick) => Person .fromJson (pick.asMap ())),
269- null );
270- });
271-
272- test ('asListOrNull(Pick -> T) reports item parsing errors' , () {
273- final data = [
274- {'name' : 'John Snow' },
275- {'asdf' : 'Daenerys Targaryen' }, // <-- wrong key
276- ];
277- expect (
278- () =>
279- pick (data).asListOrNull ((pick) => Person .fromJson (pick.asMap ())),
280- throwsA (isA <PickException >().having ((e) => e.message, 'message' ,
281- contains ('required value at location `name` is absent' ))));
282- });
283- });
284-
285105 group ('isAbsent' , () {
286106 test ('out of range in list returns null pick' , () {
287107 final data = [
@@ -336,7 +156,8 @@ void main() {
336156 expect (root.context, {'lang' : 'de' });
337157 });
338158
339- test ('add and read from context with syntax sugar' , () {
159+ test ('add and read from context with syntax sugar (deprecated)' , () {
160+ // ignore: deprecated_member_use_from_same_package
340161 final root = pick ([]).addContext ('lang' , 'de' );
341162 expect (root.fromContext ('lang' ).asStringOrNull (), 'de' );
342163 });
0 commit comments