The attribute cannot be repeated in C ++ / CLI, but OK in C #?

I get error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeatedin C ++ / CLI code, but not C #.

xUnit.net looks like the answer to my prayers - a modern unit test environment with a graphical interface that works with C ++ / CLI. However, using their approach to parameterized testing, I get error C3095, as shown below.

Any ideas?

I am using the latest xUnit.net 1.6 with Visual Studio 2008SP1.

using namespace Xunit;
using namespace Xunit::Extensions;

public ref class ParameterisedTestClass
{
public:

    [Theory]
    [InlineData("Kilroy", 6)]
    // uncomment to cause c3095 [InlineData("Jones", 5)]
    void PropTest(String^ msg, int msgLen)
    {
        Assert::Equal(msg->Length, msgLen);
    }
};

equivalent in c # is exact

using Xunit;
using Xunit.Extensions;

public  class ParameterisedTestClass
{

    [Theory]
    [InlineData("Kilroy", 6)]
    [InlineData("Jones", 5)]
    public void PropTest(String msg, int msgLen)
    {
        Assert.Equal(msg.Length, msgLen);
    }
};
+3
source share
3 answers

Hmmm ... I looked at the defs here and here , and play them (cut) below; inheritance AllowMultiplethrough DataAttributeworks fine in C #:

class Test
{
    [InlineData]
    [InlineData]
    static void Main() { }
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
class DataAttribute : Attribute {}

class InlineDataAttribute : DataAttribute { }

, ++/CLI, , ++/CLI [AttributeUsage]. Xunit [AttributeUsage] InlineDataAttribute.

+6

, , .

InlineDataAttribute DataAttribute. DataAttribute, :

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]

InlineDataAttribute AttributeUsage. , ++ AllowMultiple... , , . AttributeUsageAttribute , Inherited=true, , , InlineDataAttribute...

+7

++/CLI:

[AttributeUsage(AttributeTargets::All, AllowMultiple = true, Inherited = true)]
ref class BaseAttribute : Attribute {};

ref class DerivedAttribute : BaseAttribute {};

[Derived]
[Derived]    // Error C3095
ref class Test {};

Obviously, this is a bug in the C ++ / CLI compiler. I do not see this being reported on connect.microsoft.com. Doing this is probably not worth the effort; the language is in maintenance mode.

Possible workarounds are to edit the xUnit source code to reassign AllowMultiple or create your own InlineDataFixedAttribute that inherits InlineDataAttribute.

+1
source

Source: https://habr.com/ru/post/1751725/


All Articles