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());
        }
        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 ManagerConverter_SetUsingStatements()
        {
            string script =
                "// Comment 1" + Environment.NewLine +
                "// Comment 2" + Environment.NewLine +
                Environment.NewLine +
                "using System" + Environment.NewLine +
                Environment.NewLine +
                "using Models.Soils;" + Environment.NewLine +
                "using APSIM.Shared.Utilities;" + Environment.NewLine +
                Environment.NewLine +
                "namespace Models" + Environment.NewLine +
                "{" + Environment.NewLine +
                "}" + Environment.NewLine;

            ManagerConverter converter = new ManagerConverter();

            converter.Read(script);
            converter.SetUsingStatements(new string[] { "System" });
            Assert.AreEqual(converter.ToString(),
                            "// Comment 1" + Environment.NewLine +
                            "// Comment 2" + Environment.NewLine +
                            Environment.NewLine +
                            "using System;" + Environment.NewLine +
                            Environment.NewLine +
                            "namespace Models" + Environment.NewLine +
                            "{" + Environment.NewLine +
                            "}" + Environment.NewLine);
        }
        public void AddManagerDeclarationToExistingDeclarationSection()
        {
            JObject rootNode = new JObject();

            rootNode["Code"] =
                "using System;" + Environment.NewLine +
                "namespace Models" + Environment.NewLine +
                "{" + Environment.NewLine +
                "    [Serializable]" + Environment.NewLine +
                "    public class Script : Model" + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        [Link]" + Environment.NewLine +
                "        A B;" + Environment.NewLine +
                "    }" + Environment.NewLine +
                "}" + Environment.NewLine;
            var manager = new ManagerConverter(rootNode);

            manager.AddDeclaration("NutrientPool", "Humic", new string[] { "[Link]" });

            // Ensure the link has been added below the using statement.
            Assert.AreEqual(manager.ToString(),
                            "using System;" + Environment.NewLine +
                            "namespace Models" + Environment.NewLine +
                            "{" + Environment.NewLine +
                            "    [Serializable]" + Environment.NewLine +
                            "    public class Script : Model" + Environment.NewLine +
                            "    {" + Environment.NewLine +
                            "        [Link]" + Environment.NewLine +
                            "        private A B;" + Environment.NewLine +
                            "        [Link]" + Environment.NewLine +
                            "        private NutrientPool Humic;" + Environment.NewLine +
                            "    }" + Environment.NewLine +
                            "}" + Environment.NewLine);
        }
        public void AddManagerDeclarationHandleProperties()
        {
            JObject rootNode = new JObject();

            rootNode["Code"] =
                "using System;" + Environment.NewLine +
                "namespace Models" + Environment.NewLine +
                "{" + Environment.NewLine +
                "    [Serializable]" + Environment.NewLine +
                "    public class Script : Model" + Environment.NewLine +
                "    {" + Environment.NewLine +
                "        [Link] private A B = null;" + Environment.NewLine +
                "        [Link] " + Environment.NewLine +
                "        public C D;" + Environment.NewLine +
                "        [Link] E F;" + Environment.NewLine +
                "        [Description(\"Turn ferliser applications on? \")]" + Environment.NewLine +
                "        public yesnoType AllowFertiliser { get; set; }" + Environment.NewLine +
                "    }" + Environment.NewLine +
                "}" + Environment.NewLine;
            var manager = new ManagerConverter(rootNode);

            manager.AddDeclaration("NutrientPool", "Humic", new string[] { "[Link]" });

            // Ensure the link has been added below the using statement.
            Assert.AreEqual(manager.ToString(),
                            "using System;" + Environment.NewLine +
                            "namespace Models" + Environment.NewLine +
                            "{" + Environment.NewLine +
                            "    [Serializable]" + Environment.NewLine +
                            "    public class Script : Model" + Environment.NewLine +
                            "    {" + Environment.NewLine +
                            "        [Link] private A B;" + Environment.NewLine +
                            "        [Link]" + Environment.NewLine +
                            "        public C D;" + Environment.NewLine +
                            "        [Link] private E F;" + Environment.NewLine +
                            "        [Link]" + Environment.NewLine +
                            "        private NutrientPool Humic;" + Environment.NewLine +
                            "        [Description(\"Turn ferliser applications on? \")]" + Environment.NewLine +
                            "        public yesnoType AllowFertiliser { get; set; }" + Environment.NewLine +
                            "    }" + Environment.NewLine +
                            "}" + Environment.NewLine);
        }