Example #1
0
        /// <summary>
        /// Check if JsonPath element has type in a passed range or element is a recursive descent applied to JsonPath element with type in a passed range
        /// </summary>
        /// <param name="element">JsonPath element</param>
        /// <param name="firstType">First JsonPath element type in the range</param>
        /// <param name="lastType">Last JsonPath element type in the range</param>
        /// <returns>True if JsonPath element has type in a passed range or element is a recursive descent applied to element with JsonPath type in a passed range</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="lastType"/> is less than <paramref name="firstType"/></exception>
        public static bool IsOfTypeInRange(this JsonPathElement element, JsonPathElementType firstType, JsonPathElementType lastType)
        {
            if (element is null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (lastType < firstType)
            {
                throw new ArgumentOutOfRangeException(nameof(lastType), lastType, "Last type is less than first type");
            }

            while (true)
            {
                if (element.Type >= firstType && element.Type <= lastType)
                {
                    return(true);
                }

                if (!(element is JsonPathRecursiveDescentElement recursiveDescentElement))
                {
                    return(false);
                }

                element = recursiveDescentElement.AppliedToElement;
            }
        }
        public void Matches_Any_ReturnsFalse(JsonPathElementType type)
        {
            var element = new JsonPathArrayIndexElement(7);
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().BeFalse();
        }
        public void Matches_Any(JsonPathElementType type, bool?expected)
        {
            var element = new JsonPathArraySliceElement(null, null);
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().Be(expected);
        }
        public void Matches_Any(JsonPathElementType type, bool?expected)
        {
            var element = new JsonPathFilterExpressionElement("@.name");
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().Be(expected);
        }
        public void Matches_Any_ReturnsFalse(JsonPathElementType type)
        {
            var element = new JsonPathPropertyListElement(new[] { "name0", "name1" });
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().BeFalse();
        }
        public void Matches_Any(JsonPathElementType type, bool?expected)
        {
            var element = new JsonPathRecursiveDescentElement(new JsonPathArrayIndexElement(0));
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().Be(expected);
        }
Example #7
0
        public void Matches(JsonPathElementType type, bool?expected)
        {
            var element = new JsonPathAnyPropertyElement();
            var other   = ElementCreator.CreateAny(type);

            bool?actual = element.Matches(other);

            actual.Should().Be(expected);
        }
        public void IsOfTypeInRange(bool expected, JsonPathElementType elementType, bool isRecursiveDescent, JsonPathElementType firstType, JsonPathElementType lastType)
        {
            var element = isRecursiveDescent
                ? new JsonPathRecursiveDescentElement(ElementCreator.CreateAny(elementType))
                : ElementCreator.CreateAny(elementType);

            bool actual = element.IsOfTypeInRange(firstType, lastType);

            actual.Should().Be(expected);
        }
        public void IsOfType_MultipleTypes(bool expected, JsonPathElementType elementType, bool isRecursiveDescent, params JsonPathElementType[] typeArgs)
        {
            var element = isRecursiveDescent
                ? new JsonPathRecursiveDescentElement(ElementCreator.CreateAny(elementType))
                : ElementCreator.CreateAny(elementType);

            bool actual = element.IsOfType(typeArgs);

            actual.Should().Be(expected);
        }
        public void IsOfType_SingleType(JsonPathElementType elementType, bool isRecursiveDescent, JsonPathElementType typeArg, bool expected)
        {
            var element = isRecursiveDescent
                ? new JsonPathRecursiveDescentElement(ElementCreator.CreateAny(elementType))
                : ElementCreator.CreateAny(elementType);

            bool actual = element.IsOfType(typeArg);

            actual.Should().Be(expected);
        }
Example #11
0
        public static JsonPathElement CreateAny(JsonPathElementType type)
        {
            switch (type)
            {
            case JsonPathElementType.Root:
                return(new JsonPathRootElement());

            case JsonPathElementType.RecursiveDescent:
                return(new JsonPathRecursiveDescentElement(new JsonPathPropertyElement("recursive~~~")));

            case JsonPathElementType.Property:
                return(new JsonPathPropertyElement("~~~"));

            case JsonPathElementType.AnyProperty:
                return(new JsonPathAnyPropertyElement());

            case JsonPathElementType.PropertyList:
                return(new JsonPathPropertyListElement(new [] { "~~~0", "~~~1" }));

            case JsonPathElementType.ArrayIndex:
                return(new JsonPathArrayIndexElement(int.MaxValue));

            case JsonPathElementType.AnyArrayIndex:
                return(new JsonPathAnyArrayIndexElement());

            case JsonPathElementType.ArrayIndexList:
                return(new JsonPathArrayIndexListElement(new [] { int.MaxValue - 1, int.MaxValue - 2 }));

            case JsonPathElementType.ArraySlice:
                return(new JsonPathArraySliceElement(int.MaxValue - 10, int.MaxValue - 9));

            case JsonPathElementType.Expression:
                return(new JsonPathExpressionElement("~~~expr~~~"));

            case JsonPathElementType.FilterExpression:
                return(new JsonPathFilterExpressionElement("~~~filter-expr~~~"));

            default:
                throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }
        }
Example #12
0
        /// <summary>
        /// Check if JsonPath element has same type as passed or element is a recursive descent applied to JsonPath element with same type as passed
        /// </summary>
        /// <param name="element">JsonPath element</param>
        /// <param name="type">JsonPath element type to check</param>
        /// <returns>True if JsonPath element has same type as passed or element is a recursive descent applied to JsonPath element with same type as passed</returns>
        public static bool IsOfType(this JsonPathElement element, JsonPathElementType type)
        {
            if (element is null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            while (true)
            {
                if (element.Type == type)
                {
                    return(true);
                }

                if (!(element is JsonPathRecursiveDescentElement recursiveDescentElement))
                {
                    return(false);
                }

                element = recursiveDescentElement.AppliedToElement;
            }
        }