diff --git a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs index 0fb8d3a..6bb849e 100644 --- a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs +++ b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; @@ -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)d)}]", + _ => data.ToString() + }; } } diff --git a/src/Snapshooter/SnapshotFullName.cs b/src/Snapshooter/SnapshotFullName.cs index ab588e2..86c9640 100644 --- a/src/Snapshooter/SnapshotFullName.cs +++ b/src/Snapshooter/SnapshotFullName.cs @@ -1,4 +1,6 @@ -namespace Snapshooter +using System.IO; + +namespace Snapshooter { /// /// The snapshot full name instance contains the file name and the path @@ -14,6 +16,13 @@ public class SnapshotFullName /// The folder path. public SnapshotFullName(string fileName, string folderPath) { + char[] invalidChars = Path.GetInvalidFileNameChars(); + + foreach (char invalidChar in invalidChars) + { + fileName = fileName.Replace(invalidChar, '_'); + } + Filename = fileName; FolderPath = folderPath; } diff --git a/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs b/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs index d86f322..06e2777 100644 --- a/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs +++ b/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs @@ -1,3 +1,4 @@ +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -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)param)}]"); + } + private static object[] TestCases => new object[] { new object[] { "testString1", 5 },