41c50d9ac9
+ add tests for various fixes + add canonicalization support in serialization Change-Id: Ib6a8bfd6073dbd93c1ba4fd9d2feff77ac43b178 Reviewed-on: https://dart-review.googlesource.com/c/81402 Commit-Queue: Johnni Winther <johnniwinther@google.com> Auto-Submit: Johnni Winther <johnniwinther@google.com> Reviewed-by: Sigmund Cherem <sigmund@google.com>
72 lines
1.9 KiB
Dart
72 lines
1.9 KiB
Dart
// Copyright (c) 2018, 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.
|
|
|
|
part of 'serialization.dart';
|
|
|
|
/// [DataSource] that read from a list of objects, useful for debugging
|
|
/// inconsistencies between serialization and deserialization.
|
|
///
|
|
/// This data source works together with [ObjectSink].
|
|
class ObjectSource extends AbstractDataSource {
|
|
int _index = 0;
|
|
final List<dynamic> _data;
|
|
|
|
ObjectSource(this._data, {bool useDataKinds})
|
|
: super(useDataKinds: useDataKinds);
|
|
|
|
T _read<T>() {
|
|
dynamic value = _data[_index++];
|
|
assert(value is T, "Expected $T value, found $value.$_errorContext");
|
|
return value;
|
|
}
|
|
|
|
void _begin(String tag) {
|
|
Tag expectedTag = new Tag('begin:$tag');
|
|
Tag actualTag = _read();
|
|
assert(
|
|
expectedTag == actualTag,
|
|
"Unexpected begin tag. "
|
|
"Expected $expectedTag, found $actualTag.$_errorContext");
|
|
}
|
|
|
|
void _end(String tag) {
|
|
Tag expectedTag = new Tag('end:$tag');
|
|
Tag actualTag = _read();
|
|
assert(
|
|
expectedTag == actualTag,
|
|
"Unexpected end tag. "
|
|
"Expected $expectedTag, found $actualTag.$_errorContext");
|
|
}
|
|
|
|
@override
|
|
String _readStringInternal() => _read();
|
|
|
|
@override
|
|
E _readEnumInternal<E>(List<E> values) => _read();
|
|
|
|
@override
|
|
Uri _readUriInternal() => _read();
|
|
|
|
@override
|
|
int _readIntInternal() => _read();
|
|
|
|
@override
|
|
String get _errorContext {
|
|
StringBuffer sb = new StringBuffer();
|
|
for (int i = _index - 50; i < _index + 10; i++) {
|
|
if (i >= 0 && i < _data.length) {
|
|
if (i == _index - 1) {
|
|
sb.write('\n> ');
|
|
} else {
|
|
sb.write('\n ');
|
|
}
|
|
sb.write(i);
|
|
sb.write(' ');
|
|
sb.write(_data[i]);
|
|
}
|
|
}
|
|
return sb.toString();
|
|
}
|
|
}
|