Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added var doc strings for Enums #245

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"dotnet-test-explorer.testProjectPath": "src\\dscom.test\\dscom.test.csproj",
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
},
"editor.suggestOnTriggerCharacters": true,
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true,
{
"dotnet-test-explorer.testProjectPath": "src\\dscom.test\\dscom.test.csproj",
"editor.quickSuggestions": {
"comments": "on",
"strings": "on",
"other": "on"
},
"editor.suggestOnTriggerCharacters": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
},
"omnisharp.enableRoslynAnalyzers": true,
"omnisharp.enableEditorConfigSupport": true,
}
238 changes: 142 additions & 96 deletions src/dscom.test/tests/EnumTest.cs
Original file line number Diff line number Diff line change
@@ -1,96 +1,142 @@
// Copyright 2022 dSPACE GmbH, Mark Lechtermann, Matthias Nissen and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Runtime.InteropServices;

namespace dSPACE.Runtime.InteropServices.Tests;

public class EnumTest : BaseTest
{
[Theory]
[InlineData(typeof(byte))]
[InlineData(typeof(sbyte))]
[InlineData(typeof(short))]
[InlineData(typeof(ushort))]
[InlineData(typeof(uint))]
[InlineData(typeof(int))]
[InlineData(typeof(ulong))]
[InlineData(typeof(long))]
public void EnumWithNumericValues_Available(Type type)
{
var result = CreateAssembly(CreateAssemblyName(assemblyNameSuffix: $"{type}"))
.WithEnum("TestEnum", type)
.WithLiteral("A", 1)
.WithLiteral("B", 20)
.WithLiteral("C", 50)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("TestEnum");
typeLibInfo.Should().NotBeNull();

var kv = typeLibInfo!.GetAllEnumValues();

kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_A", 1));
kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_B", 20));
kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_C", 50));
}

[Fact]
public void EnumComVisibleFalse_EnumIsNotGenerated()
{
var result = CreateAssembly()
.WithEnum<int>("TestEnum")
.WithCustomAttribute<ComVisibleAttribute>(false)
.WithLiteral("A", 1)
.WithLiteral("B", 20)
.WithLiteral("C", 50)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("TestEnum");
typeLibInfo.Should().BeNull();
}

[Fact]
public void TwoEnumsWithTheSameNameInDifferentNamespaces_EnumFieldsShouldUseNamespaceAsPrefix()
{
var result = CreateAssembly()
.WithEnum<int>("TestEnum").WithNamespace("dspace.test.namespace1")
.WithLiteral("A", 1)
.WithLiteral("B", 2)
.Build()
.WithEnum<int>("TestEnum").WithNamespace("dspace.test.namespace2")
.WithLiteral("A", 1)
.WithLiteral("B", 2)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("dspace_test_namespace1_TestEnum");
typeLibInfo.Should().NotBeNull();
var kv = typeLibInfo!.GetAllEnumValues();
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace1_TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace1_TestEnum_B");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_B");

typeLibInfo = result.TypeLib.GetTypeInfoByName("dspace_test_namespace2_TestEnum");
typeLibInfo.Should().NotBeNull();
kv = typeLibInfo!.GetAllEnumValues();
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace2_TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace2_TestEnum_B");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_B");
}
}
// Copyright 2022 dSPACE GmbH, Mark Lechtermann, Matthias Nissen and Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.ComponentModel;
using System.Runtime.InteropServices;

namespace dSPACE.Runtime.InteropServices.Tests;

