public void Resolve_JsonContainsPath_ReturnsResolverWithCorrectJtokenAndPath(string path) { JToken jtoken = JObject.Parse( @"{ ""NoChildren"": true, ""OneChildLevel"": { ""Child"": ""aValue"", ""Child2"": 2 }, ""TwoChildLevels"": { ""Child"": [ 0, 1, 2 ], ""Child2"": { ""LastProp"": true } }, }"); var resolver = new JsonPathResolver(jtoken, jtoken.Path); // Do twice to verify internal cache correctness for (int i = 0; i < 2; i++) { var results = resolver.Resolve(path).ToList(); Assert.AreEqual(1, results.Count); // Verify correct property was resolved and resolver returns correct path Assert.AreEqual(path, results[0].JToken.Path, ignoreCase: true); Assert.AreEqual(path, results[0].Path, ignoreCase: true); } }
public void Resolve_InvalidPath_ReturnsResolverWithNullJtoken(string path) { var jtoken = JObject.Parse("{ \"Property\": \"Value\" }"); var resolver = new JsonPathResolver(jtoken, jtoken.Path); var results = resolver.Resolve(path).ToList(); Assert.AreEqual(1, results.Count); Assert.AreEqual(null, results[0].JToken); }
public void Resolve_NullOrEmptyPath_ReturnsResolverWithOriginalJtoken(string path) { var jtoken = JObject.Parse("{ \"Property\": \"Value\" }"); var resolver = new JsonPathResolver(jtoken, jtoken.Path); // Do twice to verify internal cache correctness for (int i = 0; i < 2; i++) { var results = resolver.Resolve(path).ToList(); Assert.AreEqual(1, results.Count); Assert.AreEqual(jtoken, results[0].JToken); } }
public void Resolve_InvalidPath_ReturnsResolverWithNullJtokenAndCorrectResolvedPath(string path) { var jtoken = JObject.Parse("{ \"Property\": \"Value\" }"); var resolver = new JsonPathResolver(jtoken, jtoken.Path); var expectedPath = $"{jtoken.Path}.{path}"; // Do twice to verify internal cache correctness for (int i = 0; i < 2; i++) { var results = resolver.Resolve(path).ToList(); Assert.AreEqual(1, results.Count); Assert.AreEqual(null, results[0].JToken); Assert.AreEqual(expectedPath, results[0].Path); } }
public void ResolveResourceType_JObjectWithExpectedResourcesArray_ReturnsResourcesOfCorrectType(string template, string resourceType, int[] matchingResourceIndexes) { var jToken = JObject.Parse(template); var resolver = new JsonPathResolver(jToken, jToken.Path); // Do twice to verify internal cache correctness for (int i = 0; i < 2; i++) { var resources = resolver.ResolveResourceType(resourceType).ToList(); Assert.AreEqual(matchingResourceIndexes.Length, resources.Count); // Verify resources of correct type were returned for (int j = 0; j < matchingResourceIndexes.Length; j++) { var resource = resources[j]; int resourceIndex = matchingResourceIndexes[j]; var expectedPath = $"resources[{resourceIndex}]"; Assert.AreEqual(expectedPath, resource.JToken.Path); } } }