public void TestIdWithTestIdArraySetsChildAndReturnsNewAndElementWithParent()
        {
            const string name1 = "name_1";
            const string name2 = "name_2";
            const string name3 = "name_3";

            string[]     names  = { name1, null, name2, string.Empty, name3 };
            const string value  = "Value_1";
            const string xmlTag = "parent_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);
            const string expectedName = name1 + "," + name2 + "," + name3;

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            INUnitFilterElementInternal id = (INUnitFilterElementInternal)element.Id(names);

            Assert.IsNotNull(id);
            Assert.AreEqual(NUnitElementType.Id, id.ElementType);
            Assert.AreSame(element, id.Parent);
            Assert.AreSame(id, element.Child);
            Assert.AreEqual(expectedName, id.ElementName);
            Assert.IsNull(id.ElementValue);
            Assert.IsFalse(id.IsRegularExpression);
        }
        public void TestToStringReturnsStringRepresentation([Values] bool isChildNull,
                                                            [Values] bool withValue, [Values] bool isRegularExpression)
        {
            const string name   = "name_1";
            const string value  = "Value_";
            const string xmlTag = "child_";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);
            XmlSerializableElementForTest child =
                new XmlSerializableElementForTest(xmlTag + 2, value + 2, NUnitElementType.Id);

            const string parentString = "{NUnitFilterContainerElement: {Type: RootFilter}}";
            string       childString  = isChildNull ? "Null" : "{XmlSerializableElementForTest: {Type: Id}}";
            string       elementValue = withValue ? "Value_1" : null;
            string       expected     =
                $"{{NUnitFilterElementForTest: {{Type: Test, Name: \"name_1\", Value: \"{elementValue}\"," +
                $" IsRegularExpression: {isRegularExpression}, Parent: {parentString}, Child: {childString}}}}}";

            NUnitFilterElementForTest element = new NUnitFilterElementForTest(parent, NUnitElementType.Test, name,
                                                                              isRegularExpression, elementValue);

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

            string actual = element.ToString();

            Assert.AreEqual(expected, actual);
        }
        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);
        }
        public void TestConstructorWithParentNullAndElementTypeRootFilter()
        {
            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            Assert.IsNull(element.Parent);
            Assert.IsNull(element.Child);
            Assert.AreEqual(NUnitElementType.RootFilter, element.ElementType);
            Assert.AreEqual(NUnitFilterTestHelper.XmlFilterTag, element.XmlTag);
        }
        public void TestConstructorThrowsArgumentExceptionWhenNameIsNullOrEmpty([Values] bool isNull)
        {
            string name = isNull ? null : string.Empty;
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message
                .EqualTo("The name cannot be null or empty. (Parameter 'name')"),
                // ReSharper disable once ObjectCreationAsStatement
                () => new NUnitFilterElement(parent, NUnitElementType.Test, name, false));
        }
        public void TestElementNamePropertyReturnsElementNameProvidedWithConstructor()
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            INUnitFilterElementInternal element =
                new NUnitFilterElement(parent, NUnitElementType.Property, name, false, value);

            Assert.AreEqual(name, element.ElementName);
        }
        public void TestXmlTagPropertyReturnsXmlTagProvidedWithConstructor()
        {
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            Assert.AreEqual(NUnitFilterTestHelper.XmlAndTag, element.XmlTag);
        }
        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);
        }
        public void TestIsRegularExpressionPropertyReturnsIsRegularExpressionProvidedWithConstructor(
            [Values] bool isRegularExpression)
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            INUnitFilterElementInternal element =
                new NUnitFilterElement(parent, NUnitElementType.Property, name, isRegularExpression, value);

            Assert.AreEqual(isRegularExpression, element.IsRegularExpression);
        }
        TestConstructorThrowsArgumentExceptionWhenElementTypeIsPropertyAndValueIsNullOrEmpty(
            [Values] bool isNull)
        {
            const string name  = "name_1";
            string       value = isNull ? null : string.Empty;
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message
                .EqualTo("The value cannot be null or empty. (Parameter 'value')"),
                // ReSharper disable once ObjectCreationAsStatement
                () => new NUnitFilterElement(parent, NUnitElementType.Property, name, false, value));
        }
        public void TestChildPropertyThrowsArgumentNullExceptionWhenChildSetToNull()
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            NUnitFilterElementForTest element =
                new NUnitFilterElementForTest(parent, NUnitElementType.Property, name, false, value);

            Assert.Throws(
                Is.TypeOf <ArgumentNullException>().And.Message
                .EqualTo("The value cannot be null. (Parameter 'value')"), () => element.SetChild(null));
        }
        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));
        }
        public void TestOrPropertySetsChildAndReturnsNewOrElementWithParent()
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            INUnitFilterElementInternal element =
                new NUnitFilterElement(parent, NUnitElementType.Property, name, false, value);

            INUnitFilterElementCollectionInternal or = (INUnitFilterElementCollectionInternal)element.Or;

            Assert.IsNotNull(or);
            Assert.AreEqual(NUnitElementType.Or, or.ElementType);
            Assert.AreSame(element, or.Parent);
            Assert.AreSame(or, element.Child);
        }
        TestIdThrowsArgumentExceptionWhenMultipleTestIdsContainsOnlyNullOrEmptyValues()
        {
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message
                .EqualTo("The name cannot be null or empty. (Parameter 'name')"), () =>
            {
                // ReSharper disable once UnusedVariable
                INUnitFilterElement id = element.Id(null, string.Empty);
            });
        }
        public void TestChildPropertyReturnsSetChild()
        {
            const string name   = "name_1";
            const string value  = "Value_";
            const string xmlTag = "child_";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);
            XmlSerializableElementForTest child =
                new XmlSerializableElementForTest(xmlTag + 2, value + 2, NUnitElementType.Test);

            NUnitFilterElementForTest element =
                new NUnitFilterElementForTest(parent, NUnitElementType.Property, name, false, value);

            Assert.IsNull(element.Child);

            element.SetChild(child);

            Assert.AreSame(child, element.Child);
        }
        public void TestConstructorWithValidArgumentsAndElementTypeProperty(
            [Values] bool isRegularExpression)
        {
            const string name  = "name_1";
            const string value = "Value_1";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);

            INUnitFilterElementInternal element =
                new NUnitFilterElement(parent, NUnitElementType.Property, name, isRegularExpression, value);

            Assert.AreSame(parent, element.Parent);
            Assert.IsNull(element.Child);
            Assert.AreEqual(name, element.ElementName);
            Assert.AreEqual(value, element.ElementValue);
            Assert.AreSame("prop", element.XmlTag);
            Assert.AreEqual(NUnitElementType.Property, element.ElementType);
            Assert.AreEqual(isRegularExpression, element.IsRegularExpression);
        }
        public void TestIdThrowsArgumentExceptionWhenTestIdArrayNullOrEmpty(
            [Values] bool isNull)
        {
            string[]     names  = isNull ? null : Array.Empty <string>();
            const string value  = "Value_1";
            const string xmlTag = "name_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message
                .EqualTo("The testIds cannot be null or empty. (Parameter 'testIds')"), () =>
            {
                // ReSharper disable once UnusedVariable
                INUnitFilterElement id = element.Id(names);
            });
        }
        public void TestNameThrowsArgumentExceptionWhenNameElementNameIsNullOrEmpty(
            [Values] bool isNull)
        {
            string       name   = isNull ? null : string.Empty;
            const string value  = "Value_1";
            const string xmlTag = "parent_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            Assert.Throws(
                Is.TypeOf <ArgumentException>().And.Message
                .EqualTo("The name cannot be null or empty. (Parameter 'name')"), () =>
            {
                // ReSharper disable once UnusedVariable
                INUnitFilterElement nameElement = element.Name(name);
            });
        }
        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);
            }
        }
        public void TestIdWithSingleTestIdSetsChildAndReturnsNewAndElementWithParent()
        {
            const string name   = "name_1";
            const string value  = "Value_1";
            const string xmlTag = "parent_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            INUnitFilterElementInternal id = (INUnitFilterElementInternal)element.Id(name);

            Assert.IsNotNull(id);
            Assert.AreEqual(NUnitElementType.Id, id.ElementType);
            Assert.AreSame(element, id.Parent);
            Assert.AreSame(id, element.Child);
            Assert.AreEqual(name, id.ElementName);
            Assert.IsNull(id.ElementValue);
            Assert.IsFalse(id.IsRegularExpression);
        }
        public void TestTestSetsChildAndReturnsNewAndElementWithParent(
            [Values] bool isRegularExpression)
        {
            const string name   = "name_1";
            const string value  = "Value_1";
            const string xmlTag = "parent_1";
            XmlSerializableElementForTest parent =
                new XmlSerializableElementForTest(xmlTag, value, NUnitElementType.Test);

            INUnitFilterContainerElementInternal element =
                new NUnitFilterContainerElement(parent, NUnitElementType.And);

            INUnitFilterElementInternal test = (INUnitFilterElementInternal)element.Test(name, isRegularExpression);

            Assert.IsNotNull(test);
            Assert.AreEqual(NUnitElementType.Test, test.ElementType);
            Assert.AreSame(element, test.Parent);
            Assert.AreSame(test, element.Child);
            Assert.AreEqual(name, test.ElementName);
            Assert.IsNull(test.ElementValue);
            Assert.AreEqual(isRegularExpression, test.IsRegularExpression);
        }
        public void TestOrPropertyThrowsInvalidOperationExceptionWhenChildIsAlreadySet()
        {
            const string name   = "name_1";
            const string value  = "Value_";
            const string xmlTag = "child_";
            NUnitFilterContainerElement parent =
                new NUnitFilterContainerElement(null, NUnitElementType.RootFilter);
            XmlSerializableElementForTest child =
                new XmlSerializableElementForTest(xmlTag + 2, value + 2, NUnitElementType.Test);

            NUnitFilterElementForTest element =
                new NUnitFilterElementForTest(parent, NUnitElementType.Property, name, false, value);

            element.SetChild(child);

            Assert.Throws(
                Is.TypeOf <InvalidOperationException>().And.Message
                .EqualTo("The child element has already been set for this instance."), () =>
            {
                // ReSharper disable once UnusedVariable
                INUnitFilterElementCollection or = element.Or;
            });
        }
        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;

            case NUnitElementType.Not:
                expectedXmlTag = NUnitFilterTestHelper.XmlNotTag;
                break;

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

            INUnitFilterContainerElementInternal element = new NUnitFilterContainerElement(parent, elementType);

            Assert.AreSame(parent, element.Parent);
            Assert.IsNull(element.Child);
            Assert.AreEqual(elementType, element.ElementType);
            Assert.AreEqual(expectedXmlTag, element.XmlTag);
        }
        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);
        }