Private
Public Access
added CommandAndParameterParser testing and fixed bugs within it
This commit is contained in:
1 parent
f198b609f2
commit
8a17d77510
2 files changed
+76
-12
No files matched your search
+71
-11
@@ -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));
|
||||||
|
|
||||||
@@ -66,7 +59,7 @@ namespace Advanced_Software_Engineering_Tests {
|
|||||||
|
|
||||||
[TestMethod]
|
[TestMethod]
|
||||||
public void ConvertToDouble_Random1() {
|
public void ConvertToDouble_Random1() {
|
||||||
for(int i = 0; i < 100000; i++) {
|
for (int i = 0; i < 100000; i++) {
|
||||||
double expected = rand.NextDouble();
|
double expected = rand.NextDouble();
|
||||||
string input = expected.ToString();
|
string input = expected.ToString();
|
||||||
|
|
||||||
@@ -80,7 +73,7 @@ namespace Advanced_Software_Engineering_Tests {
|
|||||||
string[] expected = (new List<string> { "a", "test", "t", "asdf" }).ToArray();
|
string[] expected = (new List<string> { "a", "test", "t", "asdf" }).ToArray();
|
||||||
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
foreach(string s in SettingsAndHelperFunctions.StripStringArray(input)) {
|
foreach (string s in SettingsAndHelperFunctions.StripStringArray(input)) {
|
||||||
Assert.AreEqual(expected[i++], s);
|
Assert.AreEqual(expected[i++], s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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++]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+5
-1
@@ -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) {
|
||||||
|
|||||||
Reference in new issue
Block a user