Skip to content

Commit 73bca45

Browse files
committed
Use more efficient name creation and update testing framework
1 parent 87c9245 commit 73bca45

File tree

12 files changed

+126
-405
lines changed

12 files changed

+126
-405
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6588,12 +6588,11 @@ private bool TryRemapOperatorName(ref string name, FunctionDecl functionDecl)
65886588
}
65896589
var returnTypeName = GetRemappedTypeName(cursor: null, context: null, returnType, out _, skipUsing: true);
65906590

6591-
var pointerSuffix = "";
6592-
if (pointerIndirectionLevel > 0)
6593-
{
6594-
pointerSuffix = string.Concat(Enumerable.Repeat("Pointer", pointerIndirectionLevel));
6595-
}
6596-
name = $"To{returnTypeName}{pointerSuffix}";
6591+
name = pointerIndirectionLevel switch {
6592+
0 => $"To{returnTypeName}",
6593+
1 => $"To{returnTypeName}Pointer",
6594+
_ => $"To{returnTypeName}Pointer{pointerIndirectionLevel}"
6595+
};
65976596
return true;
65986597
}
65996598

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
2+
3+
using System.Threading.Tasks;
4+
5+
namespace ClangSharp.UnitTests;
6+
7+
public abstract class CXXMethodDeclarationCSharpTest : CXXMethodDeclarationTest
8+
{
9+
protected override Task ConversionTestImpl()
10+
{
11+
var inputContents = @"struct MyStruct
12+
{
13+
int value;
14+
int* pointer;
15+
int** pointer2;
16+
17+
operator int()
18+
{
19+
return value;
20+
}
21+
22+
operator int*()
23+
{
24+
return pointer;
25+
}
26+
27+
operator int**()
28+
{
29+
return pointer2;
30+
}
31+
};
32+
";
33+
34+
var expectedOutputContents = @"namespace ClangSharp.Test
35+
{
36+
public unsafe partial struct MyStruct
37+
{
38+
public int value;
39+
40+
public int* pointer;
41+
42+
public int** pointer2;
43+
44+
public int ToInt32()
45+
{
46+
return value;
47+
}
48+
49+
public int* ToInt32Pointer()
50+
{
51+
return pointer;
52+
}
53+
54+
public int** ToInt32Pointer2()
55+
{
56+
return pointer2;
57+
}
58+
}
59+
}
60+
";
61+
62+
return ValidateBindingsAsync(inputContents, expectedOutputContents);
63+
}
64+
65+
protected override Task MacrosExpansionTestImpl()
66+
{
67+
var inputContents = @"typedef struct
68+
{
69+
unsigned char *buf;
70+
int size;
71+
} context_t;
72+
73+
int buf_close(void *pContext)
74+
{
75+
((context_t*)pContext)->buf=0;
76+
return 0;
77+
}
78+
";
79+
80+
var expectedOutputContents = @"namespace ClangSharp.Test
81+
{
82+
public unsafe partial struct context_t
83+
{
84+
[NativeTypeName(""unsigned char *"")]
85+
public byte* buf;
86+
87+
public int size;
88+
}
89+
90+
public static unsafe partial class Methods
91+
{
92+
public static int buf_close(void* pContext)
93+
{
94+
((context_t*)(pContext))->buf = null;
95+
return 0;
96+
}
97+
}
98+
}
99+
";
100+
101+
return ValidateBindingsAsync(inputContents, expectedOutputContents);
102+
}
103+
}

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/CXXMethodDeclarationTest.cs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public abstract class CXXMethodDeclarationTest : PInvokeGeneratorTest
2525
[Test]
2626
public Task InstanceTest() => InstanceTestImpl();
2727

28+
[Test]
29+
public Task MacrosExpansionTest() => MacrosExpansionTestImpl();
30+
2831
[Test]
2932
public Task MemberCallTest() => MemberCallTestImpl();
3033

@@ -64,46 +67,6 @@ public abstract class CXXMethodDeclarationTest : PInvokeGeneratorTest
6467
[Test]
6568
public Task VirtualWithVtblIndexAttributeTest() => VirtualWithVtblIndexAttributeTestImpl();
6669

67-
[Test]
68-
public virtual Task MacrosExpansionTest()
69-
{
70-
var inputContents = @"typedef struct
71-
{
72-
unsigned char *buf;
73-
int size;
74-
} context_t;
75-
76-
int buf_close(void *pContext)
77-
{
78-
((context_t*)pContext)->buf=0;
79-
return 0;
80-
}
81-
";
82-
83-
var expectedOutputContents = @"namespace ClangSharp.Test
84-
{
85-
public unsafe partial struct context_t
86-
{
87-
[NativeTypeName(""unsigned char *"")]
88-
public byte* buf;
89-
90-
public int size;
91-
}
92-
93-
public static unsafe partial class Methods
94-
{
95-
public static int buf_close(void* pContext)
96-
{
97-
((context_t*)(pContext))->buf = null;
98-
return 0;
99-
}
100-
}
101-
}
102-
";
103-
104-
return ValidateBindingsAsync(inputContents, expectedOutputContents);
105-
}
106-
10770
protected abstract Task ConstructorTestImpl();
10871

10972
protected abstract Task ConstructorWithInitializeTestImpl();
@@ -116,6 +79,8 @@ public static int buf_close(void* pContext)
11679

11780
protected abstract Task InstanceTestImpl();
11881

82+
protected abstract Task MacrosExpansionTestImpl();
83+
11984
protected abstract Task MemberCallTestImpl();
12085

12186
protected abstract Task MemberTestImpl();

tests/ClangSharp.PInvokeGenerator.UnitTests/Base/CXXMethodDeclarationXmlTest.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information.
22

