From dc8c6e215d699e96da8ddb5afb29cdb0d741091c Mon Sep 17 00:00:00 2001 From: Oliver Bevan Date: Fri, 2 Aug 2024 23:14:38 +0100 Subject: [PATCH] Add unit tests Change namespace of GameCubeBanner --- .github/workflows/build.yml | 8 +- IndustrialPark.sln | 6 ++ .../MainForm/CreateGameCubeBanner.cs | 1 - IndustrialPark/Other/GameCubeBanner.cs | 2 +- IndustrialParkTest/GameCubeBannerTests.cs | 66 +++++++++++++++++ IndustrialParkTest/IndustrialParkTest.csproj | 73 +++++++++++++++++++ IndustrialParkTest/Properties/AssemblyInfo.cs | 35 +++++++++ IndustrialParkTest/packages.config | 9 +++ 8 files changed, 197 insertions(+), 3 deletions(-) create mode 100644 IndustrialParkTest/GameCubeBannerTests.cs create mode 100644 IndustrialParkTest/IndustrialParkTest.csproj create mode 100644 IndustrialParkTest/Properties/AssemblyInfo.cs create mode 100644 IndustrialParkTest/packages.config diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7004613b..f6ed3a7d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -25,4 +25,10 @@ jobs: run: nuget restore IndustrialPark.sln - name: Build solution - run: msbuild IndustrialPark.sln -t:rebuild -property:Configuration=Release -property:platform="Any CPU" \ No newline at end of file + run: msbuild IndustrialPark.sln -t:rebuild -property:Configuration=Release -property:platform="Any CPU" + + - name: Run unit tests + uses: microsoft/vstest-action@v1.0.0 + with: + testAssembly: '**\bin\Release\**\*Test.dll' + diff --git a/IndustrialPark.sln b/IndustrialPark.sln index d112c3fa..864f1d1e 100644 --- a/IndustrialPark.sln +++ b/IndustrialPark.sln @@ -18,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Randomizer", "IndustrialPar EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ArchiveEditor", "ArchiveEditor\ArchiveEditor.csproj", "{2B20165D-B6D3-4FA5-93B4-64EC117BA732}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IndustrialParkTest", "IndustrialParkTest\IndustrialParkTest.csproj", "{BD22CB98-D975-4E37-8707-9FA07C067806}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -48,6 +50,10 @@ Global {2B20165D-B6D3-4FA5-93B4-64EC117BA732}.Debug|Any CPU.Build.0 = Debug|Any CPU {2B20165D-B6D3-4FA5-93B4-64EC117BA732}.Release|Any CPU.ActiveCfg = Release|Any CPU {2B20165D-B6D3-4FA5-93B4-64EC117BA732}.Release|Any CPU.Build.0 = Release|Any CPU + {BD22CB98-D975-4E37-8707-9FA07C067806}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BD22CB98-D975-4E37-8707-9FA07C067806}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BD22CB98-D975-4E37-8707-9FA07C067806}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BD22CB98-D975-4E37-8707-9FA07C067806}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/IndustrialPark/MainForm/CreateGameCubeBanner.cs b/IndustrialPark/MainForm/CreateGameCubeBanner.cs index 89b6d5ae..e06d8414 100644 --- a/IndustrialPark/MainForm/CreateGameCubeBanner.cs +++ b/IndustrialPark/MainForm/CreateGameCubeBanner.cs @@ -3,7 +3,6 @@ using System.Drawing; using System.IO; using System.Windows.Forms; -using IndustrialPark.Other; using Image = System.Web.UI.WebControls.Image; namespace IndustrialPark diff --git a/IndustrialPark/Other/GameCubeBanner.cs b/IndustrialPark/Other/GameCubeBanner.cs index cd52047d..09b83bb2 100644 --- a/IndustrialPark/Other/GameCubeBanner.cs +++ b/IndustrialPark/Other/GameCubeBanner.cs @@ -4,7 +4,7 @@ using System.IO; using System.Text; -namespace IndustrialPark.Other +namespace IndustrialPark { /// /// Represents a (single-language) GameCube banner (BNR1). diff --git a/IndustrialParkTest/GameCubeBannerTests.cs b/IndustrialParkTest/GameCubeBannerTests.cs new file mode 100644 index 00000000..9c94fb0f --- /dev/null +++ b/IndustrialParkTest/GameCubeBannerTests.cs @@ -0,0 +1,66 @@ +using System.Drawing; +using Xunit; +using IndustrialPark; + +namespace IndustrialParkTest +{ + public class GameCubeBannerTests + { + private readonly GameCubeBanner banner; + + public GameCubeBannerTests() + { + banner = new GameCubeBanner() + { + Title = "Test Title", + TitleFull = "Test Full Title", + Creator = "Test Creator", + CreatorFull = "Test Full Creator", + Description = "Test Description", + Image = new Bitmap(96, 32) + }; + } + + [Fact] + public void SaveToFile_Output_Banner_Filesize_Is_Correct() + { + int expectedFilesizeBytes = 0x1960; + banner.SaveToFile("test.bnr"); + Assert.Equal(expectedFilesizeBytes, new System.IO.FileInfo("test.bnr").Length); + System.IO.File.Delete("test.bnr"); + } + + [Fact] + public void SaveToFile_Magic_Bytes_Are_Correct() + { + string expectedMagic = "BNR1"; + banner.SaveToFile("test.bnr"); + + using (System.IO.BinaryReader reader = new System.IO.BinaryReader( + System.IO.File.Open("test.bnr", System.IO.FileMode.Open))) + { + string magic = new string(reader.ReadChars(4)); + Assert.Equal(expectedMagic, magic); + } + System.IO.File.Delete("test.bnr"); + } + + [Fact] + public void SaveToFile_Short_Game_Title_Is_Correct() + { + string expectedTitle = "Test Title"; + + banner.SaveToFile("test.bnr"); + + using (System.IO.BinaryReader reader = new System.IO.BinaryReader( + System.IO.File.Open("test.bnr", System.IO.FileMode.Open))) + { + // Game title begins at 0x1820 + reader.BaseStream.Seek(0x1820, System.IO.SeekOrigin.Begin); + string title = new string(reader.ReadChars(0x20)).TrimEnd('\0'); + Assert.Equal(expectedTitle, title); + } + System.IO.File.Delete("test.bnr"); + } + } +} \ No newline at end of file diff --git a/IndustrialParkTest/IndustrialParkTest.csproj b/IndustrialParkTest/IndustrialParkTest.csproj new file mode 100644 index 00000000..d553d2d2 --- /dev/null +++ b/IndustrialParkTest/IndustrialParkTest.csproj @@ -0,0 +1,73 @@ + + + + + Debug + AnyCPU + {BD22CB98-D975-4E37-8707-9FA07C067806} + {FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Library + Properties + IndustrialParkTest + IndustrialParkTest + v4.8 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + ..\packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll + + + ..\packages\xunit.assert.2.1.0\lib\dotnet\xunit.assert.dll + + + ..\packages\xunit.extensibility.core.2.1.0\lib\dotnet\xunit.core.dll + + + ..\packages\xunit.extensibility.execution.2.1.0\lib\net45\xunit.execution.desktop.dll + + + + + + + + + {62f1a222-225b-463f-85a7-70f56c34c324} + IndustrialPark + + + + + diff --git a/IndustrialParkTest/Properties/AssemblyInfo.cs b/IndustrialParkTest/Properties/AssemblyInfo.cs new file mode 100644 index 00000000..78c949ea --- /dev/null +++ b/IndustrialParkTest/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("IndustrialParkTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("IndustrialParkTest")] +[assembly: AssemblyCopyright("Copyright © 2024")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("BD22CB98-D975-4E37-8707-9FA07C067806")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/IndustrialParkTest/packages.config b/IndustrialParkTest/packages.config new file mode 100644 index 00000000..69a4ec43 --- /dev/null +++ b/IndustrialParkTest/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file