Skip to content

Commit

Permalink
Don't create duplicates in UrlBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
blowfishpro committed Sep 14, 2017
1 parent b43f79b commit 534eee2
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 3 deletions.
21 changes: 18 additions & 3 deletions TestUtils/URLBuilder.cs
Expand Up @@ -41,7 +41,15 @@ public static UrlDir CreateRoot()

public static UrlDir CreateDir(string url, UrlDir parent = null)
{
if (parent == null) parent = CreateRoot();
if (parent == null)
{
parent = CreateRoot();
}
else
{
UrlDir existingDir = parent.GetDirectory(url);
if (existingDir != null) return existingDir;
}

UrlDir current = parent;

Expand Down Expand Up @@ -74,10 +82,17 @@ public static UrlDir.UrlFile CreateFile(string path, UrlDir parent = null)
parent = CreateRoot();
}

string nameWithoutExtension = Path.GetFileNameWithoutExtension(name);
string extension = Path.GetExtension(name);
if (!string.IsNullOrEmpty(extension)) extension = extension.Substring(1);

UrlDir.UrlFile existingFile = parent.files.FirstOrDefault(f => f.name == nameWithoutExtension && f.fileExtension == extension);
if (existingFile != null) return existingFile;

bool cfg = false;
string newName = name;

if (Path.GetExtension(name) == ".cfg")
if (extension == "cfg")
{
cfg = true;
newName = name + ".not_cfg";
Expand All @@ -87,7 +102,7 @@ public static UrlDir.UrlFile CreateFile(string path, UrlDir parent = null)

if (cfg)
{
UrlFile__field__name.SetValue(file, Path.GetFileNameWithoutExtension(name));
UrlFile__field__name.SetValue(file, nameWithoutExtension);
UrlFile__field__fileExtension.SetValue(file, "cfg");
UrlFile__field__fileType.SetValue(file, UrlDir.FileType.Config);
}
Expand Down
102 changes: 102 additions & 0 deletions TestUtilsTests/UrlBuilderTest.cs
Expand Up @@ -82,6 +82,8 @@ public void TestCreateDir__Url()
Assert.Same(root, root.root);
Assert.Same(root, parent.root);
Assert.Same(root, dir.root);

Assert.Contains(dir, root.AllDirectories);
}

[Fact]
Expand All @@ -105,6 +107,32 @@ public void TestCreateDir__Url__Parent()

Assert.Same(root, dir.root);
Assert.Same(root, parent2.root);

Assert.Contains(dir, root.AllDirectories);
Assert.Contains(dir, parent1.AllDirectories);
}

[Fact]
public void TestCreateDir__Url__AlreadyExists()
{
UrlDir root = UrlBuilder.CreateRoot();

UrlDir dir1 = UrlBuilder.CreateDir("abc/def", root);
UrlDir dir2 = UrlBuilder.CreateDir("abc/def", root);

Assert.Same(dir1, dir2);

Assert.Equal("def", dir1.name);

UrlDir parent = dir1.parent;

Assert.NotNull(parent);
Assert.Equal("abc", parent.name);
Assert.Contains(dir1, parent.children);

Assert.Same(root, dir1.root);
Assert.Same(root, parent.root);
Assert.Contains(dir1, root.AllDirectories);
}

[Fact]
Expand Down Expand Up @@ -196,6 +224,31 @@ public void TestCreateFile__Url()
Assert.Contains(file, parent2.AllFiles);
}

[Fact]
public void TestCreateFile__Url__AlreadyExists()
{
UrlDir root = UrlBuilder.CreateRoot();
UrlDir.UrlFile file1 = UrlBuilder.CreateFile("abc/def.txt", root);
UrlDir.UrlFile file2 = UrlBuilder.CreateFile("abc/def.txt", root);

Assert.Same(file1, file2);

Assert.Equal("def", file1.name);
Assert.Equal("txt", file1.fileExtension);
Assert.Equal(UrlDir.FileType.Unknown, file1.fileType);
Assert.Equal("abc/def", file1.url);

UrlDir parent1 = file1.parent;
Assert.NotNull(parent1);
Assert.Equal("abc", parent1.name);
Assert.Contains(file1, parent1.files);

Assert.Same(root, file1.root);
Assert.Same(root, parent1.root);

Assert.Contains(file1, root.AllFiles);
}

[Fact]
public void TestCreateConfig()
{
Expand Down Expand Up @@ -255,5 +308,54 @@ public void TestCreateConfig__Url()
Assert.Contains(config, root.AllConfigs);
Assert.Contains(config, root.GetConfigs("SOME_NODE"));
}

[Fact]
public void TestCreateConfig__Url__FileAlreadyExists()
{
UrlDir root = UrlBuilder.CreateRoot();

ConfigNode node1 = new TestConfigNode("SOME_NODE")
{
{ "name", "blah" },
{ "foo", "bar" },
};

ConfigNode node2 = new TestConfigNode("SOME_OTHER_NODE")
{
{ "name", "bleh" },
{ "jazz", "hands" },
};

UrlDir.UrlConfig config1 = UrlBuilder.CreateConfig("abc/def", node1, root);
UrlDir.UrlConfig config2 = UrlBuilder.CreateConfig("abc/def", node2, root);

UrlDir.UrlFile file = config1.parent;
Assert.NotNull(file);

Assert.Same(file, config2.parent);

Assert.Equal("def", file.name);
Assert.Equal("cfg", file.fileExtension);
Assert.Equal(UrlDir.FileType.Config, file.fileType);
Assert.Contains(config1, file.configs);
Assert.Contains(config2, file.configs);

UrlDir parent = file.parent;
Assert.NotNull(parent);
Assert.Equal("abc", parent.name);
Assert.Contains(file, parent.files);

Assert.Contains(parent, root.children);

Assert.Same(root, file.root);
Assert.Same(root, parent.root);
Assert.Same(root, root.root);

Assert.Contains(config1, root.AllConfigs);
Assert.Contains(config1, root.GetConfigs("SOME_NODE"));

Assert.Contains(config2, root.AllConfigs);
Assert.Contains(config2, root.GetConfigs("SOME_OTHER_NODE"));
}
}
}

0 comments on commit 534eee2

Please sign in to comment.