/// <summary>
        ///     Constructs a new NUnit filter element with the given parent, name, and other attributes.
        /// </summary>
        /// <param name="parent">The parent of the NUnit element or <c>null</c> if the element is the root.</param>
        /// <param name="elementType">The type of NUnit filter element.</param>
        /// <param name="name">The name of the element used in the condition check.</param>
        /// <param name="isRegularExpression">If the filter is a regular expression.</param>
        /// <param name="value">The value of the element used in the condition check, if applicable otherwise is <c>null</c>.</param>
        /// <exception cref="ArgumentNullException"><see cref="parent" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">
        ///     <see cref="name" /> is <c>null</c> or empty
        ///     or <see cref="name" /> is <c>null</c> or empty and <see cref="elementType" /> is
        ///     <see cref="NUnitElementType.Property" />.
        /// </exception>
        public NUnitFilterElement(INUnitFilterBaseElement parent, NUnitElementType elementType, string name,
                                  bool isRegularExpression, string value = null)
        {
            Parent = parent ?? throw ExceptionHelper.ThrowArgumentNullException(nameof(parent));

            if (string.IsNullOrEmpty(name))
            {
                throw ExceptionHelper.ThrowArgumentExceptionForNullOrEmpty(nameof(name));
            }

            ElementName = name;

            // Element value is only needed for the Property filter
            // It can be set for other element types but will be ignored
            if (elementType == NUnitElementType.Property && string.IsNullOrEmpty(value))
            {
                throw ExceptionHelper.ThrowArgumentExceptionForNullOrEmpty(nameof(value));
            }

            ElementValue = value;

            XmlTag              = MapXmlTag(elementType);
            ElementType         = elementType;
            IsRegularExpression = isRegularExpression;
        }
 /// <inheritdoc />
 public NUnitFilterElementCollection(INUnitFilterBaseElement parent, NUnitElementType elementType) : base(
         parent, elementType)
 {
     if (elementType == NUnitElementType.Not)
     {
         throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForElementTypeEnum(nameof(elementType),
                                                                                  elementType);
     }
 }
Exemplo n.º 3
0
        public void TestCopyToThrowsArgumentOutOfRangeExceptionWhenArrayIndexLessThanZero()
        {
            ExpressionCollection <INUnitFilterBaseElement> collection =
                new ExpressionCollection <INUnitFilterBaseElement>(NUnitFilterTestHelper.XmlAndTag);

            INUnitFilterBaseElement[] array = new INUnitFilterBaseElement[1];

            Assert.Throws(
                Is.TypeOf <ArgumentOutOfRangeException>().And.Message.EqualTo(
                    "Number was less than the array's lower bound in the first dimension. (Parameter 'destinationIndex')"),
                () => collection.CopyTo(array, -1));
        }
Exemplo n.º 4
0
        public void TestCopyToWithoutItemsInCollectionCopiesNoneToArray(
            [Range(0, 2)] int arrayIndex)
        {
            // Create collection and expected
            ExpressionCollection <INUnitFilterBaseElement> collection =
                new ExpressionCollection <INUnitFilterBaseElement>(NUnitFilterTestHelper.XmlAndTag);

            INUnitFilterBaseElement[] expected = new INUnitFilterBaseElement[arrayIndex];
            INUnitFilterBaseElement[] array    = new INUnitFilterBaseElement[arrayIndex];

            Assert.AreEqual(0, collection.Count);

            collection.CopyTo(array, arrayIndex);

            CollectionAssert.AreEqual(expected, array);
        }
Exemplo n.º 5
0
        /// <summary>
        ///     Constructs a new NUnit filter container element with the given parent and type.
        /// </summary>
        /// <param name="parent">The parent of the NUnit element or <c>null</c> if the element is the root.</param>
        /// <param name="elementType">The type of NUnit filter element.</param>
        /// <exception cref="ArgumentNullException">
        ///     <see cref="parent" /> is <c>null</c> and <see cref="elementType" /> is not
        ///     <see cref="NUnitElementType.RootFilter" />.
        /// </exception>
        /// <exception cref="ArgumentException">
        ///     <see cref="parent" /> is not <c>null</c> and <see cref="elementType" /> is
        ///     <see cref="NUnitElementType.RootFilter" />.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException"><see cref="elementType" /> is not supported by this class.</exception>
        public NUnitFilterContainerElement(INUnitFilterBaseElement parent, NUnitElementType elementType)
        {
            // Parent element can be null for root filter as the root has no parent
            if (parent == null && elementType != NUnitElementType.RootFilter)
            {
                throw ExceptionHelper.ThrowArgumentNullException(nameof(parent));
            }

            if (parent != null && elementType == NUnitElementType.RootFilter)
            {
                throw ExceptionHelper.ThrowArgumentException(Resource.ArgumentExceptionForRootFilterElementNullMessage,
                                                             nameof(parent));
            }

            Parent      = parent;
            XmlTag      = MapXmlTag(elementType);
            ElementType = elementType;
        }
