From 60ee5eadd22dae26c7a0bcda0c1966bce7607a7e Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Mon, 27 Apr 2020 19:27:05 -0700 Subject: [PATCH] Adds testing (#118) --- .circleci/config.yml | 21 ++++++++++- .gitignore | 7 ++-- ModernUO.sln | 16 +++++++++ Projects/Scripts.Tests/EmptyTests.cs | 13 +++++++ Projects/Scripts.Tests/Scripts.Tests.csproj | 12 +++++++ .../Network/Packets/DamageOldPacketTests.cs | 36 +++++++++++++++++++ Projects/Server.Tests/Server.Tests.csproj | 16 +++++++++ Projects/Server.Tests/ServerFixture.cs | 24 +++++++++++++ Projects/Server/Map.cs | 9 ++--- Projects/Server/Serial.cs | 2 +- 10 files changed, 143 insertions(+), 13 deletions(-) create mode 100644 Projects/Scripts.Tests/EmptyTests.cs create mode 100644 Projects/Scripts.Tests/Scripts.Tests.csproj create mode 100644 Projects/Server.Tests/Network/Packets/DamageOldPacketTests.cs create mode 100644 Projects/Server.Tests/Server.Tests.csproj create mode 100644 Projects/Server.Tests/ServerFixture.cs diff --git a/.circleci/config.yml b/.circleci/config.yml index 1a6e3dcb0..53c4fa85c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -67,10 +67,29 @@ jobs: - run: name: Building with Analyzers command: dotnet build -c Analyze /p:PublishProfile=Linux - + tests: + description: Unit Tests + executor: linux-build + steps: + - run: rm -rf ~/project/.git # CircleCI git caching is likely broken + - checkout + - restore_cache: + keys: + - dotnet-packages-linux-{{ checksum "Projects/Server/Server.csproj" }}-{{ checksum "Projects/Scripts/Scripts.csproj" }} + - run: + name: Install ModernUO dependencies + command: dotnet restore + - save_cache: + paths: + - ~/.nuget/packages + key: dotnet-packages-linux-{{ checksum "Projects/Server/Server.csproj" }}-{{ checksum "Projects/Scripts/Scripts.csproj" }} + - run: + name: Running unit tests + command: dotnet test workflows: build-and-test: jobs: - build_linux - build_windows - code_styling + - tests diff --git a/.gitignore b/.gitignore index 1469a7d1c..b814ef215 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ /Distribution/System.ComponentModel.Annotations.dll /Distribution/System.Runtime.CompilerServices.Unsafe.dll /Distribution/Data/modernuo.json +/Distribution/runtimes # LibUv Dependencies /Distribution/libuv.dylib @@ -29,10 +30,8 @@ /Distribution/Microsoft.Extensions.Options.dll /Distribution/Microsoft.Extensions.Primitives.dll -/Projects/Scripts/obj -/Projects/Scripts/bin -/Projects/Server/obj -/Projects/Server/bin +/Projects/*/obj +/Projects/*/bin *.log *.user diff --git a/ModernUO.sln b/ModernUO.sln index 8c149d643..a2edfb108 100644 --- a/ModernUO.sln +++ b/ModernUO.sln @@ -6,6 +6,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Server", "Projects\Server\S EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Scripts", "Projects\Scripts\Scripts.csproj", "{83CF2484-BCCB-4B7C-9C5F-7AB43AEA5E8F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Server.Tests", "Projects\Server.Tests\Server.Tests.csproj", "{D7A5D3AF-D607-46EF-BAAD-0D424190311F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Scripts.Tests", "Projects\Scripts.Tests\Scripts.Tests.csproj", "{3C4797F9-603E-44EF-8E8C-9275CC9EA74B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Analyze|Any CPU = Analyze|Any CPU @@ -25,6 +29,18 @@ Global {83CF2484-BCCB-4B7C-9C5F-7AB43AEA5E8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {83CF2484-BCCB-4B7C-9C5F-7AB43AEA5E8F}.Release|Any CPU.ActiveCfg = Release|Any CPU {83CF2484-BCCB-4B7C-9C5F-7AB43AEA5E8F}.Release|Any CPU.Build.0 = Release|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Analyze|Any CPU.ActiveCfg = Debug|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Analyze|Any CPU.Build.0 = Debug|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D7A5D3AF-D607-46EF-BAAD-0D424190311F}.Release|Any CPU.Build.0 = Release|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Analyze|Any CPU.ActiveCfg = Debug|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Analyze|Any CPU.Build.0 = Debug|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3C4797F9-603E-44EF-8E8C-9275CC9EA74B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Projects/Scripts.Tests/EmptyTests.cs b/Projects/Scripts.Tests/EmptyTests.cs new file mode 100644 index 000000000..a3b623714 --- /dev/null +++ b/Projects/Scripts.Tests/EmptyTests.cs @@ -0,0 +1,13 @@ +using Xunit; + +namespace Server.Tests +{ + public class EmptyTests + { + [Fact] + public void EmptyTest() + { + Assert.True(true); + } + } +} diff --git a/Projects/Scripts.Tests/Scripts.Tests.csproj b/Projects/Scripts.Tests/Scripts.Tests.csproj new file mode 100644 index 000000000..7b6f91feb --- /dev/null +++ b/Projects/Scripts.Tests/Scripts.Tests.csproj @@ -0,0 +1,12 @@ + + + netcoreapp3.1 + false + + + + + + + + diff --git a/Projects/Server.Tests/Network/Packets/DamageOldPacketTests.cs b/Projects/Server.Tests/Network/Packets/DamageOldPacketTests.cs new file mode 100644 index 000000000..0a22cf266 --- /dev/null +++ b/Projects/Server.Tests/Network/Packets/DamageOldPacketTests.cs @@ -0,0 +1,36 @@ +using System; +using System.Buffers.Binary; +using Server.Network; +using Xunit; + +namespace Server.Tests.Network.Packets +{ + public class DamageOldPacketTests : IClassFixture + { + private Mobile mobile; + + public DamageOldPacketTests(ServerFixture fixture) => mobile = fixture.mobile; + + [Theory] + [InlineData(10, 10)] + [InlineData(-5, 0)] + [InlineData(1024, 255)] + public void TestDamagePacketOld(int inputAmount, byte expectedAmount) + { + DamagePacketOld packet = new DamagePacketOld(mobile, inputAmount); + + Span data = packet.Compile(false, out int length).AsSpan(0, length); + + byte[] expectedData = { + 0xBF, // Packet + 0x00, 0x0B, // Length + 0x00, 0x22, // Sub-packet + 0x01, // Command + 0x00, 0x00, 0x00, 0x01, // Serial + expectedAmount // Amount + }; + + Assert.Equal(data.ToArray(), expectedData); + } + } +} diff --git a/Projects/Server.Tests/Server.Tests.csproj b/Projects/Server.Tests/Server.Tests.csproj new file mode 100644 index 000000000..09142eb9b --- /dev/null +++ b/Projects/Server.Tests/Server.Tests.csproj @@ -0,0 +1,16 @@ + + + netcoreapp3.1 + false + + + + + + + + + + + + diff --git a/Projects/Server.Tests/ServerFixture.cs b/Projects/Server.Tests/ServerFixture.cs new file mode 100644 index 000000000..df6426e79 --- /dev/null +++ b/Projects/Server.Tests/ServerFixture.cs @@ -0,0 +1,24 @@ +using System; +using Server.Misc; + +namespace Server.Tests +{ + public class ServerFixture : IDisposable + { + public Mobile mobile { get; } + + public ServerFixture() + { + // Configure / Initialize + MapDefinitions.Configure(); + + World.Load(); + + mobile = new Mobile(0x1); + } + + public void Dispose() + { + } + } +} diff --git a/Projects/Server/Map.cs b/Projects/Server/Map.cs index f2ea98043..30a8d2eee 100644 --- a/Projects/Server/Map.cs +++ b/Projects/Server/Map.cs @@ -1130,13 +1130,8 @@ namespace Server public bool CanSpawnMobile(Point2D p, int z) => CanSpawnMobile(p.m_X, p.m_Y, z); - public bool CanSpawnMobile(int x, int y, int z) - { - if (!Region.Find(new Point3D(x, y, z), this).AllowSpawn()) - return false; - - return CanFit(x, y, z, 16); - } + public bool CanSpawnMobile(int x, int y, int z) => + Region.Find(new Point3D(x, y, z), this).AllowSpawn() && CanFit(x, y, z, 16); public Sector GetSector(Point3D p) => InternalGetSector(p.m_X >> SectorShift, p.m_Y >> SectorShift); diff --git a/Projects/Server/Serial.cs b/Projects/Server/Serial.cs index c518816fd..b7fb5e788 100644 --- a/Projects/Server/Serial.cs +++ b/Projects/Server/Serial.cs @@ -35,7 +35,7 @@ namespace Server { get { - while (World.FindMobile(LastMobile = LastMobile + 1) != null) + while (World.FindMobile(LastMobile += 1) != null) { }