From 7b24324c9006a1cb10c63da2c19d95e1aa8d26b3 Mon Sep 17 00:00:00 2001 From: Daniel Paoliello Date: Thu, 18 Apr 2024 11:52:55 -0700 Subject: [PATCH] Enable matching `*` for `--with-using` --- .../PInvokeGenerator.cs | 2 +- .../OptionsTest.cs | 57 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/OptionsTest.cs diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index 08320b66..0242724e 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -7001,7 +7001,7 @@ private void WithUsings(NamedDecl namedDecl) { Debug.Assert(_outputBuilder is not null); - if (TryGetRemappedValue(namedDecl, _config.WithUsings, out var usings)) + if (TryGetRemappedValue(namedDecl, _config.WithUsings, out var usings, matchStar: true)) { foreach (var @using in usings) { diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/OptionsTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/OptionsTest.cs new file mode 100644 index 00000000..5ceeceaf --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/OptionsTest.cs @@ -0,0 +1,57 @@ +// 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. + +using System.Collections.Generic; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace ClangSharp.UnitTests; + +public sealed class OptionsTest : PInvokeGeneratorTest +{ + [Test] + public Task WithUsings() + { + var inputContents = @"struct StructA {}; +namespace NS +{ + struct StructB {}; + struct StructC {}; +} +struct StructD {}; +"; + var expectedOutputContents = @"using ForStar; +using ForStructA1; +using ForStructA2; +using ForStructBWithDoubleColon; +using ForStructCWithDot; + +namespace ClangSharp.Test +{ + public partial struct StructA + { + } + + public partial struct StructB + { + } + + public partial struct StructC + { + } + + public partial struct StructD + { + } +} +"; + var withUsings = new Dictionary> { + ["StructA"] = ["ForStructA1", "ForStructA2"], + ["*"] = ["ForStar"], + ["NS::StructB"] = ["ForStructBWithDoubleColon"], + ["NS.StructC"] = ["ForStructCWithDot"], + ["DoesNotExist"] = ["ErrorShouldNotBeInOutput"], + }; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withUsings: withUsings); + } +}