move mock classes out of sample code (#471)

This commit is contained in:
Jesse Plamondon-Willard 2018-04-10 18:22:16 -04:00
parent 971ed1a175
commit 9e5c3912b6
7 changed files with 57 additions and 24 deletions

View File

@ -20,6 +20,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework
private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);
private static readonly MetadataReference SelfReference = MetadataReference.CreateFromFile(typeof(DiagnosticVerifier).Assembly.Location);
internal static string DefaultFilePathPrefix = "Test";
internal static string CSharpDefaultFileExt = "cs";
@ -150,6 +151,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests.Framework
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, DiagnosticVerifier.SelfReference)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)

View File

@ -0,0 +1,16 @@
// ReSharper disable CheckNamespace -- matches Stardew Valley's code
namespace Netcode
{
/// <summary>A simplified version of Stardew Valley's <c>Netcode.NetFieldBase</c> for unit testing.</summary>
/// <typeparam name="T">The type of the synchronised value.</typeparam>
/// <typeparam name="TSelf">The type of the current instance.</typeparam>
public class NetFieldBase<T, TSelf> where TSelf : NetFieldBase<T, TSelf>
{
/// <summary>The synchronised value.</summary>
public T Value { get; set; }
/// <summary>Implicitly convert a net field to the its type.</summary>
/// <param name="field">The field to convert.</param>
public static implicit operator T(NetFieldBase<T, TSelf> field) => field.Value;
}
}

View File

@ -0,0 +1,6 @@
// ReSharper disable CheckNamespace -- matches Stardew Valley's code
namespace Netcode
{
/// <summary>A simplified version of Stardew Valley's <c>Netcode.NetInt</c> for unit testing.</summary>
public class NetInt : NetFieldBase<int, NetInt> { }
}

View File

@ -0,0 +1,6 @@
// ReSharper disable CheckNamespace -- matches Stardew Valley's code
namespace Netcode
{
/// <summary>A simplified version of Stardew Valley's <c>Netcode.NetRef</c> for unit testing.</summary>
public class NetRef : NetFieldBase<object, NetRef> { }
}

View File

@ -0,0 +1,18 @@
// ReSharper disable CheckNamespace, InconsistentNaming -- matches Stardew Valley's code
using Netcode;
namespace StardewValley
{
/// <summary>A simplified version of Stardew Valley's <c>StardewValley.Item</c> class for unit testing.</summary>
public class Item
{
/// <summary>A net int field with an equivalent non-net <c>Category</c> property.</summary>
public NetInt category { get; } = new NetInt { Value = 42 };
/// <summary>A net int field with no equivalent non-net property.</summary>
public NetInt type { get; } = new NetInt { Value = 42 };
/// <summary>A net reference field.</summary>
public NetRef refField { get; } = null;
}
}

View File

@ -0,0 +1,6 @@
// ReSharper disable CheckNamespace -- matches Stardew Valley's code
namespace StardewValley
{
/// <summary>A simplified version of Stardew Valley's <c>StardewValley.Object</c> class for unit testing.</summary>
public class Object : Item { }
}

View File

@ -18,28 +18,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests
using System;
using StardewValley;
using Netcode;
namespace Netcode
{
public class NetInt : NetFieldBase<int, NetInt> { }
public class NetRef : NetFieldBase<object, NetRef> { }
public class NetFieldBase<T, TSelf> where TSelf : NetFieldBase<T, TSelf>
{
public T Value { get; set; }
public static implicit operator T(NetFieldBase<T, TSelf> field) => field.Value;
}
}
namespace StardewValley
{
class Item
{
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;
}
class SObject : Item { }
}
using SObject = StardewValley.Object;
namespace SampleMod
{
@ -51,7 +30,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests
SObject obj = null;
// this line should raise diagnostics
{{test-code}} // line 38
{{test-code}}
// these lines should not
if (item.type.Value != 42);
@ -61,7 +40,7 @@ namespace SMAPI.ModBuildConfig.Analyzer.Tests
";
/// <summary>The line number where the unit tested code is injected into <see cref="SampleProgram"/>.</summary>
private const int SampleCodeLine = 38;
private const int SampleCodeLine = 17;
/// <summary>The column number where the unit tested code is injected into <see cref="SampleProgram"/>.</summary>
private const int SampleCodeColumn = 25;