From 9852c42ca14b9b8d85f401a058f3d993d90c1b6f Mon Sep 17 00:00:00 2001 From: Evan Dixon Date: Wed, 10 Aug 2016 09:24:40 -0500 Subject: [PATCH] Added tests for DateUtil --- PKHeX.sln | 19 ++++ PKHeX/Util/DateUtil.cs | 2 +- Tests/PKHeX.Tests/PKHeX.Tests.csproj | 89 +++++++++++++++ Tests/PKHeX.Tests/Properties/AssemblyInfo.cs | 36 ++++++ Tests/PKHeX.Tests/Util/DateUtilTests.cs | 112 +++++++++++++++++++ 5 files changed, 257 insertions(+), 1 deletion(-) create mode 100644 Tests/PKHeX.Tests/PKHeX.Tests.csproj create mode 100644 Tests/PKHeX.Tests/Properties/AssemblyInfo.cs create mode 100644 Tests/PKHeX.Tests/Util/DateUtilTests.cs diff --git a/PKHeX.sln b/PKHeX.sln index 7599a9cb6..b990fba3c 100644 --- a/PKHeX.sln +++ b/PKHeX.sln @@ -5,18 +5,37 @@ VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKHeX", "PKHeX\PKHeX.csproj", "{B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PKHeX.Tests", "Tests\PKHeX.Tests\PKHeX.Tests.csproj", "{8E2499BC-C11A-4809-8737-66D35A625425}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{7C0598C9-DDF3-4ACC-B15D-6A626ADB7530}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Debug|Any CPU.ActiveCfg = Debug|x86 {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Debug|x86.ActiveCfg = Debug|x86 {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Debug|x86.Build.0 = Debug|x86 + {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Release|Any CPU.ActiveCfg = Release|x86 {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Release|x86.ActiveCfg = Release|x86 {B4EFF030-C75A-49F9-A4BC-738D1B61C4AF}.Release|x86.Build.0 = Release|x86 + {8E2499BC-C11A-4809-8737-66D35A625425}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Debug|x86.ActiveCfg = Debug|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Debug|x86.Build.0 = Debug|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Release|Any CPU.Build.0 = Release|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Release|x86.ActiveCfg = Release|Any CPU + {8E2499BC-C11A-4809-8737-66D35A625425}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {8E2499BC-C11A-4809-8737-66D35A625425} = {7C0598C9-DDF3-4ACC-B15D-6A626ADB7530} + EndGlobalSection EndGlobal diff --git a/PKHeX/Util/DateUtil.cs b/PKHeX/Util/DateUtil.cs index 702410538..c7edda0ae 100644 --- a/PKHeX/Util/DateUtil.cs +++ b/PKHeX/Util/DateUtil.cs @@ -28,7 +28,7 @@ public static bool IsDateValid(int year, int month, int day) /// A boolean indicating whether or not the date is valid. public static bool IsDateValid(uint year, uint month, uint day) { - return !(year < 0 || year > DateTime.MaxValue.Year || month < 1 || month > 12 || day < 1 || day > DateTime.DaysInMonth((int)year, (int)month)); + return year < int.MaxValue && month < int.MaxValue && day < int.MaxValue && IsDateValid((int)year, (int)month, (int)day); } } } diff --git a/Tests/PKHeX.Tests/PKHeX.Tests.csproj b/Tests/PKHeX.Tests/PKHeX.Tests.csproj new file mode 100644 index 000000000..88f9d09a7 --- /dev/null +++ b/Tests/PKHeX.Tests/PKHeX.Tests.csproj @@ -0,0 +1,89 @@ + + + + Debug + AnyCPU + {8E2499BC-C11A-4809-8737-66D35A625425} + Library + Properties + PKHeX.Tests + PKHeX.Tests + v4.6 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 10.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 + + + + + + + + + + + + + + + + + + + + + + + {b4eff030-c75a-49f9-a4bc-738d1b61c4af} + PKHeX + + + + + + + False + + + False + + + False + + + False + + + + + + + + \ No newline at end of file diff --git a/Tests/PKHeX.Tests/Properties/AssemblyInfo.cs b/Tests/PKHeX.Tests/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..24b24f48a --- /dev/null +++ b/Tests/PKHeX.Tests/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +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("PKHeX.Tests")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PKHeX.Tests")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[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("8e2499bc-c11a-4809-8737-66d35a625425")] + +// 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")] diff --git a/Tests/PKHeX.Tests/Util/DateUtilTests.cs b/Tests/PKHeX.Tests/Util/DateUtilTests.cs new file mode 100644 index 000000000..4356003de --- /dev/null +++ b/Tests/PKHeX.Tests/Util/DateUtilTests.cs @@ -0,0 +1,112 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PKHeX.Tests.Util +{ + [TestClass] + public class DateUtilTests + { + const string DateUtilCategory = "Date Util Tests"; + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void RecognizesCorrectDates() + { + Assert.IsTrue(PKHeX.Util.IsDateValid(2000, 1, 1), "Failed to recognize 1/1/2000"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2001, 1, 31), "Failed to recognize 1/31/2001"); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void MonthBoundaries() + { + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 1, 31), "Incorrect month boundary for January"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 2, 28), "Incorrect month boundary for February"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 3, 31), "Incorrect month boundary for March"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 4, 30), "Incorrect month boundary for April"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 5, 31), "Incorrect month boundary for May"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 6, 30), "Incorrect month boundary for June"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 7, 31), "Incorrect month boundary for July"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 8, 31), "Incorrect month boundary for August"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 9, 30), "Incorrect month boundary for September"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 10, 31), "Incorrect month boundary for October"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 11, 30), "Incorrect month boundary for November"); + Assert.IsTrue(PKHeX.Util.IsDateValid(2016, 12, 31), "Incorrect month boundary for December"); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void RecognizeCorrectLeapYear() + { + Assert.IsTrue(PKHeX.Util.IsDateValid(2004, 2, 29)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithIncorrectLeapYear() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(2005, 2, 29)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithZeroDate() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(0, 0, 0)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithNegativeDate() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(-1, -1, -1)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithBigDay() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(2000, 1, 32)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithBigMonth() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(2000, 13, 1)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithBigYear() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(10000, 1, 1)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithZeroDay() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(2000, 1, 0)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void FailsWithZeroMonth() + { + Assert.IsFalse(PKHeX.Util.IsDateValid(2000, 0, 1)); + } + + [TestMethod] + [TestCategory(DateUtilCategory)] + public void TestUIntOverload() + { + Assert.IsTrue(PKHeX.Util.IsDateValid((uint)2000, (uint)1, (uint)1), "Failed 1/1/2000"); + Assert.IsFalse(PKHeX.Util.IsDateValid(uint.MaxValue, uint.MaxValue, uint.MaxValue), "Failed with uint.MaxValue"); + } + } +}