From aacefd312c9a863a397c9cdad45817c234d24630 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Sun, 10 Sep 2023 00:04:31 +0200 Subject: [PATCH] test(dynamite_runtime): test serialization failure Signed-off-by: Nikolas Rimikis --- .../lib/src/content_string.dart | 4 ++-- .../test/content_string_test.dart | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/dynamite/dynamite_runtime/lib/src/content_string.dart b/packages/dynamite/dynamite_runtime/lib/src/content_string.dart index 885d7ce9..af7118b1 100644 --- a/packages/dynamite/dynamite_runtime/lib/src/content_string.dart +++ b/packages/dynamite/dynamite_runtime/lib/src/content_string.dart @@ -28,7 +28,7 @@ class ContentStringPlugin implements SerializerPlugin { } if (object is! Map) { - throw ArgumentError('ContentStringPlugin can only be applied to Map. ' + throw StateError('ContentStringPlugin can only be applied to Map. ' 'Please ensure the StandardJsonPlugin is applied and run before.'); } @@ -44,7 +44,7 @@ class ContentStringPlugin implements SerializerPlugin { } if (object is! String) { - throw ArgumentError('The serialized ContentString must be of type String. ' + throw StateError('The serialized ContentString must be of type String. ' 'Please ensure the StandardJsonPlugin is applied and run before.'); } diff --git a/packages/dynamite/dynamite_runtime/test/content_string_test.dart b/packages/dynamite/dynamite_runtime/test/content_string_test.dart index 331e98dd..c80e2214 100644 --- a/packages/dynamite/dynamite_runtime/test/content_string_test.dart +++ b/packages/dynamite/dynamite_runtime/test/content_string_test.dart @@ -42,6 +42,11 @@ final Serializers serializers = (_$serializers.toBuilder() )) .build(); +final Serializers invalidSerializers = (_$serializers.toBuilder() + ..addPlugin(const ContentStringPlugin()) + ..addBuilderFactory(const FullType(ContentString, [FullType(bool)]), ContentStringBuilder.new)) + .build(); + void main() { group('ContentString with known specifiedType holding bool', () { final data = ContentString((final b) => b..content = true); @@ -310,4 +315,21 @@ void main() { expect(serializers.deserialize(serialized, specifiedType: specifiedType), data); }); }); + + group('Serialize without registered StandardJsonPlugin', () { + final data = ContentString((final b) => b..content = true); + const serialized = true; + const specifiedType = FullType(ContentString, [FullType(bool)]); + + test('serialization error', () { + expect(() => invalidSerializers.serialize(data, specifiedType: specifiedType), throwsA(isA())); + }); + + test('deserialization error', () { + expect( + () => invalidSerializers.deserialize(serialized, specifiedType: specifiedType), + throwsA(isA()), + ); + }); + }); }