Merge pull request #2340 from hvitved/csharp/nunit-assertions

Approved by calumgrant
This commit is contained in:
semmle-qlci
2019-11-18 13:02:49 +00:00
committed by GitHub
8 changed files with 215 additions and 22 deletions

View File

@@ -205,6 +205,57 @@ class VSTestAssertNonNullMethod extends AssertNonNullMethod {
override AssertFailedExceptionClass getExceptionClass() { any() }
}
/** An NUnit assertion method. */
abstract class NUnitAssertMethod extends AssertMethod {
override int getAssertionIndex() { result = 0 }
override AssertionExceptionClass getExceptionClass() { any() }
}
/** An NUnit assertion method. */
class NUnitAssertTrueMethod extends AssertTrueMethod, NUnitAssertMethod {
NUnitAssertTrueMethod() {
exists(NUnitAssertClass c |
this = c.getATrueMethod()
or
this = c.getAnIsTrueMethod()
or
this = c.getAThatMethod() and
this.getParameter(0).getType() instanceof BoolType
)
}
}
/** An NUnit negated assertion method. */
class NUnitAssertFalseMethod extends AssertFalseMethod, NUnitAssertMethod {
NUnitAssertFalseMethod() {
exists(NUnitAssertClass c |
this = c.getAFalseMethod() or
this = c.getAnIsFalseMethod()
)
}
}
/** An NUnit `null` assertion method. */
class NUnitAssertNullMethod extends AssertNullMethod, NUnitAssertMethod {
NUnitAssertNullMethod() {
exists(NUnitAssertClass c |
this = c.getANullMethod() or
this = c.getAnIsNullMethod()
)
}
}
/** An NUnit non-`null` assertion method. */
class NUnitAssertNonNullMethod extends AssertNonNullMethod, NUnitAssertMethod {
NUnitAssertNonNullMethod() {
exists(NUnitAssertClass c |
this = c.getANotNullMethod() or
this = c.getAnIsNotNullMethod()
)
}
}
/** A method that forwards to another assertion method. */
class ForwarderAssertMethod extends AssertMethod {
Assertion a;

View File

@@ -117,3 +117,67 @@ class TestCaseSourceAttribute extends Attribute {
result.getName() = this.getFieldName()
}
}
/** The `NUnit.Framework.Assert` class. */
class NUnitAssertClass extends Class {
NUnitAssertClass() { this.hasQualifiedName("NUnit.Framework.Assert") }
/** Gets a `Null(object, ...)` method. */
Method getANullMethod() {
result.getDeclaringType() = this and
result.hasName("Null")
}
/** Gets an `IsNull(object, ...)` method. */
Method getAnIsNullMethod() {
result.getDeclaringType() = this and
result.hasName("IsNull")
}
/** Gets a `NotNull(object, ...)` method. */
Method getANotNullMethod() {
result.getDeclaringType() = this and
result.hasName("NotNull")
}
/** Gets an `IsNotNull(object, ...)` method. */
Method getAnIsNotNullMethod() {
result.getDeclaringType() = this and
result.hasName("IsNotNull")
}
/** Gets a `True(bool, ...)` method. */
Method getATrueMethod() {
result.getDeclaringType() = this and
result.hasName("True")
}
/** Gets an `IsTrue(bool, ...)` method. */
Method getAnIsTrueMethod() {
result.getDeclaringType() = this and
result.hasName("IsTrue")
}
/** Gets a `False(bool, ...)` method. */
Method getAFalseMethod() {
result.getDeclaringType() = this and
result.hasName("False")
}
/** Gets an `IsFalse(bool, ...)` method. */
Method getAnIsFalseMethod() {
result.getDeclaringType() = this and
result.hasName("IsFalse")
}
/** Gets a `That(...)` method. */
Method getAThatMethod() {
result.getDeclaringType() = this and
result.hasName("That")
}
}
/** The `NUnit.Framework.AssertionException` class. */
class AssertionExceptionClass extends Class {
AssertionExceptionClass() { this.hasQualifiedName("NUnit.Framework.AssertionException") }
}

View File

