Tests, refactoring

This commit is contained in:
Aaron Yarborough 2020-09-09 15:49:07 +02:00
parent 0e3557f596
commit 42bc9ed385
6 changed files with 89 additions and 19 deletions

View file

@ -34,7 +34,7 @@ namespace GraalGmapGenerator
stringBuilder.AppendLine("LEVELNAMES"); stringBuilder.AppendLine("LEVELNAMES");
var levelNames = GetLevelNames(gmap).ToList(); List<string> levelNames = GetLevelNames(gmap).ToList();
for (var i = 0; i < levelNames.Count; i++) for (var i = 0; i < levelNames.Count; i++)
{ {
// Start a new line once the current line has hit the width of the gmap // Start a new line once the current line has hit the width of the gmap
@ -59,14 +59,10 @@ namespace GraalGmapGenerator
public IEnumerable<string> GetLevelNames(Gmap gmap) public IEnumerable<string> GetLevelNames(Gmap gmap)
{ {
var levelNames = new List<string>(); for (int i = 0; i < (gmap.Width * gmap.Height); i++)
for (var i = 0; i < (gmap.Width * gmap.Height); i++)
{ {
levelNames.Add(GetLevelName(i, gmap.Name, _levelType)); yield return GetLevelName(i, gmap.Name, _levelType);
} }
return levelNames;
} }
private string GetLevelName(int index, string name, LevelType levelType) private string GetLevelName(int index, string name, LevelType levelType)

View file

@ -2,13 +2,13 @@ using System;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
namespace GraalGmapGenerator.Validators namespace GraalGmapGenerator
{ {
public static class GmapPropertyValidators public static class GmapPropertyValidators
{ {
public static bool IsValidDimension(string input) public static bool IsValidDimension(string input)
{ {
return int.TryParse(input, out int width) && width > 0; return int.TryParse(input, out int dimension) && dimension > 0;
} }
public static bool IsValidYesNoInput(string input) public static bool IsValidYesNoInput(string input)

View file

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.IO; using System.IO;
using GraalGmapGenerator.Enums; using GraalGmapGenerator.Enums;
@ -16,15 +17,13 @@ namespace GraalGmapGenerator
var gmapContentGen = new GmapContentGenerator(LevelType.Nw); var gmapContentGen = new GmapContentGenerator(LevelType.Nw);
// Create a new level file for each level IEnumerable<string> levelNames = gmapContentGen.GetLevelNames(gmap);
var levelNames = gmapContentGen.GetLevelNames(gmap);
foreach (string levelName in levelNames) foreach (string levelName in levelNames)
{ {
File.Copy(TemplateFile, $"{destinationPath}/{levelName}"); File.Copy(TemplateFile, $"{destinationPath}/{levelName}");
} }
// Create the gmap file string gmapContent = gmapContentGen.Generate(gmap);
var gmapContent = gmapContentGen.Generate(gmap);
File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent); File.AppendAllText($"{destinationPath}/{gmap.Name}.gmap", gmapContent);
} }
} }

View file

@ -6,7 +6,7 @@ namespace GraalGmapGenerator
{ {
public static bool YesNoToBool(string input) public static bool YesNoToBool(string input)
{ {
var inputLowered = input.ToLower(); string inputLowered = input.ToLower();
if (input == "y" || input == "yes") if (input == "y" || input == "yes")
return true; return true;
if (input == "n" || input == "no") if (input == "n" || input == "no")

View file

@ -1,10 +1,8 @@
using System; using System;
using System.IO;
using GraalGmapGenerator.Validators;
namespace GraalGmapGenerator namespace GraalGmapGenerator
{ {
class Programfg class Program
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
@ -15,7 +13,7 @@ namespace GraalGmapGenerator
); );
Console.WriteLine(); Console.WriteLine();
Console.WriteLine( Console.WriteLine(
"If you run into any problems with the tool, drop me an email at me@aaronjy.me and I " + "If you run into any problems, drop me an email at me@aaronjy.me and I " +
"\nwill endeavour to respond as soon as possible. " + "\nwill endeavour to respond as soon as possible. " +
"\nThanks for using my software! - Aaron Yarborough" "\nThanks for using my software! - Aaron Yarborough"
); );
@ -120,7 +118,6 @@ namespace GraalGmapGenerator
if (saveDirectory == "") if (saveDirectory == "")
{ {
saveDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "gmaps", gmapName);
} }
Console.WriteLine("Generating gmap..."); Console.WriteLine("Generating gmap...");

View file

@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GraalGmapGenerator;
using NUnit.Framework;
namespace GraalGmapGeneratorTests
{
[TestFixture]
public class GmapPropertyValidatorsTests
{
internal static IEnumerable<string> GetInvalidPaths()
{
const string dummyPath = "dir/";
char[] invalidChars = System.IO.Path.GetInvalidPathChars();
foreach (char invalidChar in invalidChars)
{
yield return dummyPath + invalidChar;
}
}
[TestCase("-1")]
[TestCase("0")]
[TestCase("abc")]
[TestCase("£$%^&*")]
[TestCase("123a")]
public void IsValidDimension_IfInvalidDimension_ReturnsFalse(string invalidDimension)
{
Assert.False(GmapPropertyValidators.IsValidDimension(invalidDimension));
}
[TestCase("1")]
[TestCase("12345")]
[TestCase("10")]
[TestCase("2147483647")]
public void IsValidDimension_IfValidDimension_ReturnsTrue(string validDimension)
{
Assert.True(GmapPropertyValidators.IsValidDimension(validDimension));
}
[TestCase("")]
[TestCase("oopsie")]
[TestCase("ye")]
[TestCase("yess")]
[TestCase("noo")]
[TestCase("12345")]
public void IsValidYesNoInput_IfIsInvalidValidInput_ReturnsFalse(string invalidInput)
{
Assert.False(GmapPropertyValidators.IsValidYesNoInput(invalidInput));
}
[TestCase("y")]
[TestCase("yes")]
[TestCase("n")]
[TestCase("no")]
public void IsValidYesNoInput_IfIsValidInput_ReturnsTrue(string validInput)
{
Assert.True(GmapPropertyValidators.IsValidYesNoInput(validInput));
}
[TestCaseSource(nameof(GetInvalidPaths))]
public void IsValidDirectory_IfIsInvalidDirectory_ReturnsFalse(string invalidPath)
{
Assert.False(GmapPropertyValidators.IsValidDirectory(invalidPath));
}
[TestCase("my/path")]
[TestCase("my/path/")]
[TestCase("directory")]
[TestCase("C:/users/Aaron/gmaps")]
public void IsValidDirectory_IfIsValidDirectory_ReturnsTrue(string validPath)
{
Assert.True(GmapPropertyValidators.IsValidDirectory(validPath));
}
}
}