Friday, February 10, 2012

I monkey with Func

I wrote this class:

using System;

namespace FuncExperiment.Models

{

   public class SomethingFunky

   {

      private Func<string> _whatever;

      private Func<Int16> _flapdoodle;

      

      public SomethingFunky()

      {

         _whatever = () => "just getting started";

         _flapdoodle = () => 7;

      }

      

      public string SprayFunc()

      {

         return _whatever();

      }

      

      public void PushInStringValue(string value)

      {

         _whatever = () => value;

      }

      

      public void PushInIntegerValue(Int16 digit)

      {

         _flapdoodle = () => digit;

      }

      

      public void MakeStringFromInteger()

      {

         _whatever = () => TryToGiveGermanNameForSingleDigit(_flapdoodle());

      }

      

      private static string TryToGiveGermanNameForSingleDigit(Int16 digit)

      {

         
//follow this link to see the guts of this method

 
 

...and got it under test like so:

using FuncExperiment.Models;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FuncExperiment.Tests

{

   [TestClass]

   public class TestsForSomethingFunky

   {

      [TestMethod]

      public void test_constructor()

      {

         SomethingFunky skunk = new SomethingFunky();

         Assert.AreEqual(skunk.SprayFunc(), "just getting started");

      }

      

      [TestMethod]

      public void test_changing_string()

      {

         SomethingFunky skunk = new SomethingFunky();

         skunk.PushInStringValue("this is new");

         Assert.AreEqual(skunk.SprayFunc(), "this is new");

      }

      

      [TestMethod]

      public void test_goofy_German_translation()

      {

         SomethingFunky skunk = new SomethingFunky();

         skunk.PushInIntegerValue(2);

         skunk.MakeStringFromInteger();

         Assert.AreEqual(skunk.SprayFunc(), "zwei");

      }

      

      [TestMethod]

      public void must_explicity_call_integer_to_string_translation()

      {

         SomethingFunky skunk = new SomethingFunky();

         skunk.PushInIntegerValue(2);

         Assert.AreNotEqual(skunk.SprayFunc(), "zwei");

      }

   }

}

No comments:

Post a Comment