public static IList <T> FromIndexes <T>(this IList <T> list, IndexOutOfRangeBehaviour outOfRange, params int[] indexes)
        {
            #region Input validation

            Insist.IsNotNull(list, "list");
            Insist.IsNotNull(indexes, "indexes");
            Insist.IsDefined(outOfRange, "outOfRange");

            #endregion

            List <T> items = new List <T>();

            if (list.Count == 0)
            {
                return(items);
            }

            foreach (int index in indexes)
            {
                if (index >= list.Count)
                //Out of bounds
                {
                    if (outOfRange == IndexOutOfRangeBehaviour.Ignore)
                    {
                        continue;
                    }
                }

                items.Add(list[index]);
            }

            return(items);
        }
Example #2
0
 public void IsDefined_Thrown_Exception_Has_Correct_Message()
 {
     try
     {
         TestEnum e = (TestEnum)0;
         Insist.IsDefined <TestEnum>(e, ARGUMENT_NAME, MESSAGE);
     }
     catch (ArgumentException ae)
     {
         Assert.IsTrue(ae.Message.Contains(MESSAGE));
     }
 }
Example #3
0
 public void IsDefined_Thrown_Exception_Has_Correct_Argument_Name()
 {
     try
     {
         TestEnum e = (TestEnum)0;
         Insist.IsDefined <TestEnum>(e, ARGUMENT_NAME);
     }
     catch (ArgumentException ae)
     {
         Assert.AreEqual(ARGUMENT_NAME, ae.ParamName);
     }
 }
        /// <summary>
        /// Dumps the .ToString() representation of each of the supplied arguments
        /// </summary>
        /// <param name="logger">
        /// The logger to write to
        /// </param>
        /// <param name="expandEnumerables">
        /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every
        /// item in the collection and not just the collection itself.
        /// </param>
        /// <param name="args">
        /// The list of arguments that need to be logged.
        /// </param>
        public static void DebugDumpArguments(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args)
        {
            #region Input validation

            Insist.IsNotNull(logger, "logger");
            Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables");

            #endregion

            if (logger.IsDebugEnabled)
            {
                logger.Debug(BuildMessage(logger, string.Empty, expandEnumerables, args));
            }
        }
        /// <summary>
        /// Output a "method returning" debug message and also dumps the .ToString() representation
        /// of the val object.
        /// </summary>
        /// <param name="logger">
        /// The logger to log to
        /// </param>
        /// <param name="expandEnumerables">
        /// If EnumerableExpansion.Expand and the val parameter is an IEnumerable then we'll print .ToString for every
        /// item in the collection and not just the collection itself.
        /// </param>
        /// <param name="val">
        /// The value that is being returned that should be logged.
        /// </param>
        public static void DebugMethodReturning(this ILog logger, EnumerableExpansion expandEnumerables, object val)
        {
            #region Input validation

            Insist.IsNotNull(logger, "logger");
            Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables");

            #endregion

            if (logger.IsDebugEnabled)
            {
                logger.Debug(BuildMessage(logger, string.Format(METHOD_RETURNING_PATTERN, GetCallingMethod()), expandEnumerables, new object[] { val }));
            }
        }
        /// <summary>
        /// Output a "method called" debug message and also dumps the .ToString() representation
        /// of all objects supplied in the args array.
        /// </summary>
        /// <param name="logger">
        /// The logger to log to
        /// </param>
        /// <param name="expandEnumerables">
        /// If EnumerableExpansion.Expand and the args collection contains any IEnumerables then we'll print .ToString for every
        /// item in the collection and not just the collection itself.
        /// </param>
        /// <param name="args">
        /// The list of arguments that need to be logged
        /// </param>
        public static void DebugMethodCalled(this ILog logger, EnumerableExpansion expandEnumerables, params object[] args)
        {
            #region Input validation

            Insist.IsNotNull(logger, "logger");
            Insist.IsDefined <EnumerableExpansion>(expandEnumerables, "expandEnumerables");

            #endregion

            if (logger.IsDebugEnabled)
            {
                logger.Debug(BuildMessage(logger, string.Format(METHOD_CALLED_PATTERN, GetCallingMethod()), expandEnumerables, args));
            }
        }
        /// <summary>
        /// Generates a new one time password from the supplied data.
        /// </summary>
        /// <param name="secretKey">The secret key to use in the HMAC</param>
        /// <param name="hmac">The hmac algorithm to use</param>
        /// <param name="dt">The date and time to generate a code for</param>
        /// <param name="offset">Any offsets that should be applied to the supplie date time</param>
        /// <param name="timeStep">The timestep value to use to calculate the current step</param>
        /// <param name="otpLength">The required legnth of the returned passcode</param>
        /// <returns>A one time password code</returns>
        /// <exception cref="System.ArgumentNullException">Thrown if hmac or secret key is null</exception>
        /// <exception cref="System.ArgumentException">Thrown if secret key is empty, optLength is
        /// not defined value or timeStep is less than 1 second.</exception>
        public static string Generate(byte[] secretKey, HMAC hmac, DateTime dt, TimeSpan offset, TimeSpan timeStep, OneTimePasswordLength otpLength)
        {
            #region Input validation

            Insist.IsNotNull(hmac, "hmac");
            Insist.IsNotNull(secretKey, "secretKey");
            Insist.IsNotEmpty(secretKey, "secretKey");
            Insist.IsDefined <OneTimePasswordLength>(otpLength, "optLength");
            Insist.IsAtLeast(timeStep.TotalSeconds, 1, "timeStep");

            #endregion

            dt = dt + offset;

            ulong stepNumber = (ulong)Math.Floor((double)dt.ToUnixTime() / (double)timeStep.TotalSeconds);

            return(HmacOneTimePassword.Generate(secretKey, stepNumber, hmac, otpLength));
        }
Example #8
0
        public void IsDefined_Value_Is_defined()
        {
            TestEnum e = (TestEnum)99;

            Insist.IsDefined <TestEnum>(e, "e");
        }