Skip to content

Commit 3a1eac7

Browse files
authored
Add support for @nested test classes in runnables (#172)
Relates to #171 This adds support for runnables in tests with one level of nesting in test classes for Mac and Linux. For Windows there would still be a custom `tasks.json` required.
1 parent f5d1c7f commit 3a1eac7

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

languages/java/runnables.scm

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,39 @@
6464
(#set! tag java-test-method)
6565
)
6666

67+
; Run nested test function
68+
(
69+
(package_declaration
70+
(scoped_identifier) @java_package_name
71+
)
72+
(class_declaration
73+
name: (identifier) @java_outer_class_name
74+
body: (class_body
75+
(class_declaration
76+
(modifiers
77+
(marker_annotation
78+
name: (identifier) @nested_annotation
79+
)
80+
)
81+
name: (identifier) @java_class_name
82+
body: (class_body
83+
(method_declaration
84+
(modifiers
85+
(marker_annotation
86+
name: (identifier) @annotation_name
87+
)
88+
)
89+
name: (identifier) @run @java_method_name
90+
(#eq? @annotation_name "Test")
91+
)
92+
)
93+
(#eq? @nested_annotation "Nested")
94+
) @_
95+
)
96+
)
97+
(#set! tag java-test-method-nested)
98+
)
99+
67100
; Run test class
68101
(
69102
(package_declaration
@@ -84,3 +117,35 @@
84117
) @_
85118
(#set! tag java-test-class)
86119
)
120+
121+
; Run nested test class
122+
(
123+
(package_declaration
124+
(scoped_identifier) @java_package_name
125+
)
126+
(class_declaration
127+
name: (identifier) @java_outer_class_name
128+
body: (class_body
129+
(class_declaration
130+
(modifiers
131+
(marker_annotation
132+
name: (identifier) @nested_annotation
133+
)
134+
)
135+
name: (identifier) @run @java_class_name
136+
body: (class_body
137+
(method_declaration
138+
(modifiers
139+
(marker_annotation
140+
name: (identifier) @annotation_name
141+
)
142+
)
143+
(#eq? @annotation_name "Test")
144+
)
145+
)
146+
(#eq? @nested_annotation "Nested")
147+
) @_
148+
)
149+
)
150+
(#set! tag java-test-class-nested)
151+
)

languages/java/tasks.json

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
[
2-
{
3-
"label": "Run $ZED_CUSTOM_java_class_name",
4-
"command": "pkg=\"${ZED_CUSTOM_java_package_name:}\"; cls=\"$ZED_CUSTOM_java_class_name\"; if [ -n \"$pkg\" ]; then c=\"$pkg.$cls\"; else c=\"$cls\"; fi; if [ -f pom.xml ]; then ./mvnw clean compile exec:java -Dexec.mainClass=\"$c\"; elif [ -f gradlew ]; then ./gradlew run -PmainClass=\"$c\"; else find . -name '*.java' -not -path './bin/*' -not -path './target/*' -not -path './build/*' -print0 | xargs -0 javac -d bin && java -cp bin \"$c\"; fi;",
5-
"use_new_terminal": false,
6-
"reveal": "always",
7-
"tags": ["java-main"],
8-
"shell": {
9-
"with_arguments": {
10-
"program": "/bin/sh",
11-
"args": ["-c"]
12-
}
2+
{
3+
"label": "Run $ZED_CUSTOM_java_class_name",
4+
"command": "pkg=\"${ZED_CUSTOM_java_package_name:}\"; cls=\"$ZED_CUSTOM_java_class_name\"; if [ -n \"$pkg\" ]; then c=\"$pkg.$cls\"; else c=\"$cls\"; fi; if [ -f pom.xml ]; then ./mvnw clean compile exec:java -Dexec.mainClass=\"$c\"; elif [ -f gradlew ]; then ./gradlew run -PmainClass=\"$c\"; else find . -name '*.java' -not -path './bin/*' -not -path './target/*' -not -path './build/*' -print0 | xargs -0 javac -d bin && java -cp bin \"$c\"; fi;",
5+
"use_new_terminal": false,
6+
"reveal": "always",
7+
"tags": ["java-main"],
8+
"shell": {
9+
"with_arguments": {
10+
"program": "/bin/sh",
11+
"args": ["-c"]
1312
}
14-
},
13+
}
14+
},
1515
{
16-
"label": "Test $ZED_CUSTOM_java_class_name.$ZED_CUSTOM_java_method_name",
17-
"command": "c=\"$ZED_CUSTOM_java_package_name.$ZED_CUSTOM_java_class_name\"; m=\"$ZED_CUSTOM_java_method_name\"; if [ -f pom.xml ]; then ./mvnw clean test -Dtest=\"$c#$m\"; elif [ -f gradlew ]; then ./gradlew test --tests $c.$m; else >&2 echo 'No build system found'; exit 1; fi;",
16+
"label": "$ZED_CUSTOM_java_class_name.${ZED_CUSTOM_java_outer_class_name:}.$ZED_CUSTOM_java_method_name",
17+
"command": "package=\"$ZED_CUSTOM_java_package_name\"; outer=\"${ZED_CUSTOM_java_outer_class_name:}\"; inner=\"$ZED_CUSTOM_java_class_name\"; method=\"$ZED_CUSTOM_java_method_name\"; sep=\"$\"; if [ -n \"$outer\" ]; then c=\"$outer$sep$inner\"; else c=\"$inner\"; fi; if [ -f pom.xml ]; then ./mvnw clean test -Dtest=\"$package.$c#$method\"; elif [ -f gradlew ]; then ./gradlew test --tests \"$package.$c.$method\"; else >&2 echo 'No build system found'; exit 1; fi;",
1818
"use_new_terminal": false,
1919
"reveal": "always",
20-
"tags": ["java-test-method"],
20+
"tags": ["java-test-method", "java-test-method-nested"],
2121
"shell": {
2222
"with_arguments": {
2323
"program": "/bin/sh",
@@ -27,10 +27,10 @@
2727
},
2828
{
2929
"label": "Test class $ZED_CUSTOM_java_class_name",
30-
"command": "c=\"$ZED_CUSTOM_java_package_name.$ZED_CUSTOM_java_class_name\"; if [ -f pom.xml ]; then ./mvnw clean test -Dtest=\"$c\"; elif [ -f gradlew ]; then ./gradlew test --tests $c; else >&2 echo 'No build system found'; exit 1; fi;",
30+
"command": "package=\"$ZED_CUSTOM_java_package_name\"; outer=\"${ZED_CUSTOM_java_outer_class_name:}\"; inner=\"$ZED_CUSTOM_java_class_name\"; sep=\"$\"; if [ -n \"$outer\" ]; then c=\"$outer$sep$inner\"; else c=\"$inner\"; fi; if [ -f pom.xml ]; then ./mvnw clean test -Dtest=\"$package.$c\"; elif [ -f gradlew ]; then ./gradlew test --tests \"$package.$c\"; else >&2 echo 'No build system found'; exit 1; fi;",
3131
"use_new_terminal": false,
3232
"reveal": "always",
33-
"tags": ["java-test-class"],
33+
"tags": ["java-test-class", "java-test-class-nested"],
3434
"shell": {
3535
"with_arguments": {
3636
"program": "/bin/sh",

0 commit comments

Comments
 (0)