Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hackily creates UrlDir, UrlFile, UrlConfig for testing purposes
- Loading branch information
1 parent
a5a095f
commit 564b226
Showing
4 changed files
with
377 additions
and
0 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
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,116 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace TestUtils | ||
{ | ||
public static class UrlBuilder | ||
{ | ||
private static FieldInfo UrlDir__field__name; | ||
private static FieldInfo UrlDir__field__root; | ||
private static FieldInfo UrlDir__field__parent; | ||
|
||
private static FieldInfo UrlFile__field__name; | ||
private static FieldInfo UrlFile__field__fileType; | ||
private static FieldInfo UrlFile__field__fileExtension; | ||
|
||
static UrlBuilder() | ||
{ | ||
FieldInfo[] UrlDirFields = typeof(UrlDir).GetFields(BindingFlags.Instance | BindingFlags.NonPublic); | ||
FieldInfo[] UrlDirFields__string = UrlDirFields.Where(field => field.FieldType == typeof(string)).ToArray(); | ||
FieldInfo[] UrlDirFields__UrlDir = UrlDirFields.Where(field => field.FieldType == typeof(UrlDir)).ToArray(); | ||
|
||
UrlDir__field__name = UrlDirFields__string[0]; | ||
UrlDir__field__root = UrlDirFields__UrlDir[0]; | ||
UrlDir__field__parent = UrlDirFields__UrlDir[1]; | ||
|
||
FieldInfo[] UrlFileFields = typeof(UrlDir.UrlFile).GetFields(BindingFlags.Instance | BindingFlags.NonPublic); | ||
FieldInfo[] UrlFileFields__string = UrlFileFields.Where(field => field.FieldType == typeof(string)).ToArray(); | ||
FieldInfo[] UrlFileFields__FileType = UrlFileFields.Where(field => field.FieldType == typeof(UrlDir.FileType)).ToArray(); | ||
|
||
UrlFile__field__name = UrlFileFields__string[0]; | ||
UrlFile__field__fileExtension = UrlFileFields__string[2]; | ||
UrlFile__field__fileType = UrlFileFields__FileType[0]; | ||
} | ||
|
||
public static UrlDir CreateRoot() | ||
{ | ||
return new UrlDir(new UrlDir.ConfigDirectory[0], new UrlDir.ConfigFileType[0]); | ||
} | ||
|
||
public static UrlDir CreateDir(string url, UrlDir parent = null) | ||
{ | ||
if (parent == null) parent = CreateRoot(); | ||
|
||
UrlDir current = parent; | ||
|
||
foreach(string name in url.Split('/')) | ||
{ | ||
UrlDir dir = CreateRoot(); | ||
UrlDir__field__name.SetValue(dir, name); | ||
UrlDir__field__root.SetValue(dir, current.root); | ||
UrlDir__field__parent.SetValue(dir, current); | ||
|
||
current.children.Add(dir); | ||
current = dir; | ||
} | ||
|
||
return current; | ||
} | ||
|
||
public static UrlDir.UrlFile CreateFile(string path, UrlDir parent = null) | ||
{ | ||
int sepIndex = path.LastIndexOf('/'); | ||
string name = path; | ||
|
||
if (sepIndex != -1) | ||
{ | ||
parent = CreateDir(path.Substring(0, sepIndex), parent); | ||
name = path.Substring(sepIndex + 1); | ||
} | ||
else if (parent == null) | ||
{ | ||
parent = CreateRoot(); | ||
} | ||
|
||
bool cfg = false; | ||
string newName = name; | ||
|
||
if (Path.GetExtension(name) == ".cfg") | ||
{ | ||
cfg = true; | ||
newName = name + ".not_cfg"; | ||
} | ||
|
||
UrlDir.UrlFile file = new UrlDir.UrlFile(parent, new FileInfo(newName)); | ||
|
||
if (cfg) | ||
{ | ||
UrlFile__field__name.SetValue(file, Path.GetFileNameWithoutExtension(name)); | ||
UrlFile__field__fileExtension.SetValue(file, "cfg"); | ||
UrlFile__field__fileType.SetValue(file, UrlDir.FileType.Config); | ||
} | ||
|
||
parent.files.Add(file); | ||
|
||
return file; | ||
} | ||
|
||
public static UrlDir.UrlConfig CreateConfig(ConfigNode node, UrlDir.UrlFile parent) | ||
{ | ||
UrlDir.UrlConfig config = new UrlDir.UrlConfig(parent, node); | ||
parent.configs.Add(config); | ||
return config; | ||
} | ||
|
||
public static UrlDir.UrlConfig CreateConfig(string url, ConfigNode node, UrlDir parent = null) | ||
{ | ||
if (Path.GetExtension(url) != ".cfg") url += ".cfg"; | ||
|
||
UrlDir.UrlFile file = CreateFile(url, parent); | ||
|
||
return CreateConfig(node, file); | ||
} | ||
} | ||
} |
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,259 @@ | ||
using System; | ||
using Xunit; | ||
using TestUtils; | ||
|
||
namespace TestUtilsTests | ||
{ | ||
public class UrlBuilderTest | ||
{ | ||
[Fact] | ||
public void TestCreateRoot() | ||
{ | ||
UrlDir root = UrlBuilder.CreateRoot(); | ||
|
||
Assert.Equal("root", root.name); | ||
Assert.Null(root.parent); | ||
Assert.Same(root, root.root); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateDir() | ||
{ | ||
UrlDir dir = UrlBuilder.CreateDir("abc"); | ||
|
||
Assert.Equal("abc", dir.name); | ||
|
||
UrlDir root = dir.parent; | ||
Assert.NotNull(root); | ||
Assert.Equal("root", root.name); | ||
Assert.Null(root.parent); | ||
Assert.Contains(dir, root.children); | ||
|
||
Assert.Same(root, dir.root); | ||
Assert.Same(root, root.root); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateDir__Parent() | ||
{ | ||
UrlDir root = UrlBuilder.CreateRoot(); | ||
|
||
UrlDir child1 = UrlBuilder.CreateDir("child1", root); | ||
|
||
Assert.Equal("child1", child1.name); | ||
Assert.Same(root, child1.parent); | ||
Assert.Same(root, child1.root); | ||
|
||
Assert.Equal("child1", child1.url); | ||
|
||
Assert.Contains(child1, root.children); | ||
|
||
UrlDir child2 = UrlBuilder.CreateDir("child2", child1); | ||
|
||
Assert.Equal("child2", child2.name); | ||
Assert.Same(child1, child2.parent); | ||
Assert.Same(root, child2.root); | ||
|
||
Assert.Equal("child1/child2", child2.url); | ||
|
||
Assert.Contains(child2, child1.children); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateDir__Url() | ||
{ | ||
UrlDir dir = UrlBuilder.CreateDir("abc/def"); | ||
|
||
Assert.Equal("def", dir.name); | ||
|
||
UrlDir parent = dir.parent; | ||
|
||
Assert.NotNull(parent); | ||
Assert.Equal("abc", parent.name); | ||
Assert.Contains(dir, parent.children); | ||
|
||
UrlDir root = parent.parent; | ||
|
||
Assert.NotNull(root); | ||
Assert.Equal("root", root.name); | ||
Assert.Contains(parent, root.children); | ||
Assert.Null(root.parent); | ||
|
||
Assert.Same(root, root.root); | ||
Assert.Same(root, parent.root); | ||
Assert.Same(root, dir.root); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateDir__Url__Parent() | ||
{ | ||
UrlDir root = UrlBuilder.CreateRoot(); | ||
UrlDir parent1 = UrlBuilder.CreateDir("abc", root); | ||
|
||
UrlDir dir = UrlBuilder.CreateDir("def/ghi", parent1); | ||
|
||
Assert.Equal("ghi", dir.name); | ||
|
||
UrlDir parent2 = dir.parent; | ||
|
||
Assert.NotNull(parent2); | ||
Assert.Equal("def", parent2.name); | ||
Assert.Contains(dir, parent2.children); | ||
|
||
Assert.Same(parent1, parent2.parent); | ||
Assert.Contains(parent2, parent1.children); | ||
|
||
Assert.Same(root, dir.root); | ||
Assert.Same(root, parent2.root); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateFile() | ||
{ | ||
UrlDir.UrlFile file = UrlBuilder.CreateFile("someFile.txt"); | ||
|
||
Assert.Equal("someFile", file.name); | ||
Assert.Equal("txt", file.fileExtension); | ||
Assert.Equal(UrlDir.FileType.Unknown, file.fileType); | ||
|
||
UrlDir root = file.parent; | ||
Assert.NotNull(root); | ||
Assert.Equal("root", root.name); | ||
Assert.Null(root.parent); | ||
Assert.Contains(file, root.files); | ||
Assert.Same(root, file.root); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateFile__Parent() | ||
{ | ||
UrlDir root = UrlBuilder.CreateRoot(); | ||
UrlDir dir = UrlBuilder.CreateDir("someDir", root); | ||
UrlDir.UrlFile file = UrlBuilder.CreateFile("someFile.txt", dir); | ||
|
||
Assert.Equal("someFile", file.name); | ||
Assert.Equal("txt", file.fileExtension); | ||
Assert.Equal(UrlDir.FileType.Unknown, file.fileType); | ||
Assert.Same(dir, file.parent); | ||
Assert.Same(root, file.root); | ||
|
||
Assert.Equal("someDir/someFile", file.url); | ||
Assert.Contains(file, dir.files); | ||
Assert.Contains(file, root.AllFiles); | ||
} | ||
|
||
// KSP tries to load .cfg files so need to have special handling | ||
[Fact] | ||
public void TestCreateFile__cfg() | ||
{ | ||
UrlDir root = UrlBuilder.CreateRoot(); | ||
UrlDir dir = UrlBuilder.CreateDir("someDir", root); | ||
UrlDir.UrlFile file = UrlBuilder.CreateFile("someFile.cfg", dir); | ||
|
||
Assert.Equal("someFile", file.name); | ||
Assert.Equal("cfg", file.fileExtension); | ||
Assert.Equal(UrlDir.FileType.Config, file.fileType); | ||
Assert.Same(dir, file.parent); | ||
Assert.Same(root, file.root); | ||
|
||
Assert.Equal("someDir/someFile", file.url); | ||
Assert.Contains(file, dir.files); | ||
Assert.Contains(file, root.AllConfigFiles); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateFile__Url() | ||
{ | ||
UrlDir.UrlFile file = UrlBuilder.CreateFile("abc/def/ghi.txt"); | ||
|
||
Assert.Equal("ghi", file.name); | ||
Assert.Equal("txt", file.fileExtension); | ||
Assert.Equal(UrlDir.FileType.Unknown, file.fileType); | ||
Assert.Equal("abc/def/ghi", file.url); | ||
|
||
UrlDir parent1 = file.parent; | ||
Assert.NotNull(parent1); | ||
Assert.Equal("def", parent1.name); | ||
Assert.Contains(file, parent1.files); | ||
|
||
UrlDir parent2 = parent1.parent; | ||
Assert.NotNull(parent2); | ||
Assert.Equal("abc", parent2.name); | ||
Assert.Contains(parent1, parent2.children); | ||
|
||
UrlDir root = parent2.parent; | ||
Assert.NotNull(root); | ||
Assert.Equal("root", root.name); | ||
Assert.Contains(parent2, root.children); | ||
Assert.Null(root.parent); | ||
|
||
Assert.Same(root, file.root); | ||
Assert.Same(root, parent1.root); | ||
Assert.Same(root, parent2.root); | ||
Assert.Same(root, root.root); | ||
|
||
Assert.Contains(file, root.AllFiles); | ||
Assert.Contains(file, parent2.AllFiles); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateConfig() | ||
{ | ||
ConfigNode node = new TestConfigNode("SOME_NODE") | ||
{ | ||
{ "name", "blah" }, | ||
{ "foo", "bar" }, | ||
}; | ||
UrlDir.UrlFile file = UrlBuilder.CreateFile("abc/def.cfg"); | ||
UrlDir.UrlConfig config = UrlBuilder.CreateConfig(node, file); | ||
|
||
Assert.Equal("SOME_NODE", config.type); | ||
Assert.Equal("blah", config.name); | ||
Assert.Same(node, config.config); | ||
Assert.Equal("abc/def/blah", config.url); // I don't know why this is correct, but it is | ||
Assert.Same(file, config.parent); | ||
Assert.Contains(config, file.configs); | ||
} | ||
|
||
[Fact] | ||
public void TestCreateConfig__Url() | ||
{ | ||
ConfigNode node = new TestConfigNode("SOME_NODE") | ||
{ | ||
{ "name", "blah" }, | ||
{ "foo", "bar" }, | ||
}; | ||
UrlDir.UrlConfig config = UrlBuilder.CreateConfig("abc/def", node); | ||
|
||
Assert.Equal("SOME_NODE", config.type); | ||
Assert.Equal("blah", config.name); | ||
Assert.Same(node, config.config); | ||
Assert.Equal("abc/def/blah", config.url); | ||
|
||
UrlDir.UrlFile file = config.parent; | ||
Assert.NotNull(file); | ||
Assert.Equal("def", file.name); | ||
Assert.Equal("cfg", file.fileExtension); | ||
Assert.Equal(UrlDir.FileType.Config, file.fileType); | ||
Assert.Contains(config, file.configs); | ||
|
||
UrlDir parent = file.parent; | ||
Assert.NotNull(parent); | ||
Assert.Equal("abc", parent.name); | ||
Assert.Contains(file, parent.files); | ||
|
||
UrlDir root = parent.parent; | ||
Assert.NotNull(root); | ||
Assert.Equal("root", root.name); | ||
Assert.Contains(parent, root.children); | ||
Assert.Null(root.parent); | ||
|
||
Assert.Same(root, file.root); | ||
Assert.Same(root, parent.root); | ||
Assert.Same(root, root.root); | ||
|
||
Assert.Contains(config, root.AllConfigs); | ||
Assert.Contains(config, root.GetConfigs("SOME_NODE")); | ||
} | ||
} | ||
} |