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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/***************************************************************************************************
Copyright (C) 2025 The Qt Company Ltd.
SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Test_Qt.DotNet.Generator
{
using Support;
/// <summary>
/// When generating property X for class B, the generation rule will receive as source
/// the PropertyInfo for B::X. The rule will need to determine if the property is
/// "notifiable", i.e. if the type implements INotifyPropertyChanged. In this case, B::X is
/// indeed notifiable, but A::X is not. If the generation rule uses DeclaringType instead of
/// ReflectingType to determine if the property is notifiable, it will obtain the Type info
/// for A instead of B, and will incorrectly not generate change notifications for B::X.
/// </summary>
[TestClass]
public class Test_DeclaringVsReflecting
{
public TestContext TestContext { get; set; }
private const string Source = """
using System.ComponentModel;
namespace Test {
public class A
{
public int X { get; set; }
}
public class B : A, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task DeclaringVsReflecting()
{
var result = await TestCodeGenerator.GenerateAsync([Source],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", aHpp);
Assert.DoesNotMatchRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", aHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", bHpp);
}
private const string OverrideSources = """
using System.ComponentModel;
namespace Test {
public class A
{
public virtual int X { get; set; }
}
public class B : A, INotifyPropertyChanged
{
public override int X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task OverridenProperty_UsesReflectedTypeForNotifiability()
{
var result = await TestCodeGenerator.GenerateAsync([OverrideSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", aHpp);
Assert.DoesNotMatchRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", aHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", bHpp);
}
private const string TwoLevelOverrideSources = """
using System.ComponentModel;
namespace Test
{
public class A
{
public virtual int X { get; set; }
}
public class B : A
{
public override int X { get; set; }
}
public class C : B, INotifyPropertyChanged
{
public override int X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task MultiLevelOverride_OnlyDerivedWithINotifyIsNotifiable()
{
var result = await TestCodeGenerator.GenerateAsync([TwoLevelOverrideSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", aHpp);
Assert.DoesNotMatchRegex(@"NOTIFY xChanged\)", aHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", bHpp);
Assert.DoesNotMatchRegex(@"NOTIFY xChanged\)", bHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/c.h", out var cHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", cHpp);
}
private const string ShadowSources = """
using System.ComponentModel;
namespace Test {
public class A
{
public int X { get; set; }
}
public class B : A, INotifyPropertyChanged
{
public new int X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
public class C : A, INotifyPropertyChanged
{
public new float X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task ShadowedProperty_UsesReflectedTypeForNotifiability()
{
var result = await TestCodeGenerator.GenerateAsync([ShadowSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", aHpp);
Assert.DoesNotMatchRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", aHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", bHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/c.h", out var cHpp));
Assert.MatchesRegex(@"Q_PROPERTY\((?:qreal|float) x [^\)]* NOTIFY xChanged\)", cHpp);
}
private const string AbstractOverrideSources = """
using System.ComponentModel;
namespace Test
{
public abstract class A
{
public abstract int X { get; set; }
}
public class B : A, INotifyPropertyChanged
{
public override int X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task AbstractProperty_Override_IsNotifiableOnDerived()
{
var result = await TestCodeGenerator.GenerateAsync([AbstractOverrideSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]*\)", aHpp);
Assert.DoesNotMatchRegex(@"NOTIFY xChanged\)", aHpp);
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", bHpp);
}
private const string ExplicitInterfaceSources = """
using System.ComponentModel;
namespace Test {
public interface IFoo
{
int X { get; set; }
}
public class A : IFoo, INotifyPropertyChanged
{
int IFoo.X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task ExplicitInterfaceProperty_NoQPropertyGenerated()
{
var result = await TestCodeGenerator.GenerateAsync([ExplicitInterfaceSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
// This must NOT generate a Q_PROPERTY:
// In the source, class A implements IFoo.X *explicitly*: `int IFoo.X { get; set; }`.
// Explicit interface implementations are non-public and do not appear on A's public API
// (you can't access `a.X`; you must cast: `((IFoo)a).X`). Our generator intentionally
// exposes only the reflected class's *public* instance properties to QML to mirror the
// public surface and preserve encapsulation.
Assert.IsTrue(result.Sink.Files.TryGetValue("source/hpp/test/a.h", out var aHpp));
Assert.DoesNotMatchRegex(@"\bQ_PROPERTY\(.+\bx\b", aHpp);
}
private const string ExplicitWithPublicForwarderSources = """
using System.ComponentModel;
namespace Test {
public interface IFoo { int X { get; set; } }
// Implements IFoo.X explicitly, and exposes a public forwarder 'X'.
public class A : IFoo, INotifyPropertyChanged
{
int IFoo.X { get; set; } // explicit (non-public)
public int X // public forwarder
{
get => ((IFoo)this).X;
set => ((IFoo)this).X = value;
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task ExplicitInterface_WithPublicForwarder_IsGeneratedAndNotifiable()
{
var result = await TestCodeGenerator.GenerateAsync([ExplicitWithPublicForwarderSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue(@"source/hpp/test/a.h", out var aHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)", aHpp);
}
private const string ImplicitInterfaceSources = """
using System.ComponentModel;
namespace Test {
public interface IFoo { int X { get; set; } }
// Implements IFoo.X implicitly (public).
public class B : IFoo, INotifyPropertyChanged
{
public int X { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
""";
[TestMethod]
public async Task ImplicitInterface_ImplicitPublicProperty_IsGeneratedAndNotifiable()
{
var result = await TestCodeGenerator.GenerateAsync([ImplicitInterfaceSources],
sourceRefs: [typeof(INotifyPropertyChanged).Assembly],
ct: TestContext.CancellationTokenSource.Token);
Assert.IsTrue(result.Sink.Files.TryGetValue(@"source/hpp/test/b.h", out var bHpp));
Assert.MatchesRegex(@"Q_PROPERTY\(qint32 x [^\)]* NOTIFY xChanged\)",bHpp);
}
}
}
|