using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; using NUnit.Framework; using SMAPI.ModBuildConfig.Analyzer.Tests.Framework; using StardewModdingAPI.ModBuildConfig.Analyzer; namespace SMAPI.ModBuildConfig.Analyzer.Tests { /// Unit tests for . [TestFixture] public class ObsoleteFieldAnalyzerTests : DiagnosticVerifier { /********* ** Properties *********/ /// Sample C# mod code, with a {{test-code}} placeholder for the code in the Entry method to test. const string SampleProgram = @" using System; using StardewValley; using Netcode; using SObject = StardewValley.Object; namespace SampleMod { class ModEntry { public void Entry() { {{test-code}} } } } "; /// The line number where the unit tested code is injected into . private const int SampleCodeLine = 13; /// The column number where the unit tested code is injected into . private const int SampleCodeColumn = 25; /********* ** Unit tests *********/ /// Test that no diagnostics are raised for an empty code block. [TestCase] public void EmptyCode_HasNoDiagnostics() { // arrange string test = @""; // assert this.VerifyCSharpDiagnostic(test); } /// Test that the expected diagnostic message is raised for an obsolete field reference. /// The code line to test. /// The column within the code line where the diagnostic message should be reported. /// The old field name which should be reported. /// The new field name which should be reported. [TestCase("var x = new Farmer().friendships;", 8, "StardewValley.Farmer.friendships", "friendshipData")] public void AvoidObsoleteField_RaisesDiagnostic(string codeText, int column, string oldName, string newName) { // arrange string code = ObsoleteFieldAnalyzerTests.SampleProgram.Replace("{{test-code}}", codeText); DiagnosticResult expected = new DiagnosticResult { Id = "SMAPI003", Message = $"The '{oldName}' field is obsolete and should be replaced with '{newName}'. See https://smapi.io/buildmsg/smapi003 for details.", Severity = DiagnosticSeverity.Warning, Locations = new[] { new DiagnosticResultLocation("Test0.cs", ObsoleteFieldAnalyzerTests.SampleCodeLine, ObsoleteFieldAnalyzerTests.SampleCodeColumn + column) } }; // assert this.VerifyCSharpDiagnostic(code, expected); } /********* ** Helpers *********/ /// Get the analyzer being tested. protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() { return new ObsoleteFieldAnalyzer(); } } }