trphoenix
2018-11-12 29fbfc5dd1d55d189f23eb6d32f000252f92985f
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (c) 2015, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
 
import 'package:json_annotation/json_annotation.dart';
import 'package:test/test.dart';
import 'package:yaml/yaml.dart';
 
import '../test_utils.dart';
import 'kitchen_sink.dart' as nullable
    show testFactory, testFromJson, JsonConverterTestClass;
import 'kitchen_sink.non_nullable.checked.dart' as checked
    show testFactory, testFromJson;
import 'kitchen_sink.non_nullable.dart' as nn
    show testFactory, testFromJson, JsonConverterTestClass;
import 'kitchen_sink.non_nullable.wrapped.dart' as nnwrapped
    show testFactory, testFromJson;
import 'kitchen_sink.wrapped.dart' as wrapped show testFactory, testFromJson;
import 'kitchen_sink_interface.dart';
import 'strict_keys_object.dart';
 
// copied and renamed as private from /lib/src/constants.dart
const _generatedLocalVarName = 'val';
const _toJsonMapHelperName = 'writeNotNull';
 
final _isATypeError = const TypeMatcher<TypeError>();
 
Matcher _isAUnrecognizedKeysEexception(expectedMessage) =>
    const TypeMatcher<UnrecognizedKeysException>()
        .having((e) => e.message, 'message', expectedMessage);
 
Matcher _isMissingKeyException(expectedMessage) =>
    const TypeMatcher<MissingRequiredKeysException>()
        .having((e) => e.message, 'message', expectedMessage);
 
void main() {
  test('valid values covers all keys', () {
    expect(_invalidValueTypes.keys, orderedEquals(_validValues.keys));
  });
 
  test('required keys', () {
    expect(
        () => StrictKeysObject.fromJson({}),
        throwsA(_isMissingKeyException(
            'Required keys are missing: value, custom_field.')));
  });
 
  group('nullable', () {
    group('unwrapped', () {
      _nullableTests(nullable.testFactory, nullable.testFromJson);
    });
 
    group('wrapped', () {
      _nullableTests(wrapped.testFactory, wrapped.testFromJson);
    });
  });
 
  group('non-nullable', () {
    group('checked', () {
      _nonNullableTests(checked.testFactory, checked.testFromJson,
          isChecked: true);
    });
 
    group('unwrapped', () {
      _nonNullableTests(nn.testFactory, nn.testFromJson);
    });
 
    group('wrapped', () {
      _nonNullableTests(nnwrapped.testFactory, nnwrapped.testFromJson);
    });
  });
 
  group('JsonConverterTestClass', () {
    final validValues = {
      'duration': 5,
      'durationList': [5],
      'bigInt': '5',
      'bigIntMap': {'vaule': '5'},
      'numberSilly': 5,
      'numberSillySet': [5],
      'dateTime': 5
    };
 
    test('nullable values are allowed in the nullable version', () {
      final instance = nullable.JsonConverterTestClass();
      final json = instance.toJson();
      expect(json.values, everyElement(isNull));
      expect(json.keys, unorderedEquals(validValues.keys));
 
      final instance2 = nullable.JsonConverterTestClass.fromJson(json);
      expect(instance2.toJson(), json);
    });
 
    test('nullable values are not allowed in non-nullable version', () {
      var instance = nn.JsonConverterTestClass();
      expect(() => instance.toJson(), throwsNoSuchMethodError,
          reason: 'Trying to call `map` on a null list');
 
      instance = nn.JsonConverterTestClass.fromJson(validValues);
      final json = instance.toJson();
      expect(json, validValues);
      expect(json.values, everyElement(isNotNull));
 
      final instance2 = nn.JsonConverterTestClass.fromJson(json);
      expect(instance2.toJson(), json);
    });
  });
}
 
typedef KitchenSink KitchenSinkCtor(
    {int ctorValidatedNo42,
    Iterable iterable,
    Iterable<dynamic> dynamicIterable,
    Iterable<Object> objectIterable,
    Iterable<int> intIterable,
    Iterable<DateTime> dateTimeIterable});
 
