public void ChildWithName() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsEnsureChildrenWorks.json"); JObject rootNode = JObject.Parse(json); Assert.NotNull(JsonUtilities.ChildWithName(rootNode, "Clock")); Assert.IsNull(JsonUtilities.ChildWithName(rootNode, "XYZ")); }
public void EnsureReleasedModelsAreNotSaved() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Resources.WheatModel.apsimx"); IModel topLevel = FileFormat.ReadFromString <Simulations>(json, out List <Exception> errors); string serialized = FileFormat.WriteToString(topLevel); JObject root = JObject.Parse(serialized); JObject wheat = JsonUtilities.ChildWithName(JsonUtilities.ChildWithName(root, "Replacements"), "Wheat"); Assert.NotNull(wheat); Assert.AreEqual(0, JsonUtilities.Children(wheat).Count); }
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 AddModelTests() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTests.AddModelTests.json"); JObject node = JObject.Parse(json); // Clock1 has an empty Children array, whereas clock2 does not have // a Children array. This test ensures that this does not matter. JObject clock1 = JsonUtilities.ChildWithName(node, "Clock1"); JObject clock2 = JsonUtilities.ChildWithName(node, "Clock2"); AddChildren(clock1); AddChildren(clock2); string clock2Name = clock2["Name"]?.ToString(); clock2["Name"] = clock1["Name"]; // For comparison purposes. Assert.AreEqual(clock1, clock2, "Clock1:" + Environment.NewLine + clock1?.ToString() + Environment.NewLine + "Clock2:" + Environment.NewLine + clock2?.ToString()); JObject nullJObject = null; IModel nullModel = null; Type nullType = null; string nullString = null; // JsonUtilities.AddModel(JObject, IModel) Assert.That(() => JsonUtilities.AddModel(nullJObject, new Clock()), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(clock1, nullModel), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, nullModel), Throws.InstanceOf <Exception>()); // JsonUtilities.AddModel(JObject, Type) Assert.That(() => JsonUtilities.AddModel(nullJObject, typeof(Clock)), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(clock1, nullType), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, nullType), Throws.InstanceOf <Exception>()); // JsonUtilities.AddModel(JObject, Type, string) Assert.That(() => JsonUtilities.AddModel(clock1, typeof(Clock), nullString), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(clock1, nullType, ""), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(clock1, nullType, nullString), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, typeof(Clock), ""), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, typeof(Clock), nullString), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, nullType, nullString), Throws.InstanceOf <Exception>()); Assert.That(() => JsonUtilities.AddModel(nullJObject, nullType, ""), Throws.InstanceOf <Exception>()); }
public void EnsureReleasedModelsAreNotSaved() { string json = ReflectionUtilities.GetResourceAsString("UnitTests.Resources.WheatModel.apsimx"); IModel topLevel = FileFormat.ReadFromString <Simulations>(json, out List <Exception> errors); string serialized = FileFormat.WriteToString(topLevel); JObject root = JObject.Parse(serialized); // This file contains 2 wheat models - one under replacements, and one under a folder. JObject fullWheat = JsonUtilities.ChildWithName(JsonUtilities.ChildWithName(root, "Replacements"), "Wheat"); JObject wheat = JsonUtilities.ChildWithName(JsonUtilities.ChildWithName(root, "Folder"), "Wheat"); // The wheat model under replacements should have its full // model structure (properties + children) serialized. Assert.NotNull(fullWheat); Assert.AreNotEqual(0, JsonUtilities.Children(fullWheat).Count); // The wheat model under the folder should *not* have its // full model structure (properties + children) serialized. Assert.NotNull(wheat); Assert.AreEqual(0, JsonUtilities.Children(wheat).Count); }