public void ReplaceManagerTextTests()
        {
            string  json     = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.ManagerConverterTestsReplaceManagerText.json");
            JObject rootNode = JObject.Parse(json);

            var manager = new ManagerConverter(rootNode);

            string newText = "new text";

            manager.Replace("original text", newText);

            // Ensure the code was modified correctly.
            Assert.AreEqual(newText + Environment.NewLine, manager.ToString());

            // Ensure that passing in a null search string causes no changes.
            manager.Replace(null, "test");
            Assert.AreEqual(newText + Environment.NewLine, manager.ToString());

            // Attempt to replace code of a node which doesn't have a code
            // property. Ensure that no code property is created (and that
            // no exception is thrown).
            var childWithNoCode = new ManagerConverter(JsonUtilities.Children(rootNode).First());

            childWithNoCode.Replace("test1", "test2");
            Assert.Null(childWithNoCode.ToString());
        }
        public void ReplaceManagerCodeRegexTests()
        {
            string  json     = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.ManagerConverterTestsReplaceManagerTextRegex.json");
            JObject rootNode = JObject.Parse(json);

            var manager = new ManagerConverter(rootNode);

            // The manager's code is "original text".
            // This regular expression will effectively remove the first space.
            // There are simpler ways to achieve this but this method tests
            // backreferencing.
            string newText = "originaltext\r\n";

            manager.ReplaceRegex(@"([^\s]*)\s", @"$1");
            Assert.AreEqual(manager.ToString(), newText);

            // Ensure that passing in a null search string causes no changes.
            manager.ReplaceRegex(null, "test");
            Assert.AreEqual(manager.ToString(), newText);

            // Attempt to replace code of a node which doesn't have a code
            // property. Ensure that no code property is created (and that
            // no exception is thrown).
            var childWithNoCode = new ManagerConverter(JsonUtilities.Children(rootNode).First());

            childWithNoCode.ReplaceRegex("test1", "test2");
            Assert.Null(childWithNoCode.ToString());
        }
示例#3
0
        /// <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));
        }
示例#4
0
        public void TestAddingInitialWater()
        {
            string json = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.Soil.json");
            ConverterReturnType   result        = Converter.DoConvert(json, Converter.LatestVersion);
            IEnumerable <JObject> initialWaters = JsonUtilities.Children(result.Root).Where(c => string.Equals(c["Name"].ToString(), "Initial water", StringComparison.InvariantCultureIgnoreCase));

            Assert.AreEqual(1, initialWaters.Count());
            Assert.AreEqual("Models.Soils.Sample, Models", initialWaters.First()["$type"].ToString());
        }
        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);
        }
示例#6
0
        public void TypeTests()
        {
            string         json     = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsEnsureTypeWorks.json");
            JObject        rootNode = JObject.Parse(json);
            List <JObject> children = JsonUtilities.Children(rootNode);

            // Ensure typename with namespace is correct.
            Assert.AreEqual("Models.Core.Simulations", JsonUtilities.Type(rootNode, true));

            // Ensure typename without namespace is correct.
            Assert.AreEqual("Simulations", JsonUtilities.Type(rootNode));

            // Ensure that typename of null is null.
            Assert.Null(JsonUtilities.Type(null));

            // Ensure that typename of node with no $type property is null.
            Assert.Null(JsonUtilities.Type(children[0]));
        }
示例#7
0
        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"));
        }
示例#8
0
        public void ChildrenTests()
        {
            string         json      = ReflectionUtilities.GetResourceAsString("UnitTests.Core.ApsimFile.JsonUtilitiesTestsEnsureChildrenWorks.json");
            JObject        rootNode  = JObject.Parse(json);
            List <JObject> children  = JsonUtilities.Children(rootNode);
            List <JObject> emptyList = new List <JObject>();

            // Ensure children is not null.
            Assert.NotNull(children);

            // Ensure number of children is correct.
            Assert.AreEqual(4, children.Count);

            // Ensure children of null is an empty list.
            Assert.AreEqual(emptyList, JsonUtilities.Children(null));

            // Ensure children of a node with empty children property is empty list.
            Assert.AreEqual(emptyList, JsonUtilities.Children(children[0] as JObject));

            // Ensure children of a node with no children property is an empty list.
            Assert.AreEqual(emptyList, JsonUtilities.Children(children[1] as JObject));
        }
示例#9
0
        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);
        }
示例#10
0
        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));
        }