Skip to content

Commit b88e5d8

Browse files
committed
Squashed commit of the following:
commit 13175cf Author: smdn <smdn@smdn.jp> Date: Mon Mar 7 23:07:28 2022 +0900 fix expected value running on .NET Framework commit bf4d129 Author: smdn <smdn@smdn.jp> Date: Mon Mar 7 23:06:47 2022 +0900 add support for generating expected value from field, method or property Merge remote-tracking branch 'refs/remotes/origin/main' into main
2 parents 614b913 + 13175cf commit b88e5d8

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

tests/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating.Tests.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,17 @@ SPDX-License-Identifier: MIT
88
<RootNamespace>Smdn.Reflection.ReverseGenerating</RootNamespace>
99
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1010
</PropertyGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\Smdn.Reflection.ReverseGenerating\Smdn.Reflection.ReverseGenerating.csproj" />
14+
<PackageReference
15+
Include="System.Runtime.InteropServices.RuntimeInformation"
16+
Version="4.3.0"
17+
Condition = "(
18+
$(TargetFramework.StartsWith('net45')) or
19+
$(TargetFramework.StartsWith('net46')) or
20+
$(TargetFramework.StartsWith('net47'))
21+
)"
22+
/>
23+
</ItemGroup>
1124
</Project>

tests/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.Attributes.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,25 @@ class AttributeTargetsMember {
4646

4747
class AttributeTargetsReturnValue {
4848
#if NETFRAMEWORK
49-
[return: AttributeTestCase("[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]")]
49+
static string M0_ExpectedResult =>
50+
RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework", StringComparison.Ordinal)
51+
? "[return: System.Runtime.InteropServices.MarshalAs(" +
52+
"System.Runtime.InteropServices.UnmanagedType.Bool, "+
53+
"ArraySubType = default(System.Runtime.InteropServices.UnmanagedType), " +
54+
"SizeParamIndex = 0, " +
55+
"SizeConst = 0, " +
56+
"IidParameterIndex = 0, " +
57+
"SafeArraySubType = System.Runtime.InteropServices.VarEnum.VT_EMPTY" +
58+
")]"
59+
: "[return: System.Runtime.InteropServices.MarshalAs(" +
60+
"System.Runtime.InteropServices.UnmanagedType.Bool)" +
61+
"]";
62+
63+
[return: AttributeTestCase(
64+
expected: null,
65+
ExpectedValueGeneratorType = typeof(AttributeTargetsReturnValue),
66+
ExpectedValueGeneratorMemberName = nameof(M0_ExpectedResult)
67+
)]
5068
[return: MarshalAs(UnmanagedType.Bool)]
5169
public bool M0() => throw new NotImplementedException();
5270
#endif

tests/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/Generator.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
namespace Smdn.Reflection.ReverseGenerating;
1111

1212
public abstract class GeneratorTestCaseAttribute : Attribute {
13-
public string Expected { get; private set; }
13+
private readonly string expectedValue;
14+
public Type ExpectedValueGeneratorType { get; set; } = null;
15+
public string ExpectedValueGeneratorMemberName { get; set; } = null;
1416
public bool TranslateLanguagePrimitiveTypeDeclaration { get; set; } = true;
1517
public bool MemberWithNamespace { get; set; } = true;
1618
public bool MemberWithDeclaringTypeName { get; set; } = false;
@@ -29,13 +31,34 @@ public abstract class GeneratorTestCaseAttribute : Attribute {
2931
public MethodBodyOption MethodBody { get; set; } = MethodBodyOption.EmptyImplementation;
3032
public string SourceLocation { get; }
3133

34+
public string Expected {
35+
get {
36+
if (ExpectedValueGeneratorType is null && ExpectedValueGeneratorMemberName is null)
37+
return expectedValue;
38+
39+
var expectedValueGeneratorMember = ExpectedValueGeneratorType.GetMember(
40+
ExpectedValueGeneratorMemberName,
41+
MemberTypes.Field | MemberTypes.Method | MemberTypes.Property,
42+
BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic
43+
).FirstOrDefault();
44+
45+
return expectedValueGeneratorMember switch {
46+
FieldInfo f => (string)f.GetValue(obj: null),
47+
MethodInfo m => (string)m.Invoke(obj: null, parameters: null),
48+
PropertyInfo p => (string)p.GetGetMethod(nonPublic: true)?.Invoke(obj: null, parameters: null),
49+
null => throw new InvalidOperationException($"member not found: {ExpectedValueGeneratorType.FullName}.{ExpectedValueGeneratorMemberName}"),
50+
_ => throw new InvalidOperationException($"invalid member type: {ExpectedValueGeneratorType.FullName}.{ExpectedValueGeneratorMemberName}"),
51+
};
52+
}
53+
}
54+
3255
public GeneratorTestCaseAttribute(
3356
string expected,
3457
string sourceFilePath,
3558
int lineNumber
3659
)
3760
{
38-
this.Expected = expected;
61+
this.expectedValue = expected;
3962
this.SourceLocation = $"{Path.GetFileName(sourceFilePath)}:{lineNumber}";
4063
}
4164
}

0 commit comments

Comments
 (0)