added CommandAndParameterParser testing and fixed bugs within it

This commit is contained in:
Lukasz Baldyga committed 2020-10-22 20:53:57 +01:00
1 parent f198b609f2
commit 8a17d77510
2 files changed
+74 -10

No files matched your search

@@ -2,6 +2,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting;
using Advanced_Software_Engineering; using Advanced_Software_Engineering;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
namespace Advanced_Software_Engineering_Tests { namespace Advanced_Software_Engineering_Tests {
[TestClass] [TestClass]
@@ -23,15 +24,7 @@ namespace Advanced_Software_Engineering_Tests {
string obfuscatedInput = correctOutput; string obfuscatedInput = correctOutput;
until = rand.Next(50); obfuscatedInput = randomSpaces() + obfuscatedInput + randomSpaces();
for (int i = 0; i < until; i++) {
if (rand.Next(1) == 0) obfuscatedInput += space;
}
until = rand.Next(50);
for (int i = 0; i < until; i++) {
if (rand.Next(1) == 0) obfuscatedInput = space + obfuscatedInput;
}
Assert.AreEqual(correctOutput, SettingsAndHelperFunctions.Strip(obfuscatedInput)); Assert.AreEqual(correctOutput, SettingsAndHelperFunctions.Strip(obfuscatedInput));
@@ -95,5 +88,72 @@ namespace Advanced_Software_Engineering_Tests {
Assert.AreEqual(expected[i++], s); Assert.AreEqual(expected[i++], s);
} }
} }
[TestMethod]
public void CommandAndParameterParser_Manual1() {
Dictionary<string, string[]> commands = SettingsAndHelperFunctions.CommandAndParameterParser("moveto 200, 200");
Assert.AreEqual("moveto", commands["command"][0]);
Assert.AreEqual("200", commands["parameters"][0]);
Assert.AreEqual("200", commands["parameters"][1]);
}
[TestMethod]
public void CommandAndParameterParser_Manual2() {
Dictionary<string, string[]> commands = SettingsAndHelperFunctions.CommandAndParameterParser("clear");
Assert.AreEqual("clear", commands["command"][0]);
Assert.IsFalse(commands.Keys.Contains("parameters"));
}
[TestMethod]
public void CommandAndParameterParser_Manual3() {
Dictionary<string, string[]> commands = SettingsAndHelperFunctions.CommandAndParameterParser("");
Assert.IsFalse(commands.Keys.Contains("command"));
Assert.IsFalse(commands.Keys.Contains("parameters"));
}
[TestMethod]
public void CommandAndParameterParser_Manual4() {
Dictionary<string, string[]> commands = SettingsAndHelperFunctions.CommandAndParameterParser(" genericCommand 2, 3,4 5");
Assert.AreEqual("genericCommand", commands["command"][0]);
Assert.AreEqual("2", commands["parameters"][0]);
Assert.AreEqual("3", commands["parameters"][1]);
Assert.AreEqual("4 5", commands["parameters"][2]);
}
string randomSpaces() {
string spaces = " ";
for (int i = 0; i < 50; i++) spaces += " ";
return spaces;
}
[TestMethod]
public void CommandAndParameterParser_Random1() {
string[] randomCommands = {"test", "moveto", "lineto", "markiplier", "clear", "shape", "etc" };
for(int test = 0; test < 10000; test++) {
string command = randomCommands[rand.Next(randomCommands.Length)];
string input = randomSpaces() + command;
List<string> parameters = new List<string>();
for(int parameterI = 0; parameterI < rand.Next(99) + 1; parameterI++) {
string parameter = (rand.NextDouble() * rand.Next(-1000, 1000)).ToString();
parameters.Add(parameter);
input += randomSpaces() + parameter + randomSpaces() + "," + randomSpaces();
}
Dictionary<string, string[]> commands = SettingsAndHelperFunctions.CommandAndParameterParser(input);
Assert.AreEqual(command, commands["command"][0]);
int i = 0;
foreach(string expected in parameters) {
Assert.AreEqual(expected, commands["parameters"][i++]);
}
}
}
} }
} }
@@ -98,8 +98,12 @@ namespace Advanced_Software_Engineering {
public static Dictionary<string, string[]> CommandAndParameterParser(string text) { public static Dictionary<string, string[]> CommandAndParameterParser(string text) {
Dictionary<string, string[]> commandAndParameters = new Dictionary<string, string[]>(); Dictionary<string, string[]> commandAndParameters = new Dictionary<string, string[]>();
//prepare the parameters by stripping them of spaces
string prepParameters = Strip(text);
if (prepParameters.Length == 0) return commandAndParameters;
//split the command from the parameters //split the command from the parameters
string[] parameters = Strip(text).Split(new char[] { " "[0] }, 2); string[] parameters = prepParameters.Split(new char[] { " "[0] }, 2);
//There are parameters, make the command //There are parameters, make the command
if (parameters.Length > 0) { if (parameters.Length > 0) {