WindowsFormsNewPackageAnalyzerTests.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Reactive.Analyzers.Test.Verifiers;
  5. using Microsoft;
  6. using Microsoft.CodeAnalysis.Testing;
  7. namespace System.Reactive.Analyzers.Test
  8. {
  9. [TestClass]
  10. public sealed class WindowsFormsNewPackageAnalyzerTests
  11. {
  12. [TestMethod]
  13. public async Task DetectIObservableSubscribeOnControl()
  14. {
  15. var test = """
  16. using System;
  17. using System.Reactive.Linq;
  18. using System.Reactive.Subjects;
  19. System.Windows.Forms.Control control = default!;
  20. Observable.Interval(TimeSpan.FromSeconds(0.5))
  21. .SubscribeOn({|#0:control|})
  22. .Subscribe(Console.WriteLine);
  23. """;
  24. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  25. .WithLocation(0);
  26. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("SubscribeOn");
  27. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  28. test,
  29. normalError,
  30. customDiagnostic);
  31. }
  32. /// <summary>
  33. /// Check that we handle SubscribeOn for types that derive from Control (and not just Control itself).
  34. /// </summary>
  35. /// <returns></returns>
  36. [TestMethod]
  37. public async Task DetectIObservableSubscribeOnButton()
  38. {
  39. var test = """
  40. using System;
  41. using System.Reactive.Linq;
  42. using System.Reactive.Subjects;
  43. System.Windows.Forms.Button button = default!;
  44. Observable.Interval(TimeSpan.FromSeconds(0.5))
  45. .SubscribeOn({|#0:button|})
  46. .Subscribe(Console.WriteLine);
  47. """;
  48. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  49. .WithLocation(0);
  50. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("SubscribeOn");
  51. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  52. test,
  53. normalError,
  54. customDiagnostic);
  55. }
  56. [TestMethod]
  57. public async Task DetectIObservableObserveOnControl()
  58. {
  59. var test = """
  60. using System;
  61. using System.Reactive.Linq;
  62. using System.Reactive.Subjects;
  63. System.Windows.Forms.Control control = default!;
  64. Observable.Interval(TimeSpan.FromSeconds(0.5))
  65. .ObserveOn({|#0:control|})
  66. .Subscribe(Console.WriteLine);
  67. """;
  68. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  69. .WithLocation(0);
  70. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("ObserveOn");
  71. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  72. test,
  73. normalError,
  74. customDiagnostic);
  75. }
  76. /// <summary>
  77. /// Check that we handle ObserveOn for types that derive from Control (and not just Control itself).
  78. /// </summary>
  79. /// <returns></returns>
  80. [TestMethod]
  81. public async Task DetectIObservableObserveOnButton()
  82. {
  83. var test = """
  84. using System;
  85. using System.Reactive.Linq;
  86. using System.Reactive.Subjects;
  87. System.Windows.Forms.Button button = default!;
  88. Observable.Interval(TimeSpan.FromSeconds(0.5))
  89. .ObserveOn({|#0:button|})
  90. .Subscribe(Console.WriteLine);
  91. """;
  92. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  93. .WithLocation(0);
  94. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("ObserveOn");
  95. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  96. test,
  97. normalError,
  98. customDiagnostic);
  99. }
  100. [TestMethod]
  101. public async Task DetectConcreteObservableSubscribeOnControl()
  102. {
  103. var test = """
  104. using System;
  105. using System.Reactive.Linq;
  106. using System.Reactive.Subjects;
  107. System.Windows.Forms.Control control = default!;
  108. new Subject<int>()
  109. .SubscribeOn({|#0:control|})
  110. .Subscribe(Console.WriteLine);
  111. """;
  112. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  113. .WithLocation(0);
  114. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("SubscribeOn");
  115. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  116. test,
  117. normalError,
  118. customDiagnostic);
  119. }
  120. [TestMethod]
  121. public async Task DetectConcreteObservableObserveOnControl()
  122. {
  123. var test = """
  124. using System;
  125. using System.Reactive.Linq;
  126. using System.Reactive.Subjects;
  127. System.Windows.Forms.Control control = default!;
  128. new Subject<int>()
  129. .ObserveOn({|#0:control|})
  130. .Subscribe(Console.WriteLine);
  131. """;
  132. DiagnosticResult normalError = new DiagnosticResult("CS1503", Microsoft.CodeAnalysis.DiagnosticSeverity.Error)
  133. .WithLocation(0);
  134. var customDiagnostic = AddUiFrameworkPackageAnalyzerVerifier.Diagnostic("RXNET0001").WithLocation(0).WithArguments("ObserveOn");
  135. await AddUiFrameworkPackageAnalyzerVerifier.VerifyAnalyzerAsync(
  136. test,
  137. normalError,
  138. customDiagnostic);
  139. }
  140. }
  141. }