Assertion utility methods that simplify things such as argument checks.

Not intended to be used directly by applications.

예제 #1
0
 public void IsTrueWithMesssage()
 {
     Assert.Throws <ArgumentException>(() => AssertUtils.IsTrue(false, "foo"), "foo");
 }
예제 #2
0
        /// <summary>
        /// Tokenize the given <see cref="System.String"/> into a
        /// <see cref="System.String"/> array.
        /// </summary>
        /// <remarks>
        /// <p>
        /// If <paramref name="s"/> is <see langword="null"/>, returns an empty
        /// <see cref="System.String"/> array.
        /// </p>
        /// <p>
        /// If <paramref name="delimiters"/> is <see langword="null"/> or the empty
        /// <see cref="System.String"/>, returns a <see cref="System.String"/> array with one
        /// element: <paramref name="s"/> itself.
        /// </p>
        /// </remarks>
        /// <param name="s">The <see cref="System.String"/> to tokenize.</param>
        /// <param name="delimiters">
        /// The delimiter characters, assembled as a <see cref="System.String"/>.
        /// </param>
        /// <param name="trimTokens">
        /// Trim the tokens via <see cref="System.String.Trim()"/>.
        /// </param>
        /// <param name="ignoreEmptyTokens">
        /// Omit empty tokens from the result array.
        /// </param>
        /// <param name="quoteChars">
        /// Pairs of quote characters. <paramref name="delimiters"/> within a pair of quotes are ignored
        /// </param>
        /// <returns>An array of the tokens.</returns>
        public static string[] Split(
            string s, string delimiters, bool trimTokens, bool ignoreEmptyTokens, string quoteChars)
        {
            if (s == null)
            {
                return(new string[0]);
            }
            if (delimiters == null || delimiters.Length == 0)
            {
                return(new string[] { s });
            }
            if (quoteChars == null)
            {
                quoteChars = string.Empty;
            }
            AssertUtils.IsTrue(quoteChars.Length % 2 == 0, "the number of quote characters must be even");

            char[] delimiterChars = delimiters.ToCharArray();

            // scan separator positions
            int[] delimiterPositions = new int[s.Length];
            int   count = MakeDelimiterPositionList(s, delimiterChars, quoteChars, delimiterPositions);

            ArrayList tokens     = new ArrayList(count + 1);
            int       startIndex = 0;

            for (int ixSep = 0; ixSep < count; ixSep++)
            {
                string token = s.Substring(startIndex, delimiterPositions[ixSep] - startIndex);
                if (trimTokens)
                {
                    token = token.Trim();
                }
                if (!(ignoreEmptyTokens && token.Length == 0))
                {
                    tokens.Add(token);
                }
                startIndex = delimiterPositions[ixSep] + 1;
            }
            // add remainder
            if (startIndex < s.Length)
            {
                string token = s.Substring(startIndex);
                if (trimTokens)
                {
                    token = token.Trim();
                }
                if (!(ignoreEmptyTokens && token.Length == 0))
                {
                    tokens.Add(token);
                }
            }
            else if (startIndex == s.Length)
            {
                if (!(ignoreEmptyTokens))
                {
                    tokens.Add(string.Empty);
                }
            }

            return((string[])tokens.ToArray(typeof(string)));
        }
예제 #3
0
 /// <summary>
 /// Returns the element at the specified index using the supplied
 /// <paramref name="enumerable"/>.
 /// </summary>
 /// <param name="enumerable">
 /// The <see cref="System.Collections.IEnumerable"/> to use to enumerate
 /// elements until the supplied <paramref name="index"/> is reached.
 /// </param>
 /// <param name="index">
 /// The index of the element in the enumeration to return.
 /// </param>
 /// <returns>
 /// The element at the specified index using the supplied
 /// <paramref name="enumerable"/>.
 /// </returns>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// If the supplied <paramref name="index"/> was less than zero, or the
 /// supplied <paramref name="enumerable"/> did not contain enough elements
 /// to be able to reach the supplied <paramref name="index"/>.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 /// If the supplied <paramref name="enumerable"/> is <see langword="null"/>.
 /// </exception>
 public static object EnumerateElementAtIndex(IEnumerable enumerable, int index)
 {
     AssertUtils.ArgumentNotNull(enumerable, "enumerable");
     return(EnumerateElementAtIndex(enumerable.GetEnumerator(), index));
 }
예제 #4
0
 public void ArgumentNotNull()
 {
     AssertUtils.ArgumentNotNull(null, "foo");
 }
예제 #5
0
 public void ArgumentHasTextWithValidTextAndMessage()
 {
     AssertUtils.ArgumentHasText("... and no-one's getting fat 'cept Mama Cas!", "foo", "Bang!");
 }
예제 #6
0
 public void IsTrueWithMessageValidExpression()
 {
     AssertUtils.IsTrue(true, "foo");
 }
예제 #7
0
 public void IsTrueValidExpression()
 {
     AssertUtils.IsTrue(true);
 }
예제 #8
0
 public void ArgumentHasTextWithMessage()
 {
     AssertUtils.ArgumentHasText(null, "foo", "Bang!");
 }
