During my previous talk on C# programming, I have asked 'How to calculate a leap and what is the easiest way to make it testable.
There're numerous answers from the audience. I was surprised that everyone is doing all the old way. In following code snippet, I found this is the easiest and shortest one way:
public static bool IsLeapYear(int year)
{
return (year%4==0 || year%400==0);
}Above, method is just asking to supply an integer having year and it will return true/false quoting whether the year is a leap year. The idea behind this is the common i.e. a leap year which comes at every fourth year.[TestFixture]
[Category("The LeapYear")]
public class LeapYearTest
{
[Test]
public void CanTestForLeapYear()
{
Assert.That(true, Is.EqualTo(LeapYear.IsLeapYear(1996)));
}
[Test]
[TestCase(false, 2013)]
[TestCase(false, 2001)]
[TestCase(true, 1996)]
[TestCase(true, 1992)]
public void CanTestForLeapYears(bool expectedResult, int year)
{
Assert.That(expectedResult, Is.EqualTo(LeapYear.IsLeapYear(year)));
}
}Above is showing Test, I did not make any special validation as this is just to say the shortest/easiest method to calculate leap year.Having more TDD-Katas go and share here: https://github.com/garora/TDD-Katas
