Skip to content

Commit 8ab0fd5

Browse files
Improvements to the test generator:
- Only reference public methods - Report rows for which test cases could not be generated - Add a blanket `throws Exception` clause to the generated method
1 parent 2036aa1 commit 8ab0fd5

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

java/ql/src/utils/GenerateFlowTestCase.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ def getTuples(queryName, jsonResult, fname):
155155
with open(generatedJson, "r") as f:
156156
generateOutput = json.load(f)
157157
expectedTables = ("getTestCase", "getASupportMethodModel",
158-
"missingSummaryModelCsv", "getAParseFailure")
158+
"missingSummaryModelCsv", "getAParseFailure", "noTestCaseGenerated")
159159

160-
testCaseRows, supportModelRows, missingSummaryModelCsvRows, parseFailureRows = \
160+
testCaseRows, supportModelRows, missingSummaryModelCsvRows, parseFailureRows, noTestCaseGeneratedRows = \
161161
tuple([getTuples(k, generateOutput, generatedJson)
162162
for k in expectedTables])
163163

@@ -170,6 +170,9 @@ def getTuples(queryName, jsonResult, fname):
170170
if any(len(row) != 2 for row in parseFailureRows):
171171
print("Expected exactly two columns in parseFailureRows relation (got: %s)" %
172172
json.dumps(parseFailureRows), file=sys.stderr)
173+
if any(len(row) != 1 for row in noTestCaseGeneratedRows):
174+
print("Expected exactly one column in noTestCaseGenerated relation (got: %s)" %
175+
json.dumps(noTestCaseGeneratedRows), file=sys.stderr)
173176

174177
if len(missingSummaryModelCsvRows) != 0:
175178
print("Tests for some CSV rows were requested that were not in scope (SummaryModelCsv.row does not hold):\n" +
@@ -179,6 +182,9 @@ def getTuples(queryName, jsonResult, fname):
179182
print("The following rows failed to generate any test case. Check package, class and method name spelling, and argument and result specifications:\n%s" %
180183
"\n".join(r[0] + ": " + r[1] for r in parseFailureRows), file=sys.stderr)
181184
sys.exit(1)
185+
if len(noTestCaseGeneratedRows) != 0:
186+
print("The following CSV rows failed to generate any test case due to a limitation of the query. Other test cases will still be generated:\n" +
187+
"\n".join(r[0] for r in noTestCaseGeneratedRows))
182188

183189
with open(resultJava, "w") as f:
184190
f.write(generateOutput["getTestCase"]["tuples"][0][0])

java/ql/src/utils/GenerateFlowTestCase.qll

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,26 @@ query string getAParseFailure(string reason) {
5959
)
6060
}
6161

62+
/**
63+
* Gets a CSV row for which a test was requested and was correctly parsed,
64+
* but for which no test case could be generated due to a limitation of the query.
65+
*/
66+
query string noTestCaseGenerated() {
67+
any(TargetSummaryModelCsv target).row(result) and
68+
any(SummaryModelCsv model).row(result) and
69+
not exists(getAParseFailure(_)) and
70+
not exists(any(TestCase tc).getATestSnippetForRow(result))
71+
}
72+
6273
private class CallableToTest extends Callable {
6374
CallableToTest() {
6475
exists(
6576
string namespace, string type, boolean subtypes, string name, string signature, string ext
6677
|
6778
summaryModel(namespace, type, subtypes, name, signature, ext, _, _, _) and
68-
this = interpretElement(namespace, type, subtypes, name, signature, ext)
79+
this = interpretElement(namespace, type, subtypes, name, signature, ext) and
80+
this.isPublic() and
81+
getRootType(this.getDeclaringType()).isPublic()
6982
)
7083
}
7184
}
@@ -530,7 +543,7 @@ query string getTestCase() {
530543
result =
531544
"package generatedtest;\n\n" + concat(getAnImportStatement() + "\n") +
532545
"\n// Test case generated by GenerateFlowTestCase.ql\npublic class Test {\n\n" +
533-
concat("\t" + getASupportMethod() + "\n") + "\n\tpublic void test() {\n\n" +
546+
concat("\t" + getASupportMethod() + "\n") + "\n\tpublic void test() throws Exception {\n\n" +
534547
concat(string row, string snippet |
535548
snippet = any(TestCase tc).getATestSnippetForRow(row)
536549
|

0 commit comments

Comments
 (0)