Seperate unit tests for commentprocessor and c_header preprocessor

This commit is contained in:
Jan 2019-10-27 14:12:05 +01:00
parent 146c992dd5
commit d4da8645b1
3 changed files with 112 additions and 95 deletions

View File

@ -1,6 +1,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using ZoneCodeGenerator.Parsing.Impl; using ZoneCodeGenerator.Parsing.Impl;
using ZoneCodeGenerator.Utils;
namespace ZoneCodeGenerator.Parsing.C_Header.Impl namespace ZoneCodeGenerator.Parsing.C_Header.Impl
{ {
@ -32,25 +33,25 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl
private void ParseCompilerExpression(string line) private void ParseCompilerExpression(string line)
{ {
Match packPush = packPushRegex.Match(line.ToLower()); var packPush = packPushRegex.Match(line.ToLower());
if(packPush.Success) if(packPush.Success)
{ {
state.PushPack(int.Parse(packPush.Groups[1].Value)); state.PushPack(int.Parse(packPush.Groups[1].Value));
return; return;
} }
Match packPop = packPopRegex.Match(line.ToLower()); var packPop = packPopRegex.Match(line.ToLower());
if(packPop.Success) if(packPop.Success)
{ {
state.PopPack(); state.PopPack();
return; return;
} }
Match define = defineRegex.Match(line); var define = defineRegex.Match(line);
if (define.Success) if (define.Success)
{ {
string key = define.Groups[1].Value; var key = define.Groups[1].Value;
string value = define.Groups[2].Value.Trim(); var value = define.Groups[2].Value.Trim();
if (defines.ContainsKey(key)) if (defines.ContainsKey(key))
defines[key] = value; defines[key] = value;
@ -59,7 +60,7 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl
return; return;
} }
Match undef = undefRegex.Match(line); var undef = undefRegex.Match(line);
if (undef.Success) if (undef.Success)
{ {
var key = undef.Groups[1].Value; var key = undef.Groups[1].Value;
@ -69,10 +70,10 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl
return; return;
} }
Match include = includeRegex.Match(line); var include = includeRegex.Match(line);
if (include.Success) if (include.Success)
{ {
string filename = include.Groups[1].Success ? include.Groups[1].Value : include.Groups[2].Value; var filename = include.Groups[1].Success ? include.Groups[1].Value : include.Groups[2].Value;
streamFileSystem.IncludeFile(filename); streamFileSystem.IncludeFile(filename);
return; return;
@ -93,13 +94,13 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl
do do
{ {
defineMatched = false; defineMatched = false;
foreach (KeyValuePair<string, string> define in defines) foreach (var (defineKey, defineValue) in defines)
{ {
Match match = Regex.Match(line, $@"^(.*\W)?{define.Key}(\W.*)?$"); var match = Regex.Match(line, $@"^(.*\W)?{defineKey}(\W.*)?$");
if (!match.Success) continue; if (!match.Success) continue;
line = match.Groups[1].Value + define.Value + match.Groups[2].Value; line = match.Groups[1].Value + defineValue + match.Groups[2].Value;
defineMatched = true; defineMatched = true;
break; break;
} }

View File

@ -7,7 +7,7 @@ using ZoneCodeGenerator.Parsing.C_Header.Impl;
using ZoneCodeGenerator.Utils; using ZoneCodeGenerator.Utils;
using ZoneCodeGeneratorTests.Parsing.Mock; using ZoneCodeGeneratorTests.Parsing.Mock;
namespace ZoneCodeGeneratorTests.Parsing.Impl namespace ZoneCodeGeneratorTests.Parsing.C_Header.Impl
{ {
[TestClass] [TestClass]
public class PreprocessorTest public class PreprocessorTest
@ -25,83 +25,6 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
preprocessor = new Preprocessor(headerStreamTest, headerParserState); preprocessor = new Preprocessor(headerStreamTest, headerParserState);
} }
[TestMethod]
public void EnsureReturnsUnmodifiedText()
{
string[] stringsThatShouldNotBeModified =
{
"This is a normal string",
"There is nothing to be preprocessed!",
"0124124124 # 124124124",
"...",
"<?php><html>asdf</html>",
""
};
headerStreamTest.Lines.AddRange(stringsThatShouldNotBeModified);
foreach (var stringThatShouldNotBeModified in stringsThatShouldNotBeModified)
{
Assert.AreEqual(stringThatShouldNotBeModified, preprocessor.ReadLine());
}
}
[TestMethod]
public void EnsureDoesRemoveLineComments()
{
var commentStrings = new Dictionary<string, string>()
{
{"// This is a comment at the beginning of the line", ""},
{"Text in front of a comment // Comment", "Text in front of a comment"},
{"/ / Not a comment", "/ / Not a comment"},
{"asdf /// Triple slash is a comment", "asdf"},
{"b2h3 /////////////// In fact after the first two slashes it should always be considered a comment", "b2h3"},
};
headerStreamTest.Lines.AddRange(commentStrings.Keys);
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
}
}
[TestMethod]
public void EnsureDoesRemoveBlockComments()
{
var commentStrings = new Dictionary<string, string>()
{
{"/* This is a block comment */", ""},
{"Text in front of a comment /** Comment ***/", "Text in front of a comment"},
{"/ * Not a comment */", "/ * Not a comment */"},
{"Text in front of comment /* Comment */ Text after the comment", "Text in front of comment Text after the comment"},
{"Hello/*Hell*/World", "HelloWorld"},
};
headerStreamTest.Lines.AddRange(commentStrings.Keys);
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
}
}
[TestMethod]
public void EnsureBlockCommentsWorkOverMultipleLines()
{
var commentStrings = new Dictionary<string, string>()
{
{"The start of the comment /* Is now", "The start of the comment"},
{"Nothing to be seen here", ""},
{"* / /* Still nothing", ""},
{"The comment ends */ now", "now"},
{"This line should not cause any issues", "This line should not cause any issues"},
};
headerStreamTest.Lines.AddRange(commentStrings.Keys);
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
}
}
[TestMethod] [TestMethod]
public void EnsureDefinesArePlacedCorrectly() public void EnsureDefinesArePlacedCorrectly()
{ {
@ -116,7 +39,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
}; };
headerStreamTest.Lines.AddRange(defineStrings.Keys); headerStreamTest.Lines.AddRange(defineStrings.Keys);
foreach (var (input, expectedResult) in defineStrings) foreach (var (_, expectedResult) in defineStrings)
{ {
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
} }
@ -133,7 +56,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
}; };
headerStreamTest.Lines.AddRange(defineStrings.Keys); headerStreamTest.Lines.AddRange(defineStrings.Keys);
foreach (var (input, expectedResult) in defineStrings) foreach (var (_, expectedResult) in defineStrings)
{ {
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
} }
@ -142,14 +65,14 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
[TestMethod] [TestMethod]
public void EnsureEmptyDefinesResolveToEmpty() public void EnsureEmptyDefinesResolveToEmpty()
{ {
var defineStrings = new Dictionary<string, string>() var defineStrings = new Dictionary<string, string>
{ {
{"#define World", ""}, {"#define World", ""},
{"Hello World!", "Hello !"} {"Hello World!", "Hello !"}
}; };
headerStreamTest.Lines.AddRange(defineStrings.Keys); headerStreamTest.Lines.AddRange(defineStrings.Keys);
foreach (var (input, expectedResult) in defineStrings) foreach (var (_, expectedResult) in defineStrings)
{ {
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
} }
@ -171,7 +94,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
}; };
headerStreamTest.Lines.AddRange(defineStrings.Keys); headerStreamTest.Lines.AddRange(defineStrings.Keys);
foreach (var (input, expectedResult) in defineStrings) foreach (var (_, expectedResult) in defineStrings)
{ {
Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim());
} }
@ -197,7 +120,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl
}; };
headerStreamTest.Lines.AddRange(packs.Select(tuple => tuple.Item1)); headerStreamTest.Lines.AddRange(packs.Select(tuple => tuple.Item1));
foreach (var (input, expectedPack) in packs) foreach (var (_, expectedPack) in packs)
{ {
preprocessor.ReadLine(); preprocessor.ReadLine();

View File

@ -0,0 +1,93 @@
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ZoneCodeGenerator.Parsing;
using ZoneCodeGenerator.Parsing.Impl;
using ZoneCodeGenerator.Utils;
namespace ZoneCodeGeneratorTests.Parsing.Impl
{
[TestClass]
public class CommentPreprocessorTest
{
private ICommentProcessor commentProcessor;
[TestInitialize]
public void Setup()
{
commentProcessor = new CommentProcessor();
}
[TestMethod]
public void EnsureReturnsUnmodifiedText()
{
string[] stringsThatShouldNotBeModified =
{
"This is a normal string",
"There is nothing to be preprocessed!",
"0124124124 # 124124124",
"...",
"<?php><html>asdf</html>",
""
};
foreach (var stringThatShouldNotBeModified in stringsThatShouldNotBeModified)
{
Assert.AreEqual(stringThatShouldNotBeModified, commentProcessor.RemoveComments(stringThatShouldNotBeModified));
}
}
[TestMethod]
public void EnsureDoesRemoveLineComments()
{
var commentStrings = new Dictionary<string, string>()
{
{"// This is a comment at the beginning of the line", ""},
{"Text in front of a comment // Comment", "Text in front of a comment"},
{"/ / Not a comment", "/ / Not a comment"},
{"asdf /// Triple slash is a comment", "asdf"},
{"b2h3 /////////////// In fact after the first two slashes it should always be considered a comment", "b2h3"},
};
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, commentProcessor.RemoveComments(input));
}
}
[TestMethod]
public void EnsureDoesRemoveBlockComments()
{
var commentStrings = new Dictionary<string, string>()
{
{"/* This is a block comment */", ""},
{"Text in front of a comment /** Comment ***/", "Text in front of a comment"},
{"/ * Not a comment */", "/ * Not a comment */"},
{"Text in front of comment /* Comment */ Text after the comment", "Text in front of comment Text after the comment"},
{"Hello/*Hell*/World", "HelloWorld"},
};
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, commentProcessor.RemoveComments(input));
}
}
[TestMethod]
public void EnsureBlockCommentsWorkOverMultipleLines()
{
var commentStrings = new Dictionary<string, string>()
{
{"The start of the comment /* Is now", "The start of the comment"},
{"Nothing to be seen here", ""},
{"* / /* Still nothing", ""},
{"The comment ends */ now", " now"},
{"This line should not cause any issues", "This line should not cause any issues"},
};
foreach (var (input, expectedResult) in commentStrings)
{
Assert.AreEqual(expectedResult, commentProcessor.RemoveComments(input));
}
}
}
}