Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Fix inpc generator for generic types (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
klemmchr authored Mar 3, 2022
1 parent 9a766e8 commit 7f0def3
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
8 changes: 0 additions & 8 deletions samples/BlazorSample.ViewModels/ClockViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ public partial class ClockViewModel : ViewModelBase, IDisposable

[Notify] private DateTime _dateTime = DateTime.Now;

/*
public DateTime DateTime
{
get => _dateTime;
set => Set(ref _dateTime, value);
}
*/

public ClockViewModel()
{
_timer = new Timer(TimeSpan.FromSeconds(1).TotalMilliseconds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,20 @@ private static string GenerateViewModelCode(
var viewModelNamespace = viewModelType.ContainingNamespace;
var viewModelClassName = viewModelClass.Identifier;

var genericArgumentString = string.Empty;
var genericArguments = viewModelType.TypeArguments.Select(x => x.Name).ToList();
if (genericArguments.Count > 0)
{
genericArgumentString = "<" + string.Join(", ", genericArguments) + ">";
}

var sb = new StringBuilder();

sb.AppendLine("#nullable enable");
sb.AppendLine();
sb.AppendLineFormat("namespace {0}", viewModelNamespace);
sb.AppendLine("{");
sb.AppendLineFormat(" partial class {0}", viewModelClassName);
sb.AppendLineFormat(" partial class {0}{1}", viewModelClassName, genericArgumentString);
sb.AppendLine(" {");

foreach (var fieldContext in fieldContexts)
Expand Down
119 changes: 116 additions & 3 deletions src/MvvmBlazor.Tests/Generators/NotifyPropertyChangedGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class NotifyPropertyChangedGeneratorTests
{
[Fact]
public void GeneratesError_WhenViewModelIsNotPartial()
public void Generates_error_when_viewmodel_is_not_partial()
{
var inputCompilation = CreateCompilation(
@$"
Expand All @@ -24,7 +24,7 @@ public class TestViewModel : MvvmBlazor.ViewModel.ViewModelBase
}

[Fact]
public void GeneratesError_WhenViewModelIsNotInheriting()
public void Generates_error_when_view_model_is_not_inheriting()
{
var inputCompilation = CreateCompilation(
@$"
Expand All @@ -45,14 +45,127 @@ public partial class TestViewModel
}

[Fact]
public void GeneratesViewModel()
public void Generates_viewmodel()
{
var inputCompilation = CreateCompilation(
@$"
public partial class TestViewModel : MvvmBlazor.ViewModel.ViewModelBase
{{
[{typeof(NotifyAttribute)}]
private bool _test;
public TestViewModel()
{{
Test = true;
}}
}}
"
);

var generator = new NotifyPropertyChangedGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out var diagnostics);
diagnostics.ShouldBeEmpty();
}

[Fact]
public void Generates_viewmodel_for_abstract_class()
{
var inputCompilation = CreateCompilation(
@$"
public abstract partial class TestViewModel : MvvmBlazor.ViewModel.ViewModelBase
{{
[{typeof(NotifyAttribute)}]
private bool _test;
}}
public TestViewModel()
{{
Test = true;
}}
"
);

var generator = new NotifyPropertyChangedGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out var diagnostics);
diagnostics.ShouldBeEmpty();
}

[Fact]
public void Generates_viewmodel_for_abstract_class_with_inheritance()
{
var inputCompilation = CreateCompilation(
@$"
public abstract partial class BaseViewModel : MvvmBlazor.ViewModel.ViewModelBase
{{
[{typeof(NotifyAttribute)}]
private bool _foo;
}}
public abstract partial class TestViewModel : BaseViewModel
{{
[{typeof(NotifyAttribute)}]
private bool _test;
}}
public TestViewModel()
{{
Test = true;
Foo = true;
}}
"
);

var generator = new NotifyPropertyChangedGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out var diagnostics);
diagnostics.ShouldBeEmpty();
}


[Fact]
public void Generates_viewmodel_for_generic_class()
{
var inputCompilation = CreateCompilation(
@$"
public abstract partial class TestViewModel<T> : MvvmBlazor.ViewModel.ViewModelBase
{{
[{typeof(NotifyAttribute)}]
private bool _test;
}}
public TestViewModel()
{{
Test = true;
}}
"
);

var generator = new NotifyPropertyChangedGenerator();
GeneratorDriver driver = CSharpGeneratorDriver.Create(generator);

driver.RunGeneratorsAndUpdateCompilation(inputCompilation, out _, out var diagnostics);
diagnostics.ShouldBeEmpty();
}

[Fact]
public void Generates_viewmodel_for_abstract_generic_class()
{
var inputCompilation = CreateCompilation(
@$"
public abstract partial class TestViewModel<T> : MvvmBlazor.ViewModel.ViewModelBase
{{
[{typeof(NotifyAttribute)}]
private bool _test;
}}
public TestViewModel()
{{
Test = true;
}}
"
);
Expand Down

0 comments on commit 7f0def3

Please sign in to comment.