public void TestBuildWithOnlyOneFilterElement(
            [ValueSource(typeof(NUnitFilterTestHelper), nameof(NUnitFilterTestHelper.GetFilterElements))]
            NUnitElementType elementType, [Values] bool isRegularExpression)
        {
            NUnitFilter filter   = null;
            string      expected = null;
            string      regexp   = isRegularExpression ? " re=\"1\"" : string.Empty;

            switch (elementType)
            {
            case NUnitElementType.Id:
                filter   = NUnitFilter.Where.Id("id1", "id2", "id3").Build();
                expected = "<id>id1,id2,id3</id>";
                break;

            case NUnitElementType.Test:
                filter   = NUnitFilter.Where.Test("test1", isRegularExpression).Build();
                expected = $"<test{regexp}>test1</test>";
                break;

            case NUnitElementType.Category:
                filter   = NUnitFilter.Where.Category("cat1", isRegularExpression).Build();
                expected = $"<cat{regexp}>cat1</cat>";
                break;

            case NUnitElementType.Class:
                filter   = NUnitFilter.Where.Class("class1", isRegularExpression).Build();
                expected = $"<class{regexp}>class1</class>";
                break;

            case NUnitElementType.Method:
                filter   = NUnitFilter.Where.Method("method1", isRegularExpression).Build();
                expected = $"<method{regexp}>method1</method>";
                break;

            case NUnitElementType.Namespace:
                filter   = NUnitFilter.Where.Namespace("ns1", isRegularExpression).Build();
                expected = $"<namespace{regexp}>ns1</namespace>";
                break;

            case NUnitElementType.Property:
                filter   = NUnitFilter.Where.Property("prop1", "value1", isRegularExpression).Build();
                expected = $"<prop{regexp} name=\"prop1\">value1</prop>";
                break;

            case NUnitElementType.NUnitName:
                filter   = NUnitFilter.Where.Name("name1", isRegularExpression).Build();
                expected = $"<name{regexp}>name1</name>";
                break;

            default:
                Assert.Fail($"The type {elementType} is not supported for this test.");
                break;
            }

            expected = $"<filter>{expected}</filter>";

            Assert.IsNotNull(filter);
            Assert.AreEqual(expected, filter.FilterXmlString);
        }
        /// <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;
        }
        public void TestToStringReturnsStringRepresentation([Values] bool isParentNull,
                                                            [Values] bool isChildNull)
        {
            // Create expected string of xml nodes
            const string valueParent             = "Value_1";
            const string xmlTagParent            = "name_1";
            XmlSerializableElementForTest parent = isParentNull
                ? null
                : new XmlSerializableElementForTest(xmlTagParent, valueParent, NUnitElementType.Test);

            const string valueChild             = "Value_2";
            const string xmlTagChild            = "name_2";
            XmlSerializableElementForTest child =
                new XmlSerializableElementForTest(xmlTagChild, valueChild, NUnitElementType.Id);

            NUnitElementType elementType  = isParentNull ? NUnitElementType.RootFilter : NUnitElementType.And;
            string           parentString = isParentNull ? "Null" : "{XmlSerializableElementForTest: {Type: Test}}";
            string           childString  = isChildNull ? "Null" : "{XmlSerializableElementForTest: {Type: Id}}";
            string           expected     =
                $"{{NUnitFilterContainerElementForTest: {{Type: {elementType}, Parent: {parentString}, Child: {childString}}}}}";

            NUnitFilterContainerElementForTest element =
                new NUnitFilterContainerElementForTest(parent, elementType);

            if (!isChildNull)
            {
                element.SetChild(child);
            }

            string actual = element.ToString();

            Assert.AreEqual(expected, actual);
        }
        /// <summary>
        ///     Maps the element type to the expected Xml tag string.
        /// </summary>
        /// <param name="elementType">The type of NUnit filter element.</param>
        /// <returns>The mapped type of the Xml tag string.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><see cref="elementType" /> is not supported by this class.</exception>
        private static string MapXmlTag(NUnitElementType elementType)
        {
            switch (elementType)
            {
            case NUnitElementType.Id:
                return("id");

            case NUnitElementType.Test:
                return("test");

            case NUnitElementType.Category:
                return("cat");

            case NUnitElementType.Class:
                return("class");

            case NUnitElementType.Method:
                return("method");

            case NUnitElementType.Namespace:
                return("namespace");

            case NUnitElementType.Property:
                return("prop");

            case NUnitElementType.NUnitName:
                return("name");

            default:
                throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForElementTypeEnum(nameof(elementType),
                                                                                         elementType);
            }
        }
