[Java]: Add precondition support for testing library asserts

This commit is contained in:
Jonathan Leitschuh
2022-03-18 20:36:53 -04:00
parent 767453520e
commit 1d0275344d
10 changed files with 202 additions and 9 deletions

View File

@@ -0,0 +1,95 @@
import org.junit.Assert;
import org.junit.jupiter.api.Assertions;
public class Preconditions {
public static void guarded() {}
void test1() {
Assert.assertTrue(true);
guarded();
}
void test2() {
Assert.assertTrue(false);
guarded();
}
void test3() {
Assert.assertFalse(false);
guarded();
}
void test4() {
Assert.assertFalse(true);
guarded();
}
void test5() {
Assert.assertTrue("Reason", true);
guarded();
}
void test6() {
Assert.assertTrue("Reason", false);
guarded();
}
void test7() {
Assert.assertFalse("Reason", false);
guarded();
}
void test8() {
Assert.assertFalse("Reason", true);
guarded();
}
void test9() {
r(true);
guarded();
}
static void r(boolean b) {
Assert.assertTrue("Unified Reason", b);
}
void test10() {
Assertions.assertTrue(true);
guarded();
}
void test11() {
Assertions.assertTrue(false);
guarded();
}
void test12() {
Assertions.assertFalse(false);
guarded();
}
void test13() {
Assertions.assertFalse(true);
guarded();
}
void test14() {
Assertions.assertTrue(true, "Reason");
guarded();
}
void test15() {
Assertions.assertTrue(false, "Reason");
guarded();
}
void test16() {
Assertions.assertFalse(false, "Reason");
guarded();
}
void test17() {
Assertions.assertFalse(true, "Reason");
guarded();
}
}

View File

@@ -0,0 +1,13 @@
| Preconditions.java:8:9:8:31 | assertTrue(...) | true | Preconditions.java:9:9:9:18 | <Expr>; |
| Preconditions.java:13:9:13:32 | assertTrue(...) | true | Preconditions.java:14:9:14:18 | <Expr>; |
| Preconditions.java:18:9:18:33 | assertFalse(...) | false | Preconditions.java:19:9:19:18 | <Expr>; |
| Preconditions.java:23:9:23:32 | assertFalse(...) | false | Preconditions.java:24:9:24:18 | <Expr>; |
| Preconditions.java:48:9:48:15 | r(...) | true | Preconditions.java:49:9:49:18 | <Expr>; |
| Preconditions.java:57:9:57:35 | assertTrue(...) | true | Preconditions.java:58:9:58:18 | <Expr>; |
| Preconditions.java:62:9:62:36 | assertTrue(...) | true | Preconditions.java:63:9:63:18 | <Expr>; |
| Preconditions.java:67:9:67:37 | assertFalse(...) | false | Preconditions.java:68:9:68:18 | <Expr>; |
| Preconditions.java:72:9:72:36 | assertFalse(...) | false | Preconditions.java:73:9:73:18 | <Expr>; |
| Preconditions.java:77:9:77:45 | assertTrue(...) | true | Preconditions.java:78:9:78:18 | <Expr>; |
| Preconditions.java:82:9:82:46 | assertTrue(...) | true | Preconditions.java:83:9:83:18 | <Expr>; |
| Preconditions.java:87:9:87:47 | assertFalse(...) | false | Preconditions.java:88:9:88:18 | <Expr>; |
| Preconditions.java:92:9:92:46 | assertFalse(...) | false | Preconditions.java:93:9:93:18 | <Expr>; |

View File

@@ -0,0 +1,8 @@
import java
import semmle.code.java.controlflow.Guards
from Guard g, BasicBlock bb, boolean branch
where
g.controls(bb, branch) and
g.getEnclosingCallable().getDeclaringType().hasName("Preconditions")
select g, branch, bb

View File

@@ -0,0 +1 @@
//semmle-extractor-options: --javac-args -cp ${testdir}/../../stubs/junit-4.11/:${testdir}/../../stubs/junit-jupiter-api-5.2.0/

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.api;
public class Assertions {
public static void assertTrue(boolean condition) {
}
public static void assertTrue(boolean condition, String message) {
}
public static void assertFalse(boolean condition) {
}
public static void assertFalse(boolean condition, String message) {
}
}