Skip to content

Commit ed4657c

Browse files
authored
Merge pull request #2340 from hvitved/csharp/nunit-assertions
Approved by calumgrant
2 parents 9828315 + 3d1ce55 commit ed4657c

File tree

8 files changed

+215
-22
lines changed

8 files changed

+215
-22
lines changed

csharp/ql/src/semmle/code/csharp/commons/Assertions.qll

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,57 @@ class VSTestAssertNonNullMethod extends AssertNonNullMethod {
205205
override AssertFailedExceptionClass getExceptionClass() { any() }
206206
}
207207

208+
/** An NUnit assertion method. */
209+
abstract class NUnitAssertMethod extends AssertMethod {
210+
override int getAssertionIndex() { result = 0 }
211+
212+
override AssertionExceptionClass getExceptionClass() { any() }
213+
}
214+
215+
/** An NUnit assertion method. */
216+
class NUnitAssertTrueMethod extends AssertTrueMethod, NUnitAssertMethod {
217+
NUnitAssertTrueMethod() {
218+
exists(NUnitAssertClass c |
219+
this = c.getATrueMethod()
220+
or
221+
this = c.getAnIsTrueMethod()
222+
or
223+
this = c.getAThatMethod() and
224+
this.getParameter(0).getType() instanceof BoolType
225+
)
226+
}
227+
}
228+
229+
/** An NUnit negated assertion method. */
230+
class NUnitAssertFalseMethod extends AssertFalseMethod, NUnitAssertMethod {
231+
NUnitAssertFalseMethod() {
232+
exists(NUnitAssertClass c |
233+
this = c.getAFalseMethod() or
234+
this = c.getAnIsFalseMethod()
235+
)
236+
}
237+
}
238+
239+
/** An NUnit `null` assertion method. */
240+
class NUnitAssertNullMethod extends AssertNullMethod, NUnitAssertMethod {
241+
NUnitAssertNullMethod() {
242+
exists(NUnitAssertClass c |
243+
this = c.getANullMethod() or
244+
this = c.getAnIsNullMethod()
245+
)
246+
}
247+
}
248+
249+
/** An NUnit non-`null` assertion method. */
250+
class NUnitAssertNonNullMethod extends AssertNonNullMethod, NUnitAssertMethod {
251+
NUnitAssertNonNullMethod() {
252+
exists(NUnitAssertClass c |
253+
this = c.getANotNullMethod() or
254+
this = c.getAnIsNotNullMethod()
255+
)
256+
}
257+
}
258+
208259
/** A method that forwards to another assertion method. */
209260
class ForwarderAssertMethod extends AssertMethod {
210261
Assertion a;

csharp/ql/src/semmle/code/csharp/frameworks/test/NUnit.qll

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,67 @@ class TestCaseSourceAttribute extends Attribute {
117117
result.getName() = this.getFieldName()
118118
}
119119
}
120+
121+
/** The `NUnit.Framework.Assert` class. */
122+
class NUnitAssertClass extends Class {
123+
NUnitAssertClass() { this.hasQualifiedName("NUnit.Framework.Assert") }
124+
125+
/** Gets a `Null(object, ...)` method. */
126+
Method getANullMethod() {
127+
result.getDeclaringType() = this and
128+
result.hasName("Null")
129+
}
130+
131+
/** Gets an `IsNull(object, ...)` method. */
132+
Method getAnIsNullMethod() {
133+
result.getDeclaringType() = this and
134+
result.hasName("IsNull")
135+
}
136+
137+
/** Gets a `NotNull(object, ...)` method. */
138+
Method getANotNullMethod() {
139+
result.getDeclaringType() = this and
140+
result.hasName("NotNull")
141+
}
142+
143+
/** Gets an `IsNotNull(object, ...)` method. */
144+
Method getAnIsNotNullMethod() {
145+
result.getDeclaringType() = this and
146+
result.hasName("IsNotNull")
147+
}
148+
149+
/** Gets a `True(bool, ...)` method. */
150+
Method getATrueMethod() {
151+
result.getDeclaringType() = this and
152+
result.hasName("True")
153+
}
154+
155+
/** Gets an `IsTrue(bool, ...)` method. */
156+
Method getAnIsTrueMethod() {
157+
result.getDeclaringType() = this and
158+
result.hasName("IsTrue")
159+
}
160+
161+
/** Gets a `False(bool, ...)` method. */
162+
Method getAFalseMethod() {
163+
result.getDeclaringType() = this and
164+
result.hasName("False")
165+
}
166+
167+
/** Gets an `IsFalse(bool, ...)` method. */
168+
Method getAnIsFalseMethod() {
169+
result.getDeclaringType() = this and
170+
result.hasName("IsFalse")
171+
}
172+
173+
/** Gets a `That(...)` method. */
174+
Method getAThatMethod() {
175+
result.getDeclaringType() = this and
176+
result.hasName("That")
177+
}
178+
}
179+
180+
/** The `NUnit.Framework.AssertionException` class. */
181+
class AssertionExceptionClass extends Class {
182+
AssertionExceptionClass() { this.hasQualifiedName("NUnit.Framework.AssertionException") }
183+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
assertTrue
2+
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:9:28:9:33 | IsTrue | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:9:40:9:40 | b |
3+
| nunit.cs:28:21:28:24 | True | nunit.cs:28:31:28:39 | condition |
4+
| nunit.cs:29:21:29:24 | True | nunit.cs:29:31:29:39 | condition |
5+
| nunit.cs:31:21:31:26 | IsTrue | nunit.cs:31:33:31:41 | condition |
6+
| nunit.cs:32:21:32:26 | IsTrue | nunit.cs:32:33:32:41 | condition |
7+
| nunit.cs:52:21:52:24 | That | nunit.cs:52:31:52:39 | condition |
8+
| nunit.cs:53:21:53:24 | That | nunit.cs:53:31:53:39 | condition |
9+
| nunit.cs:54:21:54:24 | That | nunit.cs:54:31:54:39 | condition |
10+
assertFalse
11+
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:10:28:10:34 | IsFalse | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:10:41:10:41 | b |
12+
| nunit.cs:34:21:34:25 | False | nunit.cs:34:32:34:40 | condition |
13+
| nunit.cs:35:21:35:25 | False | nunit.cs:35:32:35:40 | condition |
14+
| nunit.cs:37:21:37:27 | IsFalse | nunit.cs:37:34:37:42 | condition |
15+
| nunit.cs:38:21:38:27 | IsFalse | nunit.cs:38:34:38:42 | condition |
16+
assertNull
17+
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:7:28:7:33 | IsNull | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:7:42:7:42 | o |
18+
| nunit.cs:40:21:40:24 | Null | nunit.cs:40:33:40:40 | anObject |
19+
| nunit.cs:41:21:41:24 | Null | nunit.cs:41:33:41:40 | anObject |
20+
| nunit.cs:43:21:43:26 | IsNull | nunit.cs:43:35:43:42 | anObject |
21+
| nunit.cs:44:21:44:26 | IsNull | nunit.cs:44:35:44:42 | anObject |
22+
assertNonNull
23+
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:8:28:8:36 | IsNotNull | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:8:45:8:45 | o |
24+
| nunit.cs:46:21:46:27 | NotNull | nunit.cs:46:36:46:43 | anObject |
25+
| nunit.cs:47:21:47:27 | NotNull | nunit.cs:47:36:47:43 | anObject |
26+
| nunit.cs:49:21:49:29 | IsNotNull | nunit.cs:49:38:49:45 | anObject |
27+
| nunit.cs:50:21:50:29 | IsNotNull | nunit.cs:50:38:50:45 | anObject |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import csharp
2+
import semmle.code.csharp.commons.Assertions
3+
4+
query predicate assertTrue(AssertTrueMethod m, Parameter p) {
5+
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
6+
}
7+
8+
query predicate assertFalse(AssertFalseMethod m, Parameter p) {
9+
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
10+
}
11+
12+
query predicate assertNull(AssertNullMethod m, Parameter p) {
13+
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
14+
}
15+
16+
query predicate assertNonNull(AssertNonNullMethod m, Parameter p) {
17+
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
18+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| nunit.cs:52:55:52:55 | n | nunit.cs:44:26:44:31 | Source |
2-
| nunit.cs:57:78:57:78 | n | nunit.cs:33:26:33:31 | Source |
1+
| nunit.cs:85:55:85:55 | n | nunit.cs:77:26:77:31 | Source |
2+
| nunit.cs:90:78:90:78 | n | nunit.cs:66:26:66:31 | Source |
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
| nunit.cs:62:21:62:25 | Test3 | nunit.cs:49:18:49:26 | TestCases |
2-
| nunit.cs:67:21:67:25 | Test4 | nunit.cs:38:18:38:26 | TestCases |
3-
| nunit.cs:72:21:72:25 | Test5 | nunit.cs:76:18:76:34 | PropertyTestCases |
1+
| nunit.cs:95:21:95:25 | Test3 | nunit.cs:82:18:82:26 | TestCases |
2+
| nunit.cs:100:21:100:25 | Test4 | nunit.cs:71:18:71:26 | TestCases |
3+
| nunit.cs:105:21:105:25 | Test5 | nunit.cs:109:18:109:34 | PropertyTestCases |

csharp/ql/test/library-tests/frameworks/test/Test.expected

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | CallableOrCFE |
1515
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | InstanceCallable |
1616
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | XUnitTestMethod |
17-
| nunit.cs:42:11:42:21 | MyTestSuite | TestClass | LeafType |
18-
| nunit.cs:42:11:42:21 | MyTestSuite | TestClass | NUnitFixture |
19-
| nunit.cs:52:21:52:25 | Test1 | TestMethod | CallableOrCFE |
20-
| nunit.cs:52:21:52:25 | Test1 | TestMethod | InstanceCallable |
21-
| nunit.cs:52:21:52:25 | Test1 | TestMethod | NUnitTestMethod |
22-
| nunit.cs:57:21:57:25 | Test2 | TestMethod | CallableOrCFE |
23-
| nunit.cs:57:21:57:25 | Test2 | TestMethod | InstanceCallable |
24-
| nunit.cs:57:21:57:25 | Test2 | TestMethod | NUnitTestMethod |
25-
| nunit.cs:62:21:62:25 | Test3 | TestMethod | CallableOrCFE |
26-
| nunit.cs:62:21:62:25 | Test3 | TestMethod | InstanceCallable |
27-
| nunit.cs:62:21:62:25 | Test3 | TestMethod | NUnitTestMethod |
28-
| nunit.cs:67:21:67:25 | Test4 | TestMethod | CallableOrCFE |
29-
| nunit.cs:67:21:67:25 | Test4 | TestMethod | InstanceCallable |
30-
| nunit.cs:67:21:67:25 | Test4 | TestMethod | NUnitTestMethod |
31-
| nunit.cs:72:21:72:25 | Test5 | TestMethod | CallableOrCFE |
32-
| nunit.cs:72:21:72:25 | Test5 | TestMethod | InstanceCallable |
33-
| nunit.cs:72:21:72:25 | Test5 | TestMethod | NUnitTestMethod |
17+
| nunit.cs:75:11:75:21 | MyTestSuite | TestClass | LeafType |
18+
| nunit.cs:75:11:75:21 | MyTestSuite | TestClass | NUnitFixture |
19+
| nunit.cs:85:21:85:25 | Test1 | TestMethod | CallableOrCFE |
20+
| nunit.cs:85:21:85:25 | Test1 | TestMethod | InstanceCallable |
21+
| nunit.cs:85:21:85:25 | Test1 | TestMethod | NUnitTestMethod |
22+
| nunit.cs:90:21:90:25 | Test2 | TestMethod | CallableOrCFE |
23+
| nunit.cs:90:21:90:25 | Test2 | TestMethod | InstanceCallable |
24+
| nunit.cs:90:21:90:25 | Test2 | TestMethod | NUnitTestMethod |
25+
| nunit.cs:95:21:95:25 | Test3 | TestMethod | CallableOrCFE |
26+
| nunit.cs:95:21:95:25 | Test3 | TestMethod | InstanceCallable |
27+
| nunit.cs:95:21:95:25 | Test3 | TestMethod | NUnitTestMethod |
28+
| nunit.cs:100:21:100:25 | Test4 | TestMethod | CallableOrCFE |
29+
| nunit.cs:100:21:100:25 | Test4 | TestMethod | InstanceCallable |
30+
| nunit.cs:100:21:100:25 | Test4 | TestMethod | NUnitTestMethod |
31+
| nunit.cs:105:21:105:25 | Test5 | TestMethod | CallableOrCFE |
32+
| nunit.cs:105:21:105:25 | Test5 | TestMethod | InstanceCallable |
33+
| nunit.cs:105:21:105:25 | Test5 | TestMethod | NUnitTestMethod |

csharp/ql/test/library-tests/frameworks/test/nunit.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ class TestFixtureAttribute : Attribute
2222
class TestAttribute : Attribute
2323
{
2424
}
25+
26+
class Assert
27+
{
28+
public void True(bool condition) { }
29+
public void True(bool condition, string message, params object[] parms) { }
30+
31+
public void IsTrue(bool condition) { }
32+
public void IsTrue(bool condition, string message, params object[] parms) { }
33+
34+
public void False(bool condition) { }
35+
public void False(bool condition, string message, params object[] parms) { }
36+
37+
public void IsFalse(bool condition) { }
38+
public void IsFalse(bool condition, string message, params object[] parms) { }
39+
40+
public void Null(object anObject) { }
41+
public void Null(object anObject, string message, params object[] parms) { }
42+
43+
public void IsNull(object anObject) { }
44+
public void IsNull(object anObject, string message, params object[] parms) { }
45+
46+
public void NotNull(object anObject) { }
47+
public void NotNull(object anObject, string message, params object[] parms) { }
48+
49+
public void IsNotNull(object anObject) { }
50+
public void IsNotNull(object anObject, string message, params object[] parms) { }
51+
52+
public void That(bool condition) { }
53+
public void That(bool condition, string message, params object[] parms) { }
54+
public void That(bool condition, Func<string> getExceptionMessage) { }
55+
}
56+
57+
public class AssertionException : Exception { }
2558
}
2659

2760
namespace NUnitTests

0 commit comments

Comments
 (0)