Skip to content

Commit 6051434

Browse files
Merge pull request #41 from wieslawsoltes/InitRefReturnSupport
Add support for init properties
2 parents 30165a1 + a1dfffc commit 6051434

10 files changed

+375
-34
lines changed

ReactiveGenerator.Tests/ReactiveGenerator/ReactiveGeneratorTests.cs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1562,4 +1562,138 @@ public partial class TestClass
15621562

15631563
return TestAndVerify(source);
15641564
}
1565+
1566+
[Fact]
1567+
public Task InitAccessorTest()
1568+
{
1569+
var source = @"
1570+
[Reactive]
1571+
public partial class Person
1572+
{
1573+
public partial string Name { get; init; }
1574+
public required partial string Id { get; init; }
1575+
}";
1576+
1577+
return TestAndVerify(source);
1578+
}
1579+
1580+
/* TODO:
1581+
[Fact]
1582+
public Task ReadOnlyPropertyTest()
1583+
{
1584+
var source = @"
1585+
[Reactive]
1586+
public partial class TestClass
1587+
{
1588+
public partial string ReadOnlyProp { get; }
1589+
public readonly partial string ExplicitReadOnlyProp { get; }
1590+
}";
1591+
1592+
return TestAndVerify(source);
1593+
}
1594+
1595+
[Fact]
1596+
public Task RefPropertyTest()
1597+
{
1598+
var source = @"
1599+
[Reactive]
1600+
public partial class Container
1601+
{
1602+
// Basic ref property
1603+
public partial ref int RefValue { get; }
1604+
1605+
// Ref readonly property
1606+
public partial ref readonly int ReadOnlyRefValue { get; }
1607+
1608+
// Ref with modifiers
1609+
public static partial ref int StaticRefValue { get; }
1610+
1611+
// Virtual ref property
1612+
public virtual partial ref int VirtualRefValue { get; }
1613+
1614+
// Override ref property
1615+
public override partial ref int BaseRefValue { get; }
1616+
}";
1617+
1618+
return TestAndVerify(source);
1619+
}
1620+
1621+
[Fact]
1622+
public Task RefReadonlyPropertyTest()
1623+
{
1624+
var source = @"
1625+
[Reactive]
1626+
public partial class Container
1627+
{
1628+
// Basic ref readonly property
1629+
public partial ref readonly int ReadOnlyValue { get; }
1630+
1631+
// Ref readonly with other modifiers
1632+
public static partial ref readonly int StaticReadOnlyValue { get; }
1633+
public virtual partial ref readonly int VirtualReadOnlyValue { get; }
1634+
1635+
// Try with different types
1636+
public partial ref readonly string StringValue { get; }
1637+
public partial ref readonly System.DateTime DateValue { get; }
1638+
}";
1639+
1640+
return TestAndVerify(source);
1641+
}
1642+
1643+
[Fact]
1644+
public Task RefReturnTest()
1645+
{
1646+
var source = @"
1647+
[Reactive]
1648+
public partial class Container
1649+
{
1650+
private int _value;
1651+
1652+
public partial ref int RefValue { get; }
1653+
public partial ref readonly int ReadOnlyRefValue { get; }
1654+
}";
1655+
1656+
return TestAndVerify(source);
1657+
}
1658+
1659+
[Fact]
1660+
public Task MixedFeaturesTest()
1661+
{
1662+
var source = @"
1663+
[Reactive]
1664+
public partial class AdvancedClass
1665+
{
1666+
// Init with required
1667+
public required partial string Id { get; init; }
1668+
1669+
// Ref return with static
1670+
public static partial ref int Counter { get; }
1671+
1672+
// Init with virtual
1673+
public virtual partial string Name { get; init; }
1674+
1675+
// Readonly with override
1676+
public override partial string ToString { get; }
1677+
}";
1678+
1679+
return TestAndVerify(source);
1680+
}
1681+
1682+
[Fact]
1683+
public Task RefPropertyInvalidCombinationsTest()
1684+
{
1685+
var source = @"
1686+
[Reactive]
1687+
public partial class Container
1688+
{
1689+
// Ref readonly should not generate a setter
1690+
public partial ref readonly int ReadOnlyValue { get; set; }
1691+
1692+
// Regular ref can have a setter
1693+
public partial ref int MutableValue { get; set; }
1694+
}";
1695+
1696+
return TestAndVerify(source);
1697+
}
1698+
*/
15651699
}

