Questions tagged [code-contracts]

Code Contracts is a Microsoft open source project which allows you to express pre-conditions, post-conditions, and assertions directly in code.

Code Contracts is a Microsoft open source project, originating from Microsoft Research, which allows you to express pre-conditions, post-conditions, and assertions directly in code.

A check in a Code Contract is one that, if the code is correct, should always return true. In other words: it should not test whether the database is accessible or whether the user has entered an empty string -- those are outside factors. Code Contracts are there only to make sure your code is correct and maintains its invariants.

So, typically, you would do input parameter validation on public methods by explicitly checking the value and then throwing an exception if out of range. But for private methods -- which get called only by other code, and so can expect certain pre-conditions -- the input parameters can be checked with Code Contracts.

private void MyMethod(string machineId, int age)
{
    Contract.Requires<ArgumentNullException>(!string.IsNullOrEmpty(machineId));
    Contract.Requires<ArgumentOutOfRangeException>(machineId.StartsWith("PEK"));
    Contract.Requires<ArgumentOutOfRangeException>(age > 16);
    //...
}

Post-conditions are expressions that must evaluate to true when the method is finished. A typical example is to verify that the output of a function is never null, or that it is within a certain range.

private string MachineNameOf(int id)
{
    Contract.Requires<ArgumentOutOfRangeException>(id > 100);
    Contract.Ensures(Contract.Result<string>() != null);
    //...
}

GitHub: https://github.com/Microsoft/CodeContracts

Tools at: http://visualstudiogallery.msdn.microsoft.com/1ec7db13-3363-46c9-851f-1ce455f66970.

See also (historical): http://research.microsoft.com/en-us/projects/contracts.

649 questions
1
vote
0 answers

Filter warning types in VS 2010

In Visual Studio 2010, all warnings share the same output window: Compiler Warnings Code Analysis Output Code Contracts Output Is there any way to filter those? For example, I'd like to display only the code analysis warnings separately. Is there…
LTR
  • 1,226
  • 2
  • 17
  • 39
1
vote
1 answer

Repeating code contract requirements down the inheritance chain?

Suppose you have an existing large project and you want to integrate Code Contracts in it. The existing code uses if-null-then-throw logic. For the given conditions, the documentation suggests to set the assembly mode to Custom Argument…
Roman Royter
  • 1,655
  • 13
  • 28
1
vote
2 answers

Code Contracts warning not showing

I have this simple code: public ArrayStack(int capacity) { Contract.Requires(capacity >= 0); Contract.Ensures(_items != null); Contract.Ensures(_items.Length == capacity); _items = new T[capacity]; _top =…
Amit Raz
  • 5,370
  • 8
  • 36
  • 63
1
vote
0 answers

Is there a way to prevent code contracts from crashing Visual Studio 2010 SP1?

I am using Visual Studio 2010 SP1 Ultimate, and some of my colleagues are using Visual Studio 2010 SP1 Professional. We have all installed code contracts. Is there anyway to prevent Visual Studio from crashing given the below scenarios? We have…
Jason
  • 4,897
  • 2
  • 33
  • 40
1
vote
0 answers

Microsoft Contracts: Assembly load resulted in metadata import warning

I'm trying to learn my way around the Microsoft Code Contracts libraries, and I have the following simple function: internal static Engine CreateBuildEngine(Microsoft.Build.Framework.ILogger logger) { Contract.Requires( logger != null ); …
0
votes
2 answers

Code Contracts -- Requires/Ensures unproven when I use IComparable or IComparable

I have the following method: public static bool IsBetween(this IComparable value, T lowerBound, T upperBound) where T : IComparable { Contract.Requires<>(value != null); Contract.Requires<>(lowerBound != null); …
myermian
  • 31,823
  • 24
  • 123
  • 215
0
votes
1 answer

Code contract inheritance

Found a bit of an oddity with code contracts and i was wondering if anyone knew the cause ... ok so time for some code samples: Assembly 1: [ContractClass(typeof(IServiceCodeContract<>))] public interface IService where T : class { ...…
War
  • 8,539
  • 4
  • 46
  • 98
0
votes
2 answers

Unit Testing with Pex and Code Contracts

I have the following method: public static DateTime SubQtrs( this DateTime dt, int qtrs ) { Contract.Requires( qtrs > -1 ); Contract.Requires( ( qtrs * 3 ) >= -120000 && ( qtrs * 3 ) <= 120000 ); // do something } I…
Chris
  • 1,690
  • 2
  • 17
  • 24
0
votes
1 answer

Code Contract Errors

is there a way to alter the way the code contracts behave during run time? as in, when a pre or post conidtion fails during run time, the application crashes. Is there a way instead of crashing, have a MessageBox displayed??
Ari
  • 1,026
  • 6
  • 20
  • 31
0
votes
0 answers

How to get transfer with token USDT of address balance in web3 python in ethereum

Hi I am trying to get all transfer from USDT Token of 1 Token holder with the following information: token contract: 0xdAC17F958D2ee523a2206206994597C13D831ec7 token holder: 0x28C6c06298d514Db089934071355E5743bf21d60 Here is the link to that…
0
votes
0 answers

Support 'oneOf' keyword in pactflow

I have a question regarding keyword support oneOf in the schema...Using BDCT workflow and we have oneOf keyword in the schema and I had followed the instructions here :…
Raj Ch
  • 1
0
votes
2 answers

Is it possible it see the progress of the Code Contracts static analyzer?

The static analyzer takes a long time to run. This is understandable, it is doing a lot of work. But while it runs in the background, it is difficult to tell what is going. Is the static analyzer running? Did it run into some problem the prevented…
Matthew
  • 28,056
  • 26
  • 104
  • 170
0
votes
1 answer

Pex not satisfying code contract

I'm trying to wrote a pex test, and I noticed that it always was feeding a false value as one of the params that I wanted. My test looked like this (simplified: there are/were more params, but otherwise no different): [PexMethod] public void…
Steven Evers
  • 16,649
  • 19
  • 79
  • 126
0
votes
1 answer

Building coded ui test project fails when "Perform runtime contract checking" is set to "full"

I have a simple Test project where a CodedUITestMethod is created. The test method simply clicks the Windows Start button in the taskbar. The method is generated by the Coded UI Test Builder tool from Visual Studio 2010 Ultimate. Then, I go to…
Jacob
  • 127
  • 1
  • 2
  • 9
0
votes
2 answers

Using Code Contracts, why does ccrewrite require access to every runtime dependency?

I'm trying to use Code Contracts and I'm running into a problem that is blocking me. With Contract Reference Assembly set to Build, ccrewrite is erroring while trying to access assemblies that are referenced indirectly by assemblies that are…
Jay
  • 383
  • 2
  • 16