public void Run(ReferenceImplTestCase testCase)
        {
            if (_notSupported.Contains(testCase.Selector))
            {
                Assert.Inconclusive("This case will not be supported.");
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine(testCase);
            Console.WriteLine();

            Json.Path.JsonPath path   = null;
            PathResult         actual = null;

            var time = Debugger.IsAttached ? int.MaxValue : 100;

            using var cts = new CancellationTokenSource(time);
            Task.Run(() =>
            {
                if (!Json.Path.JsonPath.TryParse(testCase.Selector, out path))
                {
                    return;
                }

                if (testCase.Document.ValueKind == JsonValueKind.Undefined)
                {
                    return;
                }

                actual = path.Evaluate(testCase.Document);
            }, cts.Token).Wait(cts.Token);

            if (path != null && testCase.InvalidSelector)
            {
                Assert.Inconclusive($"{testCase.Selector} is not a valid path but was parsed without error.");
            }

            if (actual == null)
            {
                if (testCase.InvalidSelector)
                {
                    return;
                }
                Assert.Fail($"Could not parse path: {testCase.Selector}");
            }

            Console.WriteLine($"Actual: {JsonSerializer.Serialize(actual)}");
            if (testCase.InvalidSelector)
            {
                Assert.Fail($"{testCase.Selector} is not a valid path.");
            }

            var expected = testCase.Result;

            Assert.IsTrue(expected.Select((v, i) => (v, i)).All(v => JsonElementEqualityComparer.Instance.Equals(v.v, TryGetValueAtIndex(actual.Matches, v.i)?.Value ?? default)));
        }
Ejemplo n.º 2
0
        internal static bool TryParse(ReadOnlySpan <char> span, ref int i, bool allowTrailingContent, out JsonPath path)
        {
            var nodes = new List <IPathNode>();

            while (i < span.Length)
            {
                var node = span[i] switch
                {
                    '$' => AddRootNode(span, ref i),
                    '@' => AddLocalRootNode(span, ref i),
                    '.' => AddPropertyOrRecursive(span, ref i),
                    '[' => AddIndex(span, ref i),
                    _ => null
                };

                if (node == null)
                {
                    if (allowTrailingContent)
                    {
                        break;
                    }
                    path = null;
                    return(false);
                }

                nodes.Add(node);
            }

            if (!nodes.Any())
            {
                path = null;
                return(false);
            }

            path = new JsonPath(nodes);
            return(true);
        }