Skip to content

Commit 5429448

Browse files
committed
C#: Add tests for Code Contracts
1 parent cb8e5fa commit 5429448

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

csharp/ql/test/library-tests/commons/Assertions/Assertions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Diagnostics.Contracts;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45

56
public static class Forwarders
@@ -37,6 +38,18 @@ void Trivial()
3738
Forwarders.MyAssert2(false);
3839
Forwarders.MyAssert2(true);
3940
}
41+
42+
void CodeContracts(string s)
43+
{
44+
Contract.Requires(s != null);
45+
Contract.Requires(s != null, "s must be non-null");
46+
Contract.Requires<Exception>(s != null);
47+
Contract.Requires<Exception>(s != null, "s must be non-null");
48+
Contract.Assert(s != null);
49+
Contract.Assert(s != null, "s is non-null");
50+
Contract.Assume(s != null);
51+
Contract.Assume(s != null, "s is non-null");
52+
}
4053
}
4154

42-
// semmle-extractor-options: ${testdir}/../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs
55+
// semmle-extractor-options: ${testdir}/../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs /r:System.Diagnostics.Contracts.dll
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
assertTrue
2+
| Assertions.cs:8:44:8:59 | call to method IsTrue | Assertions.cs:8:58:8:58 | access to parameter b |
3+
| Assertions.cs:9:45:9:55 | call to method MyAssert | Assertions.cs:9:54:9:54 | access to parameter b |
4+
| Assertions.cs:17:9:17:31 | call to method Assert | Assertions.cs:17:22:17:30 | ... != ... |
5+
| Assertions.cs:20:9:20:32 | call to method IsTrue | Assertions.cs:20:23:20:31 | ... == ... |
6+
| Assertions.cs:21:9:21:32 | call to method IsTrue | Assertions.cs:21:23:21:31 | ... != ... |
7+
| Assertions.cs:24:9:24:38 | call to method MyAssert | Assertions.cs:24:29:24:37 | ... == ... |
8+
| Assertions.cs:25:9:25:39 | call to method MyAssert2 | Assertions.cs:25:30:25:38 | ... == ... |
9+
| Assertions.cs:30:9:30:27 | call to method Assert | Assertions.cs:30:22:30:26 | false |
10+
| Assertions.cs:31:9:31:26 | call to method Assert | Assertions.cs:31:22:31:25 | true |
11+
| Assertions.cs:32:9:32:28 | call to method IsTrue | Assertions.cs:32:23:32:27 | false |
12+
| Assertions.cs:33:9:33:27 | call to method IsTrue | Assertions.cs:33:23:33:26 | true |
13+
| Assertions.cs:36:9:36:34 | call to method MyAssert | Assertions.cs:36:29:36:33 | false |
14+
| Assertions.cs:37:9:37:33 | call to method MyAssert | Assertions.cs:37:29:37:32 | true |
15+
| Assertions.cs:38:9:38:35 | call to method MyAssert2 | Assertions.cs:38:30:38:34 | false |
16+
| Assertions.cs:39:9:39:34 | call to method MyAssert2 | Assertions.cs:39:30:39:33 | true |
17+
assertFalse
18+
| Assertions.cs:22:9:22:33 | call to method IsFalse | Assertions.cs:22:24:22:32 | ... != ... |
19+
| Assertions.cs:23:9:23:33 | call to method IsFalse | Assertions.cs:23:24:23:32 | ... == ... |
20+
| Assertions.cs:34:9:34:28 | call to method IsFalse | Assertions.cs:34:24:34:27 | true |
21+
| Assertions.cs:35:9:35:29 | call to method IsFalse | Assertions.cs:35:24:35:28 | false |
22+
assertNull
23+
| Assertions.cs:18:9:18:24 | call to method IsNull | Assertions.cs:18:23:18:23 | access to local variable s |
24+
assertNonNull
25+
| Assertions.cs:19:9:19:27 | call to method IsNotNull | Assertions.cs:19:26:19:26 | access to local variable s |
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import csharp
2+
import semmle.code.csharp.commons.Assertions
3+
4+
query predicate assertTrue(Assertion a, Expr e) {
5+
a.getExpr() = e and
6+
a.getTarget() instanceof AssertTrueMethod
7+
}
8+
9+
query predicate assertFalse(Assertion a, Expr e) {
10+
a.getExpr() = e and
11+
a.getTarget() instanceof AssertFalseMethod
12+
}
13+
14+
query predicate assertNull(Assertion a, Expr e) {
15+
a.getExpr() = e and
16+
a.getTarget() instanceof AssertNullMethod
17+
}
18+
19+
query predicate assertNonNull(Assertion a, Expr e) {
20+
a.getExpr() = e and
21+
a.getTarget() instanceof AssertNonNullMethod
22+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| Assertions.cs:29:9:29:27 | call to method Assert |
2-
| Assertions.cs:31:9:31:28 | call to method IsTrue |
3-
| Assertions.cs:33:9:33:28 | call to method IsFalse |
4-
| Assertions.cs:35:9:35:34 | call to method MyAssert |
5-
| Assertions.cs:37:9:37:35 | call to method MyAssert2 |
1+
| Assertions.cs:30:9:30:27 | call to method Assert |
2+
| Assertions.cs:32:9:32:28 | call to method IsTrue |
3+
| Assertions.cs:34:9:34:28 | call to method IsFalse |
4+
| Assertions.cs:36:9:36:34 | call to method MyAssert |
5+
| Assertions.cs:38:9:38:35 | call to method MyAssert2 |

0 commit comments

Comments
 (0)