Beispiel #5
0
        public void TestConstructorWithParentNotNullAndElementTypeSupportedButNotRootFilter(
            NUnitElementType elementType)
        {
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            string expectedXmlTag = null;

            switch (elementType)
            {
            // NUnitElementType.RootFilter covered in a dedicated test case
            case NUnitElementType.And:
                expectedXmlTag = NUnitFilterTestHelper.XmlAndTag;
                break;

            case NUnitElementType.Or:
                expectedXmlTag = NUnitFilterTestHelper.XmlOrTag;
                break;

            default:
                Assert.Fail($"The type {elementType} is not supported for this test.");
                break;
            }

            INUnitFilterElementCollectionInternal element = new NUnitFilterElementCollection(parent, elementType);

            Assert.AreSame(parent, element.Parent);
            Assert.IsNull(element.Child);
            Assert.AreEqual(elementType, element.ElementType);
            Assert.AreEqual(expectedXmlTag, element.XmlTag);
        }
        public void TestBuildWithOnlyANotPlusFilterElement(
            [ValueSource(typeof(NUnitFilterTestHelper), nameof(NUnitFilterTestHelper.GetFilterElements))]
            NUnitElementType elementType)
        {
            const string value = "Value_";
            NUnitFilterContainerElementForTest root =
                new NUnitFilterContainerElementForTest(null, NUnitElementType.RootFilter);
            XmlSerializableElementForTest child1 =
                new XmlSerializableElementForTest("name", value + 1, NUnitElementType.Not);
            XmlSerializableElementForTest child2 =
                new XmlSerializableElementForTest("class", value + 2, elementType);

            root.SetChild(child1);
            child1.Parent = root;
            child1.Child  = child2;
            child2.Parent = child1;

            // Not will skip its child element
            const string expected = "<filter><name>Value_1</name></filter>";

            NUnitFilter filter = NUnitFilter.Build(child2);

            Assert.IsNotNull(filter);
            Assert.AreEqual(expected, filter.FilterXmlString);
            Assert.AreEqual(expected, GetXmlString(filter.Filter));
        }
 /// <summary>
 ///     Constructs a new element for test with the given xml tag and value and type.
 /// </summary>
 /// <param name="xmlTag">The Xml string element tag.</param>
 /// <param name="xmlName">The Xml string element name.</param>
 /// <param name="elementType">The type of the NUnit filter.</param>
 /// <param name="xmlValue">The Xml string element value.</param>
 public XmlSerializableElementForTest(string xmlTag, string xmlName,
                                      NUnitElementType elementType, string xmlValue = null)
 {
     XmlTag      = xmlTag;
     v_XmlName   = xmlName;
     ElementType = elementType;
     v_XmlValue  = xmlValue;
 }
        public void TestConstructorWithValidArgumentsAndElementTypeSupportedButNotProperty(
            [ValueSource(typeof(NUnitFilterTestHelper), nameof(NUnitFilterTestHelper.GetFilterElementsExceptProperty))]
            NUnitElementType elementType, [Values] bool isRegularExpression)
        {
            const string name = "name_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            string expectedXmlTag = null;

            switch (elementType)
            {
            case NUnitElementType.Id:
                expectedXmlTag = "id";
                break;

            case NUnitElementType.Test:
                expectedXmlTag = "test";
                break;

            case NUnitElementType.Category:
                expectedXmlTag = "cat";
                break;

            case NUnitElementType.Class:
                expectedXmlTag = "class";
                break;

            case NUnitElementType.Method:
                expectedXmlTag = "method";
                break;

            case NUnitElementType.Namespace:
                expectedXmlTag = "namespace";
                break;

            // NUnitElementType.Property covered in a dedicated test case
            case NUnitElementType.NUnitName:
                expectedXmlTag = "name";
                break;

            default:
                Assert.Fail($"The type {elementType} is not supported for this test.");
                break;
            }

            INUnitFilterElementInternal
                element = new NUnitFilterElement(parent, elementType, name, isRegularExpression);

            Assert.AreSame(parent, element.Parent);
            Assert.IsNull(element.Child);
            Assert.AreEqual(name, element.ElementName);
            Assert.IsNull(element.ElementValue);
            Assert.AreSame(expectedXmlTag, element.XmlTag);
            Assert.AreEqual(elementType, element.ElementType);
            Assert.AreEqual(isRegularExpression, element.IsRegularExpression);
        }
 /// <inheritdoc />
 public NUnitFilterElementCollection(INUnitFilterBaseElement parent, NUnitElementType elementType) : base(
         parent, elementType)
 {
     if (elementType == NUnitElementType.Not)
     {
         throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForElementTypeEnum(nameof(elementType),
                                                                                  elementType);
     }
 }
        public void TestElementValuePropertyReturnsElementValueProvidedWithConstructor(
            [Values] bool isNull)
        {
            const string                name        = "name_1";
            string                      value       = isNull ? null : "Value_1";
            NUnitElementType            elementType = isNull ? NUnitElementType.Test : NUnitElementType.Property;
            NUnitFilterContainerElement parent      =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            INUnitFilterElementInternal element = new NUnitFilterElement(parent, elementType, name, false, value);

            Assert.AreEqual(value, element.ElementValue);
        }
        TestConstructorThrowsArgumentOutOfRangeExceptionWhenElementTypeNotSupported(
            NUnitElementType elementType)
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            Assert.Throws(
                Is.TypeOf <ArgumentOutOfRangeException>().And.Message
                .EqualTo(
                    $"The given element type is not supported. (Parameter 'elementType'){Environment.NewLine}" +
                    $"Actual value was {elementType}."),
                // ReSharper disable once ObjectCreationAsStatement
                () => new NUnitFilterElement(parent, elementType, name, false, value));
        }
        TestConstructorThrowsArgumentOutOfRangeExceptionWhenElementTypeNotSupported(
            [ValueSource(typeof(NUnitFilterTestHelper), nameof(NUnitFilterTestHelper.GetFilterElements))]
            NUnitElementType elementType)
        {
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            Assert.Throws(
                Is.TypeOf <ArgumentOutOfRangeException>().And.Message
                .EqualTo(
                    $"The given element type is not supported. (Parameter 'elementType'){Environment.NewLine}" +
                    $"Actual value was {elementType}."),
                // ReSharper disable once ObjectCreationAsStatement
                () => new NUnitFilterContainerElement(parent, elementType));
        }
