Skip to content

Commit

Permalink
Extract ShallowCopy
Browse files Browse the repository at this point in the history
"this" is the node you're copying to so that the extension method is
only modifying "its" node
  • Loading branch information
blowfishpro committed Sep 14, 2017
1 parent a9a990d commit b43f79b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 11 deletions.
19 changes: 19 additions & 0 deletions ModuleManager/Extensions/ConfigNodeExtensions.cs
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModuleManager.Extensions
{
public static class ConfigNodeExtensions
{
public static void ShallowCopyFrom(this ConfigNode toNode, ConfigNode fromeNode)
{
toNode.ClearData();
foreach (ConfigNode.Value value in fromeNode.values)
toNode.values.Add(value);
foreach (ConfigNode node in fromeNode.nodes)
toNode.nodes.Add(node);
}
}
}
13 changes: 2 additions & 11 deletions ModuleManager/MMPatchLoader.cs
Expand Up @@ -802,7 +802,7 @@ private void CheckNeeds()
}

ConfigNode copy = new ConfigNode(type);
ShallowCopy(currentMod.config, copy);
copy.ShallowCopyFrom(currentMod.config);
currentMod = new UrlDir.UrlConfig(currentMod.parent, copy);
mod.parent.configs.Add(currentMod);
}
Expand Down Expand Up @@ -889,7 +889,7 @@ private void CheckNeeds(NodeStack stack, PatchContext context)
}

if (needsCopy)
ShallowCopy(copy, original);
original.ShallowCopyFrom(copy);
}

/// <summary>
Expand Down Expand Up @@ -2304,15 +2304,6 @@ private static void InsertValue(ConfigNode newNode, int index, string name, stri
newNode.AddValue(name, value);
}

private static void ShallowCopy(ConfigNode from, ConfigNode to)
{
to.ClearData();
foreach (ConfigNode.Value value in from.values)
to.values.Add(value);
foreach (ConfigNode node in from.nodes)
to.nodes.Add(node);
}

private string PrettyConfig(UrlDir.UrlConfig config)
{
StringBuilder sb = new StringBuilder();
Expand Down
1 change: 1 addition & 0 deletions ModuleManager/ModuleManager.csproj
Expand Up @@ -38,6 +38,7 @@
<Compile Include="Collections\ImmutableStack.cs" />
<Compile Include="Command.cs" />
<Compile Include="CommandParser.cs" />
<Compile Include="Extensions\ConfigNodeExtensions.cs" />
<Compile Include="Extensions\NodeStackExtensions.cs" />
<Compile Include="IPatchProgress.cs" />
<Compile Include="Logging\IBasicLogger.cs" />
Expand Down
87 changes: 87 additions & 0 deletions ModuleManagerTests/Extensions/ConfigNodeExtensionsTest.cs
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;
using TestUtils;
using ModuleManager.Extensions;

namespace ModuleManagerTests.Extensions
{
public class ConfigNodeExtensionsTest
{
public void TestShallowCopyFrom()
{
ConfigNode fromNode = 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"),
},
};

ConfigNode.Value value1 = fromNode.values[0];
ConfigNode.Value value2 = fromNode.values[1];

ConfigNode innerNode1 = fromNode.nodes[0];
ConfigNode innerNode2 = fromNode.nodes[1];

ConfigNode toNode = new TestConfigNode("SOME_OTHER_NODE")
{
{ "value", "will be removed" },
new TestConfigNode("NODE_WILL_BE_REMOVED"),
};

toNode.ShallowCopyFrom(fromNode);

Assert.Equal("SOME_NODE", fromNode.name);
Assert.Equal("SOME_OTHER_NODE", toNode.name);

Assert.Equal(2, fromNode.values.Count);
Assert.Equal(2, toNode.values.Count);

Assert.Same(value1, fromNode.values[0]);
Assert.Same(value1, toNode.values[0]);
Assert.Equal("abc", value1.name);
Assert.Equal("def", value1.value);

Assert.Same(value2, fromNode.values[1]);
Assert.Same(value2, toNode.values[1]);
Assert.Equal("ghi", value2.name);
Assert.Equal("jkl", value2.value);

Assert.Equal(2, fromNode.nodes.Count);
Assert.Equal(2, toNode.nodes.Count);

Assert.Same(innerNode1, fromNode.nodes[0]);
Assert.Same(innerNode1, toNode.nodes[0]);
Assert.Equal("INNER_NODE_1", innerNode1.name);
Assert.Equal(1, innerNode1.values.Count);
Assert.Equal("mno", innerNode1.values[0].name);
Assert.Equal("pqr", innerNode1.values[0].value);
Assert.Equal(1, innerNode1.nodes.Count);
Assert.Equal("INNER_INNER_NODE_1", innerNode1.nodes[0].name);
Assert.Equal(0, innerNode1.nodes[0].values.Count);
Assert.Equal(0, innerNode1.nodes[0].nodes.Count);

Assert.Same(innerNode2, fromNode.nodes[1]);
Assert.Same(innerNode2, toNode.nodes[1]);
Assert.Equal("INNER_NODE_2", innerNode2.name);
Assert.Equal(1, innerNode2.values.Count);
Assert.Equal("stu", innerNode2.values[0].name);
Assert.Equal("vwx", innerNode2.values[0].value);
Assert.Equal(1, innerNode2.nodes.Count);
Assert.Equal("INNER_INNER_NODE_2", innerNode2.nodes[0].name);
Assert.Equal(0, innerNode2.nodes[0].values.Count);
Assert.Equal(0, innerNode2.nodes[0].nodes.Count);
}
}
}
1 change: 1 addition & 0 deletions ModuleManagerTests/ModuleManagerTests.csproj
Expand Up @@ -52,6 +52,7 @@
<Compile Include="Collections\ImmutableStackTest.cs" />
<Compile Include="CommandParserTest.cs" />
<Compile Include="DummyTest.cs" />
<Compile Include="Extensions\ConfigNodeExtensionsTest.cs" />
<Compile Include="Extensions\NodeStackExtensionsTest.cs" />
<Compile Include="Logging\ModLoggerTest.cs" />
<Compile Include="PatchProgressTest.cs" />
Expand Down

0 comments on commit b43f79b

Please sign in to comment.