Skip to content

Commit 8bc6d91

Browse files
Address review findings: fix tests, deduplicate JNI parsing
- Fix UCO attribute test to verify actual UnmanagedCallersOnlyAttribute type name instead of just Assert.NotEmpty(attrs) - Replace non-asserting NegativeEdgeCase Theory with three Facts that each assert a specific expected behavior - Remove duplicate SkipSingleType method; ParseParameterTypeStrings now reuses ParseSingleType to advance the index Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2574cc1 commit 8bc6d91

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/JniSignatureHelper.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static List<string> ParseParameterTypeStrings (string jniSignature)
4141
int i = 1; // skip opening '('
4242
while (i < jniSignature.Length && jniSignature [i] != ')') {
4343
int start = i;
44-
SkipSingleType (jniSignature, ref i);
44+
ParseSingleType (jniSignature, ref i);
4545
result.Add (jniSignature.Substring (start, i - start));
4646
}
4747
return result;
@@ -85,25 +85,6 @@ static JniParamKind ParseSingleType (string sig, ref int i)
8585
}
8686
}
8787

88-
static void SkipSingleType (string sig, ref int i)
89-
{
90-
switch (sig [i]) {
91-
case 'V': case 'Z': case 'B': case 'C': case 'S':
92-
case 'I': case 'J': case 'F': case 'D':
93-
i++;
94-
break;
95-
case 'L':
96-
i = sig.IndexOf (';', i) + 1;
97-
break;
98-
case '[':
99-
i++;
100-
SkipSingleType (sig, ref i);
101-
break;
102-
default:
103-
throw new ArgumentException ($"Unknown JNI type character '{sig [i]}' in '{sig}' at index {i}");
104-
}
105-
}
106-
10788
/// <summary>Encodes the CLR type for a JNI parameter kind into a signature type encoder.</summary>
10889
public static void EncodeClrType (SignatureTypeEncoder encoder, JniParamKind kind)
10990
{

tests/Microsoft.Android.Sdk.TrimmableTypeMap.Tests/Generator/TypeMapAssemblyGeneratorTests.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,16 @@ public void Generate_AcwProxy_HasUnmanagedCallersOnlyAttribute ()
170170
.Select (h => reader.GetMethodDefinition (h))
171171
.First (m => reader.GetString (m.Name).Contains ("_uco_"));
172172

173-
var attrs = ucoMethod.GetCustomAttributes ()
173+
var attrNames = ucoMethod.GetCustomAttributes ()
174174
.Select (h => reader.GetCustomAttribute (h))
175+
.Select (a => {
176+
var ctorHandle = (MemberReferenceHandle) a.Constructor;
177+
var ctor = reader.GetMemberReference (ctorHandle);
178+
var typeRef = reader.GetTypeReference ((TypeReferenceHandle) ctor.Parent);
179+
return $"{reader.GetString (typeRef.Namespace)}.{reader.GetString (typeRef.Name)}";
180+
})
175181
.ToList ();
176-
Assert.NotEmpty (attrs);
182+
Assert.Contains ("System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute", attrNames);
177183
}
178184
}
179185

@@ -321,17 +327,22 @@ public void ParseReturnType_MapsToCorrectKind (string signature, JniParamKind ex
321327
public class NegativeEdgeCase
322328
{
323329

324-
[Theory]
325-
[InlineData ("")]
326-
[InlineData ("not-a-sig")]
327-
[InlineData ("(")]
328-
public void ParseParameterTypes_InvalidSignature_ThrowsOrReturnsEmpty (string signature)
330+
[Fact]
331+
public void ParseParameterTypes_EmptyString_ReturnsEmptyList ()
329332
{
330-
try {
331-
var result = JniSignatureHelper.ParseParameterTypes (signature);
332-
Assert.NotNull (result);
333-
} catch (Exception ex) when (ex is ArgumentException || ex is IndexOutOfRangeException || ex is FormatException) {
333+
Assert.Empty (JniSignatureHelper.ParseParameterTypes (""));
334334
}
335+
336+
[Fact]
337+
public void ParseParameterTypes_InvalidSignature_Throws ()
338+
{
339+
Assert.ThrowsAny<ArgumentException> (() => JniSignatureHelper.ParseParameterTypes ("not-a-sig"));
340+
}
341+
342+
[Fact]
343+
public void ParseParameterTypes_UnterminatedSignature_ReturnsEmptyList ()
344+
{
345+
Assert.Empty (JniSignatureHelper.ParseParameterTypes ("("));
335346
}
336347

337348
}

0 commit comments

Comments
 (0)