Skip to content

Commit

Permalink
Merge pull request #41 from wieslawsoltes/InitRefReturnSupport
Browse files Browse the repository at this point in the history
Add support for init properties
  • Loading branch information
wieslawsoltes authored Jan 4, 2025
2 parents 30165a1 + a1dfffc commit 6051434
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 34 deletions.
134 changes: 134 additions & 0 deletions ReactiveGenerator.Tests/ReactiveGenerator/ReactiveGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,4 +1562,138 @@ public partial class TestClass

return TestAndVerify(source);
}

[Fact]
public Task InitAccessorTest()
{
var source = @"
[Reactive]
public partial class Person
{
public partial string Name { get; init; }
public required partial string Id { get; init; }
}";

return TestAndVerify(source);
}

/* TODO:
[Fact]
public Task ReadOnlyPropertyTest()
{
var source = @"
[Reactive]
public partial class TestClass
{
public partial string ReadOnlyProp { get; }
public readonly partial string ExplicitReadOnlyProp { get; }
}";
return TestAndVerify(source);
}
[Fact]
public Task RefPropertyTest()
{
var source = @"
[Reactive]
public partial class Container
{
// Basic ref property
public partial ref int RefValue { get; }
// Ref readonly property
public partial ref readonly int ReadOnlyRefValue { get; }
// Ref with modifiers
public static partial ref int StaticRefValue { get; }
// Virtual ref property
public virtual partial ref int VirtualRefValue { get; }
// Override ref property
public override partial ref int BaseRefValue { get; }
}";
return TestAndVerify(source);
}
[Fact]
public Task RefReadonlyPropertyTest()
{
var source = @"
[Reactive]
public partial class Container
{
// Basic ref readonly property
public partial ref readonly int ReadOnlyValue { get; }
// Ref readonly with other modifiers
public static partial ref readonly int StaticReadOnlyValue { get; }
public virtual partial ref readonly int VirtualReadOnlyValue { get; }
// Try with different types
public partial ref readonly string StringValue { get; }
public partial ref readonly System.DateTime DateValue { get; }
}";
return TestAndVerify(source);
}
[Fact]
public Task RefReturnTest()
{
var source = @"
[Reactive]
public partial class Container
{
private int _value;
public partial ref int RefValue { get; }
public partial ref readonly int ReadOnlyRefValue { get; }
}";
return TestAndVerify(source);
}
[Fact]
public Task MixedFeaturesTest()
{
var source = @"
[Reactive]
public partial class AdvancedClass
{
// Init with required
public required partial string Id { get; init; }
// Ref return with static
public static partial ref int Counter { get; }
// Init with virtual
public virtual partial string Name { get; init; }
// Readonly with override
public override partial string ToString { get; }
}";
return TestAndVerify(source);
}
[Fact]
public Task RefPropertyInvalidCombinationsTest()
{
var source = @"
[Reactive]
public partial class Container
{
// Ref readonly should not generate a setter
public partial ref readonly int ReadOnlyValue { get; set; }
// Regular ref can have a setter
public partial ref int MutableValue { get; set; }
}";
return TestAndVerify(source);
}
*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public partial class TestClass
public partial string InitOnlyProp
{
get => field;
set
init
{
if (!Equals(field, value))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ internal partial class GenericViewModel<T>
public partial T InitValue
{
get => field;
set
init
{
if (!Equals(field, value))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
Sources: [
{
FileName: IgnoreReactiveAttribute.g.cs,
Source:
// <auto-generated/>
using System;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
sealed class IgnoreReactiveAttribute : Attribute
{
public IgnoreReactiveAttribute() { }
}
},
{
FileName: Person.INPC.g.cs,
Source:
// <auto-generated/>
#nullable enable

using System.ComponentModel;
using System.Runtime.CompilerServices;

public partial class Person : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
{
PropertyChanged?.Invoke(this, args);
}
}

},
{
FileName: Person.ReactiveProperties.g.cs,
Source:
// <auto-generated/>
#nullable enable

using System.ComponentModel;
using System.Runtime.CompilerServices;

/// <summary>
/// A partial class implementation for Person.
/// </summary>
public partial class Person
{
private static readonly PropertyChangedEventArgs _nameChangedEventArgs = new PropertyChangedEventArgs(nameof(Name));
private static readonly PropertyChangedEventArgs _idChangedEventArgs = new PropertyChangedEventArgs(nameof(Id));

public partial string Name
{
get => field;
init
{
if (!Equals(field, value))
{
field = value;
OnPropertyChanged(_nameChangedEventArgs);
}
}
}

public required partial string Id
{
get => field;
init
{
if (!Equals(field, value))
{
field = value;
OnPropertyChanged(_idChangedEventArgs);
}
}
}
}

},
{
FileName: ReactiveAttribute.g.cs,
Source:
// <auto-generated/>
using System;

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
sealed class ReactiveAttribute : Attribute
{
public ReactiveAttribute() { }
}
}
],
Diagnostics: null
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public partial class Container
public partial string InitOnlyProp
{
get => field;
set
init
{
if (!Equals(field, value))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public partial class TestViewModel
public partial string InitOnlyProp
{
get => field;
set => this.RaiseAndSetIfChanged(ref field, value);
init => this.RaiseAndSetIfChanged(ref field, value);
}

public partial string GetOnlyProp
Expand Down
Loading

0 comments on commit 6051434

Please sign in to comment.