Skip to content

Commit

Permalink
Addressed cross platform new line isses and handled MSTest defect See m…
Browse files Browse the repository at this point in the history
…icrosoft/vstest#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 microsoft/vstest#311
  • Loading branch information
MarkMichaelis committed Aug 3, 2018
1 parent acff111 commit f61ad94
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 603 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -12,16 +14,25 @@ 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, () =>
{
Program.ChapterMain();
});

IEnumerable<string> outputItems = output.Split('\n');
IEnumerable<string> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,36 @@
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
{
[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<string> outputItems = output.Split('\n');
IEnumerable<string> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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, () =>
Expand All @@ -21,7 +30,7 @@ public void ProjectionWithLinqsSelect()

IEnumerable<string> outputItems = output.Split('\n');

Assert.AreEqual(14, outputItems.Count());
Assert.AreEqual(expectedItemCount, outputItems.Count());
foreach (string item in outputItems)
{
Assert.IsTrue(item.IsLike(expectedPattern));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter15.Listing15_08.Tests
{
Expand All @@ -13,19 +14,29 @@ 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, () =>
{
Program.ChapterMain();
});

IEnumerable<string> outputItems = output.Split('\n');
IEnumerable<string> 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}");
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Chapter15.Tests/Listing15.10.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
() =>
Expand Down
3 changes: 2 additions & 1 deletion src/Chapter15.Tests/Listing15.11.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
26 changes: 26 additions & 0 deletions src/Chapter15.Tests/Listing15.12.Tests.cs
Original file line number Diff line number Diff line change
@@ -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();
});
}
}
}
Loading

0 comments on commit f61ad94

Please sign in to comment.