void _nonNullableTests(KitchenSinkCtor ctor, KitchenSink fromJson(Map json),
    {bool isChecked = false}) {
  test('with null values fails serialization', () {
    expect(() => (ctor()..objectDateTimeMap = null).toJson(),
        throwsNoSuchMethodError);
  });
 
  test('with empty json fails deserialization', () {
    if (isChecked) {
      expect(() => fromJson({}), throwsA(_checkedMatcher('intIterable')));
    } else {
      expect(() => fromJson({}), throwsNoSuchMethodError);
    }
  });
  _sharedTests(ctor, fromJson, isChecked: isChecked);
}
 
void _nullableTests(KitchenSinkCtor ctor, KitchenSink fromJson(Map json)) {
  void roundTripItem(KitchenSink p) {
    roundTripObject(p, (json) => fromJson(json));
  }
 
  test('Fields with `!includeIfNull` should not be included when null', () {
    final item = ctor();
 
    final expectedDefaultKeys = _validValues.keys.toSet()
      ..removeAll(_excludeIfNullKeys);
 
    final encoded = item.toJson();
 
    expect(encoded.keys, orderedEquals(expectedDefaultKeys));
 
    for (final key in expectedDefaultKeys) {
      expect(encoded, containsPair(key, isNull));
    }
  });
 
  test('list and map of DateTime', () {
    final now = DateTime.now();
    final item = ctor(dateTimeIterable: <DateTime>[now])
      ..dateTimeList = <DateTime>[now, null]
      ..objectDateTimeMap = <Object, DateTime>{'value': now, 'null': null};
 
    roundTripItem(item);
  });
 
  test('complex nested type', () {
    final item = ctor()
      ..crazyComplex = [
        null,
        {},
        {
          'null': null,
          'empty': {},
          'items': {
            'null': null,
            'empty': [],
            'items': [
              null,
              [],
              [DateTime.now()]
            ]
          }
        }
      ];
    roundTripItem(item);
  });
 
  _sharedTests(ctor, fromJson);
}
 
void _sharedTests(KitchenSinkCtor ctor, KitchenSink fromJson(Map json),
    {bool isChecked = false}) {
  void roundTripSink(KitchenSink p) {
    roundTripObject(p, fromJson);
  }
 
  test('empty', () {
    final item = ctor();
    roundTripSink(item);
  });
 
  test('list and map of DateTime - not null', () {
    final now = DateTime.now();
    final item = ctor(dateTimeIterable: <DateTime>[now])
      ..dateTimeList = <DateTime>[now, now]
      ..objectDateTimeMap = <Object, DateTime>{'value': now};
 
    roundTripSink(item);
  });
 
  test('complex nested type - not null', () {
    final item = ctor()
      ..crazyComplex = [
        {},
        {
          'empty': {},
          'items': {
            'empty': [],
            'items': [
              [],
              [DateTime.now()]
            ]
          }
        }
      ];
    roundTripSink(item);
  });
 
  test('JSON keys should be defined in field/property order', () {
    /// Explicitly setting values from [_excludeIfNullKeys] to ensure
    /// they exist for KitchenSink where they are excluded when null
    final item = ctor(iterable: [])
      ..dateTime = DateTime.now()
      ..dateTimeList = []
      ..crazyComplex = []
      ..val = {};
 
    final json = item.toJson();
    expect(json.keys, orderedEquals(_validValues.keys));
  });
 
  test('valid values round-trip - json', () {
    expect(loudEncode(_validValues), loudEncode(fromJson(_validValues)));
  });
 
  test('valid values round-trip - yaml', () {
    final jsonEncoded = loudEncode(_validValues);
    final yaml = loadYaml(jsonEncoded, sourceUrl: 'input.yaml');
    expect(jsonEncoded, loudEncode(fromJson(yaml as YamlMap)));
  });
 
  group('a bad value for', () {
    for (final e in _invalidValueTypes.entries) {
      _testBadValue(isChecked, e.key, e.value, fromJson, false);
    }
    for (final e in _invalidCheckedValues.entries) {
      _testBadValue(isChecked, e.key, e.value, fromJson, true);
    }
  });
}
 
