|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +library; |
| 21 | + |
| 22 | +import 'package:checks/checks.dart'; |
| 23 | +import 'package:fory/src/collection/stack.dart'; |
| 24 | +import 'package:fory/src/config/fory_config.dart'; |
| 25 | +import 'package:fory/src/deserialization_dispatcher.dart'; |
| 26 | +import 'package:fory/src/deserialization_context.dart'; |
| 27 | +import 'package:fory/src/fory_exception.dart'; |
| 28 | +import 'package:fory/src/memory/byte_reader.dart'; |
| 29 | +import 'package:fory/src/memory/byte_writer.dart'; |
| 30 | +import 'package:fory/src/meta/spec_wraps/type_spec_wrap.dart'; |
| 31 | +import 'package:fory/src/resolver/deserialization_ref_resolver.dart'; |
| 32 | +import 'package:fory/src/resolver/meta_string_writing_resolver.dart'; |
| 33 | +import 'package:fory/src/resolver/serialization_ref_resolver.dart'; |
| 34 | +import 'package:fory/src/resolver/struct_hash_resolver.dart'; |
| 35 | +import 'package:fory/src/resolver/type_resolver.dart'; |
| 36 | +import 'package:fory/src/serialization_dispatcher.dart'; |
| 37 | +import 'package:fory/src/serialization_context.dart'; |
| 38 | +import 'package:fory/src/serializer/enum_serializer.dart'; |
| 39 | +import 'package:fory_test/entity/enum_id_foo.dart'; |
| 40 | +import 'package:test/test.dart'; |
| 41 | + |
| 42 | +String _unusedTagLookup(Type _) => ''; |
| 43 | + |
| 44 | +final ForyConfig _config = ForyConfig(); |
| 45 | +final TypeResolver _typeResolver = TypeResolver.newOne(_config); |
| 46 | + |
| 47 | +SerializationContext _newSerializationContext() { |
| 48 | + return SerializationContext( |
| 49 | + StructHashResolver.inst, |
| 50 | + _unusedTagLookup, |
| 51 | + SerializationDispatcher.I, |
| 52 | + _typeResolver, |
| 53 | + SerializationRefResolver.getOne(false), |
| 54 | + SerializationRefResolver.noRefResolver, |
| 55 | + MetaStringWritingResolver.newInst, |
| 56 | + Stack<TypeSpecWrap>(), |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +DeserializationContext _newDeserializationContext() { |
| 61 | + return DeserializationContext( |
| 62 | + StructHashResolver.inst, |
| 63 | + _unusedTagLookup, |
| 64 | + _config, |
| 65 | + (isXLang: true, oobEnabled: false), |
| 66 | + DeserializationDispatcher.I, |
| 67 | + DeserializationRefResolver.getOne(false), |
| 68 | + _typeResolver, |
| 69 | + Stack<TypeSpecWrap>(), |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +void main() { |
| 74 | + group('Enum serializer', () { |
| 75 | + test('writes and reads annotated enum ids when all values are annotated', |
| 76 | + () { |
| 77 | + final EnumSerializer serializer = EnumSerializer(false, [ |
| 78 | + EnumWithIds.A, |
| 79 | + EnumWithIds.B, |
| 80 | + EnumWithIds.C, |
| 81 | + ], { |
| 82 | + 10: EnumWithIds.A, |
| 83 | + 20: EnumWithIds.B, |
| 84 | + 30: EnumWithIds.C, |
| 85 | + }); |
| 86 | + |
| 87 | + final ByteWriter writer = ByteWriter(); |
| 88 | + serializer.write( |
| 89 | + writer, |
| 90 | + EnumWithIds.B, |
| 91 | + _newSerializationContext(), |
| 92 | + ); |
| 93 | + final ByteReader encodedIdReader = |
| 94 | + ByteReader.forBytes(writer.takeBytes()); |
| 95 | + check(encodedIdReader.readVarUint32Small7()).equals(20); |
| 96 | + |
| 97 | + final ByteWriter idWriter = ByteWriter(); |
| 98 | + idWriter.writeVarUint32Small7(30); |
| 99 | + final Enum value = serializer.read( |
| 100 | + ByteReader.forBytes(idWriter.takeBytes()), |
| 101 | + 0, |
| 102 | + _newDeserializationContext(), |
| 103 | + ); |
| 104 | + check(value).equals(EnumWithIds.C); |
| 105 | + }); |
| 106 | + |
| 107 | + test('falls back to ordinal serialization when id mapping is absent', () { |
| 108 | + final EnumSerializer serializer = EnumSerializer(false, [ |
| 109 | + EnumPartialIds.A, |
| 110 | + EnumPartialIds.B, |
| 111 | + EnumPartialIds.C, |
| 112 | + ]); |
| 113 | + |
| 114 | + final ByteWriter writer = ByteWriter(); |
| 115 | + serializer.write( |
| 116 | + writer, |
| 117 | + EnumPartialIds.B, |
| 118 | + _newSerializationContext(), |
| 119 | + ); |
| 120 | + final ByteReader encodedIdReader = |
| 121 | + ByteReader.forBytes(writer.takeBytes()); |
| 122 | + check(encodedIdReader.readVarUint32Small7()).equals(1); |
| 123 | + }); |
| 124 | + |
| 125 | + test('throws on unknown annotated enum id', () { |
| 126 | + final EnumSerializer serializer = EnumSerializer(false, [ |
| 127 | + EnumWithIds.A, |
| 128 | + EnumWithIds.B, |
| 129 | + EnumWithIds.C, |
| 130 | + ], { |
| 131 | + 10: EnumWithIds.A, |
| 132 | + 20: EnumWithIds.B, |
| 133 | + 30: EnumWithIds.C, |
| 134 | + }); |
| 135 | + |
| 136 | + final ByteWriter writer = ByteWriter(); |
| 137 | + writer.writeVarUint32Small7(99); |
| 138 | + |
| 139 | + check( |
| 140 | + () => serializer.read( |
| 141 | + ByteReader.forBytes(writer.takeBytes()), |
| 142 | + 0, |
| 143 | + _newDeserializationContext(), |
| 144 | + ), |
| 145 | + ).throws<DeserializationRangeException>(); |
| 146 | + }); |
| 147 | + }); |
| 148 | +} |
0 commit comments