Skip to content

Commit 55cd802

Browse files
committed
Attempt to write tests for ArrayParameterTransformer
1 parent d6f2401 commit 55cd802

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Microsoft.CodeAnalysis.CSharp.Syntax;
5+
using Silk.NET.SilkTouch.Mods.Transformation;
6+
7+
namespace Silk.NET.SilkTouch.UnitTests;
8+
9+
public class DummyTransformationContext : ITransformationContext
10+
{
11+
public string? JobKey { get; set; } = "Test";
12+
public MethodDeclarationSyntax? Original { get; set; }
13+
public IFunctionTransformer[]? Transformers { get; set; } = [];
14+
public Predicate<UsingDirectiveSyntax> OnAddUsing = _ => true;
15+
16+
public bool AddUsing(UsingDirectiveSyntax use) => OnAddUsing.Invoke(use);
17+
}
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Diagnostics.CodeAnalysis;
5+
using Microsoft.CodeAnalysis;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.CodeAnalysis.CSharp.Syntax;
8+
using Silk.NET.SilkTouch.Mods.Metadata;
9+
using Silk.NET.SilkTouch.Mods.Transformation;
10+
11+
namespace Silk.NET.SilkTouch.UnitTests.FunctionTransformation;
12+
13+
public class ArrayParameterTransformerTests
14+
{
15+
[Test]
16+
public async Task SingularizeAffixedName_ShouldSingularizeBaseName()
17+
{
18+
var method = (MethodDeclarationSyntax)
19+
SyntaxFactory.ParseMemberDeclaration(
20+
"""
21+
[NameAffix("Suffix", "KhronosNonVendor", "Direct")]
22+
[NativeFunction("opengl", EntryPoint = "alDeleteAuxiliaryEffectSlotsDirect")]
23+
public static void alDeleteAuxiliaryEffectSlotsDirect(ContextHandle context, int n, uint* effectslots) { }
24+
"""
25+
)!;
26+
27+
var transformer = new ArrayParameterTransformer();
28+
var context = new DummyTransformationContext()
29+
{
30+
Transformers =
31+
[
32+
new SingularizeAffixedName_ShouldSingularizeBaseName_MetadataProvider(),
33+
],
34+
};
35+
36+
var results = new List<MethodDeclarationSyntax>();
37+
transformer.Transform(
38+
method,
39+
context,
40+
result =>
41+
{
42+
results.Add(result);
43+
}
44+
);
45+
46+
// "Slots" should be pluralized as "Slot"
47+
await Verify(results.Select(result => result.NormalizeWhitespace().ToString()));
48+
}
49+
50+
private class SingularizeAffixedName_ShouldSingularizeBaseName_MetadataProvider
51+
: IApiMetadataProvider<SymbolConstraints>,
52+
IFunctionTransformer
53+
{
54+
public bool TryGetChildSymbolMetadata(
55+
string? jobKey,
56+
string nativeName,
57+
string childNativeName,
58+
[NotNullWhen(true)] out SymbolConstraints? metadata
59+
)
60+
{
61+
if (childNativeName == "effectslots")
62+
{
63+
metadata = MetadataUtils.CreateBasicSymbolConstraints(
64+
["n"],
65+
[true, false],
66+
false,
67+
false,
68+
0
69+
);
70+
}
71+
72+
metadata = null;
73+
return false;
74+
}
75+
76+
public void Transform(
77+
MethodDeclarationSyntax current,
78+
ITransformationContext ctx,
79+
Action<MethodDeclarationSyntax> next
80+
) => next(current);
81+
}
82+
83+
[Test]
84+
public async Task SingularizeAffixedName_ShouldNotAffectAffix()
85+
{
86+
var method = (MethodDeclarationSyntax)
87+
SyntaxFactory.ParseMemberDeclaration(
88+
"""
89+
[NameAffix("Suffix", "KhronosVendor", "OES")]
90+
[NativeFunction("opengl", EntryPoint = "glFeedbackBufferxOES")]
91+
public static void glFeedbackBufferxOES(uint n, uint type, int* buffer) { }
92+
"""
93+
)!;
94+
95+
var transformer = new ArrayParameterTransformer();
96+
var context = new DummyTransformationContext()
97+
{
98+
Transformers = [new SingularizeAffixedName_ShouldNotAffectAffix_MetadataProvider()],
99+
};
100+
101+
var results = new List<MethodDeclarationSyntax>();
102+
transformer.Transform(
103+
method,
104+
context,
105+
result =>
106+
{
107+
results.Add(result);
108+
}
109+
);
110+
111+
// The "OES" suffix should not be singularized as "O"
112+
await Verify(results.Select(result => result.NormalizeWhitespace().ToString()));
113+
}
114+
115+
private class SingularizeAffixedName_ShouldNotAffectAffix_MetadataProvider
116+
: IApiMetadataProvider<SymbolConstraints>,
117+
IFunctionTransformer
118+
{
119+
public bool TryGetChildSymbolMetadata(
120+
string? jobKey,
121+
string nativeName,
122+
string childNativeName,
123+
[NotNullWhen(true)] out SymbolConstraints? metadata
124+
)
125+
{
126+
if (childNativeName == "buffer")
127+
{
128+
metadata = MetadataUtils.CreateBasicSymbolConstraints(
129+
["n"],
130+
[true, false],
131+
false,
132+
false,
133+
0
134+
);
135+
}
136+
137+
metadata = null;
138+
return false;
139+
}
140+
141+
public void Transform(
142+
MethodDeclarationSyntax current,
143+
ITransformationContext ctx,
144+
Action<MethodDeclarationSyntax> next
145+
) => next(current);
146+
}
147+
}

0 commit comments

Comments
 (0)