From 6c82c79745e7596a693ef3b6d31df2d55d9bdf2e Mon Sep 17 00:00:00 2001 From: Mark Michaelis Date: Fri, 14 Jul 2017 13:53:25 +0300 Subject: [PATCH] Addressed cross platform new line isses and handled MSTest defect See https://github.com/Microsoft/vstest/issues/311 - Converted all new lines to use Evironment.NewLine - Used reflection to locate directory of test assembly and then switched that to the current directory due to incorrect starting directory as described at See https://github.com/Microsoft/vstest/issues/311 --- ...5.03.TuplesWithinQueryExpressions.Tests.cs | 15 +- ...nymousTypesWithinQueryExpressions.Tests.cs | 19 +- ...ueryExpressionWithAnOrderbyClause.Tests.cs | 11 +- ...nfoCollectionAndSortingByFileSize.Tests.cs | 17 +- src/Chapter15.Tests/Listing15.10.Tests.cs | 7 +- src/Chapter15.Tests/Listing15.11.Tests.cs | 3 +- src/Chapter15.Tests/Listing15.12.Tests.cs | 26 + src/Chapter15.Tests/Listing15.13.Tests.cs | 604 +----------------- src/Chapter15.Tests/Listing15.14.Tests.cs | 38 ++ .../Listing15.13.MultipleSelection.cs | 2 +- submodules/TestTools | 2 +- 11 files changed, 140 insertions(+), 604 deletions(-) create mode 100644 src/Chapter15.Tests/Listing15.12.Tests.cs create mode 100644 src/Chapter15.Tests/Listing15.14.Tests.cs diff --git a/src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs b/src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs index 85076c4e0..b9f218183 100644 --- a/src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs +++ b/src/Chapter15.Tests/Listing15.03.TuplesWithinQueryExpressions.Tests.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System; +using System.Reflection; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03.Tests { @@ -12,6 +14,14 @@ public class ProgramTests [TestMethod] public void ProjectionWithLinqsSelect() { + // Required due to defect in MSTest that has the current directory + // set to the MSTest executable directory, rather than the + // assembly directory. See https://github.com/Microsoft/vstest/issues/311 + Directory.SetCurrentDirectory(Path.GetDirectoryName( + typeof(Program).GetTypeInfo().Assembly.Location)); + + int expectedItemCount = Directory.EnumerateFiles( + Directory.GetCurrentDirectory(), "*").Count(); string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*(*)"; string output = IntelliTect.TestTools.Console.ConsoleAssert.Execute(null, () => @@ -19,9 +29,10 @@ public void ProjectionWithLinqsSelect() Program.ChapterMain(); }); - IEnumerable outputItems = output.Split('\n'); + IEnumerable outputItems = output.Split( + Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries); - Assert.AreEqual(14, outputItems.Count()); + Assert.AreEqual(expectedItemCount, outputItems.Count()); foreach (string item in outputItems) { Assert.IsTrue(item.IsLike(expectedPattern)); diff --git a/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs b/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs index 85076c4e0..2fd6c97e4 100644 --- a/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs +++ b/src/Chapter15.Tests/Listing15.03A.AnonymousTypesWithinQueryExpressions.Tests.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; +using System; -namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03.Tests +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_03A.Tests { [TestClass] public class ProgramTests @@ -12,16 +14,25 @@ public class ProgramTests [TestMethod] public void ProjectionWithLinqsSelect() { - string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*(*)"; + // Required due to defect in MSTest that has the current directory + // set to the MSTest executable directory, rather than the + // assembly directory. See https://github.com/Microsoft/vstest/issues/311 + Directory.SetCurrentDirectory(Path.GetDirectoryName( + typeof(Program).GetTypeInfo().Assembly.Location)); + + string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*"; + int expectedItemCount = Directory.EnumerateFiles( + Directory.GetCurrentDirectory(), "*").Count(); string output = IntelliTect.TestTools.Console.ConsoleAssert.Execute(null, () => { Program.ChapterMain(); }); - IEnumerable outputItems = output.Split('\n'); + IEnumerable outputItems = output.Split( + new string[]{ Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries); - Assert.AreEqual(14, outputItems.Count()); + Assert.AreEqual(expectedItemCount, outputItems.Count()); foreach (string item in outputItems) { Assert.IsTrue(item.IsLike(expectedPattern)); diff --git a/src/Chapter15.Tests/Listing15.07.SortingUsingAQueryExpressionWithAnOrderbyClause.Tests.cs b/src/Chapter15.Tests/Listing15.07.SortingUsingAQueryExpressionWithAnOrderbyClause.Tests.cs index 348580394..7767482b7 100644 --- a/src/Chapter15.Tests/Listing15.07.SortingUsingAQueryExpressionWithAnOrderbyClause.Tests.cs +++ b/src/Chapter15.Tests/Listing15.07.SortingUsingAQueryExpressionWithAnOrderbyClause.Tests.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_07.Tests { @@ -12,6 +13,14 @@ public class ProgramTests [TestMethod] public void ProjectionWithLinqsSelect() { + // Required due to defect in MSTest that has the current directory + // set to the MSTest executable directory, rather than the + // assembly directory. See https://github.com/Microsoft/vstest/issues/311 + Directory.SetCurrentDirectory(Path.GetDirectoryName( + typeof(Program).GetTypeInfo().Assembly.Location)); + + int expectedItemCount = Directory.EnumerateFiles( + Directory.GetCurrentDirectory(), "*").Count(); string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*"; string output = IntelliTect.TestTools.Console.ConsoleAssert.Execute(null, () => @@ -21,7 +30,7 @@ public void ProjectionWithLinqsSelect() IEnumerable outputItems = output.Split('\n'); - Assert.AreEqual(14, outputItems.Count()); + Assert.AreEqual(expectedItemCount, outputItems.Count()); foreach (string item in outputItems) { Assert.IsTrue(item.IsLike(expectedPattern)); diff --git a/src/Chapter15.Tests/Listing15.08.ProjectingAFileInfoCollectionAndSortingByFileSize.Tests.cs b/src/Chapter15.Tests/Listing15.08.ProjectingAFileInfoCollectionAndSortingByFileSize.Tests.cs index d3abba64b..9151925b8 100644 --- a/src/Chapter15.Tests/Listing15.08.ProjectingAFileInfoCollectionAndSortingByFileSize.Tests.cs +++ b/src/Chapter15.Tests/Listing15.08.ProjectingAFileInfoCollectionAndSortingByFileSize.Tests.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_08.Tests { @@ -13,6 +14,14 @@ public class ProgramTests [TestMethod] public void ProjectionWithLinqsSelect() { + // Required due to defect in MSTest that has the current directory + // set to the MSTest executable directory, rather than the + // assembly directory. See https://github.com/Microsoft/vstest/issues/311 + Directory.SetCurrentDirectory(Path.GetDirectoryName( + typeof(Program).GetTypeInfo().Assembly.Location)); + + int expectedItemCount = Directory.EnumerateFiles( + Directory.GetCurrentDirectory(), "*").Count(); string expectedPattern = $@".{Path.DirectorySeparatorChar}*(*)"; string output = IntelliTect.TestTools.Console.ConsoleAssert.Execute(null, () => @@ -20,12 +29,14 @@ public void ProjectionWithLinqsSelect() Program.ChapterMain(); }); - IEnumerable outputItems = output.Split('\n'); + IEnumerable outputItems = output.Split( + new string[] { Environment.NewLine }, StringSplitOptions.None); - Assert.AreEqual(14, outputItems.Count()); + Assert.AreEqual(expectedItemCount, outputItems.Count()); foreach (string item in outputItems) { - Assert.IsTrue(item.IsLike(expectedPattern), item); + Assert.IsTrue(item.IsLike(expectedPattern), + $"{item} is not like {expectedPattern}"); } } } diff --git a/src/Chapter15.Tests/Listing15.10.Tests.cs b/src/Chapter15.Tests/Listing15.10.Tests.cs index 25fa01349..f047eeb34 100644 --- a/src/Chapter15.Tests/Listing15.10.Tests.cs +++ b/src/Chapter15.Tests/Listing15.10.Tests.cs @@ -10,11 +10,8 @@ public class ProgramTests [TestMethod] public void InvokingDelegateWithoutCheckingForNull() { - string expected = $@" -Keywords: - abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null operator out override object params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unsafe ushort using virtual unchecked void volatile while -Contextual Keywords: - add alias ascending async await by descending dynamic equals from get global group into join let nameof on orderby partial remove select set value var where yield"; + string expected = $@"{Environment.NewLine}Keywords:{Environment.NewLine} abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null operator out override object params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unsafe ushort using virtual unchecked void volatile while{Environment.NewLine}Contextual Keywords:{Environment.NewLine} add alias ascending async await by descending dynamic equals from get global group into join let nameof on orderby partial remove select set value var where yield"; + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, () => diff --git a/src/Chapter15.Tests/Listing15.11.Tests.cs b/src/Chapter15.Tests/Listing15.11.Tests.cs index 1f4ae511d..aafe7de38 100644 --- a/src/Chapter15.Tests/Listing15.11.Tests.cs +++ b/src/Chapter15.Tests/Listing15.11.Tests.cs @@ -10,7 +10,8 @@ public class ProgramTests [TestMethod] public void SelectingAnonymousTypeFollowingGroupClause() { - string expected = $@"{Environment.NewLine}Keywords: + string expected = $@" +Keywords: abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null operator out override object params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unsafe ushort using virtual unchecked void volatile while Contextual Keywords: add alias ascending async await by descending dynamic equals from get global group into join let nameof on orderby partial remove select set value var where yield"; diff --git a/src/Chapter15.Tests/Listing15.12.Tests.cs b/src/Chapter15.Tests/Listing15.12.Tests.cs new file mode 100644 index 000000000..4ff3e304c --- /dev/null +++ b/src/Chapter15.Tests/Listing15.12.Tests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_12.Tests +{ + + [TestClass] + public class ProgramTests + { + [TestMethod] + public void SelectingAnonymousTypeFollowingGroupClause() + { + string expected = $@" +Keywords: + abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null operator out override object params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unsafe ushort using virtual unchecked void volatile while +Contextual Keywords: + add alias ascending async await by descending dynamic equals from get global group into join let nameof on orderby partial remove select set value var where yield"; + + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, + () => + { + Program.ChapterMain(); + }); + } + } +} \ No newline at end of file diff --git a/src/Chapter15.Tests/Listing15.13.Tests.cs b/src/Chapter15.Tests/Listing15.13.Tests.cs index 42d4b3c7b..69def0cc8 100644 --- a/src/Chapter15.Tests/Listing15.13.Tests.cs +++ b/src/Chapter15.Tests/Listing15.13.Tests.cs @@ -1,4 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; +using System; namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_13.Tests { @@ -9,592 +11,22 @@ public class ProgramTests [TestMethod] public void SelectingAnonymousTypeFollowingGroupClause() { - string expected = @"a -b -s -t -r -a -c -t -a -d -d -* -a -l -i -a -s -* -a -s -a -s -c -e -n -d -i -n -g -* -a -s -y -n -c -* -a -w -a -i -t -* -b -a -s -e -b -o -o -l -b -r -e -a -k -b -y -* -b -y -t -e -c -a -s -e -c -a -t -c -h -c -h -a -r -c -h -e -c -k -e -d -c -l -a -s -s -c -o -n -s -t -c -o -n -t -i -n -u -e -d -e -c -i -m -a -l -d -e -f -a -u -l -t -d -e -l -e -g -a -t -e -d -e -s -c -e -n -d -i -n -g -* -d -o -d -o -u -b -l -e -d -y -n -a -m -i -c -* -e -l -s -e -e -n -u -m -e -v -e -n -t -e -q -u -a -l -s -* -e -x -p -l -i -c -i -t -e -x -t -e -r -n -f -a -l -s -e -f -i -n -a -l -l -y -f -i -x -e -d -f -r -o -m -* -f -l -o -a -t -f -o -r -f -o -r -e -a -c -h -g -e -t -* -g -l -o -b -a -l -* -g -r -o -u -p -* -g -o -t -o -i -f -i -m -p -l -i -c -i -t -i -n -i -n -t -i -n -t -o -* -i -n -t -e -r -f -a -c -e -i -n -t -e -r -n -a -l -i -s -l -o -c -k -l -o -n -g -j -o -i -n -* -l -e -t -* -n -a -m -e -o -f -* -n -a -m -e -s -p -a -c -e -n -e -w -n -u -l -l -o -n -* -o -p -e -r -a -t -o -r -o -r -d -e -r -b -y -* -o -u -t -o -v -e -r -r -i -d -e -o -b -j -e -c -t -p -a -r -a -m -s -p -a -r -t -i -a -l -* -p -r -i -v -a -t -e -p -r -o -t -e -c -t -e -d -p -u -b -l -i -c -r -e -a -d -o -n -l -y -r -e -f -r -e -m -o -v -e -* -r -e -t -u -r -n -s -b -y -t -e -s -e -a -l -e -d -s -e -l -e -c -t -* -s -e -t -* -s -h -o -r -t -s -i -z -e -o -f -s -t -a -c -k -a -l -l -o -c -s -t -a -t -i -c -s -t -r -i -n -g -s -t -r -u -c -t -s -w -i -t -c -h -t -h -i -s -t -h -r -o -w -t -r -u -e -t -r -y -t -y -p -e -o -f -u -i -n -t -u -l -o -n -g -u -n -s -a -f -e -u -s -h -o -r -t -u -s -i -n -g -v -a -l -u -e -* -v -a -r -* -v -i -r -t -u -a -l -u -n -c -h -e -c -k -e -d -v -o -i -d -v -o -l -a -t -i -l -e -w -h -e -r -e -* -w -h -i -l -e -y -i -e -l -d -*"; + // Intentionally use something other than select to + // determine the result. + string expected = Program.Keywords.Aggregate("", + (string result, string word) => { + var splitCharactersOntoEachLine = word.Aggregate("", + (string wordResult, char character) => + wordResult + character + Environment.NewLine); + return result + splitCharactersOntoEachLine; + }); + + string nl = Environment.NewLine; + Assert.IsTrue( + expected.StartsWith($"a{nl}b{nl}s{nl}t{nl}r{nl}a{nl}c{nl}t{nl}a")); + Assert.IsTrue( + expected.EndsWith($"e{nl}y{nl}i{nl}e{nl}l{nl}d{nl}*{nl}")); + IntelliTect.TestTools.Console.ConsoleAssert.Expect(expected, () => diff --git a/src/Chapter15.Tests/Listing15.14.Tests.cs b/src/Chapter15.Tests/Listing15.14.Tests.cs new file mode 100644 index 000000000..97ebc8b30 --- /dev/null +++ b/src/Chapter15.Tests/Listing15.14.Tests.cs @@ -0,0 +1,38 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; + +namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_14.Tests +{ + + [TestClass] + public class ProgramTests + { + [TestMethod] + public void SelectingAnonymousTypeFollowingGroupClause() + { + string expected = $@"(abstract, 1) +(abstract, 2) +(abstract, 3) +(add\*, 1) +(add\*, 2) +(add\*, 3) +(alias\*, 1) +* +(where\*, 3) +(while, 1) +(while, 2) +(while, 3) +(yield\*, 1) +(yield\*, 2) +(yield\*, 3)"; + + + + IntelliTect.TestTools.Console.ConsoleAssert.ExpectLike(expected, '\\', + () => + { + Program.ChapterMain(); + }); + } + } +} \ No newline at end of file diff --git a/src/Chapter15/Listing15.13.MultipleSelection.cs b/src/Chapter15/Listing15.13.MultipleSelection.cs index b8cf91ae8..490338b55 100644 --- a/src/Chapter15/Listing15.13.MultipleSelection.cs +++ b/src/Chapter15/Listing15.13.MultipleSelection.cs @@ -23,7 +23,7 @@ from character in word } } - private static string[] Keywords = { + public static string[] Keywords = { "abstract", "add*", "alias*", "as", "ascending*", "async*", "await*", "base","bool", "break", "by*", "byte", "case", "catch", "char", "checked", diff --git a/submodules/TestTools b/submodules/TestTools index 3629b9376..eb6af6d64 160000 --- a/submodules/TestTools +++ b/submodules/TestTools @@ -1 +1 @@ -Subproject commit 3629b9376afb0a40b4f9561e53aaabef87d21c23 +Subproject commit eb6af6d642a8ef0742ee7c3c0499697f991973d9