我是否错误地执行了这个简单的合同?

发布于 2024-09-13 22:07:31 字数 1349 浏览 10 评论 0原文

这是我的代码:

public class RegularPolygon
{
    public int VertexCount;
    public double SideLength;

    public RegularPolygon(int vertexCount, double sideLength)
    {
        Contract.Requires(vertexCount >= 3);
        VertexCount = vertexCount;
        SideLength = sideLength;
    }

    [ContractInvariantMethod]
    private void RegularPolygonInvariants()
    {
        Contract.Invariant(VertexCount>=3);
    }

}

我尝试使用 Contract.RequiresContract.Invariant 方法来防止 vertexCount 变量变得小于或等于 2;但是我仍然能够初始化一个具有 2 条或更少边的正则多边形。我的(简化的)NUnit 测试如下所示:

[TestFixture]
class TestRegularPolygon
{
    private RegularPolygon _polygon;

    [SetUp]
    public void Init()
    {
        _polygon = new RegularPolygon(1, 50);
    }

    [Test]
    public void Constructor()
    {
        Assert.That(_polygon.VertexCount,Is.GreaterThanOrEqualTo(3));
    }

}

上面的测试也通过了,但我不明白为什么!

起初,我认为 ReSharper 可能搞砸了一些事情,因为每当我尝试使用 Contract 命名空间中的方法时,它都会使该行变灰并显示此消息:

方法调用被跳过。编译器不会生成方法调用,因为该方法是有条件的,或者是没有实现的部分方法。

但是暂停 R# 并在 NUnit 中运行测试具有相同的结果,在 VS 中也没有错误或警告。所以我认为这只是因为 ReSharper 还没有代码契约的突出显示兼容性。

我已经查看了文档,据我所知,我不应该遇到这个问题。

我是否错误地使用了代码合约,或者我的环境是否以某种方式阻止了它工作?

谢谢。

This is my code:

public class RegularPolygon
{
    public int VertexCount;
    public double SideLength;

    public RegularPolygon(int vertexCount, double sideLength)
    {
        Contract.Requires(vertexCount >= 3);
        VertexCount = vertexCount;
        SideLength = sideLength;
    }

    [ContractInvariantMethod]
    private void RegularPolygonInvariants()
    {
        Contract.Invariant(VertexCount>=3);
    }

}

I have tried with both the Contract.Requires and Contract.Invariant methods to prevent the vertexCount variable from becoming less than or equal to 2; however I am still able to initialise a RegularPolygon with 2 or fewer sides. My (simplified) NUnit test looks like this:

[TestFixture]
class TestRegularPolygon
{
    private RegularPolygon _polygon;

    [SetUp]
    public void Init()
    {
        _polygon = new RegularPolygon(1, 50);
    }

    [Test]
    public void Constructor()
    {
        Assert.That(_polygon.VertexCount,Is.GreaterThanOrEqualTo(3));
    }

}

The above test also passes and I cannot figure out why!

At first I thought ReSharper might have been messing something up because it greys out the line and displays this message whenever I try to use a method in the Contract namespace:

Method invocation is skipped. Compiler will not generate method invocation because the method is conditional, or it is partial method without implementation.

But suspending R# and running the tests in NUnit has the same result with no errors or warnings in VS either. So I assume that is just because ReSharper does not have highlighting compatibility for code contracts yet.

I have looked at the documentation and as far as I can tell I shouldn't be having this problem.

Am I using code contracts incorrectly or is my environment preventing it from working somehow?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉 2024-09-20 22:07:31

首先要检查的是——你真的开启了合同检查吗?如果没有,您的任何合同都不会起到任何作用。这也可以解释 R# 警告。查看构建属性中的“代码契约”,并查看“运行时检查”下的内容。

根据注释,确保您已将 CONTRACTS_FULL 定义为编译符号 - 这似乎是 R# 所需要的。

第二点:你有公共可变字段,这意味着你的不变量可以随时被写“

polygon.VertexCount = 0;

请不要使用公共字段,特别是不可写字段”的人违反。 :)

First thing to check - have you actually got contract checking turned on? If not, none of your contracts will do anything. That would explain the R# warning, too. Look under "Code Contracts" in the build properties, and see what it says under "Runtime Checking".

As per comments, ensure you have CONTRACTS_FULL defined as a compilation symbol - that appears to be what R# requires.

Second point: you've got public mutable fields, which means your invariant can be violated at any moment by someone writing

polygon.VertexCount = 0;

Please don't use public fields, particularly not writable ones. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文