Skip to content

Commit

Permalink
Fixed MS Test Issue and add cross platform for new lines.
Browse files Browse the repository at this point in the history
-Fixed to handle cross platform new lines;
-Addressed MSTest but which required setting the initial directory to be the location of the assembly (required due to bug - see See microsoft/vstest#311)
  • Loading branch information
MarkMichaelis committed Aug 3, 2018
1 parent 477afd8 commit 412a52b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
Binary file modified src/Chapter14.Tests/Chapter14.Tests.csproj
Binary file not shown.
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.Reflection;
using System;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_15.Tests
{
Expand All @@ -12,21 +14,30 @@ public class ProgramTests
[TestMethod]
public void Listing14_15_Test()
{
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.
Directory.SetCurrentDirectory(Path.GetDirectoryName(
typeof(Program).GetTypeInfo().Assembly.Location));

//string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{Path.DirectorySeparatorChar}*";
string expectedPattern = $@"{ Directory.GetCurrentDirectory() }{
$"{Path.DirectorySeparatorChar}"}*";
int expectedItemCount = Directory.EnumerateFiles(
Directory.GetCurrentDirectory(), "*").Count();
string output = ConsoleAssert.Execute(null, () =>
{
Program.ChapterMain();
});

IEnumerable<string> outputItems = output.Split('\n');
IEnumerable<string> outputItems = output.Split(
new string[] { Environment.NewLine }, StringSplitOptions.None);

Assert.AreEqual<int>(
expectedItemCount, outputItems.Count());
foreach (string item in outputItems)
{
Assert.IsTrue(item.IsLike(expectedPattern));
Assert.IsTrue(item.IsLike(expectedPattern, '?'));
}
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/Chapter14.Tests/Listing14.16.ProjectionToTuple.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System;

namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter14.Listing14_16.Tests
{
Expand All @@ -12,7 +13,7 @@ public class ProgramTests
[TestMethod]
public void ProjectionToAnAnonymousType()
{
string expectedPattern = "FileName = *, Size = ";
string expectedPattern = "FileName = *, Size = *";
int expectedItemCount = Directory.EnumerateFiles(
Directory.GetCurrentDirectory(), "*").Count();

Expand All @@ -21,12 +22,14 @@ public void ProjectionToAnAnonymousType()
Program.ChapterMain();
});

IEnumerable<string> outputItems = output.Split('\n');
IEnumerable<string> outputItems = output.Split(
new string[] { Environment.NewLine }, StringSplitOptions.None);

Assert.AreEqual(expectedItemCount, outputItems.Count());
foreach (string item in outputItems)
{
Assert.IsTrue(item.IsLike(expectedPattern));
Assert.IsTrue(item.IsLike(expectedPattern),
$"{item} is not like {expectedPattern}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@ public class ProgramTests
[TestMethod]
public void ProjectionWithLinqsSelect()
{
string expectedPattern = "{ FileName = *, Size = ";
string expectedPattern = "{ FileName = *, Size = * }";
int expectedItemCount = Directory.EnumerateFiles(
Directory.GetCurrentDirectory(), "*").Count();
string output = ConsoleAssert.Execute(null, () =>
{
Program.ChapterMain();
});

IEnumerable<string> outputItems = output.Split('\n');
IEnumerable<string> outputItems = output.Split(
new string[] { Environment.NewLine }, StringSplitOptions.None);

Assert.AreEqual(expectedItemCount, outputItems.Count());
foreach (string item in outputItems)
{
Assert.IsTrue(item.IsLike(expectedPattern));
Assert.IsTrue(item.IsLike(expectedPattern),
$"{item} is not like {expectedPattern}");
}
}
}
Expand Down

0 comments on commit 412a52b

Please sign in to comment.