属性在 C++/CLI 中不能重复,但在 C# 中可以吗?
我在 C++/CLI 代码中收到 error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute can berepeated
,但在 C# 中却没有。
xUnit.net 看起来像是我祈祷的答案 - 一个现代单元测试框架,带有 GUI,可与 C++/CLI 一起使用。但是,使用他们的参数化测试方法会出现错误 C3095,如下所示。
有什么想法吗?
我正在使用最新的 xUnit.net 1.6 和 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);
}
};
C# 中的等效项很好
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);
}
};
I'm getting error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated
in C++/CLI code but not C#.
xUnit.net looks like the answer to my prayers - a modern unit test framework with GUI working with C++/CLI. However, using their approach to parameterised testing gives me the error C3095 as shown below.
Any ideas?
I'm 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);
}
};
the equivalent in C# is fine
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);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的猜测是,这是由于继承造成的,并且编译器之一出错了。
InlineDataAttribute
继承自数据属性
。现在,DataAttribute
被声明为允许多个实例:但
InlineDataAttribute
本身没有任何显式的AttributeUsage
属性。我怀疑 C++ 编译器没有发现继承的AllowMultiple...或者它不应该可能被继承。我找不到有关AttributeUsageAttribute 继承的任何详细文档
本身 - 尽管它有Inherited=true
,所以我猜它应该由InlineDataAttribute
完全继承。 。My guess is that this is due to inheritance, and one of the compilers getting it wrong.
InlineDataAttribute
inherits fromDataAttribute
. NowDataAttribute
is declared to allow multiple instances:but
InlineDataAttribute
doesn't have any explicitAttributeUsage
attribute itself. I suspect the C++ compiler isn't spotting the inherited AllowMultiple... or it's possible that it shouldn't be inherited. I can't find any detailed documentation about the inheritance ofAttributeUsageAttribute
itself - although it hasInherited=true
, so I guess it should be inherited completely byInlineDataAttribute
...嗯...我查看了 defs 这里和此处,并在下面复制它们(删减);通过
DataAttribute
继承AllowMultiple
在 C# 中工作正常:因此,如果它不适用于 C++/CLI,我猜 C++/CLI 根本无法处理隐含的 <代码>[属性用法]。您应该向 Xunit 提出请求,要求他们在
InlineDataAttribute
上显式显示[AttributeUsage]
。Hmmm... I looked at the defs here and here, and reproduced them (cut-down) below; the inheritance of
AllowMultiple
viaDataAttribute
works fine in C#:So if it isn't working for C++/CLI, I guess C++/CLI simply isn't processing the implied
[AttributeUsage]
. You should file a request against Xunit asking them to make the[AttributeUsage]
explicit onInlineDataAttribute
.我用这个 C++/CLI 片段重现:
显然,这是 C++/CLI 编译器中的一个错误。我在 connect.microsoft.com 上没有看到相关报道。自己这样做可能不值得付出努力,该语言处于维护模式。
可能的解决方法是编辑 xUnit 的源代码以再次分配AllowMultiple 或创建您自己的继承InlineDataAttribute 的InlineDataFixedAttribute。
I repro with this C++/CLI snippet:
Clearly this is a bug in the C++/CLI compiler. I don't see it reported at connect.microsoft.com. Doing so yourself probably isn't worth the effort, the language is in maintenance mode.
Possible workarounds are editing the source code of xUnit to assign AllowMultiple again or to create your own InlineDataFixedAttribute that inherits InlineDataAttribute.