/// <summary> /// Adds children to a node using each overload of the AddModel() /// function, and ensures that all children are identical. And are /// correctly named and typed. /// </summary> /// <param name="node"></param> private void AddChildren(JObject node) { Clock clock = new Clock(); clock.Name = "Clock"; // First, add a model using type and instance methods, and ensure // that the resultant children are identical. JsonUtilities.AddModel(node, clock); JsonUtilities.AddModel(node, typeof(Clock), "Clock"); JsonUtilities.AddModel(node, typeof(Clock)); List <JObject> children = JsonUtilities.Children(node); // Node should now have 3 children. Assert.NotNull(children); Assert.AreEqual(3, children.Count); JObject childClock1 = children[0]; JObject childClock2 = children[1]; JObject childClock3 = children[2]; // Ensure that all children are identical. Assert.AreEqual(childClock1, childClock2); Assert.AreEqual(childClock1, childClock3); // Ensure that first child node has correct name and type. Assert.AreEqual("Clock", JsonUtilities.Name(childClock1)); Assert.AreEqual("Clock", JsonUtilities.Type(childClock1, withNamespace: false)); }
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 NameTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsEnsureNameWorks.json"); JObject rootNode = JObject.Parse(json); // Ensure name is correct for a model. Assert.AreEqual(JsonUtilities.Name(rootNode), "Simulations"); // Ensure name is correct for a property. Assert.AreEqual(JsonUtilities.Name(rootNode.Property("Version")), "Version"); // Ensure property value has a name of "" (empty string). Assert.AreEqual(JsonUtilities.Name(rootNode.Property("Version").Value), string.Empty); // Ensure that the name of null is null. Assert.Null(JsonUtilities.Name(null)); }