ReactiveGenerator.Tests/Snapshots/ReactiveGeneratorTests.ClassWithInitOnlySetters.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public partial class TestClass
6969
public partial string InitOnlyProp
7070
{
7171
get => field;
72-
set
72+
init
7373
{
7474
if (!Equals(field, value))
7575
{

ReactiveGenerator.Tests/Snapshots/ReactiveGeneratorTests.GenericPropertyWithInitSetter.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ internal partial class GenericViewModel<T>
4242
public partial T InitValue
4343
{
4444
get => field;
45-
set
45+
init
4646
{
4747
if (!Equals(field, value))
4848
{
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
Sources: [
3+
{
4+
FileName: IgnoreReactiveAttribute.g.cs,
5+
Source:
6+
// <auto-generated/>
7+
using System;
8+
9+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
10+
sealed class IgnoreReactiveAttribute : Attribute
11+
{
12+
public IgnoreReactiveAttribute() { }
13+
}
14+
},
15+
{
16+
FileName: Person.INPC.g.cs,
17+
Source:
18+
// <auto-generated/>
19+
#nullable enable
20+
21+
using System.ComponentModel;
22+
using System.Runtime.CompilerServices;
23+
24+
public partial class Person : INotifyPropertyChanged
25+
{
26+
public event PropertyChangedEventHandler? PropertyChanged;
27+
28+
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
29+
{
30+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
31+
}
32+
33+
protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
34+
{
35+
PropertyChanged?.Invoke(this, args);
36+
}
37+
}
38+
39+
},
40+
{
41+
FileName: Person.ReactiveProperties.g.cs,
42+
Source:
43+
// <auto-generated/>
44+
#nullable enable
45+
46+
using System.ComponentModel;
47+
using System.Runtime.CompilerServices;
48+
49+
/// <summary>
50+
/// A partial class implementation for Person.
51+
/// </summary>
52+
public partial class Person
53+
{
54+
private static readonly PropertyChangedEventArgs _nameChangedEventArgs = new PropertyChangedEventArgs(nameof(Name));
55+
private static readonly PropertyChangedEventArgs _idChangedEventArgs = new PropertyChangedEventArgs(nameof(Id));
56+
57+
public partial string Name
58+
{
59+
get => field;
60+
init
61+
{
62+
if (!Equals(field, value))
63+
{
64+
field = value;
65+
OnPropertyChanged(_nameChangedEventArgs);
66+
}
67+
}
68+
}
69+
70+
public required partial string Id
71+
{
72+
get => field;
73+
init
74+
{
75+
if (!Equals(field, value))
76+
{
77+
field = value;
78+
OnPropertyChanged(_idChangedEventArgs);
79+
}
80+
}
81+
}
82+
}
83+
84+
},
85+
{
86+
FileName: ReactiveAttribute.g.cs,
87+
Source:
88+
// <auto-generated/>
89+
using System;
90+
91+
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
92+
sealed class ReactiveAttribute : Attribute
93+
{
94+
public ReactiveAttribute() { }
95+
}
96+
}
97+
],
98+
Diagnostics: null
99+
}

ReactiveGenerator.Tests/Snapshots/ReactiveGeneratorTests.NestedClassesWithInitOnlyProperties.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public partial class Container
6363
public partial string InitOnlyProp
6464
{
6565
get => field;
66-
set
66+
init
6767
{
6868
if (!Equals(field, value))
6969
{

ReactiveGenerator.Tests/Snapshots/ReactiveGeneratorTests.ReactiveObjectDerivedWithInitProperties.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public partial class TestViewModel
4646
public partial string InitOnlyProp
4747
{
4848
get => field;
49-
set => this.RaiseAndSetIfChanged(ref field, value);
49+
init => this.RaiseAndSetIfChanged(ref field, value);
5050
}
5151

5252
public partial string GetOnlyProp

0 commit comments

Comments
 (0)