-
-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathNestedUnionTest.cs
More file actions
39 lines (33 loc) · 1.09 KB
/
NestedUnionTest.cs
File metadata and controls
39 lines (33 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace MemoryPack.Tests;
[MemoryPackable]
public partial record NestedUnion
{
[MemoryPackable]
[MemoryPackUnion(0, typeof(NestedUnionA))]
[MemoryPackUnion(1, typeof(NestedUnionB))]
public partial interface INestedUnion
{
}
[MemoryPackable]
public partial record NestedUnionA(string Value) : INestedUnion;
[MemoryPackable]
public partial record NestedUnionB(string Value) : INestedUnion;
}
[MemoryPackable]
public partial record NestedUnionContainer
{
public required NestedUnion.INestedUnion NestedUnion { get; init; }
}
public class NestedUnionTest
{
[Fact]
public void CanSerializeNestedUnion()
{
var data = new NestedUnionContainer { NestedUnion = new NestedUnion.NestedUnionB("Foo") };
var bytes = MemoryPackSerializer.Serialize(data);
var result = MemoryPackSerializer.Deserialize<NestedUnionContainer>(bytes);
result.Should().NotBeNull();
result?.NestedUnion.Should().BeOfType<NestedUnion.NestedUnionB>();
(result?.NestedUnion as NestedUnion.NestedUnionB)?.Value.Should().Be("Foo");
}
}