-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTest1.cs
179 lines (144 loc) · 4.39 KB
/
UnitTest1.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using Sera.TaggedUnion;
namespace Tests;
public partial class Tests
{
[SetUp]
public void Setup() { }
[Test]
public void Test1()
{
var u = Union1.MakeA(123);
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.A));
Assert.That(u.A, Is.EqualTo(123));
u = Union1.MakeB("asd");
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.B));
Assert.That(u.B, Is.EqualTo("asd"));
u = Union1.MakeC(true);
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.C));
Assert.That(u.C, Is.True);
u = Union1.MakeD((1, 2));
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.D));
Assert.That(u.D, Is.EqualTo((1, 2)));
u = Union1.MakeE();
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.E));
u = Union1.MakeF(null);
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.F));
Assert.That(u.F, Is.Null);
var l = new List<int>();
u = Union1.MakeF(l);
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.F));
Assert.That(u.F, Is.EqualTo(l));
u = Union1.MakeG((123, "asd"));
Assert.That(u.Tag, Is.EqualTo(Union1.Tags.G));
Assert.That(u.G, Is.EqualTo((123, "asd")));
u = Union1.MakeA(123);
Assert.That(u.B, Is.EqualTo(null));
}
[Union]
public partial struct Union2
{
[UnionTemplate]
private abstract class Template
{
public abstract int A();
public abstract string B();
public abstract bool C();
public abstract (int a, int b) D();
public abstract void E();
public abstract List<int>? F();
public abstract (int a, string b) G();
}
}
[Test]
public void Test2()
{
var u = Union2.MakeA(123);
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.A));
Assert.That(u.A, Is.EqualTo(123));
u = Union2.MakeB("asd");
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.B));
Assert.That(u.B, Is.EqualTo("asd"));
u = Union2.MakeC(true);
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.C));
Assert.That(u.C, Is.True);
u = Union2.MakeD((1, 2));
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.D));
Assert.That(u.D, Is.EqualTo((1, 2)));
u = Union2.MakeE();
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.E));
u = Union2.MakeF(null);
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.F));
Assert.That(u.F, Is.Null);
var l = new List<int>();
u = Union2.MakeF(l);
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.F));
Assert.That(u.F, Is.EqualTo(l));
u = Union2.MakeG((123, "asd"));
Assert.That(u.Tag, Is.EqualTo(Union2.Tags.G));
Assert.That(u.G, Is.EqualTo((123, "asd")));
u = Union2.MakeA(123);
Assert.That(u.B, Is.EqualTo(null));
u.A = 456;
Assert.That(u.A, Is.EqualTo(456));
}
[Union]
public partial struct Option<T>
{
[UnionTemplate]
private abstract class Template
{
public abstract T Some();
public abstract void None();
}
}
[Union]
public partial struct Result<T, E>
{
[UnionTemplate]
private abstract class Template
{
public abstract E Ok();
public abstract E Err();
}
}
[Union]
public partial struct SomeUnmanaged<T> where T : unmanaged
{
[UnionTemplate]
private abstract class Template
{
public abstract T Foo();
}
}
[Union]
public partial struct SomeClass<T> where T : class
{
[UnionTemplate]
private abstract class Template
{
public abstract T Foo();
}
}
[Union]
public partial struct Empty { }
[Union(TagsName = "Kind")]
public partial struct Union3 { }
[Union(ExternalTags = true)]
public partial struct Union4 { }
[Union(ExternalTags = true, ExternalTagsName = "{0}Kind")]
public partial struct Union5 { }
[Union(TagsUnderlying = typeof(ulong))]
public partial struct Union6 { }
[Union]
public partial struct Union7
{
[UnionTemplate]
private abstract class Template
{
[UnionTag(123)]
public abstract int Foo();
}
}
[Union(ExternalTags = true, ExternalTagsName = "Union8Foo")]
public partial struct Union8 { }
}