public void UnrecognizedMethodName_ThrowsException()
        {
            string path  = "$.store.book[?(@.price.nonExistingMethodName() > 10)]";
            string input = TestDataLoader.Store();

            Assert.Throws <UnrecognizedMethodNameException>(() => JsonPath.ExecutePath(path, input));
        }
Beispiel #2
0
        public ReadMeExamplesTests()
        {
            _json           = TestDataLoader.Store();
            _jsonDoc        = JsonDocument.Parse(_json);
            _jsonDocElement = _jsonDoc.RootElement;

            _lotr = @"{""category"":""fiction"",""author"":""J. R. R. Tolkien"",""title"":""The Lord of the Rings"",""isbn"":""0-395-19395-8"",""price"":22.99}";
        }
        public void AllProperties_ReturnsValidResult(string path, string expected)
        {
            string json = TestDataLoader.Store();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result).RemoveWhiteSpace();

            Assert.Equal(expected, resultJson);
        }
        public void PathWithSlice_ReturnsExpectedResult(string path, string expectedJson)
        {
            expectedJson = expectedJson.Replace("`", "\"");
            string json = TestDataLoader.AbcArray();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result);

            Assert.Equal(expectedJson, resultJson);
        }
        public void FilterOnNonExistingPropertyEqualNull_ReturnsCorrectResult()
        {
            string input = TestDataLoader.BooksWithNulls();

            ExpressionList expression = ExpressionList.TokenizeAndParse("$.books[?(@.plumbus == null)]");

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(expression, input);

            Assert.Equal(4, result.Count);
        }
        public void FilterWithWildcardPropertyFilter_ReturnsCorrectResult()
        {
            string path  = "$.store.bicycle.*";
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(2, result.Count);

            Assert.Contains(result, x => x.ValueKind == JsonValueKind.String && x.GetString() == "red");
            Assert.Contains(result, x => x.ValueKind == JsonValueKind.Number && x.GetDouble() == 19.95);

            path   = "store.*";
            result = JsonPath.ExecutePath(path, input);

            string resultJson = JsonSerializer.Serialize(result).RemoveWhiteSpace();

            string expected = @"
                [
                  [
                    {
                      `category`: `reference`,
                      `author`: `Nigel Rees`,
                      `title`: `Sayings of the Century`,
                      `price`: 8.95
                    },
                    {
                      `category`: `fiction`,
                      `author`: `Evelyn Waugh`,
                      `title`: `Sword of Honour`,
                      `price`: 12.99
                    },
                    {
                      `category`: `fiction`,
                      `author`: `Herman Melville`,
                      `title`: `Moby Dick`,
                      `isbn`: `0-553-21311-3`,
                      `price`: 8.99
                    },
                    {
                      `category`: `fiction`,
                      `author`: `J. R. R. Tolkien`,
                      `title`: `The Lord of the Rings`,
                      `isbn`: `0-395-19395-8`,
                      `price`: 22.99
                    }
                  ],
                  {
                    `color`: `red`,
                    `price`: 19.95
                  }
                ]"
                              .Replace("`", "\"").RemoveWhiteSpace();

            Assert.Equal(expected, resultJson);
        }
        public void PathWithAllElements_ReturnsExpectedResult()
        {
            string path = "[*]";
            string json = TestDataLoader.AbcArray();

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, json);
            string resultJson = JsonSerializer.Serialize(result);

            json = new string(json.Where(x => !char.IsWhiteSpace(x)).ToArray());

            Assert.Equal(json, resultJson);
        }
        public void FilterOnNumberEqualNull_ReturnsCorrectResult()
        {
            string input = TestDataLoader.BooksWithNulls();

            ExpressionList expression = ExpressionList.TokenizeAndParse("$.books[?(@.price== null)]");

            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(expression, input);

            Assert.Equal(1, result.Count);
            Assert.Equal(JsonValueKind.Object, result[0].ValueKind);
            Assert.Equal("Sayings of the Century", result[0].EnumerateObject().FirstOrDefault(x => x.Name == "title").Value.GetString());
        }
        public void FilterOnBooks_ReturnsCorrectResult(string path, params string[] expected)
        {
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(expected.Length, result.Count);
            string[] resultJsons = result.Select(x => JsonSerializer.Serialize(x)).Select(x => x.RemoveWhiteSpace()).ToArray();

            foreach (string e in expected)
            {
                Assert.Contains(_bookJsons[e], resultJsons);
            }
        }
        public void RecursiveFilterAndWildcardArrayFilter_ReturnsCorrectResult(string path, params string[] expected)
        {
            string input = TestDataLoader.Store();
            IReadOnlyList <JsonElement> result = JsonPath.ExecutePath(path, input);

            Assert.Equal(expected.Length, result.Count);

            foreach (JsonElement r in result)
            {
                Assert.Equal(JsonValueKind.String, r.ValueKind);
                string rString = r.GetString();
                Assert.Contains(rString, expected);
            }
        }