Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving name generation in MSTest #179

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -130,9 +131,21 @@ private static string GetMethodSnapshotName(MethodBase method)
DataRowAttribute currentRow =
dataRowAttributes.ElementAt(dataTestMethodRowIndex[method.Name]);

if (!string.IsNullOrEmpty(currentRow.DisplayName))
{
return $"{method.DeclaringType.Name}.{currentRow.DisplayName}";
}

return $"{method.DeclaringType.Name}." +
method.Name +
$"_{string.Join("_", currentRow.Data.Select(d => d.ToString()))}";
$"_{string.Join("_", currentRow.Data.Select(ParamDataFormatter))}";
}

private static string ParamDataFormatter(object data) => data switch
{
var d when d is null => "null",
var d when d is IEnumerable => $"[{string.Join("_", (IEnumerable<object>)d)}]",
_ => data.ToString()
};
}
}
11 changes: 10 additions & 1 deletion src/Snapshooter/SnapshotFullName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Snapshooter
using System.IO;

namespace Snapshooter
{
/// <summary>
/// The snapshot full name instance contains the file name and the path
Expand All @@ -14,6 +16,13 @@ public class SnapshotFullName
/// <param name="folderPath">The folder path.</param>
public SnapshotFullName(string fileName, string folderPath)
{
char[] invalidChars = Path.GetInvalidFileNameChars();

foreach (char invalidChar in invalidChars)
{
fileName = fileName.Replace(invalidChar, '_');
}

Filename = fileName;
FolderPath = folderPath;
}
Expand Down
44 changes: 44 additions & 0 deletions test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -118,6 +119,49 @@ public async Task ReadSnapshotFullName_ResolveTheoryDataSnapshotNameAsync_NameRe
$"_{param1}_{param2}");
}

[DataTestMethod]
[DataRow("testString1", 5)]
[DataRow("testString2", 6)]
[DataRow("testString3", null)]
public async Task ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolvedWithNullParameters(
string param1, int? param2)
{
// arrange
var snapshotFullNameResolver = new MSTestSnapshotFullNameReader();
await Task.Delay(1);

// act
SnapshotFullName snapshotFullName = snapshotFullNameResolver.ReadSnapshotFullName();

// assert
await Task.Delay(1);
Assert.AreEqual(snapshotFullName.Filename,
$"{nameof(MSTestSnapshotFullNameReaderTests)}." +
$"{nameof(ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolvedWithNullParameters)}" +
$"_{param1}_{(param2 is null ? "null" : param2)}");
}

[DataTestMethod]
[DataRow(new string[] { })]
[DataRow(new string[] { "a" })]
public async Task ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolvedCollectionParameters(
object param)
{
// arrange
var snapshotFullNameResolver = new MSTestSnapshotFullNameReader();
await Task.Delay(1);

// act
SnapshotFullName snapshotFullName = snapshotFullNameResolver.ReadSnapshotFullName();

// assert
await Task.Delay(1);
Assert.AreEqual(snapshotFullName.Filename,
$"{nameof(MSTestSnapshotFullNameReaderTests)}." +
$"{nameof(ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolvedCollectionParameters)}" +
$"_[{string.Join("_", (IEnumerable<object>)param)}]");
}

private static object[] TestCases => new object[]
{
new object[] { "testString1", 5 },
Expand Down
Loading