33
using System.Threading.Tasks;
4-
using NUnit.Framework;
54

65
namespace ClangSharp.UnitTests;
76

87
public abstract class CXXMethodDeclarationXmlTest: CXXMethodDeclarationTest
98
{
10-
[Test]
11-
public override Task MacrosExpansionTest()
9+
protected override Task MacrosExpansionTestImpl()
1210
{
1311
var inputContents = @"typedef struct
1412
{
15-
unsigned char *buf;
16-
int size;
13+
unsigned char *buf;
14+
int size;
1715
} context_t;
1816
1917
int buf_close(void *pcontext)
2018
{
21-
((context_t*)pcontext)->buf=0;
22-
return 0;
19+
((context_t*)pcontext)->buf=0;
20+
return 0;
2321
}
2422
";
2523

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/CXXMethodDeclarationTest.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace ClangSharp.UnitTests;
99

1010
[Platform("unix")]
11-
public sealed class CSharpCompatibleUnix_CXXMethodDeclarationTest : CXXMethodDeclarationTest
11+
public sealed class CSharpCompatibleUnix_CXXMethodDeclarationTest : CXXMethodDeclarationCSharpTest
1212
{
1313
protected override Task ConstructorTestImpl()
1414
{
@@ -95,49 +95,6 @@ public MyStruct(int x, int y, int z)
9595
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
9696
}
9797

98-
protected override Task ConversionTestImpl()
99-
{
100-
var inputContents = @"struct MyStruct
101-
{
102-
int value;
103-
int* pointer;
104-
105-
operator int()
106-
{
107-
return value;
108-
}
109-
110-
operator int*()
111-
{
112-
return pointer;
113-
}
114-
};
115-
";
116-
117-
var expectedOutputContents = @"namespace ClangSharp.Test
118-
{
119-
public unsafe partial struct MyStruct
120-
{
121-
public int value;
122-
123-
public int* pointer;
124-
125-
public int ToInt32()
126-
{
127-
return value;
128-
}
129-
130-
public int* ToInt32Pointer()
131-
{
132-
return pointer;
133-
}
134-
}
135-
}
136-
";
137-
138-
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
139-
}
140-
14198
protected override Task DefaultParameterInheritedFromTemplateTestImpl()
14299
{
143100
// NOTE: This is a regression test where a struct inherits a function from a template with a default parameter.

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/CXXMethodDeclarationTest.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace ClangSharp.UnitTests;
99

1010
[Platform("win")]
11-
public sealed class CSharpCompatibleWindows_CXXMethodDeclarationTest : CXXMethodDeclarationTest
11+
public sealed class CSharpCompatibleWindows_CXXMethodDeclarationTest : CXXMethodDeclarationCSharpTest
1212
{
1313
protected override Task ConstructorTestImpl()
1414
{
@@ -95,49 +95,6 @@ public MyStruct(int x, int y, int z)
9595
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents);
9696
}
9797

98-
protected override Task ConversionTestImpl()
99-
{
100-
var inputContents = @"struct MyStruct
101-
{
102-
int value;
103-
int* pointer;
104-
105-
operator int()
106-
{
107-
return value;
108-
}
109-
110-
operator int*()
111-
{
112-
return pointer;
113-
}
114-
};
115-
";
116-
117-
var expectedOutputContents = @"namespace ClangSharp.Test
118-
{
119-
public unsafe partial struct MyStruct
120-
{
121-
public int value;
122-
123-
public int* pointer;
124-
125-
public int ToInt32()
126-
{
127-
return value;
128-
}
129-
130-
public int* ToInt32Pointer()
131-
{
132-
return pointer;
133-
}
134-
}
135-
}
136-
";
137-
138-
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents);
139-
}
140-
14198
protected override Task DefaultParameterInheritedFromTemplateTestImpl()
14299
{
143100
// NOTE: This is a regression test where a struct inherits a function from a template with a default parameter.

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpDefaultUnix/CXXMethodDeclarationTest.cs

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace ClangSharp.UnitTests;
99

1010
[Platform("unix")]
11-
public sealed class CSharpDefaultUnix_CXXMethodDeclarationTest : CXXMethodDeclarationTest
11+
public sealed class CSharpDefaultUnix_CXXMethodDeclarationTest : CXXMethodDeclarationCSharpTest
1212
{
1313
protected override Task ConstructorTestImpl()
1414
{
@@ -95,49 +95,6 @@ public MyStruct(int x, int y, int z)
9595
return ValidateGeneratedCSharpDefaultUnixBindingsAsync(inputContents, expectedOutputContents);
9696
}
9797

98-
protected override Task ConversionTestImpl()
99-
{
100-
var inputContents = @"struct MyStruct
101-
{
102-
int value;
103-
int* pointer;
104-
105-
operator int()
106-
{
107-
return value;
108-
}
109-
110-
operator int*()
111-
{
112-
return pointer;
113-
}
114-
};
115-
";
116-
117-
var expectedOutputContents = @"namespace ClangSharp.Test
118-
{
119-
public unsafe partial struct MyStruct
120-
{
121-
public int value;
122-
123-
public int* pointer;
124-
125-
public int ToInt32()
126-
{
127-
return value;
128-
}
129-
130-
public int* ToInt32Pointer()
131-
{
132-
return pointer;
133-
}
134-
}
135-
}
136-
";
137-
138-
return ValidateGeneratedCSharpDefaultUnixBindingsAsync(inputContents, expectedOutputContents);
139-
}
140-
14198
protected override Task DefaultParameterInheritedFromTemplateTestImpl()
14299
{
143100
// NOTE: This is a regression test where a struct inherits a function from a template with a default parameter.

0 commit comments

Comments
 (0)