@@ -0,0 +1,27 @@
assertTrue
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:9:28:9:33 | IsTrue | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:9:40:9:40 | b |
| nunit.cs:28:21:28:24 | True | nunit.cs:28:31:28:39 | condition |
| nunit.cs:29:21:29:24 | True | nunit.cs:29:31:29:39 | condition |
| nunit.cs:31:21:31:26 | IsTrue | nunit.cs:31:33:31:41 | condition |
| nunit.cs:32:21:32:26 | IsTrue | nunit.cs:32:33:32:41 | condition |
| nunit.cs:52:21:52:24 | That | nunit.cs:52:31:52:39 | condition |
| nunit.cs:53:21:53:24 | That | nunit.cs:53:31:53:39 | condition |
| nunit.cs:54:21:54:24 | That | nunit.cs:54:31:54:39 | condition |
assertFalse
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:10:28:10:34 | IsFalse | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:10:41:10:41 | b |
| nunit.cs:34:21:34:25 | False | nunit.cs:34:32:34:40 | condition |
| nunit.cs:35:21:35:25 | False | nunit.cs:35:32:35:40 | condition |
| nunit.cs:37:21:37:27 | IsFalse | nunit.cs:37:34:37:42 | condition |
| nunit.cs:38:21:38:27 | IsFalse | nunit.cs:38:34:38:42 | condition |
assertNull
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:7:28:7:33 | IsNull | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:7:42:7:42 | o |
| nunit.cs:40:21:40:24 | Null | nunit.cs:40:33:40:40 | anObject |
| nunit.cs:41:21:41:24 | Null | nunit.cs:41:33:41:40 | anObject |
| nunit.cs:43:21:43:26 | IsNull | nunit.cs:43:35:43:42 | anObject |
| nunit.cs:44:21:44:26 | IsNull | nunit.cs:44:35:44:42 | anObject |
assertNonNull
| ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:8:28:8:36 | IsNotNull | ../../../resources/stubs/Microsoft.VisualStudio.TestTools.UnitTesting.cs:8:45:8:45 | o |
| nunit.cs:46:21:46:27 | NotNull | nunit.cs:46:36:46:43 | anObject |
| nunit.cs:47:21:47:27 | NotNull | nunit.cs:47:36:47:43 | anObject |
| nunit.cs:49:21:49:29 | IsNotNull | nunit.cs:49:38:49:45 | anObject |
| nunit.cs:50:21:50:29 | IsNotNull | nunit.cs:50:38:50:45 | anObject |

View File

@@ -0,0 +1,18 @@
import csharp
import semmle.code.csharp.commons.Assertions
query predicate assertTrue(AssertTrueMethod m, Parameter p) {
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
}
query predicate assertFalse(AssertFalseMethod m, Parameter p) {
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
}
query predicate assertNull(AssertNullMethod m, Parameter p) {
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
}
query predicate assertNonNull(AssertNonNullMethod m, Parameter p) {
m.fromSource() and m.fromSource() and p = m.getAssertedParameter()
}

View File

@@ -1,2 +1,2 @@
| nunit.cs:52:55:52:55 | n | nunit.cs:44:26:44:31 | Source |
| nunit.cs:57:78:57:78 | n | nunit.cs:33:26:33:31 | Source |
| nunit.cs:85:55:85:55 | n | nunit.cs:77:26:77:31 | Source |
| nunit.cs:90:78:90:78 | n | nunit.cs:66:26:66:31 | Source |

View File

@@ -1,3 +1,3 @@
| nunit.cs:62:21:62:25 | Test3 | nunit.cs:49:18:49:26 | TestCases |
| nunit.cs:67:21:67:25 | Test4 | nunit.cs:38:18:38:26 | TestCases |
| nunit.cs:72:21:72:25 | Test5 | nunit.cs:76:18:76:34 | PropertyTestCases |
| nunit.cs:95:21:95:25 | Test3 | nunit.cs:82:18:82:26 | TestCases |
| nunit.cs:100:21:100:25 | Test4 | nunit.cs:71:18:71:26 | TestCases |
| nunit.cs:105:21:105:25 | Test5 | nunit.cs:109:18:109:34 | PropertyTestCases |

View File

