Skip to content

Commit

Permalink
Format test display name with Wast file name, line and Wasm file name (
Browse files Browse the repository at this point in the history
…#498)

A minor improvement to how spec tests are generated and displayed that
should help with tracking down issues.

This adds the `DisplayName(...)` annotation on most generate tests, so
that they are now named `$wast_name.wast:$line` with optionally `@
$wasm_name`. For instance, `test23` in `SpecV1ImportsTest` would show up
as `imports.wast:154 @ spec.19.wasm`.

If you highlight `imports.wast:154` you should be able to jump to that
file in your IDE; for instance, with IntelliJ you can hit Shift twice,
the "Look anywhere" window will pop up:

![Screenshot 2024-08-29 at 12 02
33](https://github.com/user-attachments/assets/a5b0a8b8-ab92-403b-8be7-f2cbda460e71)

Hit enter and you are brought to the right point of the target Wast
file:

![Screenshot 2024-08-29 at 12 02
48](https://github.com/user-attachments/assets/25a02b54-8f89-4a59-96d8-873af701fdc3)

The Wasm file name is useful for reference and to disasm the binary when
necessary.

Suggestions for improvements are welcome. If you don't like it, also
fine, but I think something similar would be useful.

---------

Signed-off-by: Edoardo Vacchi <[email protected]>
  • Loading branch information
evacchi authored Aug 29, 2024
1 parent b3e5581 commit 780d3f2
Showing 1 changed file with 40 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public CompilationUnit generate(
cu.addImport("java.io.File");
cu.addImport("org.junit.jupiter.api.Disabled");
cu.addImport("org.junit.jupiter.api.Test");
cu.addImport("org.junit.jupiter.api.DisplayName");
cu.addImport("org.junit.jupiter.api.MethodOrderer");
cu.addImport("org.junit.jupiter.api.TestMethodOrder");
cu.addImport("org.junit.jupiter.api.Order");
Expand Down Expand Up @@ -159,6 +160,13 @@ public CompilationUnit generate(
instantiateMethod.addAnnotation("Test");
instantiateMethod.addSingleMemberAnnotation(
"Order", new IntegerLiteralExpr(Integer.toString(testNumber++)));
instantiateMethod.addSingleMemberAnnotation(
"DisplayName",
new StringLiteralExpr(
formatWastFileCoordinates(
wast.sourceFilename().getName(),
cmd.line(),
cmd.filename())));

instantiateMethod.setBody(
new BlockStmt()
Expand All @@ -180,7 +188,13 @@ public CompilationUnit generate(
case ASSERT_TRAP:
case ASSERT_EXHAUSTION:
{
method = createTestMethod(testClass, testNumber++, excludedMethods);
method =
createTestMethod(
wast.sourceFilename().getName(),
cmd,
testClass,
testNumber++,
excludedMethods);

var baseVarName = escapedCamelCase(cmd.action().field());
var varNum = fallbackVarNumber++;
Expand Down Expand Up @@ -213,7 +227,13 @@ public CompilationUnit generate(
case ASSERT_UNINSTANTIABLE:
case ASSERT_UNLINKABLE:
{
method = createTestMethod(testClass, testNumber++, excludedMethods);
method =
createTestMethod(
wast.sourceFilename().getName(),
cmd,
testClass,
testNumber++,
excludedMethods);
String hostFuncs =
detectImports(importsName, "fallback", importsSourceRoot);
generateAssertThrows(
Expand All @@ -235,6 +255,14 @@ public CompilationUnit generate(
return cu;
}

private static String formatWastFileCoordinates(String wastName, int line, String wasmName) {
if (wasmName != null) {
return wastName + ":" + line + " @ " + wasmName;
} else {
return wastName + ":" + line;
}
}

private boolean getExcluded(CommandType typ, String name) {
switch (typ) {
case ASSERT_MALFORMED:
Expand Down Expand Up @@ -272,7 +300,11 @@ private String getExceptionType(CommandType typ) {
}

private MethodDeclaration createTestMethod(
ClassOrInterfaceDeclaration testClass, int testNumber, List<String> excludedTests) {
String wastName,
Command cmd,
ClassOrInterfaceDeclaration testClass,
int testNumber,
List<String> excludedTests) {
var methodName = "test" + testNumber;
var method = testClass.addMethod(methodName, Modifier.Keyword.PUBLIC);
if (excludedTests.contains(methodName)) {
Expand All @@ -283,6 +315,10 @@ private MethodDeclaration createTestMethod(
method.addAnnotation("Test");
method.addSingleMemberAnnotation(
"Order", new IntegerLiteralExpr(Integer.toString(testNumber)));
method.addSingleMemberAnnotation(
"DisplayName",
new StringLiteralExpr(
formatWastFileCoordinates(wastName, cmd.line(), cmd.filename())));

return method;
}
Expand Down Expand Up @@ -487,7 +523,7 @@ private Expression exceptionMessageMatch(String text) {
return new NameExpr(
"assertTrue(exception.getMessage().contains(\""
+ text
+ "\"), \"'\" + exception.getMessage() + \"' doesn't contains: '"
+ "\"), \"'\" + exception.getMessage() + \"' doesn't contain: '"
+ text
+ "\")");
}
Expand Down

0 comments on commit 780d3f2

Please sign in to comment.