platform/packages/json_god/test/to_json_test.dart

33 lines
707 B
Dart
Raw Normal View History

2021-05-14 12:24:45 +00:00
import 'package:angel3_json_god/angel3_json_god.dart' as god;
2021-03-07 15:56:09 +00:00
import 'package:test/test.dart';
import 'shared.dart';
main() {
god.logger.onRecord.listen(printRecord);
test('fromJson', () {
var foo = god.deserialize('{"bar":"baz"}', outputType: Foo) as Foo;
expect(foo is Foo, true);
expect(foo.text, equals('baz'));
});
test('toJson', () {
2021-05-14 12:24:45 +00:00
var foo = Foo(text: 'baz');
2021-03-07 15:56:09 +00:00
var data = god.serializeObject(foo);
expect(data, equals({'bar': 'baz', 'foo': 'poobaz'}));
});
}
class Foo {
2021-04-10 12:42:55 +00:00
String? text;
2021-03-07 15:56:09 +00:00
String get foo => 'poo$text';
Foo({this.text});
2021-05-14 12:24:45 +00:00
factory Foo.fromJson(Map json) => Foo(text: json['bar'].toString());
2021-03-07 15:56:09 +00:00
Map toJson() => {'bar': text, 'foo': foo};
}