From 4f5f463bd227a81c26224a81b178e2221946b9b2 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 9 Apr 2018 22:33:45 -0400 Subject: warn when directly using a net field that has a non-net wrapper (#471) --- .../UnitTests.cs | 43 ++++++++++++++++++---- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs') diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs index d5e4ef52..b4f54234 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs @@ -16,6 +16,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests /// Sample C# code which contains a simplified representation of Stardew Valley's Netcode types, and sample mod code with a {{test-code}} placeholder for the code being tested. const string SampleProgram = @" using System; + using StardewValley; using Netcode; namespace Netcode @@ -29,13 +30,16 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests } } - namespace SampleMod + namespace StardewValley { class Item { public NetInt category { get; } = new NetInt { Value = 42 }; } + } + namespace SampleMod + { class ModEntry { public void Entry() @@ -45,17 +49,17 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests Item item = null; // this line should raise diagnostics - {{test-code}} // line 32 + {{test-code}} // line 36 // these lines should not - if ((int)field != 42); + if ((int)intField != 42); } } } "; /// The line number where the unit tested code is injected into . - private const int SampleCodeLine = 32; + private const int SampleCodeLine = 36; /// The column number where the unit tested code is injected into . private const int SampleCodeColumn = 25; @@ -75,7 +79,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests this.VerifyCSharpDiagnostic(test); } - /// Test that the expected diagnostic message is raised. + /// Test that the expected diagnostic message is raised for implicit net field comparisons. /// The code line to test. /// The column within the code line where the diagnostic message should be reported. /// The expression which should be reported. @@ -89,7 +93,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests [TestCase("if (intField != 42);", 4, "intField", "NetInt", "Int32")] [TestCase("if (refField != null);", 4, "refField", "NetRef", "Object")] [TestCase("if (item?.category != 42);", 4, "item?.category", "NetInt", "Int32")] - public void ImplicitComparisons_RaiseDiagnostics(string codeText, int column, string expression, string fromType, string toType) + public void AvoidImplicitNetFieldComparisons_RaisesDiagnostic(string codeText, int column, string expression, string fromType, string toType) { // arrange string code = UnitTests.SampleProgram.Replace("{{test-code}}", codeText); @@ -105,6 +109,31 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests this.VerifyCSharpDiagnostic(code, expected); } + /// Test that the expected diagnostic message is raised for avoidable net field references. + /// The code line to test. + /// The column within the code line where the diagnostic message should be reported. + /// The expression which should be reported. + /// The net type name which should be reported. + /// The suggested property name which should be reported. + [TestCase("int category = item.category;", 15, "item.category", "NetInt", "Category")] + [TestCase("int category = (item).category;", 15, "(item).category", "NetInt", "Category")] + [TestCase("int category = ((Item)item).category;", 15, "((Item)item).category", "NetInt", "Category")] + public void AvoidNetFields_RaisesDiagnostic(string codeText, int column, string expression, string netType, string suggestedProperty) + { + // arrange + string code = UnitTests.SampleProgram.Replace("{{test-code}}", codeText); + DiagnosticResult expected = new DiagnosticResult + { + Id = "SMAPI002", + Message = $"'{expression}' is a {netType} field; consider using the {suggestedProperty} property instead. See https://smapi.io/buildmsg/SMAPI002 for details.", + Severity = DiagnosticSeverity.Warning, + Locations = new[] { new DiagnosticResultLocation("Test0.cs", UnitTests.SampleCodeLine, UnitTests.SampleCodeColumn + column) } + }; + + // assert + this.VerifyCSharpDiagnostic(code, expected); + } + /********* ** Helpers @@ -112,7 +141,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests /// Get the analyzer being tested. protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() { - return new ImplicitNetFieldCastAnalyzer(); + return new NetFieldAnalyzer(); } } } -- cgit