Exemplo n.º 6
0
        /// <summary>
        ///     Traverse the filter structure from the leaf node to the root node.
        /// </summary>
        /// <param name="leafElement">The leaf node to start with.</param>
        /// <returns>The root node.</returns>
        private static INUnitFilterBaseElement TraverseFilterToRoot(INUnitFilterBaseElement leafElement)
        {
            if (leafElement.Child != null)
            {
                throw ExceptionHelper.ThrowArgumentException(Resource.ArgumentExceptionLeafElementMessage,
                                                             nameof(leafElement));
            }

            // Leaf doesn't have a child so start check with the child of the leaf's parent
            INUnitFilterBaseElement current = leafElement;
            INUnitFilterBaseElement parent  = current.Parent;

            // Walk backwards from leaf to root
            while (parent != null)
            {
                if (parent.Child == null)
                {
                    throw ExceptionHelper.ThrowInvalidOperationExceptionForFilterBuild(
                              Resource.InvalidOperationExceptionFilterBuildChildNullMessage, parent.ToString());
                }

                if (!ReferenceEquals(parent.Child, current))
                {
                    throw ExceptionHelper.ThrowInvalidOperationExceptionForFilterBuild(
                              Resource.InvalidOperationExceptionFilterBuildChildMismatchMessage, parent.ToString());
                }

                // Swap parent to current and get next current
                current = parent;
                parent  = parent.Parent;
            }

            // The root is expected to be of the type RootFilter
            if (current.ElementType != NUnitElementType.RootFilter)
            {
                throw ExceptionHelper.ThrowInvalidOperationExceptionForFilterBuild(
                          Resource.InvalidOperationExceptionFilterBuildElementTypeEnumMessage,
                          current.ElementType.ToString());
            }

            return(current);
        }
Exemplo n.º 7
0
        public void TestCopyToWithItemsInCollectionCopiesToArray([Range(0, 2)] int arrayIndex)
        {
            // Create collection and expected
            const int count = 3;
            ExpressionCollection <INUnitFilterBaseElement> collection =
                NUnitFilterTestHelper.CreateCollection(count, out _);
            IList <INUnitFilterBaseElement> collectionList = collection.ToList();

            INUnitFilterBaseElement[] expected = new INUnitFilterBaseElement[count + arrayIndex];
            for (int i = arrayIndex, j = 0; i < expected.Length; i++, j++)
            {
                expected[i] = collectionList[j];
            }

            INUnitFilterBaseElement[] array = new INUnitFilterBaseElement[count + arrayIndex];

            Assert.AreEqual(count, collection.Count);

            collection.CopyTo(array, arrayIndex);

            CollectionAssert.AreEqual(expected, array);
        }
Exemplo n.º 8
0
        public void TestCopyToThrowsArgumentExceptionWhenCollectionDoesNotFitInArrayLength(
            [Values] bool indexOutOfRange)
        {
            // Create collection
            const int count = 3;
            ExpressionCollection <INUnitFilterBaseElement> collection =
                NUnitFilterTestHelper.CreateCollection(count, out _);

            // If indexOutOfRange then the array index plus collection length is longer than the array,
            // otherwise the array is just not long enough to hold collection
            int arrayLength = indexOutOfRange ? count : 0;
            int arrayIndex  = indexOutOfRange ? 1 : 0;

            INUnitFilterBaseElement[] array = new INUnitFilterBaseElement[arrayLength];

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message.EqualTo(
                    "Destination array was not long enough." +
                    " Check the destination index, length, and the array's lower bounds." +
                    " (Parameter 'destinationArray')"),
                () => collection.CopyTo(array, arrayIndex));
        }
