@@ -4,6 +4,309 @@ namespace Rocks.Tests.Generators;
4
4
5
5
public static class InheritanceGeneratorTests
6
6
{
7
+ [ Test ]
8
+ public static async Task GenerateWhenNewPropertyIsIntroducedAsync ( )
9
+ {
10
+ var code =
11
+ """
12
+ #nullable enable
13
+
14
+ using Rocks;
15
+ using System;
16
+
17
+ [assembly: Rock(typeof(BindableReactiveProperty<>), BuildType.Create | BuildType.Make)]
18
+
19
+ public class ReactiveProperty<T>
20
+ {
21
+ #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
22
+ public virtual T Value { get; set; }
23
+ #pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
24
+ }
25
+
26
+ public class BindableReactiveProperty<T>
27
+ : ReactiveProperty<T>
28
+ {
29
+ public new T Value
30
+ {
31
+ get => base.Value;
32
+ set => base.Value = value;
33
+ }
34
+ }
35
+ """ ;
36
+
37
+ var createGeneratedCode =
38
+ """"
39
+ // <auto-generated/>
40
+
41
+ #pragma warning disable CS8618
42
+ #pragma warning disable CS8633
43
+ #pragma warning disable CS8714
44
+ #pragma warning disable CS8775
45
+
46
+ #nullable enable
47
+
48
+ using Rocks.Extensions;
49
+
50
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
51
+ internal sealed class BindableReactivePropertyCreateExpectations<T>
52
+ : global::Rocks.Expectations
53
+ {
54
+ internal sealed class Handler0
55
+ : global::Rocks.Handler<global::System.Func<object?, bool>, bool>
56
+ {
57
+ public global::Rocks.Argument<object?> @obj { get; set; }
58
+ }
59
+ private global::Rocks.Handlers<global::BindableReactivePropertyCreateExpectations<T>.Handler0>? @handlers0;
60
+ internal sealed class Handler1
61
+ : global::Rocks.Handler<global::System.Func<int>, int>
62
+ { }
63
+ private global::Rocks.Handlers<global::BindableReactivePropertyCreateExpectations<T>.Handler1>? @handlers1;
64
+ internal sealed class Handler2
65
+ : global::Rocks.Handler<global::System.Func<string?>, string?>
66
+ { }
67
+ private global::Rocks.Handlers<global::BindableReactivePropertyCreateExpectations<T>.Handler2>? @handlers2;
68
+
69
+ public override void Verify()
70
+ {
71
+ if (this.WasInstanceInvoked)
72
+ {
73
+ var failures = new global::System.Collections.Generic.List<string>();
74
+
75
+ if (this.handlers0 is not null) { failures.AddRange(this.Verify(this.handlers0, 0)); }
76
+ if (this.handlers1 is not null) { failures.AddRange(this.Verify(this.handlers1, 1)); }
77
+ if (this.handlers2 is not null) { failures.AddRange(this.Verify(this.handlers2, 2)); }
78
+
79
+ if (failures.Count > 0)
80
+ {
81
+ throw new global::Rocks.Exceptions.VerificationException(failures);
82
+ }
83
+ }
84
+ }
85
+
86
+ private sealed class Mock
87
+ : global::BindableReactiveProperty<T>
88
+ {
89
+ public Mock(global::BindableReactivePropertyCreateExpectations<T> @expectations)
90
+ {
91
+ this.Expectations = @expectations;
92
+ }
93
+
94
+ [global::Rocks.MemberIdentifier(0)]
95
+ public override bool Equals(object? @obj)
96
+ {
97
+ if (this.Expectations.handlers0 is not null)
98
+ {
99
+ foreach (var @handler in this.Expectations.handlers0)
100
+ {
101
+ if (@handler.@obj.IsValid(@obj!))
102
+ {
103
+ @handler.CallCount++;
104
+ var @result = @handler.Callback is not null ?
105
+ @handler.Callback(@obj!) : @handler.ReturnValue;
106
+ return @result!;
107
+ }
108
+ }
109
+
110
+ throw new global::Rocks.Exceptions.ExpectationException(
111
+ $"""
112
+ No handlers match for {this.GetType().GetMemberDescription(0)}
113
+ obj: {@obj.FormatValue()}
114
+ """);
115
+ }
116
+ else
117
+ {
118
+ return base.Equals(@obj: @obj!);
119
+ }
120
+ }
121
+
122
+ [global::Rocks.MemberIdentifier(1)]
123
+ public override int GetHashCode()
124
+ {
125
+ if (this.Expectations.handlers1 is not null)
126
+ {
127
+ var @handler = this.Expectations.handlers1.First;
128
+ @handler.CallCount++;
129
+ var @result = @handler.Callback is not null ?
130
+ @handler.Callback() : @handler.ReturnValue;
131
+ return @result!;
132
+ }
133
+ else
134
+ {
135
+ return base.GetHashCode();
136
+ }
137
+ }
138
+
139
+ [global::Rocks.MemberIdentifier(2)]
140
+ public override string? ToString()
141
+ {
142
+ if (this.Expectations.handlers2 is not null)
143
+ {
144
+ var @handler = this.Expectations.handlers2.First;
145
+ @handler.CallCount++;
146
+ var @result = @handler.Callback is not null ?
147
+ @handler.Callback() : @handler.ReturnValue;
148
+ return @result!;
149
+ }
150
+ else
151
+ {
152
+ return base.ToString();
153
+ }
154
+ }
155
+
156
+ private global::BindableReactivePropertyCreateExpectations<T> Expectations { get; }
157
+ }
158
+
159
+ internal sealed class MethodExpectations
160
+ {
161
+ internal MethodExpectations(global::BindableReactivePropertyCreateExpectations<T> expectations) =>
162
+ this.Expectations = expectations;
163
+
164
+ internal global::BindableReactivePropertyCreateExpectations<T>.Adornments.AdornmentsForHandler0 Equals(global::Rocks.Argument<object?> @obj)
165
+ {
166
+ global::Rocks.Exceptions.ExpectationException.ThrowIf(this.Expectations.WasInstanceInvoked);
167
+ global::System.ArgumentNullException.ThrowIfNull(@obj);
168
+
169
+ var @handler = new global::BindableReactivePropertyCreateExpectations<T>.Handler0
170
+ {
171
+ @obj = @obj,
172
+ };
173
+
174
+ if (this.Expectations.handlers0 is null) { this.Expectations.handlers0 = new(@handler); }
175
+ else { this.Expectations.handlers0.Add(@handler); }
176
+ return new(@handler);
177
+ }
178
+
179
+ internal new global::BindableReactivePropertyCreateExpectations<T>.Adornments.AdornmentsForHandler1 GetHashCode()
180
+ {
181
+ global::Rocks.Exceptions.ExpectationException.ThrowIf(this.Expectations.WasInstanceInvoked);
182
+ var handler = new global::BindableReactivePropertyCreateExpectations<T>.Handler1();
183
+ if (this.Expectations.handlers1 is null) { this.Expectations.handlers1 = new(handler); }
184
+ else { this.Expectations.handlers1.Add(handler); }
185
+ return new(handler);
186
+ }
187
+
188
+ internal new global::BindableReactivePropertyCreateExpectations<T>.Adornments.AdornmentsForHandler2 ToString()
189
+ {
190
+ global::Rocks.Exceptions.ExpectationException.ThrowIf(this.Expectations.WasInstanceInvoked);
191
+ var handler = new global::BindableReactivePropertyCreateExpectations<T>.Handler2();
192
+ if (this.Expectations.handlers2 is null) { this.Expectations.handlers2 = new(handler); }
193
+ else { this.Expectations.handlers2.Add(handler); }
194
+ return new(handler);
195
+ }
196
+
197
+ private global::BindableReactivePropertyCreateExpectations<T> Expectations { get; }
198
+ }
199
+
200
+ internal global::BindableReactivePropertyCreateExpectations<T>.MethodExpectations Methods { get; }
201
+
202
+ internal BindableReactivePropertyCreateExpectations() =>
203
+ (this.Methods) = (new(this));
204
+
205
+ internal global::BindableReactiveProperty<T> Instance()
206
+ {
207
+ if (!this.WasInstanceInvoked)
208
+ {
209
+ this.WasInstanceInvoked = true;
210
+ var @mock = new Mock(this);
211
+ this.MockType = @mock.GetType();
212
+ return @mock;
213
+ }
214
+ else
215
+ {
216
+ throw new global::Rocks.Exceptions.NewMockInstanceException("Can only create a new mock once.");
217
+ }
218
+ }
219
+
220
+ internal static class Adornments
221
+ {
222
+ public interface IAdornmentsForBindableReactiveProperty<TAdornments>
223
+ : global::Rocks.IAdornments<TAdornments>
224
+ where TAdornments : IAdornmentsForBindableReactiveProperty<TAdornments>
225
+ { }
226
+
227
+ public sealed class AdornmentsForHandler0
228
+ : global::Rocks.Adornments<AdornmentsForHandler0, global::BindableReactivePropertyCreateExpectations<T>.Handler0, global::System.Func<object?, bool>, bool>, IAdornmentsForBindableReactiveProperty<AdornmentsForHandler0>
229
+ {
230
+ public AdornmentsForHandler0(global::BindableReactivePropertyCreateExpectations<T>.Handler0 handler)
231
+ : base(handler) { }
232
+ }
233
+ public sealed class AdornmentsForHandler1
234
+ : global::Rocks.Adornments<AdornmentsForHandler1, global::BindableReactivePropertyCreateExpectations<T>.Handler1, global::System.Func<int>, int>, IAdornmentsForBindableReactiveProperty<AdornmentsForHandler1>
235
+ {
236
+ public AdornmentsForHandler1(global::BindableReactivePropertyCreateExpectations<T>.Handler1 handler)
237
+ : base(handler) { }
238
+ }
239
+ public sealed class AdornmentsForHandler2
240
+ : global::Rocks.Adornments<AdornmentsForHandler2, global::BindableReactivePropertyCreateExpectations<T>.Handler2, global::System.Func<string?>, string?>, IAdornmentsForBindableReactiveProperty<AdornmentsForHandler2>
241
+ {
242
+ public AdornmentsForHandler2(global::BindableReactivePropertyCreateExpectations<T>.Handler2 handler)
243
+ : base(handler) { }
244
+ }
245
+ }
246
+ }
247
+
248
+ #pragma warning restore CS8618
249
+ #pragma warning restore CS8633
250
+ #pragma warning restore CS8714
251
+ #pragma warning restore CS8775
252
+ """" ;
253
+
254
+ var makeGeneratedCode =
255
+ """
256
+ // <auto-generated/>
257
+
258
+ #pragma warning disable CS8618
259
+ #pragma warning disable CS8633
260
+ #pragma warning disable CS8714
261
+ #pragma warning disable CS8775
262
+
263
+ #nullable enable
264
+
265
+ [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
266
+ internal sealed class BindableReactivePropertyMakeExpectations<T>
267
+ {
268
+ internal global::BindableReactiveProperty<T> Instance()
269
+ {
270
+ return new Mock();
271
+ }
272
+
273
+ private sealed class Mock
274
+ : global::BindableReactiveProperty<T>
275
+ {
276
+ public Mock()
277
+ {
278
+ }
279
+
280
+ public override bool Equals(object? @obj)
281
+ {
282
+ return default!;
283
+ }
284
+ public override int GetHashCode()
285
+ {
286
+ return default!;
287
+ }
288
+ public override string? ToString()
289
+ {
290
+ return default!;
291
+ }
292
+ }
293
+ }
294
+
295
+ #pragma warning restore CS8618
296
+ #pragma warning restore CS8633
297
+ #pragma warning restore CS8714
298
+ #pragma warning restore CS8775
299
+
300
+ """ ;
301
+
302
+ await TestAssistants . RunGeneratorAsync < RockGenerator > ( code ,
303
+ [
304
+ ( "BindableReactivePropertyT_Rock_Create.g.cs" , createGeneratedCode ) ,
305
+ ( "BindableReactivePropertyT_Rock_Make.g.cs" , makeGeneratedCode )
306
+ ] ,
307
+ [ ] ) ;
308
+ }
309
+
7
310
[ Test ]
8
311
public static async Task GenerateWhenMethodIsIntroducedAsync ( )
9
312
{
0 commit comments