예제 #9
0
 public void ArgumentHasLengthArgumentIsNull()
 {
     AssertUtils.ArgumentHasLength(null, "foo");
 }
예제 #10
0
 public void ArgumentHasText()
 {
     Assert.Throws <ArgumentNullException>(() => AssertUtils.ArgumentHasText(null, "foo"));
 }
예제 #11
0
 public void ArgumentHasText()
 {
     AssertUtils.ArgumentHasText(null, "foo");
 }
예제 #12
0
 public void ArgumentNotNullWithMessage()
 {
     Assert.Throws <ArgumentNullException>(() => AssertUtils.ArgumentNotNull(null, "foo", "Bang!"));
 }
예제 #13
0
 public void StateTrue()
 {
     Assert.Throws <InvalidOperationException>(() => AssertUtils.State(false, "foo"));
 }
예제 #14
0
 public void IsTrue()
 {
     Assert.Throws <ArgumentException>(() => AssertUtils.IsTrue(false), "[Assertion failed] - this expression must be true");
 }
예제 #15
0
 public void ArgumentHasElementsArgumentContainsNonNullsOnly()
 {
     AssertUtils.ArgumentHasElements(new object[] { new object(), new object(), new object() }, "foo");
 }
예제 #16
0
 public void ArgumentHasLengthArgumentIsNullWithMessage()
 {
     AssertUtils.ArgumentHasLength(null, "foo", "Bang!");
 }
예제 #17
0
 public void IsTrueWithMesssage()
 {
     AssertUtils.IsTrue(false, "foo");
 }
예제 #18
0
 public void ArgumentHasLengthArgumentIsEmpty()
 {
     AssertUtils.ArgumentHasLength(new byte[0], "foo");
 }
예제 #19
0
 public void IsTrue()
 {
     AssertUtils.IsTrue(false);
 }
예제 #20
0
 public void ArgumentHasLengthArgumentIsEmptyWithMessage()
 {
     AssertUtils.ArgumentHasLength(new byte[0], "foo", "Bang!");
 }
예제 #21
0
 public void StateTrue()
 {
     AssertUtils.State(false, "foo");
 }
예제 #22
0
 public void ArgumentHasLengthArgumentHasElements()
 {
     AssertUtils.ArgumentHasLength(new byte[1], "foo");
 }
예제 #23
0
 public void ArgumentNotNullWithMessage()
 {
     AssertUtils.ArgumentNotNull(null, "foo", "Bang!");
 }
예제 #24
0
 public void ArgumentHasLengthArgumentHasElementsWithMessage()
 {
     AssertUtils.ArgumentHasLength(new byte[1], "foo", "Bang!");
 }
예제 #25
0
 /// <summary>
 /// Initialize a new instance of <see cref="UniqueKey"/> from its string representation.
 /// See <see cref="GetInstanceScoped"/> and See <see cref="GetTypeScoped"/> for details.
 /// </summary>
 /// <param name="key">The string representation of the new <see cref="UniqueKey"/> instance.</param>
 internal UniqueKey(string key)
 {
     AssertUtils.ArgumentNotNull(key, "key");
     _generatedKey = key;
 }
예제 #26
0
 public void ArgumentHasElementsArgumentIsNull()
 {
     AssertUtils.ArgumentHasElements(null, "foo");
 }
예제 #27
0
 /// <summary>
 /// Returns the first element in the supplied <paramref name="enumerable"/>.
 /// </summary>
 /// <param name="enumerable">
 /// The <see cref="System.Collections.IEnumerable"/> to use to enumerate
 /// elements.
 /// </param>
 /// <returns>
 /// The first element in the supplied <paramref name="enumerable"/>.
 /// </returns>
 /// <exception cref="System.IndexOutOfRangeException">
 /// If the supplied <paramref name="enumerable"/> did not have any elements.
 /// </exception>
 /// <exception cref="System.ArgumentNullException">
 /// If the supplied <paramref name="enumerable"/> is <see langword="null"/>.
 /// </exception>
 public static object EnumerateFirstElement(IEnumerable enumerable)
 {
     AssertUtils.ArgumentNotNull(enumerable, "enumerable");
     return(ObjectUtils.EnumerateElementAtIndex(enumerable.GetEnumerator(), 0));
 }
예제 #28
0
 public void ArgumentHasElementsArgumentIsEmpty()
 {
     AssertUtils.ArgumentHasElements(new object[0], "foo");
 }
예제 #29
0
 /// <summary>
 /// Gets the qualified name of the given method, consisting of
 /// fully qualified interface/class name + "." method name.
 /// </summary>
 /// <param name="method">The method.</param>
 /// <returns>qualified name of the method.</returns>
 public static string GetQualifiedMethodName(MethodInfo method)
 {
     AssertUtils.ArgumentNotNull(method, "method", "MethodInfo must not be null");
     return(method.DeclaringType.FullName + "." + method.Name);
 }
예제 #30
0
 public void ArgumentHasElementsArgumentContainsNull()
 {
     Assert.Throws <ArgumentException>(() => AssertUtils.ArgumentHasElements(new object[] { new object(), null, new object() }, "foo"));
 }