我执行这个简单的合同不正确吗? [英] Am I implementing this simple contract incorrectly?

查看:107
本文介绍了我执行这个简单的合同不正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

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.Requires Contract.Invariant 方法,可防止vertexCount变量小于或等于2;但是,我仍然可以初始化2面或更少面的RegularPolygon。我的(简化的)NUnit测试如下所示:

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!

起初,我认为ReSharper可能已经搞砸了,因为它使行变灰并在我尝试在其中使用方法时显示此消息。 合同命名空间:

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.

但是,挂起R#并运行该程序时,编译器不会生成方法调用,因为该方法是有条件的,或者它是不执行的部分方法。 NUnit中的测试具有相同的结果,而VS中没有错误或警告。因此,我认为这仅仅是因为ReSharper尚未突出强调代码合同的兼容性。

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.

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

谢谢。

推荐答案

要检查的第一件事-您是否已打开合同检查功能?如果没有,您的任何合同都不会做任何事情。那也将解释R#警告。在构建属性的代码合同下查看,并在运行时检查下查看其内容。

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".

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

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. :)

这篇关于我执行这个简单的合同不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