Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SinZ163 committed Nov 12, 2023
1 parent 8e578c0 commit ca6a1ac
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void EmptyCode_HasNoDiagnostics()
/// <param name="expression">The expression which should be reported.</param>
/// <param name="netType">The net type name which should be reported.</param>
/// <param name="suggestedProperty">The suggested property name which should be reported.</param>
[TestCase("Game1.content.Load<Dictionary<int, string>>(\"Data\\\\Fish\");", 0, "Data\\Fish", "System.Collections.Generic.Dictionary<System.Int32, System.String>", "System.Collections.Generic.Dictionary<System.String,System.String>")]
[TestCase("Game1.content.Load<Dictionary<int, string>>(\"Data\\\\Fish\");", 0, "Data\\Fish", "System.Collections.Generic.Dictionary<System.Int32,System.String>", "System.Collections.Generic.Dictionary<System.String,System.String>")]
public void BadType_RaisesDiagnostic(string codeText, int column, string assetName, string expectedType, string suggestedType)
{
// arrange
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace StardewValley
{
public class Game1
{
public static LocalizedContentManager content = new();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ReSharper disable CheckNamespace, InconsistentNaming -- matches Stardew Valley's code
// ReSharper disable UnusedMember.Global -- used dynamically for unit tests
namespace StardewValley
{
/// <summary>A simplified version of Stardew Valley's <c>StardewValley.LocalizedContentManager</c> class for unit testing.</summary>
public class LocalizedContentManager
{
public T Load<T>(string assetName)
{
return default!;
}
}
}
5 changes: 5 additions & 0 deletions src/SMAPI.ModBuildConfig.Analyzer/ContentManagerAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ private void AnalyzeContentManagerLoads(SyntaxNodeAnalysisContext context)
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if (memberAccess == null || memberAccess.Name.Identifier.ValueText != "Load")
return;
string? loadNamespace = context.SemanticModel.GetSymbolInfo(memberAccess).Symbol?.ContainingNamespace.Name;
if (!(loadNamespace == "StardewValley" || loadNamespace == "StardewModdingAPI"))
return;
// "Data\\Fish" -> Data\Fish
string assetName = invocation.ArgumentList.Arguments[0].ToString().Replace("\"", "").Replace("\\\\", "\\");

Expand All @@ -78,6 +81,8 @@ private void AnalyzeContentManagerLoads(SyntaxNodeAnalysisContext context)

if (this.OneSixRules.AssetMap.TryGetValue(assetName, out string expectedType))
{
// delete `3 as I can't convince Roslyn and Cecil to agree
// TODO: Move into DataGeneration
expectedType = Regex.Replace(expectedType, "`\\d+", "");
if (genericArgument != expectedType)
{
Expand Down

0 comments on commit ca6a1ac

Please sign in to comment.