Skip to content

Commit

Permalink
Extract PrettyConfig (for UrlConfig)
Browse files Browse the repository at this point in the history
  • Loading branch information
blowfishpro committed Sep 24, 2017
1 parent 4fdfb89 commit 70eca81
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ModuleManager/Extensions/ConfigNodeExtensions.cs
Expand Up @@ -34,7 +34,7 @@ public static void PrettyPrint(this ConfigNode node, ref StringBuilder sb, strin
if (indent == null) indent = string.Empty;
if (node == null)
{
sb.Append(indent + "<null node>");
sb.Append(indent + "<null node>\n");
return;
}
sb.AppendFormat("{0}{1}\n{2}{{\n", indent, node.name ?? "<null>", indent);
Expand Down
19 changes: 19 additions & 0 deletions ModuleManager/Extensions/UrlConfigExtensions.cs
@@ -1,4 +1,5 @@
using System;
using System.Text;

namespace ModuleManager.Extensions
{
Expand Down Expand Up @@ -42,5 +43,23 @@ public static string SafeUrl(this UrlDir.UrlConfig url)
else
return parentUrl + "/" + nodeName;
}

public static string PrettyPrint(this UrlDir.UrlConfig config)
{
if (config == null) return "<null UrlConfig>";

StringBuilder sb = new StringBuilder();

if (config.type == null) sb.Append("<null type>");
else sb.Append(config.type);

if (config.name == null) sb.Append("[<null name>]");
else if (config.name != config.type) sb.AppendFormat("[{0}]", config.name);

sb.Append('\n');
config.config.PrettyPrint(ref sb, " ");

return sb.ToString();
}
}
}
20 changes: 2 additions & 18 deletions ModuleManager/MMPatchLoader.cs
Expand Up @@ -816,7 +816,7 @@ private void CheckNeeds(IEnumerable<string> mods, IPatchProgress progress)

try
{
logger.Error("Node is : " + PrettyConfig(currentMod));
logger.Error("Node is : " + currentMod.PrettyPrint());
}
catch(Exception ex2)
{
Expand Down Expand Up @@ -1061,7 +1061,7 @@ public IEnumerator ApplyPatch(string Stage, IEnumerable<UrlDir.UrlConfig> patche

try
{
logger.Error("Processed node was\n" + PrettyConfig(mod));
logger.Error("Processed node was\n" + mod.PrettyPrint());
}
catch (Exception ex2)
{
Expand Down Expand Up @@ -2245,22 +2245,6 @@ private static void InsertValue(ConfigNode newNode, int index, string name, stri
newNode.AddValue(name, value);
}

private static string PrettyConfig(UrlDir.UrlConfig config)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("{0}[{1}]\n", config.type ?? "NULL", config.name ?? "NULL");
if (config.config != null)
{
config.config.PrettyPrint(ref sb, " ");
}
else
{
sb.Append("NULL\n");
}
sb.Append("\n");
return sb.ToString();
}

//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
2 changes: 1 addition & 1 deletion ModuleManagerTests/Extensions/ConfigNodeExtensionsTest.cs
Expand Up @@ -197,7 +197,7 @@ public void TestPrettyPrint__NullNode()
ConfigNode node = null;
StringBuilder sb = new StringBuilder();
node.PrettyPrint(ref sb, "XX");
Assert.Equal("XX<null node>", sb.ToString());
Assert.Equal("XX<null node>\n", sb.ToString());
}

[Fact]
Expand Down
146 changes: 146 additions & 0 deletions ModuleManagerTests/Extensions/UrlConfigExtensionsTest.cs
Expand Up @@ -72,5 +72,151 @@ public void TestSafeUrl__BlankName()
UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def", node);
Assert.Equal("abc/def/<blank>", url.SafeUrl());
}

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

UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def.cfg", node);

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

Assert.Equal(expected, url.PrettyPrint());
}

[Fact]
public void TestPrettyPrint__NameValue()
{
ConfigNode node = new TestConfigNode("SOME_NODE")
{
{ "name", "Inigo Montoya" },
{ "abc", "def" },
{ "ghi", "jkl" },
new TestConfigNode("INNER_NODE_1")
{
{ "mno", "pqr" },
},
};

UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def.cfg", node);

string expected = @"
SOME_NODE[Inigo Montoya]
SOME_NODE
{
name = Inigo Montoya
abc = def
ghi = jkl
INNER_NODE_1
{
mno = pqr
}
}
".TrimStart().Replace("\r", null);

Assert.Equal(expected, url.PrettyPrint());
}

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

node.name = null;

UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def.cfg", node);

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

Assert.Equal(expected, url.PrettyPrint());
}

[Fact]
public void TestPrettyPrint__NullNameValue()
{
ConfigNode node = new TestConfigNode("SOME_NODE")
{
{ "name", "temp" },
{ "abc", "def" },
{ "ghi", "jkl" },
new TestConfigNode("INNER_NODE_1")
{
{ "mno", "pqr" },
},
};

node.values[0].value = null;

UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def.cfg", node);

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

Assert.Equal(expected, url.PrettyPrint());
}

[Fact]
public void TestPrettyPrint__NullNode()
{
UrlDir.UrlConfig url = UrlBuilder.CreateConfig("abc/def.cfg", new ConfigNode("SOME_NODE"));
url.config = null;

string expected = @"
SOME_NODE
<null node>
".TrimStart().Replace("\r", null);

Assert.Equal(expected, url.PrettyPrint());
}
}
}

0 comments on commit 70eca81

Please sign in to comment.