|
6 | 6 | using System;
|
7 | 7 | using System.Runtime.Serialization;
|
8 | 8 | using MessagePack.Resolvers;
|
| 9 | +using Nerdbank.Streams; |
9 | 10 | using Xunit;
|
10 | 11 | using Xunit.Abstractions;
|
11 | 12 |
|
@@ -65,6 +66,182 @@ private void PrivateMembersInBaseClass_Helper(MessagePackSerializerOptions optio
|
65 | 66 | Assert.Equal(obj.Name, obj2.Name);
|
66 | 67 | }
|
67 | 68 |
|
| 69 | + [Fact] |
| 70 | + public void DefaultValueStringKeyClassWithoutExplicitConstructorTest() |
| 71 | + { |
| 72 | + var seq = new Sequence<byte>(); |
| 73 | + var writer = new MessagePackWriter(seq); |
| 74 | + writer.WriteMapHeader(0); |
| 75 | + writer.Flush(); |
| 76 | + |
| 77 | + var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyClassWithoutExplicitConstructor>(seq); |
| 78 | + Assert.Equal(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop1Constant, instance.Prop1); |
| 79 | + Assert.Equal(DefaultValueStringKeyClassWithoutExplicitConstructor.Prop2Constant, instance.Prop2); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void DefaultValueStringKeyClassWithExplicitConstructorTest() |
| 84 | + { |
| 85 | + var seq = new Sequence<byte>(); |
| 86 | + var writer = new MessagePackWriter(seq); |
| 87 | + writer.WriteMapHeader(1); |
| 88 | + writer.Write(nameof(DefaultValueStringKeyClassWithExplicitConstructor.Prop1)); |
| 89 | + writer.Write(-1); |
| 90 | + writer.Flush(); |
| 91 | + |
| 92 | + var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyClassWithExplicitConstructor>(seq); |
| 93 | + Assert.Equal(-1, instance.Prop1); |
| 94 | + Assert.Equal(DefaultValueStringKeyClassWithExplicitConstructor.Prop2Constant, instance.Prop2); |
| 95 | + } |
| 96 | + |
| 97 | + [Fact] |
| 98 | + public void DefaultValueStringKeyStructWithExplicitConstructorTest() |
| 99 | + { |
| 100 | + var seq = new Sequence<byte>(); |
| 101 | + var writer = new MessagePackWriter(seq); |
| 102 | + writer.WriteMapHeader(1); |
| 103 | + writer.Write(nameof(DefaultValueStringKeyStructWithExplicitConstructor.Prop1)); |
| 104 | + writer.Write(-1); |
| 105 | + writer.Flush(); |
| 106 | + |
| 107 | + var instance = MessagePackSerializer.Deserialize<DefaultValueStringKeyStructWithExplicitConstructor>(seq); |
| 108 | + Assert.Equal(-1, instance.Prop1); |
| 109 | + Assert.Equal(DefaultValueStringKeyStructWithExplicitConstructor.Prop2Constant, instance.Prop2); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void DefaultValueIntKeyClassWithoutExplicitConstructorTest() |
| 114 | + { |
| 115 | + var seq = new Sequence<byte>(); |
| 116 | + var writer = new MessagePackWriter(seq); |
| 117 | + writer.WriteArrayHeader(0); |
| 118 | + writer.Flush(); |
| 119 | + |
| 120 | + var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyClassWithoutExplicitConstructor>(seq); |
| 121 | + Assert.Equal(DefaultValueIntKeyClassWithoutExplicitConstructor.Prop1Constant, instance.Prop1); |
| 122 | + Assert.Equal(DefaultValueIntKeyClassWithoutExplicitConstructor.Prop2Constant, instance.Prop2); |
| 123 | + } |
| 124 | + |
| 125 | + [Fact] |
| 126 | + public void DefaultValueIntKeyClassWithExplicitConstructorTest() |
| 127 | + { |
| 128 | + var seq = new Sequence<byte>(); |
| 129 | + var writer = new MessagePackWriter(seq); |
| 130 | + writer.WriteArrayHeader(1); |
| 131 | + writer.Write(-1); |
| 132 | + writer.Flush(); |
| 133 | + |
| 134 | + var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyClassWithExplicitConstructor>(seq); |
| 135 | + Assert.Equal(-1, instance.Prop1); |
| 136 | + Assert.Equal(DefaultValueIntKeyClassWithExplicitConstructor.Prop2Constant, instance.Prop2); |
| 137 | + } |
| 138 | + |
| 139 | + [Fact] |
| 140 | + public void DefaultValueIntKeyStructWithExplicitConstructorTest() |
| 141 | + { |
| 142 | + var seq = new Sequence<byte>(); |
| 143 | + var writer = new MessagePackWriter(seq); |
| 144 | + writer.WriteArrayHeader(1); |
| 145 | + writer.Write(-1); |
| 146 | + writer.Flush(); |
| 147 | + |
| 148 | + var instance = MessagePackSerializer.Deserialize<DefaultValueIntKeyStructWithExplicitConstructor>(seq); |
| 149 | + Assert.Equal(-1, instance.Prop1); |
| 150 | + Assert.Equal(DefaultValueIntKeyStructWithExplicitConstructor.Prop2Constant, instance.Prop2); |
| 151 | + } |
| 152 | + |
| 153 | + [MessagePackObject(true)] |
| 154 | + public class DefaultValueStringKeyClassWithoutExplicitConstructor |
| 155 | + { |
| 156 | + public const int Prop1Constant = 11; |
| 157 | + public const int Prop2Constant = 45; |
| 158 | + |
| 159 | + public int Prop1 { get; set; } = Prop1Constant; |
| 160 | + |
| 161 | + public int Prop2 { get; set; } = Prop2Constant; |
| 162 | + } |
| 163 | + |
| 164 | + [MessagePackObject(true)] |
| 165 | + public class DefaultValueStringKeyClassWithExplicitConstructor |
| 166 | + { |
| 167 | + public const int Prop2Constant = 1419; |
| 168 | + |
| 169 | + public int Prop1 { get; set; } |
| 170 | + |
| 171 | + public int Prop2 { get; set; } |
| 172 | + |
| 173 | + public DefaultValueStringKeyClassWithExplicitConstructor(int prop1) |
| 174 | + { |
| 175 | + Prop1 = prop1; |
| 176 | + Prop2 = Prop2Constant; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + [MessagePackObject(true)] |
| 181 | + public struct DefaultValueStringKeyStructWithExplicitConstructor |
| 182 | + { |
| 183 | + public const int Prop2Constant = 198; |
| 184 | + |
| 185 | + public int Prop1 { get; set; } |
| 186 | + |
| 187 | + public int Prop2 { get; set; } |
| 188 | + |
| 189 | + public DefaultValueStringKeyStructWithExplicitConstructor(int prop1) |
| 190 | + { |
| 191 | + Prop1 = prop1; |
| 192 | + Prop2 = Prop2Constant; |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + [MessagePackObject] |
| 197 | + public class DefaultValueIntKeyClassWithoutExplicitConstructor |
| 198 | + { |
| 199 | + public const int Prop1Constant = 33; |
| 200 | + public const int Prop2Constant = -4; |
| 201 | + |
| 202 | + [Key(0)] |
| 203 | + public int Prop1 { get; set; } = Prop1Constant; |
| 204 | + |
| 205 | + [Key(1)] |
| 206 | + public int Prop2 { get; set; } = Prop2Constant; |
| 207 | + } |
| 208 | + |
| 209 | + [MessagePackObject] |
| 210 | + public class DefaultValueIntKeyClassWithExplicitConstructor |
| 211 | + { |
| 212 | + public const int Prop2Constant = -109; |
| 213 | + |
| 214 | + [Key(0)] |
| 215 | + public int Prop1 { get; set; } |
| 216 | + |
| 217 | + [Key(1)] |
| 218 | + public int Prop2 { get; set; } |
| 219 | + |
| 220 | + public DefaultValueIntKeyClassWithExplicitConstructor(int prop1) |
| 221 | + { |
| 222 | + Prop1 = prop1; |
| 223 | + Prop2 = Prop2Constant; |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + [MessagePackObject] |
| 228 | + public struct DefaultValueIntKeyStructWithExplicitConstructor |
| 229 | + { |
| 230 | + public const int Prop2Constant = 31; |
| 231 | + |
| 232 | + [Key(0)] |
| 233 | + public int Prop1 { get; set; } |
| 234 | + |
| 235 | + [Key(1)] |
| 236 | + public int Prop2 { get; set; } |
| 237 | + |
| 238 | + public DefaultValueIntKeyStructWithExplicitConstructor(int prop1) |
| 239 | + { |
| 240 | + Prop1 = prop1; |
| 241 | + Prop2 = Prop2Constant; |
| 242 | + } |
| 243 | + } |
| 244 | + |
68 | 245 | [MessagePackObject]
|
69 | 246 | public class TestMessageWithReadOnlyField
|
70 | 247 | {
|
|
0 commit comments