void _testBadValue(bool isChecked, String key, Object badValue,
    KitchenSink fromJson(Map json), bool checkedAssignment) {
  final matcher = _getMatcher(isChecked, key, checkedAssignment);
 
  for (final isJson in [true, false]) {
    test('`$key` fails with value `$badValue`- ${isJson ? 'json' : 'yaml'}',
        () {
      var copy = Map.from(_validValues);
      copy[key] = badValue;
 
      if (!isJson) {
        copy = loadYaml(loudEncode(copy)) as YamlMap;
      }
 
      expect(() => fromJson(copy), matcher);
    });
  }
}
 
Matcher _checkedMatcher(String expectedKey) =>
    const TypeMatcher<CheckedFromJsonException>()
        .having((e) => e.className, 'className', 'KitchenSink')
        .having((e) => e.key, 'key', expectedKey);
 
Matcher _getMatcher(bool checked, String expectedKey, bool checkedAssignment) {
  Matcher innerMatcher;
 
  if (checked) {
    if (checkedAssignment &&
        const ['intIterable', 'datetime-iterable'].contains(expectedKey)) {
      expectedKey = null;
    }
 
    innerMatcher = _checkedMatcher(expectedKey);
  } else {
    innerMatcher = anyOf(
        isCastError,
        _isATypeError,
        _isAUnrecognizedKeysEexception(
            'Unrecognized keys: [invalid_key]; supported keys: [value, custom_field]'));
 
    if (checkedAssignment) {
      switch (expectedKey) {
        case 'validatedPropertyNo42':
          innerMatcher = isStateError;
          break;
        case 'no-42':
          innerMatcher = isArgumentError;
          break;
        case 'strictKeysObject':
          innerMatcher = _isAUnrecognizedKeysEexception('bob');
          break;
        case 'intIterable':
        case 'datetime-iterable':
          innerMatcher = isCastError;
          break;
        default:
          throw StateError('Not expected! - $expectedKey');
      }
    }
  }
 
  return throwsA(innerMatcher);
}
 
final _validValues = const {
  'no-42': 0,
  'dateTime': '2018-05-10T14:20:58.927',
  'iterable': [],
  'dynamicIterable': [],
  'objectIterable': [],
  'intIterable': [],
  'set': [],
  'dynamicSet': [],
  'objectSet': [],
  'intSet': [],
  'dateTimeSet': [],
  'datetime-iterable': [],
  'list': [],
  'dynamicList': [],
  'objectList': [],
  'intList': [],
  'dateTimeList': [],
  'map': <String, dynamic>{},
  'stringStringMap': {},
  'dynamicIntMap': {},
  'objectDateTimeMap': <String, dynamic>{},
  'crazyComplex': [],
  _generatedLocalVarName: {},
  _toJsonMapHelperName: null,
  r'$string': null,
  'simpleObject': {'value': 42},
  'strictKeysObject': {'value': 10, 'custom_field': 'cool'},
  'validatedPropertyNo42': 0
};
 
final _invalidValueTypes = const {
  'no-42': true,
  'dateTime': true,
  'iterable': true,
  'dynamicIterable': true,
  'objectIterable': true,
  'intIterable': true,
  'set': true,
  'dynamicSet': true,
  'objectSet': true,
  'intSet': true,
  'dateTimeSet': true,
  'datetime-iterable': true,
  'list': true,
  'dynamicList': true,
  'objectList': true,
  'intList': [true],
  'dateTimeList': [true],
  'map': true,
  'stringStringMap': {'key': 42},
  'dynamicIntMap': {'key': 'value'},
  'objectDateTimeMap': {'key': 42},
  'crazyComplex': [true],
  _generatedLocalVarName: {'key': 42},
  _toJsonMapHelperName: 42,
  r'$string': true,
  'simpleObject': 42,
  'strictKeysObject': {
    'value': 10,
    'invalid_key': true,
  },
  'validatedPropertyNo42': true
};
 
/// Invalid values that are found after the property set or ctor call
final _invalidCheckedValues = const {
  'no-42': 42,
  'validatedPropertyNo42': 42,
  'intIterable': [true],
  'datetime-iterable': [true],
};
 
final _excludeIfNullKeys = const [
  'dateTime',
  'iterable',
  'dateTimeList',
  'crazyComplex',
  _generatedLocalVarName
];