Tuesday, March 15, 2016

an example of using an indexer at a class in C#s

using System;
using System.Collections.Generic;
using System.Linq;
namespace ComfyCozyAreWe.Core.Objects
{
   public class Sleigh
   {
      private string _lead;
      private Dictionary<string, string> _lackeys;
      
      public Sleigh(string lead, Dictionary<string, string> lackeys)
      {
         if (String.IsNullOrWhiteSpace(lead) || lackeys == null || lackeys.Count == 0 ||
               lackeys.Keys.Any(x => String.IsNullOrWhiteSpace(x)) ||
               lackeys.Values.Any(y => String.IsNullOrWhiteSpace(y)))
         {
            throw new Exception("The sleigh won't go far.");
         } else {
            _lead = lead;
            _lackeys = lackeys;
         }
      }
      
      public string this[int index]
      {
         get
         {
            SanityCheckPosition(index);
            if (index != (_lackeys.Count*2))
            {
               int position;
               if (index%2 == 0)
               {
                  position = index/2;
               } else {
                  position = (index-1)/2;
               }
               int counter = 0;
               foreach (KeyValuePair<string, string> pairing in _lackeys)
               {
                  if (counter == position)
                  {
                     if (index%2 == 0)
                     {
                        return pairing.Key;
                     } else {
                        return pairing.Value;
                     }
                  }
                  counter++;
               }
            }
            return _lead;
         }
         set
         {
            SanityCheckPosition(index);
            string key = null;
            if (index != (_lackeys.Count*2))
            {
               int position;
               if (index%2 == 0)
               {
                  throw new Exception("Don't mess with the left side.");
               } else {
                  position = (index-1)/2;
               }
               int counter = 0;
               foreach (KeyValuePair<string, string> pairing in _lackeys)
               {
                  if (counter == position)
                  {
                     key = pairing.Key;
                     break;
                  }
                  counter++;
               }
            }
            if (key != null)
            {
               _lackeys[key] = value;
            } else {
               _lead = value;
            }
         }
      }
      
      public int Count()
      {
         return (_lackeys.Count*2)+1;
      }
      
      private void SanityCheckPosition(int position)
      {
         if (!(position < Count()) || position < 0)
         {
            throw new Exception("There is no one there.");
         }
      }
   }
}


 
 

Let's test what is above like so:

using System;
using System.Collections.Generic;
using ComfyCozyAreWe.Core.Objects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ComfyCozyAreWe.Core.Tests.Objects
{
   [TestClass]
   public class SleighTests
   {
      private Sleigh _sleigh;
      
      [TestInitialize]
      public void Prepare()
      {
         Dictionary<string, string> lackeys = new Dictionary<string, string>();
         lackeys.Add("Dasher", "Dancer");
         lackeys.Add("Prancer", "Vixen");
         lackeys.Add("Comet", "Cupid");
         lackeys.Add("Dunder", "Blixem");
         _sleigh = new Sleigh("Rudolph", lackeys);
      }
      
      [TestMethod]
      public void getter_behaves_as_expected()
      {
         Assert.AreEqual(_sleigh[0], "Dasher");
         Assert.AreEqual(_sleigh[1], "Dancer");
         Assert.AreEqual(_sleigh[2], "Prancer");
         Assert.AreEqual(_sleigh[3], "Vixen");
         Assert.AreEqual(_sleigh[4], "Comet");
         Assert.AreEqual(_sleigh[5], "Cupid");
         Assert.AreEqual(_sleigh[6], "Dunder");
         Assert.AreEqual(_sleigh[7], "Blixem");
         Assert.AreEqual(_sleigh[8], "Rudolph");
      }
      
      [TestMethod]
      public void setter_behaves_as_expected()
      {
         string error = null;
         try
         {
            _sleigh[6] = "Donner";
         }
         catch (Exception exception)
         {
            error = exception.Message;
         }
         Assert.AreEqual("Don't mess with the left side.", error);
         _sleigh[7] = "Blitzen";
         Assert.AreEqual(_sleigh[7], "Blitzen");
         _sleigh[8] = "Olive";
         Assert.AreEqual(_sleigh[8], "Olive");
      }
   }
}

No comments:

Post a Comment