From 8598ea426de7f0239bedce94e9811a16837a8e63 Mon Sep 17 00:00:00 2001 From: Jan Romann Date: Sat, 23 Dec 2023 01:37:29 +0100 Subject: [PATCH] fixup! feat: improve DataSchemaValue handling --- test/core/content_serdes_test.dart | 209 ++++++++++++++--------------- 1 file changed, 103 insertions(+), 106 deletions(-) diff --git a/test/core/content_serdes_test.dart b/test/core/content_serdes_test.dart index 21812375..c7cd67da 100644 --- a/test/core/content_serdes_test.dart +++ b/test/core/content_serdes_test.dart @@ -17,112 +17,109 @@ Content _getTestContent(String input) { void main() { group('Content Serdes Tests', () { - setUp(() { - // Additional setup goes here. + test('Content Validation', () async { + final contentSerdes = ContentSerdes(); + + final testContent1 = _getTestContent('42'); + final successfulSchema = DataSchema.fromJson( + {'type': 'number'}, + PrefixMapping(), + ); + + expect( + await contentSerdes.contentToValue(testContent1, successfulSchema), + 42, + ); + + final testContent2 = _getTestContent('42'); + final failingSchema = DataSchema.fromJson( + {'type': 'string'}, + PrefixMapping(), + ); + + expect( + contentSerdes.contentToValue(testContent2, failingSchema), + throwsA(const TypeMatcher()), + ); + + expect( + () => + contentSerdes.valueToContent(42, failingSchema, 'application/json'), + throwsA(const TypeMatcher()), + ); + + final testContent3 = _getTestContent(''); + expect( + await contentSerdes.contentToValue(testContent3, null), + null, + ); + }); + test('Codec Registration', () async { + final contentSerdes = ContentSerdes(); + + expect( + contentSerdes.supportedMediaTypes, + ['application/json', 'application/cbor', 'application/link-format'], + ); + + expect( + contentSerdes.offeredMediaTypes, + ['application/json', 'application/cbor'], + ); + + expect( + () => contentSerdes.addOfferedMediaType('application/xml'), + throwsArgumentError, + ); + + contentSerdes.addOfferedMediaType('application/td+json; charset=utf-8'); + + expect( + contentSerdes.offeredMediaTypes, + [ + 'application/json', + 'application/cbor', + 'application/td+json; charset=utf-8', + ], + ); + + contentSerdes.removeOfferedMediaType('application/json'); + + expect( + contentSerdes.offeredMediaTypes, + [ + 'application/cbor', + 'application/td+json; charset=utf-8', + ], + ); + + contentSerdes + ..assignCodec('application/xml', JsonCodec()) + ..addOfferedMediaType('application/xml'); + + expect( + contentSerdes.supportedMediaTypes, + [ + 'application/json', + 'application/cbor', + 'application/link-format', + 'application/xml', + ], + ); + + expect( + contentSerdes.offeredMediaTypes, + [ + 'application/cbor', + 'application/td+json; charset=utf-8', + 'application/xml', + ], + ); + + expect( + () => contentSerdes.assignCodec('foo', JsonCodec()), + throwsArgumentError, + ); }); - }); - - test('Content Validation', () async { - final contentSerdes = ContentSerdes(); - - final testContent1 = _getTestContent('42'); - final successfulSchema = DataSchema.fromJson( - {'type': 'number'}, - PrefixMapping(), - ); - - expect( - await contentSerdes.contentToValue(testContent1, successfulSchema), - 42, - ); - - final testContent2 = _getTestContent('42'); - final failingSchema = DataSchema.fromJson( - {'type': 'string'}, - PrefixMapping(), - ); - - expect( - contentSerdes.contentToValue(testContent2, failingSchema), - throwsA(const TypeMatcher()), - ); - - expect( - () => contentSerdes.valueToContent(42, failingSchema, 'application/json'), - throwsA(const TypeMatcher()), - ); - - final testContent3 = _getTestContent(''); - expect( - await contentSerdes.contentToValue(testContent3, null), - null, - ); - }); - test('Codec Registration', () async { - final contentSerdes = ContentSerdes(); - - expect( - contentSerdes.supportedMediaTypes, - ['application/json', 'application/cbor', 'application/link-format'], - ); - - expect( - contentSerdes.offeredMediaTypes, - ['application/json', 'application/cbor'], - ); - - expect( - () => contentSerdes.addOfferedMediaType('application/xml'), - throwsArgumentError, - ); - - contentSerdes.addOfferedMediaType('application/td+json; charset=utf-8'); - - expect( - contentSerdes.offeredMediaTypes, - [ - 'application/json', - 'application/cbor', - 'application/td+json; charset=utf-8', - ], - ); - - contentSerdes.removeOfferedMediaType('application/json'); - - expect( - contentSerdes.offeredMediaTypes, - [ - 'application/cbor', - 'application/td+json; charset=utf-8', - ], - ); - - contentSerdes - ..assignCodec('application/xml', JsonCodec()) - ..addOfferedMediaType('application/xml'); - - expect( - contentSerdes.supportedMediaTypes, - [ - 'application/json', - 'application/cbor', - 'application/link-format', - 'application/xml', - ], - ); - - expect( - contentSerdes.offeredMediaTypes, - [ - 'application/cbor', - 'application/td+json; charset=utf-8', - 'application/xml', - ], - ); - - expect( - () => contentSerdes.assignCodec('foo', JsonCodec()), - throwsArgumentError, - ); }); }