From ce8a171d8eba18282f4a2d38ac8c828ace1e1bef Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Fri, 21 Apr 2023 15:09:37 +1000 Subject: [PATCH 1/5] Fixing null handling for parameters --- .../MSTestSnapshotFullNameReader.cs | 2 +- .../MSTestSnapshotFullNameReaderTests.cs | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs index 0fb8d3a..27a4763 100644 --- a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs +++ b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs @@ -132,7 +132,7 @@ private static string GetMethodSnapshotName(MethodBase method) return $"{method.DeclaringType.Name}." + method.Name + - $"_{string.Join("_", currentRow.Data.Select(d => d.ToString()))}"; + $"_{string.Join("_", currentRow.Data.Select(d => d is null ? "null" : d.ToString()))}"; } } } diff --git a/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs b/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs index d86f322..c7cf8fd 100644 --- a/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs +++ b/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs @@ -118,6 +118,28 @@ 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)}"); + } + private static object[] TestCases => new object[] { new object[] { "testString1", 5 }, From 2c7fd9c9805e659d226f24ce18c6272cf81e088c Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Sat, 22 Apr 2023 11:43:20 +1000 Subject: [PATCH 2/5] Adding handling for enumerables as well --- .../MSTestSnapshotFullNameReader.cs | 10 ++++++++- .../MSTestSnapshotFullNameReaderTests.cs | 22 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs index 27a4763..4d83f6c 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; @@ -132,7 +133,14 @@ private static string GetMethodSnapshotName(MethodBase method) return $"{method.DeclaringType.Name}." + method.Name + - $"_{string.Join("_", currentRow.Data.Select(d => d is null ? "null" : 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/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs b/test/Snapshooter.MSTest.Tests/MSTestSnapshotFullNameReaderTests.cs index c7cf8fd..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; @@ -140,6 +141,27 @@ public async Task ReadSnapshotFullName_ResolveTheorySnapshotNameAsync_NameResolv $"_{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 }, From 55ebc4f3ec544ffaaf60a53b5e3bf2a8781b3120 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Sat, 22 Apr 2023 13:56:03 +1000 Subject: [PATCH 3/5] using the provided test name if it exists rather than generating one. --- src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs index 4d83f6c..9708f2e 100644 --- a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs +++ b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs @@ -116,6 +116,14 @@ private static string GetMethodSnapshotName(MethodBase method) return method.ToName(); } + DataTestMethodAttribute dataTestMethodAttribute = + method.GetCustomAttribute(); + + if (!string.IsNullOrWhiteSpace(dataTestMethodAttribute.DisplayName)) + { + return $"{method.DeclaringType.Name}.{dataTestMethodAttribute.DisplayName}"; + } + if (!dataTestMethodRowIndex.ContainsKey(method.Name)) { dataTestMethodRowIndex[method.Name] = 0; From 55062931fc0fbc4d29d4d7c4954ef845b59151c0 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Sat, 22 Apr 2023 13:58:19 +1000 Subject: [PATCH 4/5] Sanitising the generated file name by removing illegal chars --- src/Snapshooter/SnapshotFullName.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) 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; } From b97efbd8c748141d6f2f9af8fb36dbe4a949c031 Mon Sep 17 00:00:00 2001 From: Aaron Powell Date: Sat, 22 Apr 2023 14:05:56 +1000 Subject: [PATCH 5/5] Should use the DisplayName of the current row --- .../MSTestSnapshotFullNameReader.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs index 9708f2e..6bb849e 100644 --- a/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs +++ b/src/Snapshooter.MSTest/MSTestSnapshotFullNameReader.cs @@ -116,14 +116,6 @@ private static string GetMethodSnapshotName(MethodBase method) return method.ToName(); } - DataTestMethodAttribute dataTestMethodAttribute = - method.GetCustomAttribute(); - - if (!string.IsNullOrWhiteSpace(dataTestMethodAttribute.DisplayName)) - { - return $"{method.DeclaringType.Name}.{dataTestMethodAttribute.DisplayName}"; - } - if (!dataTestMethodRowIndex.ContainsKey(method.Name)) { dataTestMethodRowIndex[method.Name] = 0; @@ -139,6 +131,11 @@ 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(ParamDataFormatter))}";