Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Contract.Invariant not check by static verifier

I'm experimenting with Code Contract and I encountered one problem. I have class:

public class SpecialPoint
{
    public int X { get; set; }
    public int Y { get; set; }

    public SpecialPoint(int x, int y)
    {
        Contract.Requires<ArgumentException>(y > x);
        X = x;
        Y = y;
    }


    [ContractInvariantMethod]
    private void ClassContract()
    {
        Contract.Invariant(Y > X);
    }
}

and I run a test against it:

[TestFixture]
class SpecialPointTests
{
    [Test]
    public void SpecialPoint()
    {
        var p = new SpecialPoint(10, 20); 
        p.X = 30;
    }
}

I expected static checker to warn me about assignment p.X =30; as this violates invariant but it only takes place during runtime. I have static analysis enable. My version is 1.7.11202.10.

like image 357
stachu Avatar asked Oct 20 '22 20:10

stachu


1 Answers

From the MSDN page on Contract.Invariant

During run-time checking, invariants are checked at the end of each public method.

like image 172
sudheeshix Avatar answered Oct 30 '22 13:10

sudheeshix