Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
"this" is the node you're copying to so that the extension method is only modifying "its" node
- Loading branch information
1 parent
a9a990d
commit b43f79b
Showing
5 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters