Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add unit tests for new WinRT analyzer
  • Loading branch information
Sergio0694 committed Nov 9, 2024
commit ae66c28c6c223f822a37677b9dda39bf90115655
Original file line number Diff line number Diff line change
Expand Up @@ -2084,6 +2084,148 @@ await CSharpAnalyzerWithLanguageVersionTest<WinRTRelayCommandIsNotGeneratedBinda
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_NotTargetingWindows_DoesNotWarn()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[ObservableObject]
public partial class SampleViewModel : BaseType
{
}

public class BaseType
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: []);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_TargetingWindows_NoCsWinRTAotOptimizer_DoesNotWarn()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[ObservableObject]
public partial class SampleViewModel : BaseType
{
}

public class BaseType
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true)]);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_TargetingWindows_NoBaseType_ObservableObject_DoesNotWarn()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[ObservableObject]
public partial class SampleViewModel
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_TargetingWindows_NoBaseType_INotifyPropertyChanged_DoesNotWarn()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[INotifyPropertyChanged]
public partial class SampleViewModel
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_TargetingWindows_BaseType_ObservableObject_Warns()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[ObservableObject]
public partial class {|MVVMTK0050:SampleViewModel|} : BaseType
{
}

public class BaseType
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}

[TestMethod]
public async Task WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer_TargetingWindows_BaseType_INotifyPropertyChanged_Warns()
{
const string source = """
using System;
using CommunityToolkit.Mvvm.ComponentModel;

namespace MyApp;

[INotifyPropertyChanged]
public partial class {|MVVMTK0049:SampleViewModel|} : BaseType
{
}

public class BaseType
{
}
""";

await CSharpAnalyzerWithLanguageVersionTest<WinRTClassUsingNotifyPropertyChangedAttributesAnalyzer>.VerifyAnalyzerAsync(
source,
LanguageVersion.CSharp10,
editorconfig: [("_MvvmToolkitIsUsingWindowsRuntimePack", true), ("CsWinRTAotOptimizerEnabled", "auto")]);
}

/// <summary>
/// Verifies the diagnostic errors for a given analyzer, and that all available source generators can run successfully with the input source (including subsequent compilation).
/// </summary>
Expand Down