From 3dca0abc367f0cb754707be23d030a811f4b1ca0 Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Sat, 15 Feb 2014 17:06:02 +0100 Subject: [PATCH 1/4] More unit testing --- UnitTestProject/Strings.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/UnitTestProject/Strings.cs b/UnitTestProject/Strings.cs index eb2b5ff..b81b435 100644 --- a/UnitTestProject/Strings.cs +++ b/UnitTestProject/Strings.cs @@ -5,7 +5,19 @@ namespace UnitTestProject { [TestClass] public class Strings { + + [TestMethod] + public void WordCount() { + + Assert.AreEqual("".WordCount(), 0); + Assert.AreEqual("hello world".WordCount(), 2); + Assert.AreEqual(" hello world ".WordCount(), 2); + Assert.AreEqual("hello world".WordCount(), 2); + Assert.AreEqual("hello world!!!".WordCount(), 2); + //Assert.AreEqual("S.H.I.E.L.D.".WordCount(), 1); // Current logic consideres this a word per letter. Wrong? + } + [TestMethod] public void FirstCharToUpper() { @@ -16,6 +28,14 @@ public void FirstCharToUpper() { } + [TestMethod] + public void InvertCase() { + + Assert.AreEqual("HelloWorld".InvertCase(), "hELLOwORLD"); + Assert.AreEqual("helloworld".InvertCase(), "HELLOWORLD"); + Assert.AreEqual("Bacon".InvertCase(), "bACON"); + + } } From e3a668b00fb3889371c78cd042bfaf74cf1a422a Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Sat, 15 Feb 2014 17:31:24 +0100 Subject: [PATCH 2/4] Some YouTube improvements and unit testing --- ExtensionMethods/YouTube/YouTubeHelpers.cs | 2 +- UnitTestProject/UnitTestProject.csproj | 1 + UnitTestProject/YouTube.cs | 32 ++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 UnitTestProject/YouTube.cs diff --git a/ExtensionMethods/YouTube/YouTubeHelpers.cs b/ExtensionMethods/YouTube/YouTubeHelpers.cs index 2bb0862..1e92613 100644 --- a/ExtensionMethods/YouTube/YouTubeHelpers.cs +++ b/ExtensionMethods/YouTube/YouTubeHelpers.cs @@ -62,7 +62,7 @@ public static string GetIdFromString(string subject) { var tests = new[] { Regex.Match(subject, "^((\\w|-){11}$)"), Regex.Match(subject, "v=((\\w|-){11})"), - Regex.Match(subject, "\\/((\\w|-){11})$"), + Regex.Match(subject.Split('?')[0], "\\/((\\w|-){11})$"), Regex.Match(subject, "\\/vi|v|embed/((\\w|-){11})") }; diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index 7fa8604..9d175a6 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -50,6 +50,7 @@ + diff --git a/UnitTestProject/YouTube.cs b/UnitTestProject/YouTube.cs new file mode 100644 index 0000000..5f8da1e --- /dev/null +++ b/UnitTestProject/YouTube.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Umbraco.Community.ExtensionMethods.YouTube; + +namespace UnitTestProject { + + [TestClass] + public class YouTube { + + [TestMethod] + public void GetYouTubeId() { + + Assert.IsNull(YouTubeHelpers.GetIdFromString("123")); + + // Mest test URLs from https://gist.github.com/FinalAngel/1876898 + + Assert.AreEqual("dQw4w9WgXcQ", YouTubeHelpers.GetIdFromString("http://www.youtube.com/watch?v=dQw4w9WgXcQ")); + Assert.AreEqual("1p3vcRhsYGo", YouTubeHelpers.GetIdFromString("http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo")); + Assert.AreEqual("1p3vcRhsYGo", YouTubeHelpers.GetIdFromString("http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0")); + Assert.AreEqual("yZ-K7nCVnBI", YouTubeHelpers.GetIdFromString("http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub")); + Assert.AreEqual("NRHVzbJVx8I", YouTubeHelpers.GetIdFromString("http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I")); + Assert.AreEqual("6dwqZw0j_jY", YouTubeHelpers.GetIdFromString("http://youtu.be/6dwqZw0j_jY")); + Assert.AreEqual("6dwqZw0j_jY", YouTubeHelpers.GetIdFromString("http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be")); + Assert.AreEqual("afa-5HQHiAs", YouTubeHelpers.GetIdFromString("http://youtu.be/afa-5HQHiAs")); + Assert.AreEqual("cKZDdG9FTKY", YouTubeHelpers.GetIdFromString("http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel")); + Assert.AreEqual("nas1rJpm7wY", YouTubeHelpers.GetIdFromString("http://www.youtube.com/embed/nas1rJpm7wY?rel=0")); + Assert.AreEqual("peFZbP64dsU", YouTubeHelpers.GetIdFromString("http://www.youtube.com/watch?v=peFZbP64dsU")); + + } + + } + +} From 5a8f3431b9829cdf5459f4f8bfac12a441081a9a Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Sat, 15 Feb 2014 17:37:53 +0100 Subject: [PATCH 3/4] Switched the order of the parameters since the expected value is the first parameter, and the actual value the second parameter #sillyMe --- UnitTestProject/Strings.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/UnitTestProject/Strings.cs b/UnitTestProject/Strings.cs index b81b435..30c9a9e 100644 --- a/UnitTestProject/Strings.cs +++ b/UnitTestProject/Strings.cs @@ -21,19 +21,19 @@ public void WordCount() { [TestMethod] public void FirstCharToUpper() { - Assert.AreEqual("".FirstCharToUpper(), ""); - Assert.AreEqual("bacon".FirstCharToUpper(), "Bacon"); - Assert.AreEqual("hello world".FirstCharToUpper(), "Hello world"); - Assert.AreEqual("Hello World".FirstCharToUpper(), "Hello World"); + Assert.AreEqual("", "".FirstCharToUpper()); + Assert.AreEqual("Bacon", "bacon".FirstCharToUpper()); + Assert.AreEqual("Hello world", "hello world".FirstCharToUpper()); + Assert.AreEqual("Hello World", "Hello World".FirstCharToUpper()); } [TestMethod] public void InvertCase() { - Assert.AreEqual("HelloWorld".InvertCase(), "hELLOwORLD"); - Assert.AreEqual("helloworld".InvertCase(), "HELLOWORLD"); - Assert.AreEqual("Bacon".InvertCase(), "bACON"); + Assert.AreEqual("hELLOwORLD", "HelloWorld".InvertCase()); + Assert.AreEqual("HELLOWORLD", "helloworld".InvertCase()); + Assert.AreEqual("bACON", "Bacon".InvertCase()); } From 27228f931a6c687e9209285d0793ffa8bd35d5ab Mon Sep 17 00:00:00 2001 From: Anders Bjerner Date: Sat, 15 Feb 2014 19:25:25 +0100 Subject: [PATCH 4/4] Various date improvements, unit testing etc. --- ExtensionMethods/Dates/DateHelpers.cs | 153 +++++++++++++ .../ExtensionMethods/DateExtensionMethods.cs} | 168 ++++++-------- ExtensionMethods/ExtensionMethods.csproj | 3 +- TestSite/Views/Home.cshtml | 1 + UnitTestProject/Dates.cs | 209 ++++++++++++++++++ UnitTestProject/UnitTestProject.csproj | 1 + 6 files changed, 439 insertions(+), 96 deletions(-) create mode 100644 ExtensionMethods/Dates/DateHelpers.cs rename ExtensionMethods/{Dates.cs => Dates/ExtensionMethods/DateExtensionMethods.cs} (67%) create mode 100644 UnitTestProject/Dates.cs diff --git a/ExtensionMethods/Dates/DateHelpers.cs b/ExtensionMethods/Dates/DateHelpers.cs new file mode 100644 index 0000000..33937c4 --- /dev/null +++ b/ExtensionMethods/Dates/DateHelpers.cs @@ -0,0 +1,153 @@ +using System; +using System.Globalization; + +namespace Umbraco.Community.ExtensionMethods.Dates { + + public class DateHelpers { + + /// + /// Gets the current age, from the specified date of birth. + /// + /// The date of birth. + /// Returns the age based on the specified date of birth. + public static int GetAge(DateTime dateOfBirth) { + return GetAge(DateTime.Today, DateTime.Now); + } + + /// + /// Gets the current age, from the specified date of birth. The age is calculated based on dt. + /// + /// The date of birth. + /// + /// Returns the age based on the specified date of birth at the moment of dt. + public static int GetAge(DateTime dateOfBirth, DateTime dt) { + int age = dt.Year - dateOfBirth.Year; + if (dt.Month < dateOfBirth.Month || (dt.Month == dateOfBirth.Month && dt.Day < dateOfBirth.Day)) age--; + return age; + } + + /// + /// Gets the Day number and ordinal suffix for a given date. + /// + ///The date. + ///The day number and ordinal suffix. + public static string GetDayNumber(DateTime date) { + switch (date.Day) { + case 1: + case 21: + case 31: + return date.Day + "st"; + case 2: + case 22: + return date.Day + "nd"; + case 3: + case 23: + return date.Day + "rd"; + default: + return date.Day + "th"; + } + } + + /// + /// Determines whether the specified date is weekday. + /// + /// The date. + /// + /// Returns TRUE if the specified day is weekday; otherwise FALSE. + /// + public static bool IsWeekday(DateTime date) { + return date.DayOfWeek != DayOfWeek.Saturday && date.DayOfWeek != DayOfWeek.Sunday; + } + + /// + /// Determines whether the specified date is weekend. + /// + /// The date. + /// + /// Returns TRUE if the specified day is weekend; otherwise FALSE. + /// + public static bool IsWeekend(DateTime date) { + return !IsWeekday(date); + } + + /// + /// Determines whether the year of the specified date is a leap year. + /// + /// The date. + /// + /// Returns TRUE if the specified date is in a leap year; otherwise FALSE. + /// + public static bool IsLeapYear(DateTime date) { + return IsLeapYear(date.Year); + } + + /// + /// Determines whether the specified year is a leap year. + /// + /// The year. + /// + /// Returns TRUE if the specified year is a leap year; otherwise FALSE. + /// + public static bool IsLeapYear(int year) { + return (DateTime.DaysInMonth(year, 2).Equals(29)); + } + + /// + /// Get the English name of the day. + /// + /// The date. + /// Returns the English name of the day. + public static string GetDayName(DateTime date) { + return date.ToString("dddd", CultureInfo.InvariantCulture); + } + + /// + /// Gets the name of the day as specified by the current culture. + /// + /// The date. + /// Returns the local name of the day. + public static string GetLocalDayName(DateTime date) { + return date.ToString("dddd", CultureInfo.CurrentCulture); + } + + /// + /// Gets the name of the day as specified by culture. + /// + /// The date. + /// The culture to be used. + /// Returns the local name of the day. + public static string GetLocalDayName(DateTime date, CultureInfo culture) { + return date.ToString("dddd", culture); + } + + /// + /// Get the English name of the month. + /// + /// The date. + /// Returns the English name of the month. + public static string GetMonthName(DateTime date) { + return date.ToString("MMMM", CultureInfo.InvariantCulture); + } + + /// + /// Gets the name of the month as specified by the current culture. + /// + /// The date. + /// Returns the local name of the month. + public static string GetLocalMonthName(DateTime date) { + return date.ToString("MMMM", CultureInfo.CurrentCulture); + } + + /// + /// Gets the name of the month as specified by culture. + /// + /// The date. + /// The culture to be used. + /// Returns the local name of the month. + public static string GetLocalMonthName(DateTime date, CultureInfo culture) { + return date.ToString("MMMM", culture); + } + + } + +} diff --git a/ExtensionMethods/Dates.cs b/ExtensionMethods/Dates/ExtensionMethods/DateExtensionMethods.cs similarity index 67% rename from ExtensionMethods/Dates.cs rename to ExtensionMethods/Dates/ExtensionMethods/DateExtensionMethods.cs index 48b7df3..bc7d6ef 100644 --- a/ExtensionMethods/Dates.cs +++ b/ExtensionMethods/Dates/ExtensionMethods/DateExtensionMethods.cs @@ -1,15 +1,11 @@ using System; -using System.Collections.Generic; using System.Globalization; -using System.Linq; -using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; -namespace Umbraco.Community.ExtensionMethods.Dates +namespace Umbraco.Community.ExtensionMethods.Dates.ExtensionMethods { /// Kudos to uComponents - http://ucomponents.codeplex.com/SourceControl/latest#uComponents.XsltExtensions/Dates.cs - public static class Dates + public static class DateExtensionMethods { /// @@ -25,100 +21,46 @@ public static class Dates /// /// Returns the age based on the specified date of birth. /// - public static int Age(this DateTime dateOfBirth) - { - //Today's date - var today = DateTime.Today; - - // if month is less, or if month is equal, and day less - if (today.Month < dateOfBirth.Month || today.Month == dateOfBirth.Month && today.Day < dateOfBirth.Day) - { - // then they haven't had this year's birthday yet! - return today.Year - dateOfBirth.Year - 1; - } - else - { - // otherwise, substract the current year from date-of-birth. - return today.Year - dateOfBirth.Year; - } - - // unable to parse date-of-birth. - return -1; + public static int Age(this DateTime dateOfBirth) { + return DateHelpers.GetAge(dateOfBirth); } - + /// - /// Gets the Day number and ordinal suffix for a given date + /// Gets the Day number and ordinal suffix for a given date. /// - ///The date - ///The day number and ordinal suffix - public static string GetDayNumber(this DateTime date) - { - switch (date.Day) - { - case 1: - case 21: - case 31: - return date.Day + "st"; - case 2: - case 22: - return date.Day + "nd"; - case 3: - case 23: - return date.Day + "rd"; - default: - return date.Day + "th"; - } + ///The date. + ///The day number and ordinal suffix. + public static string GetDayNumber(this DateTime date) { + return DateHelpers.GetDayNumber(date); } - /// - /// Determines whether the specified day is weekday. + /// Determines whether the specified date is weekday. /// - /// The date - /// - /// true if the specified day is weekday; otherwise, false. - /// - public static bool IsWeekday(this DateTime date) - { - var day = date.DayOfWeek; - - switch (day) - { - // is a weekend? - case DayOfWeek.Saturday: - case DayOfWeek.Sunday: - return false; - - // otherwise its a weekday? - default: - return true; - } + /// The date. + /// Returns TRUE if the specified day is weekday; otherwise FALSE. + public static bool IsWeekday(this DateTime date) { + return DateHelpers.IsWeekday(date); } /// /// Determines whether the specified date is weekend. /// - /// The date - /// - /// true if the specified date is weekend; otherwise, false. - /// - public static bool IsWeekend(this DateTime date) - { - return !IsWeekday(date); + /// The date. + /// Returns TRUE if the specified day is weekend; otherwise FALSE. + public static bool IsWeekend(this DateTime date) { + return !DateHelpers.IsWeekday(date); } - /// - /// Determines whether [is leap year] [the specified date]. + /// Determines whether the year of the specified date is a leap year. /// /// The date. /// - /// true if [is leap year] [the specified date]; otherwise, false. + /// Returns TRUE if the specified date is in a leap year; otherwise FALSE. /// - public static bool IsLeapYear(this DateTime date) - { - // test if number of days in Feburary is 29 for the current year in the date - return (DateTime.DaysInMonth(date.Year, 2).Equals(29)); + public static bool IsLeapYear(this DateTime date) { + return DateHelpers.IsLeapYear(date); } /// @@ -319,25 +261,61 @@ private static string GetDayNumberSuffix(DateTime date) } /// - /// Gets the month string of the given DateTime + /// Get the English name of the day. /// - /// The DateTime object - /// The string representation of month - /// Returns "January" for the date 1.1.2013 - public static string GetMonthName(this DateTime date) - { - return date.ToString("MMMM"); + /// The date. + /// Returns the English name of the day. + public static string GetDayName(DateTime date) { + return DateHelpers.GetDayName(date); } /// - /// Gets the day string of the given DateTime. + /// Gets the name of the day as specified by the current culture. /// - /// The DateTime object - /// The string representation of day - /// Returns "Sunday" for the date 1.1.2013 - public static string GetDayName(this DateTime date) - { - return date.DayOfWeek.ToString(); + /// The date. + /// Returns the local name of the day. + public static string GetLocalDayName(DateTime date) { + return DateHelpers.GetLocalDayName(date); + } + + /// + /// Gets the name of the day as specified by culture. + /// + /// The date. + /// The culture to be used. + /// Returns the local name of the day. + public static string GetLocalDayName(DateTime date, CultureInfo culture) { + return DateHelpers.GetLocalDayName(date, culture); + } + + /// + /// Get the English name of the month. + /// + /// The date. + /// Returns the English name of the month. + public static string GetMonthName(DateTime date) { + return DateHelpers.GetMonthName(date); + } + + /// + /// Gets the name of the month as specified by the current culture. + /// + /// The date. + /// Returns the local name of the month. + public static string GetLocalMonthName(DateTime date) { + return DateHelpers.GetLocalMonthName(date); + } + + /// + /// Gets the name of the month as specified by culture. + /// + /// The date. + /// The culture to be used. + /// Returns the local name of the month. + public static string GetLocalMonthName(DateTime date, CultureInfo culture) { + return DateHelpers.GetLocalMonthName(date, culture); } + } + } diff --git a/ExtensionMethods/ExtensionMethods.csproj b/ExtensionMethods/ExtensionMethods.csproj index b52718a..f135a15 100644 --- a/ExtensionMethods/ExtensionMethods.csproj +++ b/ExtensionMethods/ExtensionMethods.csproj @@ -195,7 +195,8 @@ - + + diff --git a/TestSite/Views/Home.cshtml b/TestSite/Views/Home.cshtml index c646df4..16a9645 100644 --- a/TestSite/Views/Home.cshtml +++ b/TestSite/Views/Home.cshtml @@ -1,5 +1,6 @@ @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @using Umbraco.Community.ExtensionMethods.Dates +@using Umbraco.Community.ExtensionMethods.Dates.ExtensionMethods @using Umbraco.Community.ExtensionMethods.Social @using Umbraco.Community.ExtensionMethods.Strings; @using Umbraco.Community.ExtensionMethods.ImageGen; diff --git a/UnitTestProject/Dates.cs b/UnitTestProject/Dates.cs new file mode 100644 index 0000000..ae42a50 --- /dev/null +++ b/UnitTestProject/Dates.cs @@ -0,0 +1,209 @@ +using System; +using System.Globalization; +using System.Threading; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Umbraco.Community.ExtensionMethods.Dates; +using Umbraco.Community.ExtensionMethods.Dates.ExtensionMethods; + +namespace UnitTestProject { + + [TestClass] + public class Dates { + + [TestMethod] + public void GetAge() { + + // Since the age doesn't have a fixed value, we fake the value of "now" + DateTime fakeNow = new DateTime(2014, 02, 15); + + Assert.AreEqual(44, DateHelpers.GetAge(new DateTime(1970, 01, 01), fakeNow)); + Assert.AreEqual(25, DateHelpers.GetAge(new DateTime(1988, 08, 17), fakeNow)); + + } + + [TestMethod] + public void GetDayNumber() { + + Assert.AreEqual("1st", DateHelpers.GetDayNumber(new DateTime(2014, 1, 1))); + Assert.AreEqual("2nd", DateHelpers.GetDayNumber(new DateTime(2014, 1, 2))); + Assert.AreEqual("3rd", DateHelpers.GetDayNumber(new DateTime(2014, 1, 3))); + Assert.AreEqual("4th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 4))); + Assert.AreEqual("5th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 5))); + Assert.AreEqual("6th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 6))); + Assert.AreEqual("7th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 7))); + Assert.AreEqual("8th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 8))); + Assert.AreEqual("9th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 9))); + + Assert.AreEqual("10th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 10))); + Assert.AreEqual("11th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 11))); + Assert.AreEqual("12th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 12))); + Assert.AreEqual("13th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 13))); + Assert.AreEqual("14th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 14))); + Assert.AreEqual("15th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 15))); + Assert.AreEqual("16th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 16))); + Assert.AreEqual("17th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 17))); + Assert.AreEqual("18th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 18))); + Assert.AreEqual("19th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 19))); + + Assert.AreEqual("20th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 20))); + Assert.AreEqual("21st", DateHelpers.GetDayNumber(new DateTime(2014, 1, 21))); + Assert.AreEqual("22nd", DateHelpers.GetDayNumber(new DateTime(2014, 1, 22))); + Assert.AreEqual("23rd", DateHelpers.GetDayNumber(new DateTime(2014, 1, 23))); + Assert.AreEqual("24th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 24))); + Assert.AreEqual("25th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 25))); + Assert.AreEqual("26th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 26))); + Assert.AreEqual("27th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 27))); + Assert.AreEqual("28th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 28))); + Assert.AreEqual("29th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 29))); + + Assert.AreEqual("30th", DateHelpers.GetDayNumber(new DateTime(2014, 1, 30))); + Assert.AreEqual("31st", DateHelpers.GetDayNumber(new DateTime(2014, 1, 31))); + + } + + [TestMethod] + public void IsWeekDay() { + + Assert.AreEqual(true, DateHelpers.IsWeekday(new DateTime(2014, 2, 10))); + Assert.AreEqual(true, DateHelpers.IsWeekday(new DateTime(2014, 2, 11))); + Assert.AreEqual(true, DateHelpers.IsWeekday(new DateTime(2014, 2, 12))); + Assert.AreEqual(true, DateHelpers.IsWeekday(new DateTime(2014, 2, 13))); + Assert.AreEqual(true, DateHelpers.IsWeekday(new DateTime(2014, 2, 14))); + Assert.AreEqual(false, DateHelpers.IsWeekday(new DateTime(2014, 2, 15))); + Assert.AreEqual(false, DateHelpers.IsWeekday(new DateTime(2014, 2, 16))); + + } + + [TestMethod] + public void IsWeekend() { + + Assert.AreEqual(false, DateHelpers.IsWeekend(new DateTime(2014, 2, 10))); + Assert.AreEqual(false, DateHelpers.IsWeekend(new DateTime(2014, 2, 11))); + Assert.AreEqual(false, DateHelpers.IsWeekend(new DateTime(2014, 2, 12))); + Assert.AreEqual(false, DateHelpers.IsWeekend(new DateTime(2014, 2, 13))); + Assert.AreEqual(false, DateHelpers.IsWeekend(new DateTime(2014, 2, 14))); + Assert.AreEqual(true, DateHelpers.IsWeekend(new DateTime(2014, 2, 15))); + Assert.AreEqual(true, DateHelpers.IsWeekend(new DateTime(2014, 2, 16))); + + } + + [TestMethod] + public void IsLeapYear() { + + // Se more here: http://en.wikipedia.org/wiki/Leap_year#Algorithm + + Assert.AreEqual(true, DateHelpers.IsLeapYear(2000)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2001)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2002)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2003)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(2004)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2005)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2006)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2007)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(2008)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2009)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2010)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2011)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(2012)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2013)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2014)); + + Assert.AreEqual(false, DateHelpers.IsLeapYear(1500)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(1600)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(1700)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(1800)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(1900)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(2000)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2100)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2200)); + Assert.AreEqual(false, DateHelpers.IsLeapYear(2300)); + Assert.AreEqual(true, DateHelpers.IsLeapYear(2400)); + + } + + [TestMethod] + public void GetDayName() { + + // Test with InvariantCulture + Assert.AreEqual("Monday", DateHelpers.GetDayName(new DateTime(2014, 2, 10))); + Assert.AreEqual("Tuesday", DateHelpers.GetDayName(new DateTime(2014, 2, 11))); + Assert.AreEqual("Wednesday", DateHelpers.GetDayName(new DateTime(2014, 2, 12))); + Assert.AreEqual("Thursday", DateHelpers.GetDayName(new DateTime(2014, 2, 13))); + Assert.AreEqual("Friday", DateHelpers.GetDayName(new DateTime(2014, 2, 14))); + Assert.AreEqual("Saturday", DateHelpers.GetDayName(new DateTime(2014, 2, 15))); + Assert.AreEqual("Sunday", DateHelpers.GetDayName(new DateTime(2014, 2, 16))); + + // Set current culture as "da-DK" + Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK"); + Assert.AreEqual("mandag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 10))); + Assert.AreEqual("tirsdag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 11))); + Assert.AreEqual("onsdag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 12))); + Assert.AreEqual("torsdag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 13))); + Assert.AreEqual("fredag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 14))); + Assert.AreEqual("lørdag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 15))); + Assert.AreEqual("søndag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 16))); + + // Test with specific culture (de-DE) + CultureInfo german = new CultureInfo("de-DE"); + Assert.AreEqual("Montag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 10), german)); + Assert.AreEqual("Dienstag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 11), german)); + Assert.AreEqual("Mittwoch", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 12), german)); + Assert.AreEqual("Donnerstag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 13), german)); + Assert.AreEqual("Freitag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 14), german)); + Assert.AreEqual("Samstag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 15), german)); + Assert.AreEqual("Sonntag", DateHelpers.GetLocalDayName(new DateTime(2014, 2, 16), german)); + + } + + [TestMethod] + public void GetMonthName() { + + // Test with InvariantCulture + Assert.AreEqual("January", DateHelpers.GetMonthName(new DateTime(2014, 1, 1))); + Assert.AreEqual("February", DateHelpers.GetMonthName(new DateTime(2014, 2, 1))); + Assert.AreEqual("March", DateHelpers.GetMonthName(new DateTime(2014, 3, 1))); + Assert.AreEqual("April", DateHelpers.GetMonthName(new DateTime(2014, 4, 1))); + Assert.AreEqual("May", DateHelpers.GetMonthName(new DateTime(2014, 5, 1))); + Assert.AreEqual("June", DateHelpers.GetMonthName(new DateTime(2014, 6, 1))); + Assert.AreEqual("July", DateHelpers.GetMonthName(new DateTime(2014, 7, 1))); + Assert.AreEqual("August", DateHelpers.GetMonthName(new DateTime(2014, 8, 1))); + Assert.AreEqual("September", DateHelpers.GetMonthName(new DateTime(2014, 9, 1))); + Assert.AreEqual("October", DateHelpers.GetMonthName(new DateTime(2014, 10, 1))); + Assert.AreEqual("November", DateHelpers.GetMonthName(new DateTime(2014, 11, 1))); + Assert.AreEqual("December", DateHelpers.GetMonthName(new DateTime(2014, 12, 1))); + + // Set current culture as "da-DK" + Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK"); + Assert.AreEqual("januar", DateHelpers.GetLocalMonthName(new DateTime(2014, 1, 1))); + Assert.AreEqual("februar", DateHelpers.GetLocalMonthName(new DateTime(2014, 2, 1))); + Assert.AreEqual("marts", DateHelpers.GetLocalMonthName(new DateTime(2014, 3, 1))); + Assert.AreEqual("april", DateHelpers.GetLocalMonthName(new DateTime(2014, 4, 1))); + Assert.AreEqual("maj", DateHelpers.GetLocalMonthName(new DateTime(2014, 5, 1))); + Assert.AreEqual("juni", DateHelpers.GetLocalMonthName(new DateTime(2014, 6, 1))); + Assert.AreEqual("juli", DateHelpers.GetLocalMonthName(new DateTime(2014, 7, 1))); + Assert.AreEqual("august", DateHelpers.GetLocalMonthName(new DateTime(2014, 8, 1))); + Assert.AreEqual("september", DateHelpers.GetLocalMonthName(new DateTime(2014, 9, 1))); + Assert.AreEqual("oktober", DateHelpers.GetLocalMonthName(new DateTime(2014, 10, 1))); + Assert.AreEqual("november", DateHelpers.GetLocalMonthName(new DateTime(2014, 11, 1))); + Assert.AreEqual("december", DateHelpers.GetLocalMonthName(new DateTime(2014, 12, 1))); + + // Test with specific culture (de-DE) + CultureInfo german = new CultureInfo("de-DE"); + Assert.AreEqual("Januar", DateHelpers.GetLocalMonthName(new DateTime(2014, 1, 1), german)); + Assert.AreEqual("Februar", DateHelpers.GetLocalMonthName(new DateTime(2014, 2, 1), german)); + Assert.AreEqual("März", DateHelpers.GetLocalMonthName(new DateTime(2014, 3, 1), german)); + Assert.AreEqual("April", DateHelpers.GetLocalMonthName(new DateTime(2014, 4, 1), german)); + Assert.AreEqual("Mai", DateHelpers.GetLocalMonthName(new DateTime(2014, 5, 1), german)); + Assert.AreEqual("Juni", DateHelpers.GetLocalMonthName(new DateTime(2014, 6, 1), german)); + Assert.AreEqual("Juli", DateHelpers.GetLocalMonthName(new DateTime(2014, 7, 1), german)); + Assert.AreEqual("August", DateHelpers.GetLocalMonthName(new DateTime(2014, 8, 1), german)); + Assert.AreEqual("September", DateHelpers.GetLocalMonthName(new DateTime(2014, 9, 1), german)); + Assert.AreEqual("Oktober", DateHelpers.GetLocalMonthName(new DateTime(2014, 10, 1), german)); + Assert.AreEqual("November", DateHelpers.GetLocalMonthName(new DateTime(2014, 11, 1), german)); + Assert.AreEqual("Dezember", DateHelpers.GetLocalMonthName(new DateTime(2014, 12, 1), german)); + + } + + } + +} diff --git a/UnitTestProject/UnitTestProject.csproj b/UnitTestProject/UnitTestProject.csproj index 9d175a6..ce5c52b 100644 --- a/UnitTestProject/UnitTestProject.csproj +++ b/UnitTestProject/UnitTestProject.csproj @@ -50,6 +50,7 @@ +