Skip to content

Commit 57b5c0d

Browse files
Merge pull request #194 from reflectronic/anonymous-enum-using
Add tests for anonymous enums
2 parents 3306057 + e5b6a90 commit 57b5c0d

File tree

13 files changed

+808
-1
lines changed

13 files changed

+808
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest
6363
[Test]
6464
public Task WithTypeStarOverrideTest() => WithTypeStarOverrideTestImpl();
6565

66+
[Test]
67+
public Task WithAnonymousEnumTest() => WithAnonymousEnumTestImpl();
68+
69+
[Test]
70+
public Task WithReferenceToAnonymousEnumEnumeratorTest() => WithReferenceToAnonymousEnumEnumeratorTestImpl();
71+
6672
protected abstract Task BasicTestImpl();
6773

6874
protected abstract Task BasicValueTestImpl();
@@ -73,6 +79,7 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest
7379

7480
protected abstract Task ExplicitTypedWithNativeTypeNameTestImpl(string nativeType, string expectedManagedType);
7581

82+
7683
protected abstract Task RemapTestImpl();
7784

7885
protected abstract Task WithAttributeTestImpl();
@@ -96,4 +103,8 @@ public abstract class EnumDeclarationTest : PInvokeGeneratorTest
96103
protected abstract Task WithTypeStarTestImpl();
97104

98105
protected abstract Task WithTypeStarOverrideTestImpl();
106+
107+
protected abstract Task WithAnonymousEnumTestImpl();
108+
109+
protected abstract Task WithReferenceToAnonymousEnumEnumeratorTestImpl();
99110
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,61 @@ public enum MyEnum2 : uint
550550
};
551551
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes);
552552
}
553+
protected override Task WithAnonymousEnumTestImpl()
554+
{
555+
var inputContents = @"enum
556+
{
557+
MyEnum1_Value1 = 1,
558+
};
559+
560+
enum MyEnum2 : int
561+
{
562+
MyEnum2_Value1 = MyEnum1_Value1,
563+
};
564+
";
565+
566+
var expectedOutputContents = @"using static ClangSharp.Test.Methods;
567+
568+
namespace ClangSharp.Test
569+
{
570+
public enum MyEnum2
571+
{
572+
MyEnum2_Value1 = MyEnum1_Value1,
573+
}
574+
575+
public static partial class Methods
576+
{
577+
public const int MyEnum1_Value1 = 1;
578+
}
579+
}
580+
";
581+
582+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
583+
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
584+
}
585+
586+
protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl()
587+
{
588+
var inputContents = @"enum
589+
{
590+
MyEnum1_Value1 = 1,
591+
};
592+
593+
const int MyEnum2_Value1 = MyEnum1_Value1 + 1;
594+
";
595+
596+
var expectedOutputContents = @"namespace ClangSharp.Test
597+
{
598+
public static partial class Methods
599+
{
600+
public const int MyEnum1_Value1 = 1;
601+
602+
[NativeTypeName(""const int"")]
603+
public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1;
604+
}
605+
}
606+
";
607+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
608+
return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
609+
}
553610
}

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,62 @@ public enum MyEnum2 : uint
550550
};
551551
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes);
552552
}
553+
554+
protected override Task WithAnonymousEnumTestImpl()
555+
{
556+
var inputContents = @"enum
557+
{
558+
MyEnum1_Value1 = 1,
559+
};
560+
561+
enum MyEnum2 : int
562+
{
563+
MyEnum2_Value1 = MyEnum1_Value1,
564+
};
565+
";
566+
567+
var expectedOutputContents = @"using static ClangSharp.Test.Methods;
568+
569+
namespace ClangSharp.Test
570+
{
571+
public enum MyEnum2
572+
{
573+
MyEnum2_Value1 = MyEnum1_Value1,
574+
}
575+
576+
public static partial class Methods
577+
{
578+
public const int MyEnum1_Value1 = 1;
579+
}
580+
}
581+
";
582+
583+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
584+
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
585+
}
586+
587+
protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl()
588+
{
589+
var inputContents = @"enum
590+
{
591+
MyEnum1_Value1 = 1,
592+
};
593+
594+
const int MyEnum2_Value1 = MyEnum1_Value1 + 1;
595+
";
596+
597+
var expectedOutputContents = @"namespace ClangSharp.Test
598+
{
599+
public static partial class Methods
600+
{
601+
public const int MyEnum1_Value1 = 1;
602+
603+
[NativeTypeName(""const int"")]
604+
public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1;
605+
}
606+
}
607+
";
608+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
609+
return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
610+
}
553611
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/EnumDeclarationTest.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,62 @@ public enum MyEnum2 : uint
550550
};
551551
return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes);
552552
}
553+
554+
protected override Task WithAnonymousEnumTestImpl()
555+
{
556+
var inputContents = @"enum
557+
{
558+
MyEnum1_Value1 = 1,
559+
};
560+
561+
enum MyEnum2 : int
562+
{
563+
MyEnum2_Value1 = MyEnum1_Value1,
564+
};
565+
";
566+
567+
var expectedOutputContents = @"using static ClangSharp.Test.Methods;
568+
569+
namespace ClangSharp.Test
570+
{
571+
public enum MyEnum2
572+
{
573+
MyEnum2_Value1 = MyEnum1_Value1,
574+
}
575+
576+
public static partial class Methods
577+
{
578+
public const int MyEnum1_Value1 = 1;
579+
}
580+
}
581+
";
582+
583+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
584+
return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
585+
}
586+
587+
protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl()
588+
{
589+
var inputContents = @"enum
590+
{
591+
MyEnum1_Value1 = 1,
592+
};
593+
594+
const int MyEnum2_Value1 = MyEnum1_Value1 + 1;
595+
";
596+
597+
var expectedOutputContents = @"namespace ClangSharp.Test
598+
{
599+
public static partial class Methods
600+
{
601+
public const int MyEnum1_Value1 = 1;
602+
603+
[NativeTypeName(""const int"")]
604+
public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1;
605+
}
606+
}
607+
";
608+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
609+
return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
610+
}
553611
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/EnumDeclarationTest.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,61 @@ public enum MyEnum2 : uint
550550
};
551551
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes);
552552
}
553+
protected override Task WithAnonymousEnumTestImpl()
554+
{
555+
var inputContents = @"enum
556+
{
557+
MyEnum1_Value1 = 1,
558+
};
559+
560+
enum MyEnum2 : int
561+
{
562+
MyEnum2_Value1 = MyEnum1_Value1,
563+
};
564+
";
565+
566+
var expectedOutputContents = @"using static ClangSharp.Test.Methods;
567+
568+
namespace ClangSharp.Test
569+
{
570+
public enum MyEnum2
571+
{
572+
MyEnum2_Value1 = MyEnum1_Value1,
573+
}
574+
575+
public static partial class Methods
576+
{
577+
public const int MyEnum1_Value1 = 1;
578+
}
579+
}
580+
";
581+
582+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
583+
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
584+
}
585+
586+
protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl()
587+
{
588+
var inputContents = @"enum
589+
{
590+
MyEnum1_Value1 = 1,
591+
};
592+
593+
const int MyEnum2_Value1 = MyEnum1_Value1 + 1;
594+
";
595+
596+
var expectedOutputContents = @"namespace ClangSharp.Test
597+
{
598+
public static partial class Methods
599+
{
600+
public const int MyEnum1_Value1 = 1;
601+
602+
[NativeTypeName(""const int"")]
603+
public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1;
604+
}
605+
}
606+
";
607+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
608+
return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
609+
}
553610
}

tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/EnumDeclarationTest.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public enum MyEnum2
224224
";
225225

226226
var withAttributes = new Dictionary<string, IReadOnlyList<string>>
227-
{
227+
{
228228
["MyEnum1"] = new List<string>() { "Flags" }
229229
};
230230
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withAttributes: withAttributes);
@@ -550,4 +550,62 @@ public enum MyEnum2 : uint
550550
};
551551
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, withTypes: withTypes);
552552
}
553+
554+
protected override Task WithAnonymousEnumTestImpl()
555+
{
556+
var inputContents = @"enum
557+
{
558+
MyEnum1_Value1 = 1,
559+
};
560+
561+
enum MyEnum2 : int
562+
{
563+
MyEnum2_Value1 = MyEnum1_Value1,
564+
};
565+
";
566+
567+
var expectedOutputContents = @"using static ClangSharp.Test.Methods;
568+
569+
namespace ClangSharp.Test
570+
{
571+
public enum MyEnum2
572+
{
573+
MyEnum2_Value1 = MyEnum1_Value1,
574+
}
575+
576+
public static partial class Methods
577+
{
578+
public const int MyEnum1_Value1 = 1;
579+
}
580+
}
581+
";
582+
583+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
584+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
585+
}
586+
587+
protected override Task WithReferenceToAnonymousEnumEnumeratorTestImpl()
588+
{
589+
var inputContents = @"enum
590+
{
591+
MyEnum1_Value1 = 1,
592+
};
593+
594+
const int MyEnum2_Value1 = MyEnum1_Value1 + 1;
595+
";
596+
597+
var expectedOutputContents = @"namespace ClangSharp.Test
598+
{
599+
public static partial class Methods
600+
{
601+
public const int MyEnum1_Value1 = 1;
602+
603+
[NativeTypeName(""const int"")]
604+
public const int MyEnum2_Value1 = (int)(MyEnum1_Value1) + 1;
605+
}
606+
}
607+
";
608+
var diagnostics = new[] { new Diagnostic(DiagnosticLevel.Info, "Found anonymous enum: __AnonymousEnum_ClangUnsavedFile_L1_C1. Mapping values as constants in: Methods", "Line 1, Column 1 in ClangUnsavedFile.h") };
609+
return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
610+
}
553611
}

0 commit comments

Comments
 (0)