Click here to Skip to main content
15,894,405 members
Articles / DevOps / Unit Testing

All About Test Part 1 - Choosing Your Tests

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
21 Jan 2013CPOL21 min read 19.5K   147   12  
Beyond basic unit tests, how do you choose your tests and when are you done?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QgOne.Core;

namespace DateTimeTests
{
  [Settable, Serializable]
  public class MDateTime
  {
    #region Category definitions

    public enum YearCategory { Min = 1, CommonYear = 2009, LeapYear = 2012, CommonCentury = 1900, LeapCentury = 2000, Max = 9999 }
    public enum MonthCategory { Jan = 1, Feb = 2, Jun = 6, Jul = 7, Aug = 8, Dec = 12 }
    public enum DayCategory { First, SecondLast, Last }
    public enum TimeCategory { Min = 0, Mid = 30, Max = 59 }

    #endregion Category definitions

    #region Factors

    public YearCategory Year { get; set; }
    public MonthCategory Month { get; set; }
    public DayCategory Day { get; set; }
    public TimeCategory Hour { get; set; }
    public TimeCategory Minute { get; set; }
    public TimeCategory Second { get; set; }
    public TimeCategory Millisecond { get; set; }

    #endregion Factors

    #region Days in ...

    public int DaysInMonth
    {
      get
      {
        return DaysInFutureMonth(new MTimeSpan { Days = MTimeSpan.DayCategory.Zero });
      }
    }

    public int DaysInFutureMonth(MTimeSpan ts)
    {
      switch (Month)
      {
        case MonthCategory.Jan:
        case MonthCategory.Jul:
        case MonthCategory.Aug:
        case MonthCategory.Dec:
          return 31;
        case MonthCategory.Jun:
          return 30;
        case MonthCategory.Feb:
          if (ts.Days == MTimeSpan.DayCategory.Year || ts.Days == MTimeSpan.DayCategory.YearAndOneDay)
          {
            return 28;
          }
          else if (Year == YearCategory.LeapCentury || Year == YearCategory.LeapYear)
          {
            return 29;
          }
          else
          {
            return 28;
          }
      }
      throw new ArgumentException(string.Format("Unable to resolve days in month for month = {0} and year = {1}", Month, Year));
    }

    public int DaysInYear
    {
      get
      {
        // Days in forward year: Is only 366 if leap year and month is Jan or Feb
        if ((Month == MonthCategory.Jan || Month == MonthCategory.Feb) &&
            (Year == YearCategory.LeapYear || Year == YearCategory.LeapCentury))
        {
          return 366;
        }
        else
        {
          return 365;
        }
      }
    }

    #endregion Days in ...

    #region Resolve

    private int ResolveDay()
    {
      switch (Day)
      {
        case DayCategory.First:
          return 1;
        case DayCategory.SecondLast:
          return DaysInMonth - 1;
        case DayCategory.Last:
          return DaysInMonth;
      }
      throw new ArgumentException(string.Format("Unable to resolve day for {0}/{1} {2}", Day, Month, Year));
    }

    private int ResolveTimeCategory(TimeCategory tc, int minValue, int midValue, int maxValue)
    {
      switch (tc)
      {
        case TimeCategory.Min:
          return minValue;
        case TimeCategory.Mid:
          {
            Random prng = new Random();
            return prng.Next(minValue + 1, maxValue);
          }
        case TimeCategory.Max:
          return maxValue;
      }
      throw new ArgumentException(string.Format("Unable to resolve time category {0}", tc));
    }

    public void Resolve()
    {
      int year = (int)Year;
      int month = (int)Month;
      int day = ResolveDay();
      int hour = ResolveTimeCategory(Hour, 0, 12, 23);
      int min = ResolveTimeCategory(Minute, 0, 30, 59);
      int sec = ResolveTimeCategory(Second, 0, 30, 59);
      int msec = ResolveTimeCategory(Millisecond, 0, 500, 999);
      Value = new DateTime(year, month, day, hour, min, sec, msec);
    }

    #endregion Resolve

    /// <summary>
    /// Resolve must be called before using this property.
    /// </summary>
    public DateTime Value { get; private set; }

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Denmark Denmark
I started programming as a kid and went through basic, C and assembler. I got a MSc.CS from University of Aarhus in 1998 and have maintained an interest for computer science ever since, that I enjoy applying to the problems I come across. I have worked in different roles as a programmer, architect, project manager and consulting in many different areas including databases, cryptography, server architecture and distributed systems. I am currently focussing on the testing done during development and seek to improve this through a combination of approach, skill and technology.

Comments and Discussions