Exemplo n.º 9
0
        /// <inheritdoc cref="INUnitFilterElement.Build" />
        /// <param name="leafElement">The leaf element to build the filter from.</param>
        /// <returns>The built NUnit filter.</returns>
        /// <exception cref="ArgumentNullException"><see cref="leafElement" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException"><see cref="leafElement" /> is not the leaf element.</exception>
        internal static NUnitFilter Build(INUnitFilterBaseElement leafElement)
        {
            if (leafElement == null)
            {
                throw ExceptionHelper.ThrowArgumentNullException(nameof(leafElement));
            }

            // Initialize elements for backwards walk from leaf to root
            INUnitFilterBaseElement current = TraverseFilterToRoot(leafElement);

            // Initialize elements for forwards walk from root to leaf where current is the root
            INUnitFilterBaseElement child = current.Child;
            bool invertNext = false;

            // Initialize parent Or collection with one And expression
            // Builder assumes sum of products (groups of And statements all combined within an Or statement, e.g. ab + bcd + e)
            ExpressionCollection <ExpressionCollection <INUnitFilterBaseElement> > orExpression =
                new ExpressionCollection <ExpressionCollection <INUnitFilterBaseElement> >(c_XmlOrTag)
            {
                new ExpressionCollection <INUnitFilterBaseElement>(c_XmlAndTag)
            };

            // Walk forward from root to leaf parsing each current node along the way
            // in doing so creating the NUnit Xml filter string
            do
            {
                // Skip element if inverted as element has already been added as part of Not element
                if (invertNext)
                {
                    // Reset invert flag
                    invertNext = false;
                    // Swap child to current and get next child
                    current = child;
                    child   = child?.Child;
                    // Skip processing of this iteration's current
                    continue;
                }

                switch (current.ElementType)
                {
                case NUnitElementType.RootFilter:
                case NUnitElementType.And:
                    // Do nothing as And/RootFilter is implicitly handled by Or collection
                    break;

                case NUnitElementType.Or:
                    // Start new And collection
                    orExpression.Add(new ExpressionCollection <INUnitFilterBaseElement>(c_XmlAndTag));
                    break;

                case NUnitElementType.Not:
                case NUnitElementType.Id:
                case NUnitElementType.Test:
                case NUnitElementType.Category:
                case NUnitElementType.Class:
                case NUnitElementType.Method:
                case NUnitElementType.Namespace:
                case NUnitElementType.Property:
                case NUnitElementType.NUnitName:
                    // Add element to And collection
                    orExpression.Last().Add(current);
                    // If Not element, then the next child should be skipped
                    invertNext = current.ElementType == NUnitElementType.Not;
                    break;

                default:
                    throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForElementTypeEnum(
                              nameof(current.ElementType), current.ElementType, current.ToString());
                }

                // Swap child to current and get next child
                current = child;
                child   = child?.Child;
            } while (current != null);

            // Return formatted Xml string
            // If only one And element exists in the Or collection, then no Or nor And Xml tag is needed
            // as the root filter element acts as an And
            bool withXmlTag = orExpression.Count > 1 || orExpression.First().Count == 1;

            return(new NUnitFilter($"<{c_XmlTag}>{orExpression.ToXmlString(withXmlTag)}</{c_XmlTag}>"));
        }
 /// <summary>
 ///     Sets the child element.
 /// </summary>
 /// <param name="child">The element to set.</param>
 public void SetChild(INUnitFilterBaseElement child)
 {
     Child = child;
 }
 /// <summary>
 ///     Constructs a new NUnit filter element with the given parent, name, and other attributes.
 /// </summary>
 /// <param name="parent">The parent of the NUnit element or null if the element is the root.</param>
 /// <param name="elementType">The type of NUnit filter element.</param>
 /// <param name="name">The name of the element used in the condition check.</param>
 /// <param name="isRegularExpression">If the filter is a regular expression.</param>
 /// <param name="value">The value of the element used in the condition check, if applicable otherwise is null.</param>
 public NUnitFilterElementForTest(INUnitFilterBaseElement parent, NUnitElementType elementType, string name,
                                  bool isRegularExpression, string value = null) :
     base(parent, elementType, name, isRegularExpression, value)
 {
 }
 /// <summary>
 ///     Constructs a new NUnit filter collection element with the given parent and type.
 /// </summary>
 /// <param name="parent">The parent of the NUnit element or null if the element is the root.</param>
 /// <param name="elementType">The type of NUnit filter element.</param>
 public NUnitFilterElementCollectionForTest(INUnitFilterBaseElement parent, NUnitElementType elementType) :
     base(parent, elementType)
 {
 }
 /// <summary>
 ///     Constructs a new NUnit filter container element with the given parent and type.
 /// </summary>
 /// <param name="parent">The parent of the NUnit element or null if the element is the root.</param>
 /// <param name="elementType">The type of NUnit filter element.</param>
 public NUnitFilterContainerElementForTest(INUnitFilterBaseElement parent, NUnitElementType elementType) :
     base(parent, elementType)
 {
 }