From cf6393b2dd54198e514afdc2ccba507f5c743d63 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Mon, 13 Mar 2017 16:30:48 +0100 Subject: [PATCH] Add leap exercise test generator --- exercises/leap/LeapTest.cs | 16 ++++++------- generators/Exercises/LeapExercise.cs | 35 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 8 deletions(-) create mode 100644 generators/Exercises/LeapExercise.cs diff --git a/exercises/leap/LeapTest.cs b/exercises/leap/LeapTest.cs index 937717e055..a3b935a1bd 100644 --- a/exercises/leap/LeapTest.cs +++ b/exercises/leap/LeapTest.cs @@ -3,26 +3,26 @@ public class LeapTest { [Fact] - public void Valid_leap_year() + public void Year_not_divisible_by_4_is_common_year() { - Assert.True(Year.IsLeap(1996)); + Assert.False(Year.IsLeap(2015)); } [Fact(Skip = "Remove to run test")] - public void Invalid_leap_year() + public void Year_divisible_by_4_not_divisible_by_100_is_leap_year() { - Assert.False(Year.IsLeap(1997)); + Assert.True(Year.IsLeap(2016)); } [Fact(Skip = "Remove to run test")] - public void Turn_of_the_20th_century_is_not_a_leap_year() + public void Year_divisible_by_100_not_divisible_by_400_is_common_year() { - Assert.False(Year.IsLeap(1900)); + Assert.False(Year.IsLeap(2100)); } [Fact(Skip = "Remove to run test")] - public void Turn_of_the_25th_century_is_a_leap_year() + public void Year_divisible_by_400_is_leap_year() { - Assert.True(Year.IsLeap(2400)); + Assert.True(Year.IsLeap(2000)); } } \ No newline at end of file diff --git a/generators/Exercises/LeapExercise.cs b/generators/Exercises/LeapExercise.cs new file mode 100644 index 0000000000..385b51bae9 --- /dev/null +++ b/generators/Exercises/LeapExercise.cs @@ -0,0 +1,35 @@ +using System; +using System.Linq; +using Humanizer; + +namespace Generators.Exercises +{ + public class LeapExercise : Exercise + { + public LeapExercise() : base("leap") + { + } + + public override TestClass CreateTestClass(CanonicalData canonicalData) + { + return new TestClass + { + ClassName = "Leap", + TestMethods = canonicalData.Cases.Select(CreateTestMethod).ToArray() + }; + } + + private static TestMethod CreateTestMethod(CanonicalDataCase canonicalDataCase, int index) + { + var year = Convert.ToInt32(canonicalDataCase.Input); + var isTrue = Convert.ToBoolean(canonicalDataCase.Expected); + + return new TestMethod + { + Index = index, + MethodName = canonicalDataCase.Description.Replace(":", " is").Transform(To.TestMethodName), + Body = $"Assert.{isTrue}(Year.IsLeap({year}));" + }; + } + } +} \ No newline at end of file