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.
From the MSDN page on Contract.Invariant
During run-time checking, invariants are checked at the end of each public method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With