Skip to content

Commit 55a6f48

Browse files
committed
test: add crash-reproduction tests for XRefHrefFixer.FixPackage
Three tests that crash before the fix is applied: - Fix_PackageMethodWithNoParenthesesInName_DoesNotThrow: name has no '(' so MethodNameRegex fails to match, methodName becomes "", uid.IndexOf("") == 0, uid.Substring(0, -1) throws. - Fix_PackageMethodNameAbsentFromUid_DoesNotThrow: methodName is not found in uid, IndexOf returns -1, uid.Substring(0, -2) throws. - Fix_PackagePropertyNameAbsentFromUid_DoesNotThrow: property name is not found in uid, IndexOf returns -1, uid.Substring(0, -2) throws. https://claude.ai/code/session_01WNaTJnpDwNjqyfL1uhYqJ4
1 parent d1c2423 commit 55a6f48

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

UnityXrefMaps.Tests/XRefHrefFixerTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,76 @@ public void Package_Fix(string apiUrl, string name, string commentId, string exp
7272

7373
Assert.Equal(expected, XRefHrefFixer.Fix(apiUrl, xrefMapReference, Array.Empty<string>(), true));
7474
}
75+
76+
/// <summary>
77+
/// The regex that extracts the bare method name requires a '(' character to match.
78+
/// When the display name has no parentheses — e.g. Name = "MyMethod" — the regex
79+
/// fails, producing an empty string. The code then called uid.Substring(0, -1),
80+
/// which throws ArgumentOutOfRangeException.
81+
/// </summary>
82+
[Fact]
83+
public void Fix_PackageMethodWithNoParenthesesInName_DoesNotThrow()
84+
{
85+
XrefMapReference xrefMapReference = new()
86+
{
87+
CommentId = "M:MyNamespace.MyClass.MyMethod",
88+
Name = "MyMethod",
89+
};
90+
91+
string result = XRefHrefFixer.Fix(
92+
"https://docs.unity3d.com/Packages/test@1.0/api/",
93+
xrefMapReference,
94+
Array.Empty<string>(),
95+
isPackage: true);
96+
97+
Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
98+
}
99+
100+
/// <summary>
101+
/// When the method name extracted from the display name — e.g. "DifferentMethod" from
102+
/// "DifferentMethod()" — does not appear in the uid, e.g.
103+
/// "MyNamespace.MyClass.ActualMethod", IndexOf returns -1. The code then called
104+
/// uid.Substring(0, -2), which throws ArgumentOutOfRangeException.
105+
/// </summary>
106+
[Fact]
107+
public void Fix_PackageMethodNameAbsentFromUid_DoesNotThrow()
108+
{
109+
XrefMapReference xrefMapReference = new()
110+
{
111+
CommentId = "M:MyNamespace.MyClass.ActualMethod",
112+
Name = "DifferentMethod()",
113+
};
114+
115+
string result = XRefHrefFixer.Fix(
116+
"https://docs.unity3d.com/Packages/test@1.0/api/",
117+
xrefMapReference,
118+
Array.Empty<string>(),
119+
isPackage: true);
120+
121+
Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
122+
}
123+
124+
/// <summary>
125+
/// When the property name — e.g. "DifferentProperty" — does not appear in the uid,
126+
/// e.g. "MyNamespace.MyClass.ActualProperty", IndexOf returns -1. The code then called
127+
/// uid.Substring(0, -2), which throws ArgumentOutOfRangeException.
128+
/// </summary>
129+
[Fact]
130+
public void Fix_PackagePropertyNameAbsentFromUid_DoesNotThrow()
131+
{
132+
XrefMapReference xrefMapReference = new()
133+
{
134+
CommentId = "P:MyNamespace.MyClass.ActualProperty",
135+
Name = "DifferentProperty",
136+
};
137+
138+
string result = XRefHrefFixer.Fix(
139+
"https://docs.unity3d.com/Packages/test@1.0/api/",
140+
xrefMapReference,
141+
Array.Empty<string>(),
142+
isPackage: true);
143+
144+
Assert.StartsWith("https://docs.unity3d.com/Packages/test@1.0/api/", result);
145+
}
75146
}
76147
}

0 commit comments

Comments
 (0)