From c8db771e11a0e5224aa3b0766134afc8e733896e Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 9 Apr 2018 23:25:10 -0400 Subject: [PATCH] tweak message output and unit tests (#471) --- .../UnitTests.cs | 24 +++++++++---------- .../NetFieldAnalyzer.cs | 4 +--- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs index b4f54234..92fc9074 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer.Tests/UnitTests.cs @@ -34,7 +34,9 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests { class Item { - public NetInt category { get; } = new NetInt { Value = 42 }; + public NetInt category { get; } = new NetInt { Value = 42 }; // SMAPI002: use Category instead + public NetInt type { get; } = new NetInt { Value = 42 }; + public NetRef refField { get; } = null; } } @@ -44,15 +46,13 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests { public void Entry() { - NetInt intField = new NetInt { Value = 42 }; - NetRef refField = new NetRef { Value = null }; Item item = null; // this line should raise diagnostics {{test-code}} // line 36 // these lines should not - if ((int)intField != 42); + if (item.type.Value != 42); } } } @@ -85,14 +85,14 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests /// The expression which should be reported. /// The source type name which should be reported. /// The target type name which should be reported. - [TestCase("if (intField < 42);", 4, "intField", "NetInt", "Int32")] - [TestCase("if (intField <= 42);", 4, "intField", "NetInt", "Int32")] - [TestCase("if (intField > 42);", 4, "intField", "NetInt", "Int32")] - [TestCase("if (intField >= 42);", 4, "intField", "NetInt", "Int32")] - [TestCase("if (intField == 42);", 4, "intField", "NetInt", "Int32")] - [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")] + [TestCase("if (item.type < 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.type <= 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.type > 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.type >= 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.type == 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.type != 42);", 4, "item.type", "NetInt", "int")] + [TestCase("if (item.refField != null);", 4, "item.refField", "NetRef", "object")] + [TestCase("if (item?.type != 42);", 4, "item?.type", "NetInt", "int")] public void AvoidImplicitNetFieldComparisons_RaisesDiagnostic(string codeText, int column, string expression, string fromType, string toType) { // arrange diff --git a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs index d64b1486..a9987733 100644 --- a/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs +++ b/src/SMAPI.ModBuildConfig.Analyzer/NetFieldAnalyzer.cs @@ -246,9 +246,7 @@ namespace StardewModdingAPI.ModBuildConfig.Analyzer TypeInfo operandType = context.SemanticModel.GetTypeInfo(operand); if (this.IsNetType(operandType.Type) && !this.IsNetType(operandType.ConvertedType)) { - string fromTypeName = operandType.Type.Name; - string toTypeName = operandType.ConvertedType.Name; - context.ReportDiagnostic(Diagnostic.Create(this.Rules["SMAPI001"], context.Node.GetLocation(), operand, fromTypeName, toTypeName)); + context.ReportDiagnostic(Diagnostic.Create(this.Rules["SMAPI001"], context.Node.GetLocation(), operand, operandType.Type.Name, operandType.ConvertedType)); return true; }