Skip to content

Commit

Permalink
Extract PrettyPrint
Browse files Browse the repository at this point in the history
  • Loading branch information
blowfishpro committed Sep 20, 2017
1 parent 20a8afb commit 35d89f8
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 55 deletions.
42 changes: 42 additions & 0 deletions ModuleManager/Extensions/ConfigNodeExtensions.cs
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace ModuleManager.Extensions
{
Expand Down Expand Up @@ -26,5 +27,46 @@ public static ConfigNode DeepCopy(this ConfigNode from)
}
return to;
}

public static void PrettyPrint(this ConfigNode node, ref StringBuilder sb, string indent)
{
if (sb == null) throw new ArgumentNullException(nameof(sb));
if (indent == null) indent = string.Empty;
if (node == null)
{
sb.Append(indent + "<null node>");
return;
}
sb.AppendFormat("{0}{1}\n{2}{{\n", indent, node.name ?? "<null>", indent);
string newindent = indent + " ";
if (node.values == null)
{
sb.AppendFormat("{0}<null value list>\n", newindent);
}
else
{
foreach (ConfigNode.Value value in node.values)
{
if (value == null)
sb.AppendFormat("{0}<null value>\n", newindent);
else
sb.AppendFormat("{0}{1} = {2}\n", newindent, value.name ?? "<null>", value.value ?? "<null>");
}
}

if (node.nodes == null)
{
sb.AppendFormat("{0}<null node list>\n", newindent);
}
else
{
foreach (ConfigNode subnode in node.nodes)
{
subnode.PrettyPrint(ref sb, newindent);
}
}

sb.AppendFormat("{0}}}\n", indent);
}
}
}
56 changes: 1 addition & 55 deletions ModuleManager/MMPatchLoader.cs
Expand Up @@ -2247,7 +2247,7 @@ private string PrettyConfig(UrlDir.UrlConfig config)
{
if (config.config != null)
{
PrettyConfig(config.config, ref sb, " ");
config.config.PrettyPrint(ref sb, " ");
}
else
{
Expand All @@ -2262,60 +2262,6 @@ private string PrettyConfig(UrlDir.UrlConfig config)
return sb.ToString();
}

private void PrettyConfig(ConfigNode node, ref StringBuilder sb, string indent)
{
sb.AppendFormat("{0}{1}\n{2}{{\n", indent, node.name ?? "NULL", indent);
string newindent = indent + " ";
if (node.values != null)
{
foreach (ConfigNode.Value value in node.values)
{
if (value != null)
{
try
{
sb.AppendFormat("{0}{1} = {2}\n", newindent, value.name ?? "null", value.value ?? "null");
}
catch (Exception)
{
logger.Error("value.name.Length=" + value.name.Length);
logger.Error("value.name.IsNullOrEmpty=" + string.IsNullOrEmpty(value.name));
logger.Error("n " + value.name);
logger.Error("v " + value.value);
throw;
}
}
else
{
sb.AppendFormat("{0} Null value\n", newindent);
}
}
}
else
{
sb.AppendFormat("{0} Null values\n", newindent);
}
if (node.nodes != null)
{
foreach (ConfigNode subnode in node.nodes)
{
if (subnode != null)
{
PrettyConfig(subnode, ref sb, newindent);
}
else
{
sb.AppendFormat("{0} Null Subnode\n", newindent);
}
}
}
else
{
sb.AppendFormat("{0} Null nodes\n", newindent);
}
sb.AppendFormat("{0}}}\n", indent);
}

//FindConfigNodeIn finds and returns a ConfigNode in src of type nodeType.
//If nodeName is not null, it will only find a node of type nodeType with the value name=nodeName.
//If nodeTag is not null, it will only find a node of type nodeType with the value name=nodeName and tag=nodeTag.
Expand Down
132 changes: 132 additions & 0 deletions ModuleManagerTests/Extensions/ConfigNodeExtensionsTest.cs
@@ -1,4 +1,5 @@
using System;
using System.Text;
using Xunit;
using TestUtils;
using ModuleManager.Extensions;
Expand Down Expand Up @@ -142,5 +143,136 @@ public void TestDeepCopy()
Assert.Equal(0, innerNode2.nodes[0].values.Count);
Assert.Equal(0, innerNode2.nodes[0].nodes.Count);
}

[Fact]
public void TestPrettyPrint()
{
ConfigNode node = new TestConfigNode("SOME_NODE")
{
{ "abc", "def" },
{ "ghi", "jkl" },
new TestConfigNode("INNER_NODE_1")
{
{ "mno", "pqr" },
new TestConfigNode("INNER_INNER_NODE_1"),
},
new TestConfigNode("INNER_NODE_2")
{
{ "stu", "vwx" },
new TestConfigNode("INNER_INNER_NODE_2"),
},
};

string expected = @"
XXSOME_NODE
XX{
XX abc = def
XX ghi = jkl
XX INNER_NODE_1
XX {
XX mno = pqr
XX INNER_INNER_NODE_1
XX {
XX }
XX }
XX INNER_NODE_2
XX {
XX stu = vwx
XX INNER_INNER_NODE_2
XX {
XX }
XX }
XX}
".TrimStart().Replace("\r", null);

StringBuilder sb = new StringBuilder();
node.PrettyPrint(ref sb, "XX");

Assert.Equal(expected, sb.ToString());
}

[Fact]
public void TestPrettyPrint__NullNode()
{
ConfigNode node = null;
StringBuilder sb = new StringBuilder();
node.PrettyPrint(ref sb, "XX");
Assert.Equal("XX<null node>", sb.ToString());
}

[Fact]
public void TestPrettyPrint__NullStringBuilder()
{
ConfigNode node = new ConfigNode("NODE");
StringBuilder sb = null;
Assert.Throws<ArgumentNullException>(delegate
{
node.PrettyPrint(ref sb, "XX");
});
}

[Fact]
public void TestPrettyPrint__NullIndent()
{
ConfigNode node = new TestConfigNode()
{
{ "abc", "def" },
{ "ghi", "jkl" },
new TestConfigNode("INNER_NODE")
{
{ "mno", "pqr" },
},
};

node.name = null;

string expected = @"
<null>
{
abc = def
ghi = jkl
INNER_NODE
{
mno = pqr
}
}
".TrimStart().Replace("\r", null);

StringBuilder sb = new StringBuilder();
node.PrettyPrint(ref sb, null);
Assert.Equal(expected, sb.ToString());
}

[Fact]
public void TestPrettyPrint__NullName()
{
ConfigNode node = new TestConfigNode()
{
{ "abc", "def" },
{ "ghi", "jkl" },
new TestConfigNode("INNER_NODE")
{
{ "mno", "pqr" },
},
};

node.name = null;

string expected = @"
XX<null>
XX{
XX abc = def
XX ghi = jkl
XX INNER_NODE
XX {
XX mno = pqr
XX }
XX}
".TrimStart().Replace("\r", null);

StringBuilder sb = new StringBuilder();
node.PrettyPrint(ref sb, "XX");
Assert.Equal(expected, sb.ToString());
}
}
}

0 comments on commit 35d89f8

Please sign in to comment.