From fa5ed36f6346ea2c373ccabb96dc99f3bf41cc33 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Wed, 6 Jan 2021 18:34:35 +1000 Subject: [PATCH] Add boilerplate for unit testing GameSharkDecoder should fail without the following commit --- .../Client.Common/cheats/CheatDecoderTests.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/BizHawk.Tests/Client.Common/cheats/CheatDecoderTests.cs diff --git a/src/BizHawk.Tests/Client.Common/cheats/CheatDecoderTests.cs b/src/BizHawk.Tests/Client.Common/cheats/CheatDecoderTests.cs new file mode 100644 index 0000000000..57a4bd9733 --- /dev/null +++ b/src/BizHawk.Tests/Client.Common/cheats/CheatDecoderTests.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +using Microsoft.VisualStudio.TestTools.UnitTesting; + +using BizHawk.Client.Common; +using BizHawk.Client.Common.cheats; + +namespace BizHawk.Tests.Client.Common.cheats +{ + [TestClass] + public class CheatDecoderTests + { + [AttributeUsage(AttributeTargets.Method)] + private sealed class CheatcodeDataAttribute : Attribute, ITestDataSource + { + public bool GenerateNonsense { get; set; } = false; + + public IEnumerable GetData(MethodInfo methodInfo) + => GenerateNonsense ? NonsenseData : RealData; + + public string GetDisplayName(MethodInfo methodInfo, object?[] data) + => $"{methodInfo.Name}({string.Join(", ", data.Select(o => o is int i ? $"0x{i:X}" : o?.ToString() ?? "null"))})"; + } + + private const string ERROR_GBA_CODEBREAKER = "Codebreaker/GameShark SP/Xploder codes are not yet supported."; + + private static readonly int? NO_COMPARE = null; + + private static readonly IEnumerable NonsenseData = new[] + { + new[] { "GBA", "33003D0E0020", ERROR_GBA_CODEBREAKER }, + }; + + private static readonly IEnumerable RealData = new[] + { + new object?[] { "GBA", "4012F5B7 3B7801A6", 0x00000006, 0xB7, NO_COMPARE, WatchSize.Byte }, + new object?[] { "GBA", "686D7FC3 24B5B832", 0x00000032, 0x7FC3, NO_COMPARE, WatchSize.Word }, + }; + + [DataTestMethod] + [CheatcodeData] + public void TestCheatcodeParsing(string systemID, string code, int address, int value, int? compare, WatchSize size) + { + var result = new GameSharkDecoder(null, systemID).Decode(code); + Assert.IsTrue(result.IsValid(out var valid), "failed to parse"); + Assert.AreEqual(address, valid.Address, "wrong addr"); + Assert.AreEqual(size, valid.Size, "wrong size"); + Assert.AreEqual( + value, + valid.Size switch + { + WatchSize.Byte => valid.Value & 0xFF, + WatchSize.Word => valid.Value & 0xFFFF, + _ => valid.Value + }, + "wrong value"); + Assert.AreEqual(compare, valid.Compare, "wrong compare"); + } + + [DataTestMethod] + [CheatcodeData(GenerateNonsense = true)] + public void TestNonsenseParsing(string systemID, string code, string error) + { + var result = new GameSharkDecoder(null, systemID).Decode(code); + Assert.IsFalse(result.IsValid(out _), "parsed unexpectedly"); + Assert.AreEqual(error, result.Error, "wrong error msg"); + } + } +}