platform/angel_serialize_generator/test/enum_test.dart

60 lines
1.7 KiB
Dart
Raw Normal View History

2018-12-08 20:53:49 +00:00
import 'dart:typed_data';
2018-06-27 05:36:57 +00:00
import 'package:test/test.dart';
import 'models/with_enum.dart';
2019-04-30 15:44:01 +00:00
const WithEnum aWithEnum = WithEnum(type: WithEnumType.a);
const WithEnum aWithEnum2 = WithEnum(type: WithEnumType.a);
2018-06-27 05:36:57 +00:00
void main() {
test('enum serializes to int', () {
2019-04-30 15:44:01 +00:00
var w = WithEnum(type: WithEnumType.b).toJson();
2018-06-27 05:36:57 +00:00
expect(w[WithEnumFields.type], WithEnumType.values.indexOf(WithEnumType.b));
});
test('enum serializes null if null', () {
2019-04-30 15:44:01 +00:00
var w = WithEnum(type: null).toJson();
2018-06-27 05:36:57 +00:00
expect(w[WithEnumFields.type], null);
});
2019-04-08 04:41:58 +00:00
test('enum deserializes to default value from null', () {
2018-06-27 05:36:57 +00:00
var map = {WithEnumFields.type: null};
var w = WithEnumSerializer.fromMap(map);
2019-04-08 04:41:58 +00:00
expect(w.type, WithEnumType.b);
2018-06-27 05:36:57 +00:00
});
test('enum deserializes from int', () {
var map = {
WithEnumFields.type: WithEnumType.values.indexOf(WithEnumType.b)
};
var w = WithEnumSerializer.fromMap(map);
expect(w.type, WithEnumType.b);
});
test('enum deserializes from value', () {
var map = {WithEnumFields.type: WithEnumType.c};
var w = WithEnumSerializer.fromMap(map);
expect(w.type, WithEnumType.c);
});
test('equality', () {
2019-04-30 15:44:01 +00:00
expect(WithEnum(type: WithEnumType.a), WithEnum(type: WithEnumType.a));
2018-06-27 05:36:57 +00:00
expect(
2019-04-30 15:44:01 +00:00
WithEnum(type: WithEnumType.a), isNot(WithEnum(type: WithEnumType.b)));
2018-06-27 05:36:57 +00:00
});
test('const', () {
expect(identical(aWithEnum, aWithEnum2), true);
});
2018-12-08 20:53:49 +00:00
test('uint8list', () {
2019-04-30 15:44:01 +00:00
var ee = WithEnum(
imageBytes: Uint8List.fromList(List<int>.generate(1000, (i) => i)));
2018-12-08 20:53:49 +00:00
var eeMap = ee.toJson();
2019-04-08 15:00:04 +00:00
print(ee);
2018-12-08 20:53:49 +00:00
var ef = WithEnumSerializer.fromMap(eeMap);
expect(ee.copyWith(), ee);
expect(ef, ee);
});
2018-06-27 05:36:57 +00:00
}