@@ -14,20 +14,20 @@
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | CallableOrCFE |
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | InstanceCallable |
| XUnit.cs:30:21:30:25 | Test2 | TestMethod | XUnitTestMethod |
| nunit.cs:42:11:42:21 | MyTestSuite | TestClass | LeafType |
| nunit.cs:42:11:42:21 | MyTestSuite | TestClass | NUnitFixture |
| nunit.cs:52:21:52:25 | Test1 | TestMethod | CallableOrCFE |
| nunit.cs:52:21:52:25 | Test1 | TestMethod | InstanceCallable |
| nunit.cs:52:21:52:25 | Test1 | TestMethod | NUnitTestMethod |
| nunit.cs:57:21:57:25 | Test2 | TestMethod | CallableOrCFE |
| nunit.cs:57:21:57:25 | Test2 | TestMethod | InstanceCallable |
| nunit.cs:57:21:57:25 | Test2 | TestMethod | NUnitTestMethod |
| nunit.cs:62:21:62:25 | Test3 | TestMethod | CallableOrCFE |
| nunit.cs:62:21:62:25 | Test3 | TestMethod | InstanceCallable |
| nunit.cs:62:21:62:25 | Test3 | TestMethod | NUnitTestMethod |
| nunit.cs:67:21:67:25 | Test4 | TestMethod | CallableOrCFE |
| nunit.cs:67:21:67:25 | Test4 | TestMethod | InstanceCallable |
| nunit.cs:67:21:67:25 | Test4 | TestMethod | NUnitTestMethod |
| nunit.cs:72:21:72:25 | Test5 | TestMethod | CallableOrCFE |
| nunit.cs:72:21:72:25 | Test5 | TestMethod | InstanceCallable |
| nunit.cs:72:21:72:25 | Test5 | TestMethod | NUnitTestMethod |
| nunit.cs:75:11:75:21 | MyTestSuite | TestClass | LeafType |
| nunit.cs:75:11:75:21 | MyTestSuite | TestClass | NUnitFixture |
| nunit.cs:85:21:85:25 | Test1 | TestMethod | CallableOrCFE |
| nunit.cs:85:21:85:25 | Test1 | TestMethod | InstanceCallable |
| nunit.cs:85:21:85:25 | Test1 | TestMethod | NUnitTestMethod |
| nunit.cs:90:21:90:25 | Test2 | TestMethod | CallableOrCFE |
| nunit.cs:90:21:90:25 | Test2 | TestMethod | InstanceCallable |
| nunit.cs:90:21:90:25 | Test2 | TestMethod | NUnitTestMethod |
| nunit.cs:95:21:95:25 | Test3 | TestMethod | CallableOrCFE |
| nunit.cs:95:21:95:25 | Test3 | TestMethod | InstanceCallable |
| nunit.cs:95:21:95:25 | Test3 | TestMethod | NUnitTestMethod |
| nunit.cs:100:21:100:25 | Test4 | TestMethod | CallableOrCFE |
| nunit.cs:100:21:100:25 | Test4 | TestMethod | InstanceCallable |
| nunit.cs:100:21:100:25 | Test4 | TestMethod | NUnitTestMethod |
| nunit.cs:105:21:105:25 | Test5 | TestMethod | CallableOrCFE |
| nunit.cs:105:21:105:25 | Test5 | TestMethod | InstanceCallable |
| nunit.cs:105:21:105:25 | Test5 | TestMethod | NUnitTestMethod |

View File

@@ -22,6 +22,39 @@ namespace NUnit.Framework
class TestAttribute : Attribute
{
}
class Assert
{
public void True(bool condition) { }
public void True(bool condition, string message, params object[] parms) { }
public void IsTrue(bool condition) { }
public void IsTrue(bool condition, string message, params object[] parms) { }
public void False(bool condition) { }
public void False(bool condition, string message, params object[] parms) { }
public void IsFalse(bool condition) { }
public void IsFalse(bool condition, string message, params object[] parms) { }
public void Null(object anObject) { }
public void Null(object anObject, string message, params object[] parms) { }
public void IsNull(object anObject) { }
public void IsNull(object anObject, string message, params object[] parms) { }
public void NotNull(object anObject) { }
public void NotNull(object anObject, string message, params object[] parms) { }
public void IsNotNull(object anObject) { }
public void IsNotNull(object anObject, string message, params object[] parms) { }
public void That(bool condition) { }
public void That(bool condition, string message, params object[] parms) { }
public void That(bool condition, Func<string> getExceptionMessage) { }
}
public class AssertionException : Exception { }
}
namespace NUnitTests