Beispiel #13
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;
        }
        public void TestParentPropertyReturnsParentProvidedWithConstructor(
            [Values] bool isNull)
        {
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                isNull ? null : new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);
            NUnitElementType elementType = isNull ? NUnitElementType.RootFilter : NUnitElementType.And;

            INUnitFilterContainerElementInternal element = new NUnitFilterContainerElement(parent, elementType);

            if (isNull)
            {
                Assert.IsNull(element.Parent);
            }
            else
            {
                Assert.AreSame(parent, element.Parent);
            }
        }
Beispiel #15
0
        /// <summary>
        ///     Maps the element type to the expected Xml tag string.
        /// </summary>
        /// <param name="elementType">The type of NUnit filter element.</param>
        /// <returns>The mapped type of the Xml tag string.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><see cref="elementType" /> is not supported by this class.</exception>
        private static string MapXmlTag(NUnitElementType elementType)
        {
            switch (elementType)
            {
            case NUnitElementType.RootFilter:
                return("filter");

            case NUnitElementType.And:
                return("and");

            case NUnitElementType.Or:
                return("or");

            case NUnitElementType.Not:
                return("not");

            default:
                throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForElementTypeEnum(nameof(elementType),
                                                                                         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)
 {
 }
 /// <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)
 {
 }
        public void TestToXmlString(
            [ValueSource(typeof(NUnitFilterTestHelper), nameof(NUnitFilterTestHelper.GetFilterElements))]
            NUnitElementType elementType, [Values] bool isRegularExpression, [Values] bool withXmlTag)
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            string expectedXmlTag = null;

            switch (elementType)
            {
            case NUnitElementType.Id:
                expectedXmlTag = "id";
                break;

            case NUnitElementType.Test:
                expectedXmlTag = "test";
                break;

            case NUnitElementType.Category:
                expectedXmlTag = "cat";
                break;

            case NUnitElementType.Class:
                expectedXmlTag = "class";
                break;

            case NUnitElementType.Method:
                expectedXmlTag = "method";
                break;

            case NUnitElementType.Namespace:
                expectedXmlTag = "namespace";
                break;

            case NUnitElementType.Property:
                expectedXmlTag = "prop";
                break;

            case NUnitElementType.NUnitName:
                expectedXmlTag = "name";
                break;

            default:
                Assert.Fail($"The type {elementType} is not supported for this test.");
                break;
            }

            string expected;

            if (elementType == NUnitElementType.Property)
            {
                expected = withXmlTag
                    ? NUnitFilterTestHelper.CreateXmlNode(expectedXmlTag, value, isRegularExpression,
                                                          new Dictionary <string, string> {
                    { "name", name }
                })
                    : value;
            }
            else
            {
                expected = withXmlTag
                    ? NUnitFilterTestHelper.CreateXmlNode(expectedXmlTag, name, isRegularExpression)
                    : name;
            }

            INUnitFilterElementInternal element =
                new NUnitFilterElement(parent, elementType, name, isRegularExpression, value);

            string actual = element.ToXmlString(withXmlTag);

            Assert.AreEqual(expected, actual);
        }
 /// <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>
        ///     Creates an ExpressionCollection and the expected inner xml.
        /// </summary>
        /// <param name="numElements">The number of elements to add to the collection.</param>
        /// <param name="innerXml">Outputs the expected innerXml of the collection.</param>
        /// <param name="xmlParentTag">The parent tag to use for the collection.</param>
        /// <param name="elementType">The type of element of the contained nodes.</param>
        /// <returns>The ExpressionCollection with elements.</returns>
        public static ExpressionCollection <INUnitFilterBaseElement> CreateCollection(int numElements,
                                                                                      out string innerXml, string xmlParentTag = XmlAndTag, NUnitElementType elementType = NUnitElementType.And)
        {
            if (numElements < 0)
            {
                throw ExceptionHelper.ThrowArgumentOutOfRangeExceptionForValueLessThanZero(nameof(numElements),
                                                                                           numElements);
            }

            innerXml = string.Empty;
            ExpressionCollection <INUnitFilterBaseElement> collection =
                new ExpressionCollection <INUnitFilterBaseElement>(xmlParentTag);

            if (numElements == 0)
            {
                return(collection);
            }

            // Create expected string of xml nodes
            const string         value    = "Value_";
            const string         xmlTag   = "name_";
            IEnumerable <int>    range    = Enumerable.Range(1, numElements);
            IEnumerable <string> elements = range.Select(i => CreateXmlNode(xmlTag + i, value + i));

            innerXml = string.Join(string.Empty, elements);

            // Add expressions to collection
            for (int i = 1; i <= numElements; i++)
            {
                collection.Add(new XmlSerializableElementForTest(xmlTag + i, value + i, elementType));
            }

            return(collection);
        }