|
| 1 | +//// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +//// |
| 6 | + |
| 7 | + |
| 8 | +using System.Collections; |
| 9 | + |
| 10 | +namespace nanoFramework.TestFramework |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// A collection of helper classes to test various conditions associated |
| 14 | + /// with collections within unit tests. If the condition being tested is not |
| 15 | + /// met, an exception is thrown. |
| 16 | + /// </summary> |
| 17 | + public sealed class CollectionAssert |
| 18 | + { |
| 19 | + private const string CollectionEqualReason = "{0}({1})"; |
| 20 | + private const string NumberOfElementsDiff = "Different number of elements."; |
| 21 | + private const string ElementsAtIndexDontMatch = "Element at index {0} do not match."; |
| 22 | + private const string BothCollectionsSameReference = "Both collection references point to the same collection object. {0}"; |
| 23 | + private const string BothCollectionsSameElements = "Both collection contain same elements."; |
| 24 | + |
| 25 | + #region collection |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Tests whether the specified collection is empty. |
| 29 | + /// </summary> |
| 30 | + /// <param name="collection">The collection the test expects to be empty.</param> |
| 31 | + /// <param name="message">The message to include in the exception when the collection is empty. The message is shown in test results.</param> |
| 32 | + /// <exception cref="AssertFailedException">Raises an exception if the collection is not empty.</exception> |
| 33 | + /// <exception cref=""></exception> |
| 34 | + public static void Empty(ICollection collection, string message = "") |
| 35 | + { |
| 36 | + Assert.CheckParameterNotNull(collection, "CollectionAssert.Empty", "collection", string.Empty); |
| 37 | + |
| 38 | + if (collection.Count != 0) |
| 39 | + { |
| 40 | + Assert.HandleFail("CollectionAssert.Empty", message); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Tests whether the specified collection is not empty. |
| 46 | + /// </summary> |
| 47 | + /// <param name="collection">The collection the test expects not to be empty.</param> |
| 48 | + /// <param name="message">The message to include in the exception when the collection is not empty. The message is shown in test results.</param> |
| 49 | + /// <exception cref="AssertFailedException">Raises an exception if the collection is not empty.</exception> |
| 50 | + public static void NotEmpty(ICollection collection, string message = "") |
| 51 | + { |
| 52 | + Assert.CheckParameterNotNull(collection, "CollectionAssert.NotEmpty", "collection", string.Empty); |
| 53 | + |
| 54 | + if (collection.Count == 0) |
| 55 | + { |
| 56 | + Assert.HandleFail("CollectionAssert.NotEmpty", message); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Tests whether the specified collections are equal and throws an exception if the two collections are not equal. Equality is defined as having the same elements in the same order and quantity. Different references to the same value are considered equal. |
| 62 | + /// </summary> |
| 63 | + /// <param name="expected">The first collection to compare. This is the collection the tests expects.</param> |
| 64 | + /// <param name="actual"> The second collection to compare. This is the collection produced by the code under test.</param> |
| 65 | + /// <param name="message">The message to include in the exception when <paramref name="actual"/> is not equal to <paramref name="expected"/>. The message is shown in test results.</param> |
| 66 | + /// <exception cref="AssertFailedException">Thrown if <paramref name="expected"/> is not equal to <paramref name="actual"/>.</exception> |
| 67 | + public static void AreEqual(ICollection expected, ICollection actual, string message = "") |
| 68 | + { |
| 69 | + string reason = string.Empty; |
| 70 | + |
| 71 | + if (!AreCollectionsEqual(expected, actual, ref reason)) |
| 72 | + { |
| 73 | + Assert.HandleFail( |
| 74 | + "CollectionAssert.AreEqual", |
| 75 | + string.Format(CollectionEqualReason, new object[2] |
| 76 | + { |
| 77 | + message, |
| 78 | + reason |
| 79 | + })); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + #endregion region |
| 84 | + |
| 85 | + private static bool AreCollectionsEqual( |
| 86 | + ICollection expected, |
| 87 | + ICollection actual, |
| 88 | + ref string reason) |
| 89 | + { |
| 90 | + if (expected != actual) |
| 91 | + { |
| 92 | + if (expected == null || actual == null) |
| 93 | + { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (expected.Count != actual.Count) |
| 98 | + { |
| 99 | + reason = NumberOfElementsDiff; |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + IEnumerator enumerator = expected.GetEnumerator(); |
| 104 | + IEnumerator enumerator2 = actual.GetEnumerator(); |
| 105 | + |
| 106 | + int num = 0; |
| 107 | + |
| 108 | + while (enumerator.MoveNext() && enumerator2.MoveNext()) |
| 109 | + { |
| 110 | + if (enumerator.Current != enumerator2.Current) |
| 111 | + { |
| 112 | + reason = string.Format( |
| 113 | + ElementsAtIndexDontMatch, |
| 114 | + new object[1] { num }); |
| 115 | + |
| 116 | + return false; |
| 117 | + } |
| 118 | + |
| 119 | + num++; |
| 120 | + } |
| 121 | + |
| 122 | + reason = BothCollectionsSameElements; |
| 123 | + |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + reason = string.Format( |
| 128 | + BothCollectionsSameReference, |
| 129 | + new object[1] { string.Empty }); |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments