From ff2e5b87369db2e6dd970bf22e5773c695cf4494 Mon Sep 17 00:00:00 2001 From: Mister_Nebula <41904486+misternebula@users.noreply.github.com> Date: Fri, 12 Feb 2021 13:29:01 +0000 Subject: [PATCH] add tests --- QSB.sln | 6 +++ QSB/Events/QSBEvent.cs | 4 ++ QSB/Events/QSBEventManager.cs | 5 ++ QSB/QSB.csproj | 1 + QSB/Utility/UnitTestDetector.cs | 17 +++++++ QSBTests/EventTests.cs | 41 ++++++++++++++++ QSBTests/Properties/AssemblyInfo.cs | 20 ++++++++ QSBTests/QSBTests.csproj | 75 +++++++++++++++++++++++++++++ QSBTests/UtilityTests.cs | 13 +++++ QSBTests/packages.config | 5 ++ 10 files changed, 187 insertions(+) create mode 100644 QSB/Utility/UnitTestDetector.cs create mode 100644 QSBTests/EventTests.cs create mode 100644 QSBTests/Properties/AssemblyInfo.cs create mode 100644 QSBTests/QSBTests.csproj create mode 100644 QSBTests/UtilityTests.cs create mode 100644 QSBTests/packages.config diff --git a/QSB.sln b/QSB.sln index 95d71f00..51984e1c 100644 --- a/QSB.sln +++ b/QSB.sln @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QSB", "QSB\QSB.csproj", "{1 EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuantumUNET", "QuantumUNET\QuantumUNET.csproj", "{C8C53004-1508-4F86-A419-4292C188DC2A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QSBTests", "QSBTests\QSBTests.csproj", "{2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {C8C53004-1508-4F86-A419-4292C188DC2A}.Debug|Any CPU.Build.0 = Debug|Any CPU {C8C53004-1508-4F86-A419-4292C188DC2A}.Release|Any CPU.ActiveCfg = Release|Any CPU {C8C53004-1508-4F86-A419-4292C188DC2A}.Release|Any CPU.Build.0 = Release|Any CPU + {2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/QSB/Events/QSBEvent.cs b/QSB/Events/QSBEvent.cs index b11b8415..0d9ef042 100644 --- a/QSB/Events/QSBEvent.cs +++ b/QSB/Events/QSBEvent.cs @@ -17,6 +17,10 @@ namespace QSB.Events protected QSBEvent() { + if (UnitTestDetector.IsInUnitTest) + { + return; + } _eventHandler = new MessageHandler(Type); _eventHandler.OnClientReceiveMessage += message => OnReceive(false, message); _eventHandler.OnServerReceiveMessage += message => OnReceive(true, message); diff --git a/QSB/Events/QSBEventManager.cs b/QSB/Events/QSBEventManager.cs index 1d7e7559..be487eee 100644 --- a/QSB/Events/QSBEventManager.cs +++ b/QSB/Events/QSBEventManager.cs @@ -64,6 +64,11 @@ namespace QSB.Events new IdentifySignalEvent() }; + if (UnitTestDetector.IsInUnitTest) + { + return; + } + _eventList.ForEach(ev => ev.SetupListener()); Ready = true; diff --git a/QSB/QSB.csproj b/QSB/QSB.csproj index 08027e97..ee3539ca 100644 --- a/QSB/QSB.csproj +++ b/QSB/QSB.csproj @@ -222,6 +222,7 @@ + diff --git a/QSB/Utility/UnitTestDetector.cs b/QSB/Utility/UnitTestDetector.cs new file mode 100644 index 00000000..439f3584 --- /dev/null +++ b/QSB/Utility/UnitTestDetector.cs @@ -0,0 +1,17 @@ +using System; +using System.Linq; + +namespace QSB.Utility +{ + public static class UnitTestDetector + { + static UnitTestDetector() + { + var testAssemblyName = "Microsoft.VisualStudio.TestPlatform.TestFramework"; + IsInUnitTest = AppDomain.CurrentDomain.GetAssemblies() + .Any(a => a.FullName.StartsWith(testAssemblyName)); + } + + public static bool IsInUnitTest { get; private set; } + } +} diff --git a/QSBTests/EventTests.cs b/QSBTests/EventTests.cs new file mode 100644 index 00000000..bfb4d7c2 --- /dev/null +++ b/QSBTests/EventTests.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using QSB.Events; + +namespace QSBTests +{ + [TestClass] + public class EventTests + { + [TestMethod] + public void CheckUnreferencedEvents() + { + var qsbAssembly = Assembly.Load("QSB"); + var allEventTypes = qsbAssembly + .GetTypes() + .Where(x => typeof(IQSBEvent).IsAssignableFrom(x) && !x.IsInterface && !x.IsAbstract); + + QSBEventManager.Init(); + var eventInstances = (List)typeof(QSBEventManager) + .GetField("_eventList", BindingFlags.NonPublic | BindingFlags.Static) + .GetValue(typeof(QSBEventManager)); + + var failedTypes = new List(); + foreach (var type in allEventTypes) + { + if (!eventInstances.Any(x => x.GetType() == type)) + { + failedTypes.Add(type); + } + } + + if (failedTypes.Count > 0) + { + Assert.Fail(string.Join(", ", failedTypes.Select(x => x.Name))); + } + } + } +} diff --git a/QSBTests/Properties/AssemblyInfo.cs b/QSBTests/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..825ccfea --- /dev/null +++ b/QSBTests/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("QSBTests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("QSBTests")] +[assembly: AssemblyCopyright("Copyright © 2021")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("2fe8256c-0cb6-48f1-a4c0-5fa18a1846a3")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/QSBTests/QSBTests.csproj b/QSBTests/QSBTests.csproj new file mode 100644 index 00000000..4507a8a3 --- /dev/null +++ b/QSBTests/QSBTests.csproj @@ -0,0 +1,75 @@ + + + + + + Debug + AnyCPU + {2FE8256C-0CB6-48F1-A4C0-5FA18A1846A3} + Library + Properties + QSBTests + QSBTests + v4.7.2 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + {1F00090A-C697-4C55-B401-192F3CFB9DC2} + QSB + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + \ No newline at end of file diff --git a/QSBTests/UtilityTests.cs b/QSBTests/UtilityTests.cs new file mode 100644 index 00000000..8ed47ceb --- /dev/null +++ b/QSBTests/UtilityTests.cs @@ -0,0 +1,13 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using QSB.Utility; + +namespace QSBTests +{ + [TestClass] + public class UtilityTests + { + [TestMethod] + public void IsInUnitTest() + => Assert.IsTrue(UnitTestDetector.IsInUnitTest, "UnitTestDetector is not working."); + } +} diff --git a/QSBTests/packages.config b/QSBTests/packages.config new file mode 100644 index 00000000..2f7c5a18 --- /dev/null +++ b/QSBTests/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file