public void RenamePropertyTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsDescendantsByType.json"); JObject rootNode = JObject.Parse(json); List <JObject> axes = JsonUtilities.ChildrenRecursively(rootNode, "Models.Graph.Axis"); JsonUtilities.RenameProperty(axes[0], "Title", "Title2"); Assert.IsNull(axes[0]["Title"]); Assert.AreEqual(axes[0]["Title2"].Value <string>(), "Date"); }
public void ParentTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsDescendantsByType.json"); JObject rootNode = JObject.Parse(json); Assert.IsNull(JsonUtilities.Parent(rootNode)); List <JObject> descendants = JsonUtilities.ChildrenRecursively(rootNode, "Models.Graph.Axis"); var graph = JsonUtilities.Parent(descendants[0]); Assert.AreEqual(JsonUtilities.Name(graph), "Graph"); }
public void AddConstantFunctionIfNotExistsTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsDescendantsByType.json"); JObject rootNode = JObject.Parse(json); List <JObject> axes = JsonUtilities.ChildrenRecursively(rootNode, "Models.Graph.Axis"); JsonUtilities.AddConstantFunctionIfNotExists(axes[0], "ConstantFunction", "1"); var constant = JsonUtilities.ChildWithName(axes[0], "ConstantFunction"); Assert.NotNull(constant); Assert.AreEqual(constant["$type"].Value <string>(), "Models.Functions.Constant, Models"); Assert.AreEqual(constant["FixedValue"].Value <string>(), "1"); }
public void DescendantsByTypeTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsDescendantsByType.json"); JObject rootNode = JObject.Parse(json); List <JObject> descendants = JsonUtilities.ChildrenRecursively(rootNode, "Models.Graph.Axis"); List <JObject> descendatnsWithoutNamespace = JsonUtilities.ChildrenRecursively(rootNode, "Axis"); List <JObject> children = JsonUtilities.Children(rootNode).Cast <JObject>().ToList(); List <JObject> emptyList = new List <JObject>(); // Ensure descendants is not null. Assert.NotNull(descendants); // Ensure number of descendants of type Models.Core.Axis is correct. Assert.AreEqual(2, descendants.Count); // Ensure number of descendatns of type Axis is correct. Assert.AreEqual(2, descendatnsWithoutNamespace.Count); // Ensure descendants of null is an empty list when filtering by type with namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(null, "Models.Graph.Axis")); // Ensure descendants of null is an empty list when filtering by type without namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(null, "Axis")); // Ensure descendants of a node with an empty children property // is an empty list when filtering by type with namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[1], "Models.Graph.Axis")); // Ensure descendants of a node with an empty children property // is an empty list when filtering by type without namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[1], "Axis")); // Ensure descendants of a node with no children property // is an empty list when filtering by type with namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[2], "Models.Graph.Axis")); // Ensure descendants of a node with no children property // is an empty list when filtering by type without namespace. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[2], "Axis")); }
public void ChildrenRecursivelyTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsEnsureChildrenRecursivelyWorks.json"); JObject rootNode = JObject.Parse(json); List <JObject> children = JsonUtilities.Children(rootNode); List <JObject> descendants = JsonUtilities.ChildrenRecursively(rootNode); List <JObject> emptyList = new List <JObject>(); // Ensure descendants is not null. Assert.NotNull(descendants); // Ensure number of descendants is correct. Assert.AreEqual(6, descendants.Count); // Ensure descendants of null is an empty list (not null). Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(null)); // Ensure descendants of a node with an empty children property is an empty list. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[0] as JObject)); // Ensure descendants of a node with no children property is an empty list. Assert.AreEqual(emptyList, JsonUtilities.ChildrenRecursively(children[1] as JObject)); }