Download Code Contracts User Manual

Transcript
• [ContractArgumentValidator] methods support factoring legacy if-then-throw style parameter validation into helper methods, while still permitting the tools to recognize them. See Section 2.11.
• [ContractAbbreviator] methods support factoring commonly used combinations of Requires and Ensures
into helper methods that can be used from multiple places. See Section 2.12.
• Section 4.8 describes how to turn off contract checking or inheritance at assembly, type, or method
level using attributes.
• Section 5.2 describes how to migrate an existing project to take advantage of contracts.
• Section 7.8 describes how to deal with test harness setup in VS2010 when the code under test is built
with .NET v3.5 and the Microsoft.Contracts. dll .
Contents
1 Code Contracts Library Overview
1.1 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
5
2 Contracts
2.1 Preconditions . . . . . . . . . . . . . . . . . . .
2.1.1 Legacy Requires . . . . . . . . . . . . .
2.1.2 Preconditions on automatic properties .
2.2 Postconditions . . . . . . . . . . . . . . . . . .
2.2.1 Normal Postconditions . . . . . . . . . .
2.2.2 Exceptional Postconditions . . . . . . .
2.2.3 Special Methods within Postconditions .
2.2.4 Postconditions on automatic properties
2.2.5 Postconditions on async methods . . . .
2.3 Object Invariants . . . . . . . . . . . . . . . . .
2.3.1 Invariants on Automatic Properties . . .
2.4 Assert . . . . . . . . . . . . . . . . . . . . . . .
2.5 Assume . . . . . . . . . . . . . . . . . . . . . .
2.6 EndContractBlock . . . . . . . . . . . . . . . .
2.7 Quantifiers . . . . . . . . . . . . . . . . . . . .
2.7.1 ForAll . . . . . . . . . . . . . . . . . . .
2.7.2 Exists . . . . . . . . . . . . . . . . . . .
2.8 Interface Contracts . . . . . . . . . . . . . . . .
2.9 Contracts on Abstract Methods . . . . . . . . .
2.10 Overloads on Contract Methods . . . . . . . . .
2.11 Contract Argument Validator Methods . . . . .
2.12 Contract Abbreviator Methods . . . . . . . . .
2.13 AssumeInvariant Helper . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.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
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
5
6
6
7
7
7
7
7
9
9
9
10
11
11
11
11
11
12
12
13
13
13
15
16
3 Contract Inheritance
3.1 Custom Parameter Validation and Inheritance .
3.2 Inheriting from Multiple Methods . . . . . . . .
3.3 Purity and Inheritance . . . . . . . . . . . . . .
3.4 Object Invariants and Inheritance . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
17
17
18
18
18
2