From d4da8645b15efbf7f5e3077c81a4e9b0818bf561 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 27 Oct 2019 14:12:05 +0100 Subject: [PATCH] Seperate unit tests for commentprocessor and c_header preprocessor --- .../Parsing/C_Header/Impl/Preprocessor.cs | 23 ++--- .../{ => C_Header}/Impl/PreprocessorTest.cs | 91 ++---------------- .../Parsing/Impl/CommentProcessorTest.cs | 93 +++++++++++++++++++ 3 files changed, 112 insertions(+), 95 deletions(-) rename test/ZoneCodeGeneratorTests/Parsing/{ => C_Header}/Impl/PreprocessorTest.cs (60%) create mode 100644 test/ZoneCodeGeneratorTests/Parsing/Impl/CommentProcessorTest.cs diff --git a/src/ZoneCodeGenerator/Parsing/C_Header/Impl/Preprocessor.cs b/src/ZoneCodeGenerator/Parsing/C_Header/Impl/Preprocessor.cs index 8368c60a..09055576 100644 --- a/src/ZoneCodeGenerator/Parsing/C_Header/Impl/Preprocessor.cs +++ b/src/ZoneCodeGenerator/Parsing/C_Header/Impl/Preprocessor.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Text.RegularExpressions; using ZoneCodeGenerator.Parsing.Impl; +using ZoneCodeGenerator.Utils; namespace ZoneCodeGenerator.Parsing.C_Header.Impl { @@ -32,25 +33,25 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl private void ParseCompilerExpression(string line) { - Match packPush = packPushRegex.Match(line.ToLower()); + var packPush = packPushRegex.Match(line.ToLower()); if(packPush.Success) { state.PushPack(int.Parse(packPush.Groups[1].Value)); return; } - Match packPop = packPopRegex.Match(line.ToLower()); + var packPop = packPopRegex.Match(line.ToLower()); if(packPop.Success) { state.PopPack(); return; } - Match define = defineRegex.Match(line); + var define = defineRegex.Match(line); if (define.Success) { - string key = define.Groups[1].Value; - string value = define.Groups[2].Value.Trim(); + var key = define.Groups[1].Value; + var value = define.Groups[2].Value.Trim(); if (defines.ContainsKey(key)) defines[key] = value; @@ -59,7 +60,7 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl return; } - Match undef = undefRegex.Match(line); + var undef = undefRegex.Match(line); if (undef.Success) { var key = undef.Groups[1].Value; @@ -69,10 +70,10 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl return; } - Match include = includeRegex.Match(line); + var include = includeRegex.Match(line); 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); return; @@ -93,13 +94,13 @@ namespace ZoneCodeGenerator.Parsing.C_Header.Impl do { defineMatched = false; - foreach (KeyValuePair 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; - line = match.Groups[1].Value + define.Value + match.Groups[2].Value; + line = match.Groups[1].Value + defineValue + match.Groups[2].Value; defineMatched = true; break; } diff --git a/test/ZoneCodeGeneratorTests/Parsing/Impl/PreprocessorTest.cs b/test/ZoneCodeGeneratorTests/Parsing/C_Header/Impl/PreprocessorTest.cs similarity index 60% rename from test/ZoneCodeGeneratorTests/Parsing/Impl/PreprocessorTest.cs rename to test/ZoneCodeGeneratorTests/Parsing/C_Header/Impl/PreprocessorTest.cs index 741252d5..4c49bc47 100644 --- a/test/ZoneCodeGeneratorTests/Parsing/Impl/PreprocessorTest.cs +++ b/test/ZoneCodeGeneratorTests/Parsing/C_Header/Impl/PreprocessorTest.cs @@ -7,7 +7,7 @@ using ZoneCodeGenerator.Parsing.C_Header.Impl; using ZoneCodeGenerator.Utils; using ZoneCodeGeneratorTests.Parsing.Mock; -namespace ZoneCodeGeneratorTests.Parsing.Impl +namespace ZoneCodeGeneratorTests.Parsing.C_Header.Impl { [TestClass] public class PreprocessorTest @@ -25,83 +25,6 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl preprocessor = new Preprocessor(headerStreamTest, headerParserState); } - [TestMethod] - public void EnsureReturnsUnmodifiedText() - { - string[] stringsThatShouldNotBeModified = - { - "This is a normal string", - "There is nothing to be preprocessed!", - "0124124124 # 124124124", - "...", - "asdf", - "" - }; - headerStreamTest.Lines.AddRange(stringsThatShouldNotBeModified); - - foreach (var stringThatShouldNotBeModified in stringsThatShouldNotBeModified) - { - Assert.AreEqual(stringThatShouldNotBeModified, preprocessor.ReadLine()); - } - } - - [TestMethod] - public void EnsureDoesRemoveLineComments() - { - var commentStrings = new Dictionary() - { - {"// 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() - { - {"/* 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() - { - {"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] public void EnsureDefinesArePlacedCorrectly() { @@ -116,7 +39,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl }; headerStreamTest.Lines.AddRange(defineStrings.Keys); - foreach (var (input, expectedResult) in defineStrings) + foreach (var (_, expectedResult) in defineStrings) { Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); } @@ -133,7 +56,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl }; headerStreamTest.Lines.AddRange(defineStrings.Keys); - foreach (var (input, expectedResult) in defineStrings) + foreach (var (_, expectedResult) in defineStrings) { Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); } @@ -142,14 +65,14 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl [TestMethod] public void EnsureEmptyDefinesResolveToEmpty() { - var defineStrings = new Dictionary() + var defineStrings = new Dictionary { {"#define World", ""}, {"Hello World!", "Hello !"} }; headerStreamTest.Lines.AddRange(defineStrings.Keys); - foreach (var (input, expectedResult) in defineStrings) + foreach (var (_, expectedResult) in defineStrings) { Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); } @@ -171,7 +94,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl }; headerStreamTest.Lines.AddRange(defineStrings.Keys); - foreach (var (input, expectedResult) in defineStrings) + foreach (var (_, expectedResult) in defineStrings) { Assert.AreEqual(expectedResult, preprocessor.ReadLine().Trim()); } @@ -197,7 +120,7 @@ namespace ZoneCodeGeneratorTests.Parsing.Impl }; headerStreamTest.Lines.AddRange(packs.Select(tuple => tuple.Item1)); - foreach (var (input, expectedPack) in packs) + foreach (var (_, expectedPack) in packs) { preprocessor.ReadLine(); diff --git a/test/ZoneCodeGeneratorTests/Parsing/Impl/CommentProcessorTest.cs b/test/ZoneCodeGeneratorTests/Parsing/Impl/CommentProcessorTest.cs new file mode 100644 index 00000000..cac4ce75 --- /dev/null +++ b/test/ZoneCodeGeneratorTests/Parsing/Impl/CommentProcessorTest.cs @@ -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", + "...", + "asdf", + "" + }; + + foreach (var stringThatShouldNotBeModified in stringsThatShouldNotBeModified) + { + Assert.AreEqual(stringThatShouldNotBeModified, commentProcessor.RemoveComments(stringThatShouldNotBeModified)); + } + } + + [TestMethod] + public void EnsureDoesRemoveLineComments() + { + var commentStrings = new Dictionary() + { + {"// 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() + { + {"/* 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() + { + {"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)); + } + } + } +}