Skip to content

Commit 778c004

Browse files
nohwndEvangelink
andauthored
Add letter number among valid identifiers in class name (#13868)
* Add letter number among valid identifiers in class name * Update src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/ManagedNameHelper.Reflection.cs Co-authored-by: Amaury Levé <[email protected]> * Update test/Microsoft.TestPlatform.AdapterUtilities.UnitTests/TestClasses.cs Co-authored-by: Amaury Levé <[email protected]> * Update test/Microsoft.TestPlatform.AdapterUtilities.UnitTests/ManagedNameUtilities/ManagedNameGeneratorTests.cs Co-authored-by: Amaury Levé <[email protected]> --------- Co-authored-by: Amaury Levé <[email protected]>
1 parent d8aa584 commit 778c004

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Microsoft.TestPlatform.AdapterUtilities/ManagedNameUtilities/ManagedNameHelper.Reflection.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,17 @@ private static bool NeedsEscaping(char c, int pos)
563563
}
564564

565565
if (c == '_'
566-
|| char.IsLetterOrDigit(c) // Lu, Ll, Lt, Lm, Lo, or Nl
566+
// 'Digit' does not include letter numbers, which are valid identifiers as per docs https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/identifier-names'.
567+
|| char.IsLetterOrDigit(c) // Lu, Ll, Lt, Lm, Lo, or Nd
567568
)
568569
{
569570
return false;
570571
}
571572

572573
var category = CharUnicodeInfo.GetUnicodeCategory(c);
573574
return category
574-
is not UnicodeCategory.NonSpacingMark // Mn
575+
is not UnicodeCategory.LetterNumber // Nl
576+
and not UnicodeCategory.NonSpacingMark // Mn
575577
and not UnicodeCategory.SpacingCombiningMark // Mc
576578
and not UnicodeCategory.ConnectorPunctuation // Pc
577579
and not UnicodeCategory.Format; // Cf

test/Microsoft.TestPlatform.AdapterUtilities.UnitTests/ManagedNameUtilities/ManagedNameGeneratorTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
using Microsoft.VisualStudio.TestTools.UnitTesting;
55

6+
using TestClasses;
7+
68
namespace Microsoft.TestPlatform.AdapterUtilities.ManagedNameUtilities.UnitTests;
79

810
[TestClass]
@@ -123,4 +125,18 @@ public void Namespaceless_InnerRecordMembers_ShouldNotReportANamespace_InHierarc
123125
Assert.AreEqual("Method0", managedMethodName);
124126
Assert.IsNull(hierarchyValues[HierarchyConstants.Levels.NamespaceIndex]);
125127
}
128+
129+
[TestMethod]
130+
public void SpecialCharacters_HierarchyShouldNotWrapMembersWithSpecialCharactersInSingleQuotes()
131+
{
132+
var methodBase = typeof(Class狧麱狵錋狾龍龪啊阿埃挨哎唉0u㐀㐁㐂㐃㐄㐅㐆㐇6ⅶ0ǒoU1U2U38丂丄丅丆丏丒丟).GetMethod("Method0")!;
133+
134+
// Act
135+
ManagedNameHelper.GetManagedName(methodBase, out var managedTypeName, out var managedMethodName, out var _);
136+
137+
// Assert
138+
Assert.AreEqual("TestClasses.Class狧麱狵錋狾龍龪啊阿埃挨哎唉0u㐀㐁㐂㐃㐄㐅㐆㐇6ⅶ0ǒoU1U2U38丂丄丅丆丏丒丟", managedTypeName);
139+
Assert.AreEqual("Method0", managedMethodName);
140+
}
141+
126142
}

test/Microsoft.TestPlatform.AdapterUtilities.UnitTests/TestClasses.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,17 @@ public void Overload0(Tuple<Tuple<string[,], int>> t0) { }
133133
public void Overload0(Tuple<Tuple<string>, Tuple<int>> t) { }
134134
public void Overload0<U>(Tuple<Tuple<Outer<U>.Inner<U>>> t) { }
135135
}
136+
137+
public class Class狧麱狵錋狾龍龪啊阿埃挨哎唉0u㐀㐁㐂㐃㐄㐅㐆㐇6ⅶ0ǒoU1U2U38丂丄丅丆丏丒丟
138+
{
139+
public void Method0()
140+
{
141+
142+
}
143+
}
136144
}
137145

146+
138147
#pragma warning restore IDE0161 // Convert to file-scoped namespace
139148
#pragma warning restore IDE0060 // Remove unused parameter
140149
#pragma warning restore CA1822 // Mark members as static

0 commit comments

Comments
 (0)