Skip to content
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
71 changes: 71 additions & 0 deletions UnityXrefMaps.Tests/XRefHrefFixerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,76 @@ public void Package_Fix(string apiUrl, string name, string commentId, string exp

Assert.Equal(expected, XRefHrefFixer.Fix(apiUrl, xrefMapReference, Array.Empty<string>(), true));
}

/// <summary>
/// The regex that extracts the bare method name requires a '(' character to match.
/// When the display name has no parentheses — e.g. Name = "MyMethod" — the regex
/// fails, producing an empty string. The code then called uid.Substring(0, -1),
/// which throws ArgumentOutOfRangeException.
/// </summary>
[Fact]
public void Fix_PackageMethodWithNoParenthesesInName_DoesNotThrow()
{
XrefMapReference xrefMapReference = new()
{
CommentId = "M:MyNamespace.MyClass.MyMethod",
Name = "MyMethod",
};

string result = XRefHrefFixer.Fix(
"https://docs.unity3d.com/Packages/test@1.0/api/",
xrefMapReference,
Array.Empty<string>(),
isPackage: true);

Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
}

/// <summary>
/// When the method name extracted from the display name — e.g. "DifferentMethod" from
/// "DifferentMethod()" — does not appear in the uid, e.g.
/// "MyNamespace.MyClass.ActualMethod", IndexOf returns -1. The code then called
/// uid.Substring(0, -2), which throws ArgumentOutOfRangeException.
/// </summary>
[Fact]
public void Fix_PackageMethodNameAbsentFromUid_DoesNotThrow()
{
XrefMapReference xrefMapReference = new()
{
CommentId = "M:MyNamespace.MyClass.ActualMethod",
Name = "DifferentMethod()",
};

string result = XRefHrefFixer.Fix(
"https://docs.unity3d.com/Packages/test@1.0/api/",
xrefMapReference,
Array.Empty<string>(),
isPackage: true);

Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
}

/// <summary>
/// When the property name — e.g. "DifferentProperty" — does not appear in the uid,
/// e.g. "MyNamespace.MyClass.ActualProperty", IndexOf returns -1. The code then called
/// uid.Substring(0, -2), which throws ArgumentOutOfRangeException.
/// </summary>
[Fact]
public void Fix_PackagePropertyNameAbsentFromUid_DoesNotThrow()
{
XrefMapReference xrefMapReference = new()
{
CommentId = "P:MyNamespace.MyClass.ActualProperty",
Name = "DifferentProperty",
};

string result = XRefHrefFixer.Fix(
"https://docs.unity3d.com/Packages/test@1.0/api/",
xrefMapReference,
Array.Empty<string>(),
isPackage: true);

Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
}
}
}
21 changes: 18 additions & 3 deletions UnityXrefMaps/XRefHrefFixer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,30 @@ private static string FixPackage(string apiUrl, string name, string type, string
case "M":
Match methodNameMatch = MethodNameRegex().Match(name);

string methodName = name.Substring(0, methodNameMatch.Groups[1].Value.Length);
// regex requires '(' — fall back to the full name when it does not match
string methodName = methodNameMatch.Success
? name.Substring(0, methodNameMatch.Groups[1].Value.Length)
: name;

classFullName = uid.Substring(0, uid.IndexOf(methodName) - 1);
int methodIdx = uid.IndexOf(methodName);
if (methodIdx < 0)
{
return $"{apiUrl}{NonWordCharRegex().Replace(uid, "_")}.html";
}

classFullName = uid.Substring(0, methodIdx - 1);

return $"{apiUrl}{classFullName}.html#{NonWordCharRegex().Replace(uid, "_")}";
case "P":
case "F":
default:
classFullName = uid.Substring(0, uid.IndexOf(name) - 1);
int nameIdx = uid.IndexOf(name);
if (nameIdx < 0)
{
return $"{apiUrl}{NonWordCharRegex().Replace(uid, "_")}.html";
}

classFullName = uid.Substring(0, nameIdx - 1);

return $"{apiUrl}{classFullName}.html#{NonWordCharRegex().Replace(uid, "_")}";
}
Expand Down
Loading