public class EnumTest : BaseTest
{
[Theory]
[InlineData(typeof(byte))]
[InlineData(typeof(sbyte))]
[InlineData(typeof(short))]
[InlineData(typeof(ushort))]
[InlineData(typeof(uint))]
[InlineData(typeof(int))]
[InlineData(typeof(ulong))]
[InlineData(typeof(long))]
public void EnumWithNumericValues_Available(Type type)
{
var result = CreateAssembly(CreateAssemblyName(assemblyNameSuffix: $"{type}"))
.WithEnum("TestEnum", type)
.WithLiteral("A", 1)
.WithLiteral("B", 20)
.WithLiteral("C", 50)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("TestEnum");
typeLibInfo.Should().NotBeNull();

var kv = typeLibInfo!.GetAllEnumValues();

kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_A", 1));
kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_B", 20));
kv.Should().Contain(new KeyValuePair<string, object>("TestEnum_C", 50));
}

[Fact]
public void EnumComVisibleFalse_EnumIsNotGenerated()
{
var result = CreateAssembly()
.WithEnum<int>("TestEnum")
.WithCustomAttribute<ComVisibleAttribute>(false)
.WithLiteral("A", 1)
.WithLiteral("B", 20)
.WithLiteral("C", 50)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("TestEnum");
typeLibInfo.Should().BeNull();
}

[Fact]
public void TwoEnumsWithTheSameNameInDifferentNamespaces_EnumFieldsShouldUseNamespaceAsPrefix()
{
var result = CreateAssembly()
.WithEnum<int>("TestEnum").WithNamespace("dspace.test.namespace1")
.WithLiteral("A", 1)
.WithLiteral("B", 2)
.Build()
.WithEnum<int>("TestEnum").WithNamespace("dspace.test.namespace2")
.WithLiteral("A", 1)
.WithLiteral("B", 2)
.Build()
.Build();

var typeLibInfo = result.TypeLib.GetTypeInfoByName("dspace_test_namespace1_TestEnum");
typeLibInfo.Should().NotBeNull();
var kv = typeLibInfo!.GetAllEnumValues();
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace1_TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace1_TestEnum_B");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_B");

typeLibInfo = result.TypeLib.GetTypeInfoByName("dspace_test_namespace2_TestEnum");
typeLibInfo.Should().NotBeNull();
kv = typeLibInfo!.GetAllEnumValues();
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace2_TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().Contain("dspace_test_namespace2_TestEnum_B");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_A");
kv.ToList().Select(kv => kv.Key).Should().NotContain("TestEnum_B");
}

[Fact]
public void EnumsValuesWithDescription_EnumValueHasDocString()
{
var result = CreateAssembly()
.WithEnum<int>("TestEnum").WithNamespace("dspace.test.namespace1")
.WithLiteralAndAttribute("A", 1, typeof(DescriptionAttribute), new Type[] { typeof(string) }, new object[] { "TestDescription_A" })
.WithLiteralAndAttribute("B", 1, typeof(DescriptionAttribute), new Type[] { typeof(string) }, new object[] { "TestDescription_B" })
.Build()
.Build();

var typeInfo = result.TypeLib.GetTypeInfoByName("TestEnum");
typeInfo.Should().NotBeNull();

using var attribute = typeInfo!.GetTypeInfoAttributes();
attribute.Should().NotBeNull();
var count = attribute!.Value.cVars;
count.Should().Be(2);
typeInfo!.GetVarDesc(0, out var ppVarDesc0);
try
{
var varDesc = Marshal.PtrToStructure<VARDESC>(ppVarDesc0);
varDesc.varkind.Should().Be(VARKIND.VAR_CONST);
typeInfo!.GetDocumentation(varDesc.memid, out _, out var strDocString, out _, out _);
strDocString.Should().Be("TestDescription_A");
}
finally
{
typeInfo.ReleaseVarDesc(ppVarDesc0);
}

typeInfo!.GetVarDesc(1, out var ppVarDesc1);
try
{
var varDesc = Marshal.PtrToStructure<VARDESC>(ppVarDesc1);
varDesc.varkind.Should().Be(VARKIND.VAR_CONST);
typeInfo!.GetDocumentation(varDesc.memid, out _, out var strDocString, out _, out _);
strDocString.Should().Be("TestDescription_B");
}
finally
{
typeInfo.ReleaseVarDesc(ppVarDesc1);
}
}
}

Loading
Loading